1From 3ac01579b647ad850ccace06ce24763ee6f71ad5 Mon Sep 17 00:00:00 2001
2From: Eddie Cai <eddie.cai.linux@gmail.com>
3Date: Thu, 17 Mar 2022 09:53:24 +0800
4Subject: [PATCH 2/3] modify export-image to meet rockchip image requirement
5
6---
7 export-image/00-allow-rerun/00-run.sh  | 10 +++-
8 export-image/03-set-partuuid/00-run.sh |  6 ---
9 export-image/04-finalise/01-run.sh     | 30 +++---------
10 export-image/prerun.sh                 | 64 +++++---------------------
11 4 files changed, 25 insertions(+), 85 deletions(-)
12
13diff --git a/export-image/00-allow-rerun/00-run.sh b/export-image/00-allow-rerun/00-run.sh
14index dd67f4c..5174d9b 100755
15--- a/export-image/00-allow-rerun/00-run.sh
16+++ b/export-image/00-allow-rerun/00-run.sh
17@@ -1,7 +1,13 @@
18 #!/bin/bash -e
19
20-if [ ! -x "${ROOTFS_DIR}/usr/bin/qemu-arm-static" ]; then
21-	cp /usr/bin/qemu-arm-static "${ROOTFS_DIR}/usr/bin/"
22+if [ "$ARCH" == "armhf" ]; then
23+	if [ ! -x "${ROOTFS_DIR}/usr/bin/qemu-arm-static" ]; then
24+		cp /usr/bin/qemu-arm-static "${ROOTFS_DIR}/usr/bin/"
25+	fi
26+elif [ "$ARCH" == "arm64"  ]; then
27+	if [ ! -x "${ROOTFS_DIR}/usr/bin/qemu-aarch64-static" ]; then
28+		cp /usr/bin/qemu-aarch64-static "${ROOTFS_DIR}/usr/bin/"
29+	fi
30 fi
31
32 if [ -e "${ROOTFS_DIR}/etc/ld.so.preload" ]; then
33diff --git a/export-image/03-set-partuuid/00-run.sh b/export-image/03-set-partuuid/00-run.sh
34index 16e1b15..28020d6 100755
35--- a/export-image/03-set-partuuid/00-run.sh
36+++ b/export-image/03-set-partuuid/00-run.sh
37@@ -6,13 +6,7 @@ if [ "${NO_PRERUN_QCOW2}" = "0" ]; then
38
39 	IMGID="$(dd if="${IMG_FILE}" skip=440 bs=1 count=4 2>/dev/null | xxd -e | cut -f 2 -d' ')"
40
41-	BOOT_PARTUUID="${IMGID}-01"
42 	ROOT_PARTUUID="${IMGID}-02"
43
44-	sed -i "s/BOOTDEV/PARTUUID=${BOOT_PARTUUID}/" "${ROOTFS_DIR}/etc/fstab"
45-	sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${ROOTFS_DIR}/etc/fstab"
46-
47-	sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${ROOTFS_DIR}/boot/cmdline.txt"
48-
49 fi
50
51diff --git a/export-image/04-finalise/01-run.sh b/export-image/04-finalise/01-run.sh
52index d7ace58..fa0e191 100755
53--- a/export-image/04-finalise/01-run.sh
54+++ b/export-image/04-finalise/01-run.sh
55@@ -16,7 +16,11 @@ if [ -d "${ROOTFS_DIR}/home/${FIRST_USER_NAME}/.config" ]; then
56 	chmod 700 "${ROOTFS_DIR}/home/${FIRST_USER_NAME}/.config"
57 fi
58
59-rm -f "${ROOTFS_DIR}/usr/bin/qemu-arm-static"
60+if [ "$ARCH" == "armhf" ]; then
61+	rm -f "${ROOTFS_DIR}/usr/bin/qemu-arm-static"
62+elif [ "$ARCH" == "arm64"  ]; then
63+	rm -f "${ROOTFS_DIR}/usr/bin/qemu-aarch64-static"
64+fi
65
66 if [ "${USE_QEMU}" != "1" ]; then
67 	if [ -e "${ROOTFS_DIR}/etc/ld.so.preload.disabled" ]; then
68@@ -52,26 +56,9 @@ find "${ROOTFS_DIR}/var/log/" -type f -exec cp /dev/null {} \;
69 rm -f "${ROOTFS_DIR}/root/.vnc/private.key"
70 rm -f "${ROOTFS_DIR}/etc/vnc/updateid"
71
72-update_issue "$(basename "${EXPORT_DIR}")"
73-install -m 644 "${ROOTFS_DIR}/etc/rpi-issue" "${ROOTFS_DIR}/boot/issue.txt"
74-
75-cp "$ROOTFS_DIR/etc/rpi-issue" "$INFO_FILE"
76-
77
78 {
79-	if [ -f "$ROOTFS_DIR/usr/share/doc/raspberrypi-kernel/changelog.Debian.gz" ]; then
80-		firmware=$(zgrep "firmware as of" \
81-			"$ROOTFS_DIR/usr/share/doc/raspberrypi-kernel/changelog.Debian.gz" | \
82-			head -n1 | sed  -n 's|.* \([^ ]*\)$|\1|p')
83-		printf "\nFirmware: https://github.com/raspberrypi/firmware/tree/%s\n" "$firmware"
84-
85-		kernel="$(curl -s -L "https://github.com/raspberrypi/firmware/raw/$firmware/extra/git_hash")"
86-		printf "Kernel: https://github.com/raspberrypi/linux/tree/%s\n" "$kernel"
87-
88-		uname="$(curl -s -L "https://github.com/raspberrypi/firmware/raw/$firmware/extra/uname_string7")"
89-		printf "Uname string: %s\n" "$uname"
90-	fi
91-
92+	echo -e "Rockbian ${IMG_DATE}\nGenerated using ${PI_GEN}, ${PI_GEN_REPO}, ${GIT_HASH}, $(basename ${EXPORT_DIR})"
93 	printf "\nPackages:\n"
94 	dpkg -l --root "$ROOTFS_DIR"
95 } >> "$INFO_FILE"
96@@ -84,12 +71,7 @@ rm -f "${DEPLOY_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img"
97 mv "$INFO_FILE" "$DEPLOY_DIR/"
98
99 if [ "${USE_QCOW2}" = "0" ] && [ "${NO_PRERUN_QCOW2}" = "0" ]; then
100-	ROOT_DEV="$(mount | grep "${ROOTFS_DIR} " | cut -f1 -d' ')"
101-
102 	unmount "${ROOTFS_DIR}"
103-	zerofree "${ROOT_DEV}"
104-
105-	unmount_image "${IMG_FILE}"
106 else
107 	unload_qimage
108 	make_bootable_image "${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.qcow2" "$IMG_FILE"
109diff --git a/export-image/prerun.sh b/export-image/prerun.sh
110index fad7f80..dde7315 100755
111--- a/export-image/prerun.sh
112+++ b/export-image/prerun.sh
113@@ -10,8 +10,7 @@ if [ "${NO_PRERUN_QCOW2}" = "0" ]; then
114 	rm -rf "${ROOTFS_DIR}"
115 	mkdir -p "${ROOTFS_DIR}"
116
117-	BOOT_SIZE="$((256 * 1024 * 1024))"
118-	ROOT_SIZE=$(du --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --exclude boot --block-size=1 | cut -f 1)
119+	ROOT_SIZE=$(du --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --block-size=1 | cut -f 1)
120
121 	# All partition sizes and starts will be aligned to this size
122 	ALIGN="$((4 * 1024 * 1024))"
123@@ -19,55 +18,18 @@ if [ "${NO_PRERUN_QCOW2}" = "0" ]; then
124 	# some overhead (since actual space usage is usually rounded up to the
125 	# filesystem block size) and gives some free space on the resulting
126 	# image.
127-	ROOT_MARGIN="$(echo "($ROOT_SIZE * 0.2 + 200 * 1024 * 1024) / 1" | bc)"
128+	ROOT_MARGIN="$((200 * 1024 * 1024))"
129
130-	BOOT_PART_START=$((ALIGN))
131-	BOOT_PART_SIZE=$(((BOOT_SIZE + ALIGN - 1) / ALIGN * ALIGN))
132-	ROOT_PART_START=$((BOOT_PART_START + BOOT_PART_SIZE))
133+	ROOT_PART_START=$((ALIGN))
134 	ROOT_PART_SIZE=$(((ROOT_SIZE + ROOT_MARGIN + ALIGN  - 1) / ALIGN * ALIGN))
135-	IMG_SIZE=$((BOOT_PART_START + BOOT_PART_SIZE + ROOT_PART_SIZE))
136+	IMG_SIZE=$((ROOT_PART_START + ROOT_PART_SIZE))
137
138 	truncate -s "${IMG_SIZE}" "${IMG_FILE}"
139+
140+	ROOT_OFFSET=${ROOT_PART_START}
141+	ROOT_LENGTH=${ROOT_PART_SIZE}
142
143-	parted --script "${IMG_FILE}" mklabel msdos
144-	parted --script "${IMG_FILE}" unit B mkpart primary fat32 "${BOOT_PART_START}" "$((BOOT_PART_START + BOOT_PART_SIZE - 1))"
145-	parted --script "${IMG_FILE}" unit B mkpart primary ext4 "${ROOT_PART_START}" "$((ROOT_PART_START + ROOT_PART_SIZE - 1))"
146-
147-	PARTED_OUT=$(parted -sm "${IMG_FILE}" unit b print)
148-	BOOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 2 | tr -d B)
149-	BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 4 | tr -d B)
150-
151-	ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 2 | tr -d B)
152-	ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 4 | tr -d B)
153-
154-	echo "Mounting BOOT_DEV..."
155-	cnt=0
156-	until BOOT_DEV=$(losetup --show -f -o "${BOOT_OFFSET}" --sizelimit "${BOOT_LENGTH}" "${IMG_FILE}"); do
157-		if [ $cnt -lt 5 ]; then
158-			cnt=$((cnt + 1))
159-			echo "Error in losetup for BOOT_DEV.  Retrying..."
160-			sleep 5
161-		else
162-			echo "ERROR: losetup for BOOT_DEV failed; exiting"
163-			exit 1
164-		fi
165-	done
166-
167-	echo "Mounting ROOT_DEV..."
168-	cnt=0
169-	until ROOT_DEV=$(losetup --show -f -o "${ROOT_OFFSET}" --sizelimit "${ROOT_LENGTH}" "${IMG_FILE}"); do
170-		if [ $cnt -lt 5 ]; then
171-			cnt=$((cnt + 1))
172-			echo "Error in losetup for ROOT_DEV.  Retrying..."
173-			sleep 5
174-		else
175-			echo "ERROR: losetup for ROOT_DEV failed; exiting"
176-			exit 1
177-		fi
178-	done
179-
180-	echo "/boot: offset $BOOT_OFFSET, length $BOOT_LENGTH"
181-	echo "/:     offset $ROOT_OFFSET, length $ROOT_LENGTH"
182+	echo "/:     offset $ROOT_OFFSET, length $ROOT_PART_SIZE"
183
184 	ROOT_FEATURES="^huge_file"
185 	for FEATURE in metadata_csum 64bit; do
186@@ -75,13 +37,9 @@ if [ "${NO_PRERUN_QCOW2}" = "0" ]; then
187 		ROOT_FEATURES="^$FEATURE,$ROOT_FEATURES"
188 	fi
189 	done
190-	mkdosfs -n boot -F 32 -v "$BOOT_DEV" > /dev/null
191-	mkfs.ext4 -L rootfs -O "$ROOT_FEATURES" "$ROOT_DEV" > /dev/null
192
193-	mount -v "$ROOT_DEV" "${ROOTFS_DIR}" -t ext4
194-	mkdir -p "${ROOTFS_DIR}/boot"
195-	mount -v "$BOOT_DEV" "${ROOTFS_DIR}/boot" -t vfat
196+	mkfs.ext4 -L rootfs -O "$ROOT_FEATURES" "$IMG_FILE" > /dev/null
197
198-	rsync -aHAXx --exclude /var/cache/apt/archives --exclude /boot "${EXPORT_ROOTFS_DIR}/" "${ROOTFS_DIR}/"
199-	rsync -rtx "${EXPORT_ROOTFS_DIR}/boot/" "${ROOTFS_DIR}/boot/"
200+	mount -v "$IMG_FILE" "${ROOTFS_DIR}" -t ext4
201+	rsync -aHAXx --exclude /var/cache/apt/archives "${EXPORT_ROOTFS_DIR}/" "${ROOTFS_DIR}/"
202 fi
203--
2042.20.1
205
206