2016-05-05 14:43:33 -04:00
|
|
|
#!/bin/bash -e
|
2017-07-27 06:11:07 -04:00
|
|
|
|
2019-01-21 13:42:54 -05:00
|
|
|
IMG_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img"
|
2016-05-05 14:43:33 -04:00
|
|
|
|
2018-03-02 15:08:24 -05:00
|
|
|
unmount_image "${IMG_FILE}"
|
2016-05-05 14:43:33 -04:00
|
|
|
|
2018-03-02 15:08:24 -05:00
|
|
|
rm -f "${IMG_FILE}"
|
2016-05-05 14:43:33 -04:00
|
|
|
|
2018-03-02 15:08:24 -05:00
|
|
|
rm -rf "${ROOTFS_DIR}"
|
|
|
|
mkdir -p "${ROOTFS_DIR}"
|
2016-05-05 14:43:33 -04:00
|
|
|
|
2018-03-02 15:08:24 -05:00
|
|
|
BOOT_SIZE=$(du --apparent-size -s "${EXPORT_ROOTFS_DIR}/boot" --block-size=1 | cut -f 1)
|
|
|
|
TOTAL_SIZE=$(du --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --block-size=1 | cut -f 1)
|
2016-05-05 14:43:33 -04:00
|
|
|
|
2017-11-24 02:33:03 -05:00
|
|
|
ROUND_SIZE="$((4 * 1024 * 1024))"
|
2018-03-01 12:52:26 -05:00
|
|
|
ROUNDED_ROOT_SECTOR=$(((2 * BOOT_SIZE + ROUND_SIZE) / ROUND_SIZE * ROUND_SIZE / 512 + 8192))
|
2018-03-01 12:55:25 -05:00
|
|
|
IMG_SIZE=$(((BOOT_SIZE + TOTAL_SIZE + (800 * 1024 * 1024) + ROUND_SIZE - 1) / ROUND_SIZE * ROUND_SIZE))
|
2016-05-05 14:43:33 -04:00
|
|
|
|
2018-03-02 15:08:24 -05:00
|
|
|
truncate -s "${IMG_SIZE}" "${IMG_FILE}"
|
|
|
|
fdisk -H 255 -S 63 "${IMG_FILE}" <<EOF
|
2016-05-05 14:43:33 -04:00
|
|
|
o
|
|
|
|
n
|
|
|
|
|
|
|
|
|
|
|
|
8192
|
2017-03-30 12:51:23 -04:00
|
|
|
+$((BOOT_SIZE * 2 /512))
|
2016-05-05 14:43:33 -04:00
|
|
|
p
|
|
|
|
t
|
|
|
|
c
|
|
|
|
n
|
|
|
|
|
|
|
|
|
2018-03-01 12:52:26 -05:00
|
|
|
${ROUNDED_ROOT_SECTOR}
|
2016-05-05 14:43:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
p
|
|
|
|
w
|
|
|
|
EOF
|
|
|
|
|
2018-03-02 15:08:24 -05:00
|
|
|
PARTED_OUT=$(parted -s "${IMG_FILE}" unit b print)
|
2017-01-23 07:44:03 -05:00
|
|
|
BOOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^ 1'| xargs echo -n \
|
|
|
|
| cut -d" " -f 2 | tr -d B)
|
|
|
|
BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^ 1'| xargs echo -n \
|
|
|
|
| cut -d" " -f 4 | tr -d B)
|
|
|
|
|
|
|
|
ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^ 2'| xargs echo -n \
|
|
|
|
| cut -d" " -f 2 | tr -d B)
|
|
|
|
ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^ 2'| xargs echo -n \
|
|
|
|
| cut -d" " -f 4 | tr -d B)
|
|
|
|
|
2018-03-02 15:08:24 -05:00
|
|
|
BOOT_DEV=$(losetup --show -f -o "${BOOT_OFFSET}" --sizelimit "${BOOT_LENGTH}" "${IMG_FILE}")
|
|
|
|
ROOT_DEV=$(losetup --show -f -o "${ROOT_OFFSET}" --sizelimit "${ROOT_LENGTH}" "${IMG_FILE}")
|
2017-01-23 07:44:03 -05:00
|
|
|
echo "/boot: offset $BOOT_OFFSET, length $BOOT_LENGTH"
|
|
|
|
echo "/: offset $ROOT_OFFSET, length $ROOT_LENGTH"
|
2016-05-05 14:43:33 -04:00
|
|
|
|
2017-07-16 11:27:30 -04:00
|
|
|
ROOT_FEATURES="^huge_file"
|
2017-08-10 10:53:18 -04:00
|
|
|
for FEATURE in metadata_csum 64bit; do
|
|
|
|
if grep -q "$FEATURE" /etc/mke2fs.conf; then
|
|
|
|
ROOT_FEATURES="^$FEATURE,$ROOT_FEATURES"
|
|
|
|
fi
|
|
|
|
done
|
2018-03-02 15:08:24 -05:00
|
|
|
mkdosfs -n boot -F 32 -v "$BOOT_DEV" > /dev/null
|
|
|
|
mkfs.ext4 -L rootfs -O "$ROOT_FEATURES" "$ROOT_DEV" > /dev/null
|
2016-05-05 14:43:33 -04:00
|
|
|
|
2018-03-02 15:08:24 -05:00
|
|
|
mount -v "$ROOT_DEV" "${ROOTFS_DIR}" -t ext4
|
|
|
|
mkdir -p "${ROOTFS_DIR}/boot"
|
|
|
|
mount -v "$BOOT_DEV" "${ROOTFS_DIR}/boot" -t vfat
|
2016-05-05 14:43:33 -04:00
|
|
|
|
2019-02-17 10:29:10 -05:00
|
|
|
rsync -aHAXx --exclude /var/cache/apt/archives --exclude /boot "${EXPORT_ROOTFS_DIR}/" "${ROOTFS_DIR}/"
|
|
|
|
rsync -rtx "${EXPORT_ROOTFS_DIR}/boot/" "${ROOTFS_DIR}/boot/"
|