clic-rpi-image/export-image/prerun.sh

62 lines
2.5 KiB
Bash
Raw Normal View History

2016-05-05 18:43:33 +00:00
#!/bin/bash -e
IMG_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img"
2016-05-05 18:43:33 +00:00
2018-03-02 20:08:24 +00:00
unmount_image "${IMG_FILE}"
2016-05-05 18:43:33 +00:00
2018-03-02 20:08:24 +00:00
rm -f "${IMG_FILE}"
2016-05-05 18:43:33 +00:00
2018-03-02 20:08:24 +00:00
rm -rf "${ROOTFS_DIR}"
mkdir -p "${ROOTFS_DIR}"
2016-05-05 18:43:33 +00:00
2019-06-07 14:51:06 +00:00
BOOT_SIZE="$((256 * 1024 * 1024))"
Use parted for partitioning (#285) Previously, fdisk was used by sending commands into its stdin, which is not very robust (since it heavily relies on the interactive prompts offered by fdisk as well as the default values it offers, which seem prone to changing in future version). It seems likely that in the past, fdisk was easier than parted since it provides default values that make it easier to create adjacent partitions, without precalculating all positions in the script. However now that partitions are manually being aligned, all data must be calculated anyway. This commit changes the partition generation to use parted rather than fdisk. For this, it rewrites various calculations and renames variables to be easier to read as well. All values are now in number of bytes, rather than mixing bytes and sectors. This commit also makes makes sure that the boot partition and root partition are always adjacent (previously the root partition was aligned without also rounding the boot partition size, leaving some empty space in between). As a side effect of using parted, this also causes the "bootcode" part of the MBR to be filled with some default x86 bootcode. This is totally irrelevant for booting the Raspberry Pi, but it does prevent triggering a bug in parted. When using parted to change the partition table (e.g. when resizing the root partition on first boot by raspi-config's init_resize.sh), the disk identifier would be changed due to this bug, which would change the PARTUUID of all partitions. The init_resize.sh script would work around this by updating the PARTUUID in e.g. fstab, but that's fragile at best. This commit prevents the bug from triggering and keeps the disk identifier the same. See https://debbugs.gnu.org/35714 for details about this parted bug. This commit fixes #284.
2019-09-25 12:46:38 +00:00
ROOT_SIZE=$(du --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --exclude boot --block-size=1 | cut -f 1)
# All partition sizes and starts will be aligned to this size
ALIGN="$((4 * 1024 * 1024))"
# Add this much space to the calculated file size. This allows for
# some overhead (since actual space usage is usually rounded up to the
# filesystem block size) and gives some free space on the resulting
# image.
2020-01-24 12:48:57 +00:00
ROOT_MARGIN="$(echo "($ROOT_SIZE * 0.2 + 200 * 1024 * 1024) / 1" | bc)"
Use parted for partitioning (#285) Previously, fdisk was used by sending commands into its stdin, which is not very robust (since it heavily relies on the interactive prompts offered by fdisk as well as the default values it offers, which seem prone to changing in future version). It seems likely that in the past, fdisk was easier than parted since it provides default values that make it easier to create adjacent partitions, without precalculating all positions in the script. However now that partitions are manually being aligned, all data must be calculated anyway. This commit changes the partition generation to use parted rather than fdisk. For this, it rewrites various calculations and renames variables to be easier to read as well. All values are now in number of bytes, rather than mixing bytes and sectors. This commit also makes makes sure that the boot partition and root partition are always adjacent (previously the root partition was aligned without also rounding the boot partition size, leaving some empty space in between). As a side effect of using parted, this also causes the "bootcode" part of the MBR to be filled with some default x86 bootcode. This is totally irrelevant for booting the Raspberry Pi, but it does prevent triggering a bug in parted. When using parted to change the partition table (e.g. when resizing the root partition on first boot by raspi-config's init_resize.sh), the disk identifier would be changed due to this bug, which would change the PARTUUID of all partitions. The init_resize.sh script would work around this by updating the PARTUUID in e.g. fstab, but that's fragile at best. This commit prevents the bug from triggering and keeps the disk identifier the same. See https://debbugs.gnu.org/35714 for details about this parted bug. This commit fixes #284.
2019-09-25 12:46:38 +00:00
BOOT_PART_START=$((ALIGN))
BOOT_PART_SIZE=$(((BOOT_SIZE + ALIGN - 1) / ALIGN * ALIGN))
ROOT_PART_START=$((BOOT_PART_START + BOOT_PART_SIZE))
ROOT_PART_SIZE=$(((ROOT_SIZE + ROOT_MARGIN + ALIGN - 1) / ALIGN * ALIGN))
IMG_SIZE=$((BOOT_PART_START + BOOT_PART_SIZE + ROOT_PART_SIZE))
2016-05-05 18:43:33 +00:00
2018-03-02 20:08:24 +00:00
truncate -s "${IMG_SIZE}" "${IMG_FILE}"
2016-05-05 18:43:33 +00:00
Use parted for partitioning (#285) Previously, fdisk was used by sending commands into its stdin, which is not very robust (since it heavily relies on the interactive prompts offered by fdisk as well as the default values it offers, which seem prone to changing in future version). It seems likely that in the past, fdisk was easier than parted since it provides default values that make it easier to create adjacent partitions, without precalculating all positions in the script. However now that partitions are manually being aligned, all data must be calculated anyway. This commit changes the partition generation to use parted rather than fdisk. For this, it rewrites various calculations and renames variables to be easier to read as well. All values are now in number of bytes, rather than mixing bytes and sectors. This commit also makes makes sure that the boot partition and root partition are always adjacent (previously the root partition was aligned without also rounding the boot partition size, leaving some empty space in between). As a side effect of using parted, this also causes the "bootcode" part of the MBR to be filled with some default x86 bootcode. This is totally irrelevant for booting the Raspberry Pi, but it does prevent triggering a bug in parted. When using parted to change the partition table (e.g. when resizing the root partition on first boot by raspi-config's init_resize.sh), the disk identifier would be changed due to this bug, which would change the PARTUUID of all partitions. The init_resize.sh script would work around this by updating the PARTUUID in e.g. fstab, but that's fragile at best. This commit prevents the bug from triggering and keeps the disk identifier the same. See https://debbugs.gnu.org/35714 for details about this parted bug. This commit fixes #284.
2019-09-25 12:46:38 +00:00
parted --script "${IMG_FILE}" mklabel msdos
parted --script "${IMG_FILE}" unit B mkpart primary fat32 "${BOOT_PART_START}" "$((BOOT_PART_START + BOOT_PART_SIZE - 1))"
parted --script "${IMG_FILE}" unit B mkpart primary ext4 "${ROOT_PART_START}" "$((ROOT_PART_START + ROOT_PART_SIZE - 1))"
2016-05-05 18:43:33 +00:00
PARTED_OUT=$(parted -sm "${IMG_FILE}" unit b print)
BOOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 2 | tr -d B)
BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 4 | tr -d B)
ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 2 | tr -d B)
ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 4 | tr -d B)
2018-03-02 20:08:24 +00: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}")
echo "/boot: offset $BOOT_OFFSET, length $BOOT_LENGTH"
echo "/: offset $ROOT_OFFSET, length $ROOT_LENGTH"
2016-05-05 18:43:33 +00:00
ROOT_FEATURES="^huge_file"
2017-08-10 14:53:18 +00: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 20:08:24 +00: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 18:43:33 +00:00
2018-03-02 20:08:24 +00: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 18:43:33 +00:00
rsync -aHAXx --exclude /var/cache/apt/archives --exclude /boot "${EXPORT_ROOTFS_DIR}/" "${ROOTFS_DIR}/"
rsync -rtx "${EXPORT_ROOTFS_DIR}/boot/" "${ROOTFS_DIR}/boot/"