export autoresize autoresize_unit define autoresize := #!/bin/sh rootfs="$$(mount |grep -w / |cut -d' ' -f1)" roottype="$$(mount |grep -w / |cut -d' ' -f5)" 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 case $$roottype in ext[234]) resize2fs "$$rootfs";; btrfs) btrfs filesystem resize max /;; nilfs2) nilfs-resize -y "$$rootfs";; esac systemctl disable autoresize else # finish first stage shutdown -r now fi endef define autoresize_unit := [Unit] Description=Resize root filesystem during first 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