]> git.plutz.net Git - blast/blob - modules/autoresize.mk
resize rootfs partition during first boot
[blast] / modules / autoresize.mk
1 export autoresize autoresize_unit
2
3 define autoresize :=
4 #!/bin/sh
5
6 rootfs="$$(mount |grep -w / |cut -d' ' -f1)"
7
8 case $$rootfs in
9   /dev/mmcblk?p2) rootdev="$${rootfs%p2}";;
10   /dev/[sh]d?2) rootdev="$${rootfs%2}";;
11   *) exit 0;;
12 esac
13
14 before="$$(sfdisk --dump "$$rootdev")"
15
16 sfdisk --dump "$$rootdev" |sed '$$s;, size=.*,;,;;' \
17 | sfdisk --force "$$rootdev"
18
19 after="$$(sfdisk --dump "$$rootdev")"
20
21 if [ "$$before" = "$$after" ]; then
22   # second stage
23   resize2fs "$$rootfs"
24   systemctl disable autoresize
25 else
26   # finish first stage
27   shutdown -r now
28 fi
29
30 endef
31
32 define autoresize_unit :=
33 [Unit]
34 Description=Resize root filesystem at boot
35
36 [Service]
37 ExecStart=/opt/autoresize.sh
38
39 [Install]
40 WantedBy=multi-user.target
41 endef
42
43 .PHONY: _autoresize
44 _config: _autoresize
45
46 _autoresize: ${CFGROOT}
47         mkdir -p "$${CFGROOT}/etc/systemd/system/"
48         printf '%s\n' "$$autoresize" >"$${CFGROOT}/opt/autoresize.sh"
49         chmod 755 "$${CFGROOT}/opt/autoresize.sh"
50         printf '%s\n' "$$autoresize_unit" >"$${CFGROOT}/etc/systemd/system/autoresize.service"
51         chroot "$${CFGROOT}" systemctl enable autoresize