Changing the size of a root volume from a live system is a pain in the rear. There are tricky approaches using pivot_root
to switch the root filesystem without rebooting, but it's usually much easier to boot another system (e.g. a live CD) and modify volumes from there.
In case of Hetzner's cloud service you can simply boot the server with a rescue system and SSH into that system. The rescue system has the same IP that the server normally has, and you can login either via password or SSH key (if you provided one).
After logging in you should first run a filesystem check on the volume and fix any issue the check reports. Then you can use resize2fs
and parted
(or whetever partition management program you prefer) for resizing the volume. First shrink the filesystem, then the partition. You can do clever size calculations so you need to run each command only once with exact sizes. However, I usually find it much more convenient to shrink the filesystem to a slightly smaller size, then shrink the partition to the desired size, then re-grow the filesystem to the partition size.
e2fsck -fp /dev/sda1
resize2fs -M /dev/sda1 9G
echo 'yes' | parted ---pretend-input-tty /dev/sda unit GiB resizepart 1 10GiB
resize2fs /dev/sda1
The parameter -f
tells e2fsck
to run the check even if it perceives the filesystem as clean. The parameter -p
tells it to automatically repair the filesystem (if there are problems).
Running resize2fs
with the parameter -M
moves data to the beginning of the filesystem to reduce the required size.
Reboot the system and check if everything works as intended, then shut it down (may require additional preparation steps like resetting cloud-init, cleaning up logs, etc.) and create the snapshot.