Maximising partitions at boot time
A possibly suggestion which we discussed on IRC.
Our use case is VM images which are uploaded to cloud VM providers. We build a standard image of a nominal size and LVM partition layout (eg. 8G), but wish to make available all the disk space on the specific VM for later use by LVM.
Example steps to resize:
$ echo ,+ | sfdisk --partno 2 /dev/vda $ lvm pvresize /dev/vda2 $ lvm lvresize +20G
However the "sfdisk" step is critical as it is the only one which cannot be done once the system is mounted, and therefore needs to be done
a) at boot time; or b) at runtime with --force, followed by a reboot.
Boot time possibilies:
-
hooks to add arbitary scripts to initramfs; eg. /etc/mkinitfs/initramfs-hooks/{pre,post}-{lvm,mdadm,cryptsetup,zpool}.d
-
first-class feature in initramfs for this case
Here are some notes from the IRC conversation:
<ncopa> https://gitlab.alpinelinux.org/alpine/mkinitfs/-/blob/master/initramfs-init.in
<ncopa> i think your problem is slightly trickier than it sounds
<ncopa> we have a C app nlplug-findfs
<ncopa> which will trigger coldplug, load kernel modules, and wait for the event with the specific device it is looking for
<ncopa> i think it will mount it too
<ncopa> i think you need run the resize thing before mount of rootfs, but after the disk is coldplugged
<ncopa> other thing you could do, that does not require modification of nlplug-findfs is to rename lvm and replace it with your own wrapper script
<ncopa> mv /sbin/lvm /sbin/lvm.orig
<ncopa> echo -e "#!/bin/sh\ndo-the-thing\nexec /sbin/lvm.orig" > /sbin/lvm
> There's added complication, which is some VM providers use /dev/sda, others use /dev/vda, and Xen uses /dev/xvda ... Right now Alpine handles that all dynamically as LVM finds its own partition and boot is LABEL=boot
<dne> partprobe/partx doesn't work?
> No, neither of those can re-read a partition table if devices are already in use ... unless I missing something
<ncopa> what i wonder is how initramfs should know if it should resize the parition or not
<ncopa> one option is to run hook, which is removed after first boot and initramfs image regenerated without the hook
<ncopa> other option woudl be to specify it as a boot option
<ncopa> "resize-rootfs" or similar
> Not sure about that; I think better to consider an idempotent process that just makes partitions as big as possible within some spec
> Though I do appreciate the risks of that on hardware etc.
<ncopa> how do you know that it is the last partition? the uevents may not come in order
<ncopa> when sda shows up, there are no sda1
<ncopa> that comes as a separate uevent
<ncopa> then sda2 may or may not show up
<ncopa> but you never know
<ncopa> so what im thinking is: if resize-lvm-rootfs option is set, whenever ther shows up an lvm member, try resize it
<ncopa> if its not the last parition, the resize will fail i suppose