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