- Shut down the virtual machine
- Resize the image with
qemu-img resize image.qcow2 +SIZE
SIZEis the size (e.g.
10Gfor 10 gigabytes).
- Boot into gparted live and resize the partition.
- Boot into gparted live and resize the partition smaller.
- If you managed to resize the partition from within the virtual machine (and thus didn’t shut it down already for resizing), shut it down now.
- KVM/QEMU images are stored in
/var/lib/libvirt/imagesby default. I store them under
/storage/imagesfor my purposes. This process is done by root-only, so
suis acceptable in this case. Create a new smaller qcow2 file to store the copied data
qemu-img create -f qcow2 -o preallocation=metadata newimage.qcow2 NEW_SIZE
NEW_SIZEis the size (
10Gfor the example at the start).
- Resize the image by copying the old image into the new one.
virt-resize oldimage.qcow2 newimage.qcow2If the image created in the previous step is larger than the combined partitions on the old image,
virt-resizewill inform you of a surplus and create a new partition. You can still terminate the process without data loss and go back to step #3 to create a smaller image. If the image is smaller than the partitions,
virt-resizewill fail and inform you how much space needs to be added. In this case, you must create a larger image in step #3.
- Start your VM. There may be some disk errors related to the stored block lengths.
fsckshould be able to automatically fix them. If
virt-resizecreated an extra partition, you can now use a partition editor to delete it and add the space to another partition.
- Once you have verified that the VM is working as expected, you can safely remove the old image.
This article is a reference to the original article at: https://maunium.net/blog/resizing-qcow2-images/