This brief guide explains what is Virt-resize command line tool and how can we expand or extend KVM virtual machine disk size using Virt-resize tool in Linux.
Introduction
The other day I noticed that one of my KVM Virtual machine is running out of disk space. I couldn't install any new applications or couldn't save files due to low disk space.
To fix this issue, I simply increased the size of a KVM virtual machine's disk using virt-size
command. I personally tested the steps given below to increase the disk size of a CentOS 8 KVM virtual machine and it worked as expected.
What is Virt-resize tool?
Virt-resize is a command line tool to resize a virtual machine disk. Using virt-resize, we can either increase or decrease the size of a virtual machine's disk.
We can also delete the partitions inside the virtual disk with virt-resize command. Please note that virt-resize the live machines. The guest systems should be turned off before resizing their disk images.
To expand a Virtual machine's disk size, we first create a larger disk image than the source image. And then we copy the source image to the destination image and finally resize it.
Install Virt-resize in Linux
Virt-resize is the part of the libguestfs-tools
package. To install libguestfs-tools package on Debian, Ubuntu and its derivatives, run:
$ sudo apt install libguestfs-tools
To install libguestfs-tools package on Fedora, RHEL and its clones, run:
$ sudo dnf install libguestfs-tools
In Fedora 34, guestfs-tools
package provides virt-resize tool. So if you're on Fedora 34, install guestfs-tools
package like below:
$ sudo dnf install guestfs-tools
Now let us see how to expand virtual machine disk size using virt-resize
command.
Expand or Extend KVM Virtual Machine Disk Size in Linux
For the purpose of this guide, I will be using CentOS 8 virtual machine disk image named centos8.qcow2
. I have stored this mage in my current working directory. You can use virsh dumpxml
command to locate the disk image that you want to resize.
Example:
$ virsh dumpxml centos8
Replace centos8 with your VM name in the above command.
After locating the disk image you want to resize, inspect the partition details inside this disk image. To print the partition details and their size of a disk image, run:
$ virt-filesystems --long --parts --blkdevs -h -a centos8.qcow2
Sample output:
Name Type MBR Size Parent /dev/sda1 partition 83 1.0G /dev/sda /dev/sda2 partition 8e 19G /dev/sda /dev/sda device - 20G -
As you can see in the above output, my disk image has only two partitions, namely /dev/sda1
with size 1 GB and /dev/sda2
with 19GB. The total size of disk image is 20 GB.
Now we will,
- increase the total disk image size from 20 GB to 30 GB,
- increase the size of the first partition
/dev/sda1
from 1.0GB to 1.5GB, - Expand the second partition
/dev/sda2
to fill the remaining space.
Virt-resize cannot resize disk images in-place. Also, Virt-resize should not be used on live virtual machines. Before resizing disk image, power off the Virtual machine and take the backup of the original disk image.
$ mv centos8.qcow2 centos8.qcow2.backup
The above command will rename current disk image called centos8.qcow2
to centos8.qcow2.backup
.
Create a new disk image with same name and with size 30GB using command:
$ qemu-img create -f qcow2 -o preallocation=metadata centos8.qcow2 30G
Sample output:
Formatting 'centos8.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off preallocation=metadata compression_type=zlib size=32212254720 lazy_refcounts=off refcount_bits=16
Make sure the size of the newly created disk is 30GB:
$ virt-filesystems --long --parts --blkdevs -h -a centos8.qcow2
Sample output:
Name Type MBR Size Parent /dev/sda device - 30G -
Now expand/extend the KVM virtual machine disk image size using command:
$ virt-resize --resize /dev/sda1=+500M --expand /dev/sda2 centos8.qcow2.backup centos8.qcow2
Here,
--resize /dev/sda1=+500M
- increases the size of the/dev/sda1
partition to 500 MB.- --expand /dev/sda2 - resizes the
/dev/sda2
partition to to fill all remaining space. In my case, it will be resized from 19.0G to 28.5G. centos8.qcow2.backup
- it is the original input disk image.centos8.qcow2
- it is the newly created output disk image.
Sample output of the above command is:
[ 0.0] Examining centos8.qcow2.backup Summary of changes: /dev/sda1: This partition will be resized from 1.0G to 1.5G. The filesystem ext4 on /dev/sda1 will be expanded using the ‘resize2fs’ method. /dev/sda2: This partition will be resized from 19.0G to 28.5G. The LVM PV on /dev/sda2 will be expanded using the ‘pvresize’ method. [ 4.2] Setting up initial partition table on centos8.qcow2 [ 5.6] Copying /dev/sda1 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00 [ 9.7] Copying /dev/sda2 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00 [ 45.1] Expanding /dev/sda1 using the ‘resize2fs’ method [ 45.3] Expanding /dev/sda2 using the ‘pvresize’ method Resize operation completed with no errors. Before deleting the old disk, carefully check that the resized disk boots and works correctly.
Check the partitions and their sizes with virt-filesystems
command:
$ virt-filesystems --long -h --all -a centos8.qcow2
Sample output:
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem ext4 - - 1.4G -
/dev/cl/root filesystem xfs - - 17G -
/dev/cl/swap filesystem swap - - 2.0G -
/dev/cl/root lv - - - 17G /dev/cl
/dev/cl/swap lv - - - 2.0G /dev/cl
/dev/cl vg - - - 29G /dev/sda2
/dev/sda2 pv - - - 29G -
/dev/sda1 partition - - 83 1.5G /dev/sda
/dev/sda2 partition - - 8e 29G /dev/sda
/dev/sda device - - - 30G -
As you see in the above output, my CentOS8 KVM disk image size has been resized from 20 GB to 30 GB.
Now boot the virtual machine with new disk image. You can also create a new virtual machine using the Qcow2 image as described in the following link.
If it works without any issues, then thoroughly inspect if all your data exists. If everything is OK, delete the old backup disk.
If there are any issues with new disk, shutdown the virtual machine and rename backup disk to its original name. Repeat the above steps carefully again. Don't forget to backup the data and disk image.
Resource:
Related read:
1 comment
Your explanations guided me through expanding my Win10 VM. It took a couple of tries, but I finally succeeded. The second time around, I didn’t add space to the /sda1, only expanding /sda2 and Windows booted after that. Thank you so much for your great help!