Running out of disk space is one of the most common VM guest issues. You might have started out with an intentionally small disk while you tested a new VM. Over time you accumulate files until the virtual disk is almost full. Here’s how to resize your disk in-place, without having to copy files to a separate larger drive.
Shut Down Your VM
Begin by shutting down your virtual machine. You can shutdown within the guest operating system, using the controls in your management UI, or by issuing the
$ sudo virsh list
Id Name State
- ubuntu22.04 running
$ sudo virsh shutdown ubuntu22.04
Domain ubuntu22.04 is being shutdown
Wait until the VM is powered off. It will disappear from the
virsh list output after it shuts down. You can see details of inactive VMs by adding the
$ sudo virsh list --all
Id Name State
- ubuntu22.04 shut off
Find The Disk’s Location
Next you need to identify the path where your VM’s disk is saved. Each VM gets a disk image file that stores the complete content of its filesystem. Several formats can be used including QCOW2, VDI, VMDK, and VHDX.
Most VMs created in a modern KVM environment will have a QCOW2 disk image stored inside the
/var/lib/libvirt/images directory. The file’s name will match the name of the VM that’s displayed in the
virsh list command’s output.
You can check the path to a VM’s disk by running
$ sudo virsh domblklist ubuntu22.04
This confirms the file to resize for this VM is
Delete VM Snapshots
You can’t resize disk images which have snapshots stored against them. You’ll have to delete snapshots before you perform the resize. If you need to keep snapshots, clone the VM first and use the copy to access the snapshotted states. You can then delete the snapshots from the original VM and continue your resize operation.
virsh snapshot-list command will reveal whether your VM has any snapshots:
$ virsh snapshot-list ubuntu22.04
Name Creation Time State
13-09-2022 2022-09-13 22:36:41 +0100 shutoff
Delete them with the
snapshot-delete command. The snapshot to delete is identified using
<vm-name> <snapshot-name> syntax.
$ virsh snapshot-delete ubuntu22.04 13-09-2022
Resizing the Disk
qemu-img command can now be used to resize the disk. Its arguments are the path to the disk image file and the amount of extra capacity to add to the disk.
$ sudo qemu-img resize /var/lib/libvirt/images/ubuntu22.04.qcow2 +16G
+16G means the disk will become 16 GB larger than its current size. You can use other standard suffixes too such as M for megabytes and T for terabytes. Omitting the
+ notation instructs QEMU to make the disc a specific size, such as
1T to provide 1 TB of capacity.
The resize process should only take a few seconds. It won’t necessarily have an immediate effect on your host’s total disk usage depending on the image format used.
Extending the Disk’s Partitions
Next boot up your virtual machine and use the guest operating system’s disk management utility to extend the disc’s partition table into the newly allocated free space. Without this step your new capacity won’t be accessible to the operating system.
resize2fs command will automatically grow a designated partition to the maximum extent allowed by any available free space:
$ sudo resize2fs /dev/sda
Find the right device to target (
/dev/sda in the command above) by running
df -h and looking for the filesystem that’s mounted to
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda 32G 17G 15G 53% /
To resize the partition graphically you could use GParted or GNOME Disks in a Linux guest or Disk Management for a Windows machine.
Here’s the Ubuntu 22.04 virtual machine that had its disk resized by 16 GB in the example above. After starting the guest and opening GParted, the 16 GB of free space is visible as an unallocated area at the end of the disk.
The space is brought into use by right-clicking the existing system partition, selecting “Resize/Move” from GParted’s menu, and growing the partition into the free space at the end. After applying the operation, the partition has a usable capacity of 48 GB as expected.
Downsizing a Disk
Disk images can be downsized if your VM’s been allocated more storage than it needs. This is a potentially risky operation. Backup your disk before you start the resize so you can recover the situation if data gets deleted:
$ sudo cp /var/lib/libvirt/images/ubuntu22.04.qcow2 ubuntu22.04.qcow2.bak
Next you must adjust the partition table from within the VM so that there’s unallocated free space available on the disk. When you’re resizing a 32 GB disk to 20 GB, you should ensure there’s at least 12 GB of free space at the end of the partition table, for example. Forgetting this step will cause the resize to indiscriminately delete data from the end of the disk, corrupting the partition table and leaving your VM unbootable.
This Windows 10 machine has a 40 GB disk of which only 32 GB is mapped into partitions. The 8 GB unallocated space at the end of the drive means you can safely downsize the disk by up to 8 GB.
Once you’ve prepared your partitions, you can use
qemu-img resize to perform the downsize. Support for this operation must be manually enabled every time by including the
--shrink flag with your command. This lets you specify a target size smaller than the disk’s current capacity. You can also
-8G syntax to downsize by a predetermined amount.
$ qemu-img resize --shrink /var/lib/libvirt/images/win10.qcow2 -8G
After rebooting the guest, Windows confirms the 8GB free space has been removed from the end of the disk.
Disk images used by KVM virtual machines can be resized with the
qemu-img commands. Resizing a disk is reliable and should complete in a matter of seconds but it doesn’t work when VMs have stored snapshots. It’s always a good idea to back up your disk image before you start work.
Some KVM GUIs such as GNOME Boxes include support for resizes, letting you enlarge a disk without using the terminal. However you apply the resize, you must boot up the guest VM afterwards and adjust the partition table to use the space. This allows the operating system to utilize the increased disk capacity.