KVM is a virtualization technologythat’s integrated into the Linux kernel. you may create virtual machines and interact with KVM using tools such asvirsh,virt-manager, andGNOME Boxes.

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.

Screenshot of partitions in gParted

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


Screenshot of partitions in gParted

  • ubuntu22.04 running

$ sudo virsh shutdown ubuntu22.04

Screenshot of Windows 10’s Disk Management utility

Domain ubuntu22.04 is being shutdown

Wait until the VM is powered off. It will disappear from thevirsh listoutput after it shuts down. You can see details of inactive VMs by adding the–allflag:

Screenshot of Windows 10’s Disk Management utility

  • 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/imagesdirectory. The file’s name will match the name of the VM that’s displayed in thevirsh listcommand’s output.

You can check the path to a VM’s disk by runningvirsh domblklist:


sdb /var/lib/libvirt/images/ubuntu22.04.qcow2

This confirms the file to resize for this VM is/var/lib/libvirt/images/ubuntu22.04.qcow2.

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.


01-08-2025 2025-05-29 22:36:41 +0100 shutoff

Delete them with thesnapshot-deletecommand. The snapshot to delete is identified using syntax.

Resizing the Disk

Theqemu-imgcommand 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.

The+16Gmeans 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 as1Tto 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.

Linux’sresize2fscommand will automatically grow a designated partition to the maximum extent allowed by any available free space:

Find the right device to target (/dev/sdain the command above) by runningdf -hand looking for the filesystem that’s mounted to/:

Filesystem Size Used Avail Use% Mounted on

/dev/sda 32G 17G 15G 53% /

To resize the partition graphically you coulduse GPartedor GNOME Disks in a Linux guest orDisk 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’re able to recover the situation if data gets deleted:

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 may safely downsize the disk by up to 8 GB.

Once you’ve prepared your partitions, you can useqemu-img resizeto perform the downsize. Support for this operation must be manually enabled every time by including the–shrinkflag with your command. This lets you specify a target size smaller than the disk’s current capacity. You can also-8Gsyntax to downsize by a predetermined amount.

After rebooting the guest, Windows confirms the 8GB free space has been removed from the end of the disk.

Summary

Disk images used by KVM virtual machines can be resized with thevirshandqemu-imgcommands. 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 asGNOME Boxesinclude 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.