From 75568a191bed499d12818cd474cf4474b406b6ab Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 29 Jan 2024 15:09:23 +0100 Subject: [bwlp-stage4-tweaks] Add service to grow rootfs to blockdev size This was previously done in initrd, but it turns out this was happening rather late and blocked the switchroot for a couple seconds. Let's do it in stage 4 instead as early as possible, and order it before gather-hw-info.service, as that service will determine the rootfs free space and needs to see the properly resized rootfs. --- .../data/etc/systemd/system/grow-rootfs.service | 9 ++++ .../sysinit.target.wants/grow-rootfs.service | 1 + .../data/opt/openslx/scripts/systemd-grow_rootfs | 53 ++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 core/modules/bwlp-stage4-tweaks/data/etc/systemd/system/grow-rootfs.service create mode 120000 core/modules/bwlp-stage4-tweaks/data/etc/systemd/system/sysinit.target.wants/grow-rootfs.service create mode 100644 core/modules/bwlp-stage4-tweaks/data/opt/openslx/scripts/systemd-grow_rootfs (limited to 'core') diff --git a/core/modules/bwlp-stage4-tweaks/data/etc/systemd/system/grow-rootfs.service b/core/modules/bwlp-stage4-tweaks/data/etc/systemd/system/grow-rootfs.service new file mode 100644 index 00000000..60985596 --- /dev/null +++ b/core/modules/bwlp-stage4-tweaks/data/etc/systemd/system/grow-rootfs.service @@ -0,0 +1,9 @@ +[Unit] +Description=Grow size of rootfs to underlying block device +DefaultDependencies=no +Before=gather-hw-info.service + +[Service] +Type=oneshot +RemainAfterExit=true +ExecStart=/opt/openslx/scripts/systemd-grow_rootfs diff --git a/core/modules/bwlp-stage4-tweaks/data/etc/systemd/system/sysinit.target.wants/grow-rootfs.service b/core/modules/bwlp-stage4-tweaks/data/etc/systemd/system/sysinit.target.wants/grow-rootfs.service new file mode 120000 index 00000000..57dd5ae4 --- /dev/null +++ b/core/modules/bwlp-stage4-tweaks/data/etc/systemd/system/sysinit.target.wants/grow-rootfs.service @@ -0,0 +1 @@ +../grow-rootfs.service \ No newline at end of file diff --git a/core/modules/bwlp-stage4-tweaks/data/opt/openslx/scripts/systemd-grow_rootfs b/core/modules/bwlp-stage4-tweaks/data/opt/openslx/scripts/systemd-grow_rootfs new file mode 100644 index 00000000..a1b9d22a --- /dev/null +++ b/core/modules/bwlp-stage4-tweaks/data/opt/openslx/scripts/systemd-grow_rootfs @@ -0,0 +1,53 @@ +#!/bin/bash + +# This tries to call growfs helpers (xfs_growfs, resize2fs, ...) to resize +# the root filesystem mounted on $NEWROOT to the maximum size of the backing +# disk partition (done by dmsetup-slx-device). + +. /opt/openslx/config + +if [ -z "$SLX_DNBD3_DEVICE_COW" ]; then + SLX_DNBD3_DEVICE_COW="$( awk '$2 == "/" {print $1; exit}' /proc/mounts )" +fi +if ! [ -b "$SLX_DNBD3_DEVICE_COW" ]; then + echo "Cannot grow rootfs on '$SLX_DNBD3_DEVICE_COW' - not a block device" + exit 0 +fi + +resize_rootfs() { + # First let's check what filesystem it is + local fstype helper + declare -a options + fstype="$(blkid "$SLX_DNBD3_DEVICE_COW" | grep -oE 'TYPE=\S+')" + if [ -z "$fstype" ]; then + echo "Failed to detect filesystem on '$SLX_DNBD3_DEVICE_COW' - ignoring." + return 1 + fi + fstype="${fstype#TYPE=}" + fstype="${fstype//\"/}" + case "$fstype" in + ext?) + helper="resize2fs" + options=() + ;; + xfs) + helper="xfs_growfs" + options=( "-d" ) + ;; + *) + echo "'$fstype' not supported - ignoring." + return 1 + ;; + esac + if ! hash "${helper}" &> /dev/null; then + echo "'$fstype' is supported, but cannot find helper binary - ignoring." + return 1 + fi + if ! "${helper}" "${options[@]}" "$SLX_DNBD3_DEVICE_COW"; then + echo "Failed to run '${helper}' on '${SLX_DNBD3_DEVICE_COW}'." + return 1 + fi + return 0 +} + +resize_rootfs -- cgit v1.2.3-55-g7522