Table of Contents
In this example, I will take you through the steps to resize qcow2 image with virt-resize using 6 Easy Steps. If you are running multiple virtual machines using qcow2 image and let's suppose one day you might feel you need some extra storage in your virtual machine then you need to go for resizing. This situation is very frequently encountered and hence it is important to understand the steps required to resize qcow2 image. Here we are basically going to use two kvm tools, virt-resize and qemu-img to resize qcow2 image.
How to Resize qcow2 Image Using virt-resize and qemu-img(KVM tools) in 6 Easy Steps
Also Read: How to Install KVM Server tools(virsh) on Linux(RHEL/CentOS 7/8) in 6 Easy Steps
Step 1: Prerequisites
a) You need to have KVM tools(virt-resize
and qemu-img
) installed in your System.
b) You should have sudo or root access to run privilege commands. To know more about providing sudo
access, you can check Step by Step: How to Add User to Sudoers.
Step 2: Shutdown Your VM
First you need to shutdown your VM using virsh destroy test1
command as shown below. You cannot resize qcow2 image while VM is active and running.
[root@localhost ~]# virsh destroy test1
Step 3: Take the Backup of qcow2 image
Before resizing the qcow2 image you need to take a backup of this image. virt-resize cannot resizes disk in-place so we need to use the backup qcow2 image(test1.qcow2.old
) as input image and original qcow2 image (test1.qcow2
) as output image. Here we will go to directory /u01/data
and take the backup of existing test1.qcow2
image to test1.qcow2.old
.
[root@localhost ~]# cd /u01/data/ [root@localhost ~]# mv test1.qcow2 test1.qcow2.old
Step 4: Create a Raw Image using qemu-img command
Now we need to create a raw qcow2 image with the same size as we need in our virtual machine filesystem. Since we are increasing the size of our VM from 1 GB
to 2 GB
so we will specify the size as 2 GB
.
[root@localhost ~]# qemu-img create -f raw /u01/data/test1.qcow2 2G Formatting '/u01/data/test1.qcow2', fmt=raw size=21990232
-f : First image format. More info on qemu-img Man Page.
Step 5: Resize Original qcow2 using virt-resize command
Now you can perform the resize operation using virt-resize command as shown below. Here we are expanding /dev/sda3
partition from old qcow2 image to fill new spaces using virt-resize --expand /dev/sda3 test1.qcow2.old test1.qcow2
command.
[root@localhost ~]# virt-resize --expand /dev/sda3 test1.qcow2.old test1.qcow2 [ 0.0] Examining test1.qcow2.old ◓ 25% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒═══════════════════════════════════════════════════════════════════════════════════════════════════════════⟧ --:-- 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:-- ********** Summary of changes: /dev/sda1: This partition will be left alone. /dev/sda2: This partition will be left alone. /dev/sda3: This partition will be resized from 1011.2M to 2022.4M. The filesystem xfs on /dev/sda3 will be expanded using the 'xfs_growfs' method. ********** [ 33.3] Setting up initial partition table on test1.qcow2 [ 43.1] Copying /dev/sda1 [ 43.1] Copying /dev/sda2 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00 [ 50.2] Copying /dev/sda3 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00 [4590.4] Expanding /dev/sda3 using the 'xfs_growfs' method Resize operation completed with no errors. Before deleting the old disk, carefully check that the resized disk boots and works correctly.
NOTE:
--expand: Expand the named partition so it uses up all extra space (space left over after any other resize changes that you request have been done). More info on virt-resize Man Page.
Step 6: Start Virtual Machine using virsh command
After successfully resizing of disk partition you can start the virtual machine using virsh start test1
command and check the disk space by logging into the VM. You will find that the disk space is now increased to 2 GB.
[root@localhost ~]#virsh start test1 Domain test1 started
How to resize qcow2 Image using qemu-img(KVM tools) in 2 easy steps:
1. qemu-img resize /path/to/image.qcow2 [+|-]size eg
qemu-img resize /home/user/.local/share/libvirt/images/Windows10-UEFI.qcow2 +24G
2. boot your guest and resize the partition.
you're done.
Perfect!
Thank you very much! As a little addendum:
Before starting the VM again with the new resized volume I had to delete the "old" volume ( I did that within the graphical VirtManager) and reconnect the "new" volume (same name).
Don't now why, but without this step I got this:
kvm:/mnt/VM # virsh start HALn
error: Failed to start domain 'HALn'
error: Interner Fehler: process exited while connecting to monitor: 2024-03-04T05:25:41.287524Z qemu-system-x86_64: -blockdev {"node-name":"libvirt-3-format","read-only":false,"driver":"qcow2","file":"libvirt-3-storage","backing":null}: Image is not in qcow2 format
Again, thanks a lot!
Juergen