Resizing KVM qcow2 images

While expanding qcow2 images is easy, finding an easy way to shrink them was not. NOTE: a qcow2 disk image can be resized only if it doesn't contain snapshots If disk image has snaps you have to clone the VM (and related disk). Restore the snap you need and make a clone. Then resize the cloned disk. Expanding images This is fairly straightforward. Due to the design of qcow2 images, you don’t even need to have the disk space available right away. Shut down the virtual machine Resize the image with qemu-img resize image.qcow2 +SIZE where SIZE is the size (e.g. 10G for 10 gibibytes). Boot into an external live OS and resize the partition. The easiest way to do this is to use a GParted live image and virt-manager to connect to the VM. Check/Fix the filesystem then! Shrinking images This is a bit more difficult. You will also need to have disk space to fit both images at the same time. For example, you want to resize a 100gb allocation to 50gb, your current image shows 60gb used from outside (the qcow2 image) and has 30gb actually used inside. For the change, you’ll need to have 50gb space for the resized image and 60gb for the existing image, so 110gb total. Resize the partition (see step #3 of expanding images) Create the new smaller image: qemu-img create -f qcow2 -o preallocation=metadata newimage.qcow2 NEW_SIZE where NEW_SIZE is the size (50G 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. If virt-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.