From 107a8d7f321374ffd5dbfa35803fb0acc46353f0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Wed, 24 Jul 2019 00:25:20 +0200 Subject: [PATCH] resize rootfs partition during first boot --- modules/autoresize.mk | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 modules/autoresize.mk diff --git a/modules/autoresize.mk b/modules/autoresize.mk new file mode 100644 index 0000000..fba31eb --- /dev/null +++ b/modules/autoresize.mk @@ -0,0 +1,51 @@ +export autoresize autoresize_unit + +define autoresize := +#!/bin/sh + +rootfs="$$(mount |grep -w / |cut -d' ' -f1)" + +case $$rootfs in + /dev/mmcblk?p2) rootdev="$${rootfs%p2}";; + /dev/[sh]d?2) rootdev="$${rootfs%2}";; + *) exit 0;; +esac + +before="$$(sfdisk --dump "$$rootdev")" + +sfdisk --dump "$$rootdev" |sed '$$s;, size=.*,;,;;' \ +| sfdisk --force "$$rootdev" + +after="$$(sfdisk --dump "$$rootdev")" + +if [ "$$before" = "$$after" ]; then + # second stage + resize2fs "$$rootfs" + systemctl disable autoresize +else + # finish first stage + shutdown -r now +fi + +endef + +define autoresize_unit := +[Unit] +Description=Resize root filesystem at boot + +[Service] +ExecStart=/opt/autoresize.sh + +[Install] +WantedBy=multi-user.target +endef + +.PHONY: _autoresize +_config: _autoresize + +_autoresize: ${CFGROOT} + mkdir -p "$${CFGROOT}/etc/systemd/system/" + printf '%s\n' "$$autoresize" >"$${CFGROOT}/opt/autoresize.sh" + chmod 755 "$${CFGROOT}/opt/autoresize.sh" + printf '%s\n' "$$autoresize_unit" >"$${CFGROOT}/etc/systemd/system/autoresize.service" + chroot "$${CFGROOT}" systemctl enable autoresize -- 2.39.2