Extending XFS root partition on KVM VPS on Centos
Practical steps for resizing root partition for KVM VPS. Our host runs Centos 8, but VPS itself has Centos 7 (with cPanel, but that does not make a big difference). Partitioning is pretty simple – with single big root partition (no separate /home partition) and XFS filesystem.
First we are going to resize disk, create a snapshot of VPS, resize partition, extend filesystem and finally remove snapshot. These steps have been gathered from various articles and I will leave links to them at the end of the article.
Step 1 – resize virtual disk for virtual machine
- Shutdown your virtual machine
- Locate path to virtual disk
# list of all VMs on host
virsh list –all
# information about disk of VM
virsh domblklist your-vm-name - Extend virtual drive by 50gb
qemu-img resize <full_path_to_virtual_disk_file> +50G
You can now boot your virtual machine.
Step 2 – create snapshot of the virtual machine
This is very important step to do – in case something goes wrong with resizing of the partitions – we will be able to revert our changes back. Never skip this step, no matter what environment you are running – partition and filesystem operations can sometimes go wrong.
- list all your virtual machines
virsh list –all - check, that virtual machine you are working on does not have other running snapshots. Even though it is nothing wrong to have several snapshots – to minimize risk of mistakes – better to work on a machine without active snapshots. You can create snapshot on running machine or on powered-off.
virsh snapshot-list –domain <your-virtual-machine-name> - Create snapshot.
virsh snapshot-create-as –domain <your virtual machine name> –name <name for snapshot> –description “my snapshot” - Verify that snapshot is up and running
virsh snapshot-list –domain <your-virtual-machine-name>
Step 3 – resizing (extending) partition and XFS filesystem
Classical flow of this procedure is to delete partition, create new bigger one, reboot and resize filesystem. If your filesystem is using “whole” disk without partition – then you can simply extend virtual disk and resize filesystem. Please keep in mind – yes, we are resizing “/” (root) partition on a running server without unmounting and that in our case – it is XFS filesystem.
- start fdisk for desired drive
fdisk /dev/vda - print current partition table and locate partition you want to extend. This example only works if you are resizing “last” partition on the disk. If your partition is squeezed between other partitions from both sides – please stop now and look for another tutorial or try in safe environment!
– use “p” inside fdisk console to print partition information - Delete partition with “d”
- Create new partition with “n” – keep in mind if you are creating primary (hopefully) or extended partition. New partition should be the same type and should start at same place, where the original partition was. Note: on AlmaLinux8 (CloudLinux8) when creating new partition I got warning Partition #3 contains a xfs signature. Do you want to remove the signature? – select “No” because we are keeping existing partition type!
- Save and quit fdisk with “w”
- Reboot your server, so that the new partition table becomes active.
Note: on AlmaLinux8 / CloudLinux8 I was able to resize filesystem right after creating resized partition, even without reboot.
Step 4 – resize XFS filesystem
This step is safe and simple – if your server came back up and is running without issues – you should run one command to extend your XFS filesystem:
- resizing root “/” partition with
xfs_growfs / - check that you already have more space with
df -h
Step 5 – removing snapshot
Just as you should not skip creating snapshots – you should also remember to remove the snapshot, so it does not grow too big and take all the space on host machine.
- list of active snapshots for virtual machine
virsh snapshot-list –domain <name of virtual machine> - delete snapshot
virsh snapshot-delete –domain <name of virtual machine> –snapshotname <name of snapshot> - check, that everything is good and clean
virsh snapshot-list –domain <name of virtual machine>
That’s it for today!
Links to source articles – don’t hesitate to check them, as they have better examples and more deep explanations of each command.
- resize KVM disk
https://computingforgeeks.com/how-to-extend-increase-kvm-virtual-machine-disk-size/ - KVM snapshot management
https://www.linuxtechi.com/create-revert-delete-kvm-virtual-machine-snapshot-virsh-command/ - partition and filesystem resize
https://nerdbyday.com/resizing-an-ext4-partition-without-unmounting/