When running Virtual Machines with KVM, there will likely come a time when you need to expand the size of your guest disk and possibly resize partitions as well. To meet these needs, the virt-resize
tool is available. It allows you to increase or decrease the size of a virtual disk, resize partitions, and even create new disk images based on an existing one.
✨ Excerpt from Mastering KVM Virtualization. Get the complete eBook to unlock hands-on labs, tips, and expert strategies. Download now.
Ensure the package that provides virt-resize
# Debian-based OS systems
sudo apt update
sudo apt install libguestfs-tools
# RHEL-based OS systems
sudo dnf install libguestfs-tools guestfs-tools
# SUSE / openSUSE
sudo zypper -n install guestfs-tools
# Arch-based Linux
sudo pacman -S libguestfs guestfs-tools
virt-resize
only works for guest virtual machines that are offline (shut down). A running guest can be powered off using the command:
virsh shutdown <vm-name>
It operates by copying the guest virtual machine image, leaving the original disk image unchanged. The trade-off is that you need twice the amount of disk space.
Expanding disk image
Locate the disk image to be resized. The virsh dumpxml
subcommand can be used:
virsh dumpxml <vm-name>
Set guest vm disk image as variable.
DISK_IMAGE=/path/vmdisk.qcow2
It’s strongly advised to back up the current disk before making any modifications to safeguard against potential issues or data loss.
# Optionally backup if you have enough storage capacity
cp $DISK_IMAGE <new-backup>.qcow2
Run virt-df -h
and virt-filesystems
on the guest virtual machine disk to check disk size and partition layout.
sudo virt-df -h -a $DISK_IMAGE
sudo virt-filesystems --all --long -a $DISK_IMAGE
Execution command output:
$ sudo virt-df -h -a $DISK_IMAGE
Filesystem Size Used Available Use%
deb12-0.qcow2:/dev/sda1 20G 1.2G 18G 7%
deb12-0.qcow2:/dev/sda15 124M 12M 112M 10%
$ sudo virt-filesystems -a $DISK_IMAGE --all --long
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem ext4 - - 20961435648 -
/dev/sda14 filesystem unknown - - 3145728 -
/dev/sda15 filesystem vfat - - 129718272 -
/dev/sda1 partition - - - 21340601856 /dev/sda
/dev/sda14 partition - - - 3145728 /dev/sda
/dev/sda15 partition - - - 130023424 /dev/sda
/dev/sda device - - - 21474836480 -
In the following example we demonstrate how to resize:
- Increate the total disk size of the guest from 20GB to 40GB
- Expand the boot partition from current size to 500MB
- Expand the second partition (/) to fill the remaining space
Begin by creating a new bigger disk to match your desired size. The following command will create a 50GB
disk.
qemu-img create -f qcow2 -o preallocation=metadata newdisk.qcow2 50G
Perform the resizing operation from the original disk image to new disk.
sudo virt-resize \
$DISK_IMAGE newdisk.qcow2 \
--resize /dev/sda15=500M \
--expand /dev/sda1
Where:
--resize <part=size>
– Used to resize partition--expand <part>
– Used to expand partition
Here is an output from above command execution:
**********
Summary of changes:
virt-resize: /dev/sda14: This partition will be left alone.
filesystem on /dev/sda15
virt-resize: /dev/sda15: This partition will be resized from 124.0M to
500.0M.
virt-resize: /dev/sda1: This partition will be resized from 19.9G to 49.5G.
The filesystem ext4 on /dev/sda1 will be expanded using the
‘resize2fs’ method.
**********
[ 1.5] Setting up initial partition table on newdisk.qcow2
[ 12.2] Copying /dev/sda14
[ 12.2] Copying /dev/sda15
[ 12.3] Copying /dev/sda1
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[ 21.4] Expanding /dev/sda1 (now /dev/sda3) using the ‘resize2fs’ method
virt-resize: Resize operation completed with no errors. Before deleting
the old disk, carefully check that the resized disk boots and works
correctly.
Inspect the new disk image using qemu-img
qemu-img info newdisk.qcow2
We can confirm the new disk size is 50 GB
image: newdisk.qcow2
file format: qcow2
virtual size: 50 GiB (53687091200 bytes)
disk size: 1.47 GiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false
Child node '/file':
filename: newdisk.qcow2
protocol type: file
file length: 50 GiB (53695545344 bytes)
disk size: 1.47 GiB
Move new disk to the old disk before starting the VM instance.
sudo mv newdisk.qcow2 $DISK_IMAGE
When done start the VM
sudo virsh start <vm-name>
Verify that the filesystems have grown as expected:
sudo virt-df -h -a $DISK_IMAGE
sudo virt-filesystems -a $DISK_IMAGE --all --long
Alternatively, login to the VM and confirm disk partitions
$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 392M 540K 391M 1% /run
/dev/vda3 ext4 49G 1.2G 46G 3% /
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/vda2 vfat 124M 12M 113M 10% /boot/efi
tmpfs tmpfs 392M 0 392M 0% /run/user/1000
Expanding LVM partition
If you’re using LVM, after increasing the disk size, boot the guest VM and use the following commands to expand the LVM partition.
# List disk / partition / mount point
lsblk
df -h
# Check VG and LV
sudo vgs
sudo lvdisplay
# Extend LV fo full VG capacity
sudo lvextend -r -l +100%free /dev/<vgname>/<lvname>
# Resize LV associated filesystem
## Ext filesystems - ext4, ext3
sudo resize2fs /dev/<vgname>/<lvname>
## XFS filesystem
sudo xfs_growfs /mountpath
Want to master more than just the basics? This guide is part of Mastering KVM Virtualization. Explore the full eBook today. Read or download.