Resizing KVM qcow2 images

Expanding images

  1. Shut down the virtual machine
  2. Resize the image with
    qemu-img resize image.qcow2 +SIZE
    Where SIZE is the size (e.g. 10G for 10 gigabytes).
  3. Boot into gparted live and resize the partition.

Shrinking images

  1. Boot into gparted live and resize the partition smaller.
  2. 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.
  3. 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, so su is 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
    where NEW_SIZE is the size (10G for the example at the start).
  4. 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.
  5. 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.
  6. 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/

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>