From 26d0a88b677090739f85245f3530d16b95446b51 Mon Sep 17 00:00:00 2001 From: Gildas Le Nadan <3ntr0p13+github@gmail.com> Date: Fri, 27 Dec 2024 22:49:41 +1000 Subject: [PATCH 1/3] Make sure we label the root filesystem with rootfs so we can reliably mount it using LABEL=rootfs in fstab --- bin/shrink-img.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bin/shrink-img.sh b/bin/shrink-img.sh index 9c25ee1..9289d61 100644 --- a/bin/shrink-img.sh +++ b/bin/shrink-img.sh @@ -56,6 +56,8 @@ function shrink() { EXTRA_BLOCKS=$(echo "1024 * 1024 * 20 / $BLOCK_SIZE" | bc) SIZE_BLOCKS=$(echo "$MIN_BLOCKS + $EXTRA_BLOCKS" | bc) resize2fs "$ROOT_PART" "$SIZE_BLOCKS" + # Make sure it has the label "rootfs" so we can mount it using LABEL in fstab + tune2fs -L rootfs "$ROOT_PART" sync && sleep 1 kpartx -d "$IMG_FILE" From e7fece8d527b4483c5d50d5255dfd050db805008 Mon Sep 17 00:00:00 2001 From: Gildas Le Nadan <3ntr0p13+github@gmail.com> Date: Fri, 27 Dec 2024 23:59:40 +1000 Subject: [PATCH 2/3] we also need to label boot which is a vfat so we need to install dosfstools first --- .github/workflows/build.yml | 4 ++-- bin/shrink-img.sh | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 939211c..80960a7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -29,10 +29,10 @@ jobs: - name: Checkout Repository uses: actions/checkout@v2 - - name: install kpartx + - name: install kpartx and dosfstools run: | sudo apt update - sudo apt install -y kpartx + sudo apt install -y kpartx dosfstools - name: install qemu build dependencies run: | diff --git a/bin/shrink-img.sh b/bin/shrink-img.sh index 9289d61..ccc807c 100644 --- a/bin/shrink-img.sh +++ b/bin/shrink-img.sh @@ -24,6 +24,7 @@ function shrink() { check_bin resize2fs check_bin parted check_bin truncate + check_bin fatlabel local OLD_SIZE local BLOCK_SIZE @@ -45,10 +46,16 @@ function shrink() { LOOP_DEV=$(losetup -O NAME,BACK-FILE -l -n | grep "$IMG_FILE" | sed "s#^/dev/\(loop[0-9]*\) *.*#\1#") - local PART_NUM=2 - local ROOT_PART=/dev/mapper/${LOOP_DEV}p${PART_NUM} + local BOOT_PART_NUM=1 + local ROOT_PART_NUM=2 + local BOOT_PART=/dev/mapper/${LOOP_DEV}p${BOOT_PART_NUM} + local ROOT_PART=/dev/mapper/${LOOP_DEV}p${ROOT_PART_NUM} e2fsck -fy "$ROOT_PART" + # Make sure partitions have the expected label so we can mount them using LABEL in fstab + tune2fs -L rootfs "$ROOT_PART" + fatlabel "$BOOT_PART" "boot" + BLOCK_SIZE=$(tune2fs -l "$ROOT_PART" | awk '/^Block size/ {print $NF}') MIN_BLOCKS=$(resize2fs -P "$ROOT_PART" | awk -F': ' '{print $2}') @@ -56,8 +63,6 @@ function shrink() { EXTRA_BLOCKS=$(echo "1024 * 1024 * 20 / $BLOCK_SIZE" | bc) SIZE_BLOCKS=$(echo "$MIN_BLOCKS + $EXTRA_BLOCKS" | bc) resize2fs "$ROOT_PART" "$SIZE_BLOCKS" - # Make sure it has the label "rootfs" so we can mount it using LABEL in fstab - tune2fs -L rootfs "$ROOT_PART" sync && sleep 1 kpartx -d "$IMG_FILE" @@ -65,7 +70,7 @@ function shrink() { FIRST_BYTE=$(parted -m "$IMG_FILE" unit B print | tail -1 | cut -d':' -f2 | tr -d 'B') LAST_BYTE=$(echo "$FIRST_BYTE + $SIZE_BYTES" | bc) - parted "$IMG_FILE" rm "$PART_NUM" + parted "$IMG_FILE" rm "$ROOT_PART_NUM" parted "$IMG_FILE" unit B mkpart primary "$FIRST_BYTE" "$LAST_BYTE" FINAL_SIZE=$(parted -m "$IMG_FILE" unit B print free | tail -1 | cut -d':' -f2 | tr -d 'B') truncate --size "$FINAL_SIZE" "$IMG_FILE" From ac97e7539f88bf258054cbad28c27f66f83ebe15 Mon Sep 17 00:00:00 2001 From: Gildas Le Nadan <3ntr0p13+github@gmail.com> Date: Sat, 28 Dec 2024 12:04:59 +1000 Subject: [PATCH 3/3] Add test for filesystem labels --- goss/goss_system.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/goss/goss_system.yaml b/goss/goss_system.yaml index c40fea1..5df38d3 100644 --- a/goss/goss_system.yaml +++ b/goss/goss_system.yaml @@ -27,3 +27,19 @@ file: contents: [ 'root=/dev/mmcblk0p2' ] +command: + 'rootfs label': + exit-status: 0 + exec: "findmnt -n -o LABEL /" + stdout: + - rootfs + stderr: [] + timeout: 1000 + skip: false + 'bootfs label': + exit-status: 0 + exec: "findmnt -n -o LABEL /boot" + stdout: + - boot + stderr: [] + timeout: 1000 \ No newline at end of file