Automatically resize cloud VM filesystem when disk size increases on Linux

One of the good things that cloud brings is that you can easily increase size of your VM disks as they fill up. Unfortunately the process is a bit manual because you also need to resize your filesystem in order to see the change. GCP has this process documented.

We can automate this with a udev rule. This was tested on GCP and Ubuntu 20.04 with ext4.

Add a new rule, for example in
SUBSYSTEM=="block", ENV{RESIZE}=="1", ACTION=="change", RUN+="/bin/sh -c 'resize2fs /dev/$name'"
Followed by
udevadm control --reload-rules
This rule listens for change events in block device subsystem and filters by the RESIZE property. If conditions are met, it will adjust your filesystem to use the rest of the free space.

You can make the udev rule more specific to target only sdb or sdc for example. I could not find a good documentation on udev rules that would explain exactly which event properties need to use the == selector (for example SUBSYSTEM), which are considered ENV (supposedly these are any custom properties) and which should use an ATTRS filter.

The property could possibly differ depending on OS and cloud provider, so before adding the rule, you can listen for the events and see the full event output while you change the disk size with
udevadm monitor --udev --property --subsystem-match=block

682 Total Views 1 Views Today