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 virsh shutdown
command:
$ 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 --all
flag:
$ 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 virsh domblklist
:
$ sudo virsh domblklist ubuntu22.04 Target Source
----------------------------------------------------- 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.
The 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
The +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
sudo lvextend -l 100%VG ubuntu-vg/ubuntu-lv
sudo fdisk -l /dev/mapper/ubuntu--vg-ubuntu--lv
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv