Expanding images
- Shut down the virtual machine
- Resize the image with
qemu-img resize image.qcow2 +SIZE
WhereSIZE
is the size (e.g.10G
for 10 gigabytes). - Boot into gparted live and resize the partition.
Shrinking images
- 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/images
by default. I store them under/storage/images
for my purposes. This process is done by root-only, sosu
is acceptable in this case. Create a new smaller qcow2 file to store the copied dataqemu-img create -f qcow2 -o preallocation=metadata newimage.qcow2 NEW_SIZE
whereNEW_SIZE
is the size (10G
for the example at the start). - Resize the image by copying the old image into the new one.
virt-resize oldimage.qcow2 newimage.qcow2
If the image created in the previous step is larger than the combined partitions on the old image,virt-resize
will 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-resize
will 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.
fsck
should be able to automatically fix them. Ifvirt-resize
created 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/
Recent Comments