QCOW2 virtual size lowering

14,612

Solution 1

If you want to shrink the virtual size, you need to use virt-resize

Solution 2

  1. You need to convert the qcow2 image to raw
    qemu-img convert -O raw guest.img guest.raw
  2. Then resize the raw file
    qemu-img resize guest.raw 3G
  3. Then convert it back to qcow2
    qemu-img convert -O qcow2 -o compat=0.10 guest.raw guest.img
  4. Then run the guest and resize your file system

Solution 3

Improved answer of @user370956

I was able to successfully shrink my image actual size from 5.9G to 3.5G and v. size, too.

  1. Backup your image cp guest.img quest.backup
  2. You need to convert the qcow2 image to raw
    qemu-img convert -O raw guest.img guest.raw
  3. Boot up some live iso mounted as -cdrom and your guest.raw mounted as -drive qemu-system-x86_64 -machine type=pc,accel=kvm -boot order=d -drive file=guest.raw,format=raw -cdrom ~/Downloads/debian-live-9.6.0-amd64-lxde.iso -m 2G
  4. Use gparted (gui tool) to shrink the partition from inside the guest. Keep some free space so the system can still boot (100 MB is enough). Leave the remaining space unallocated. Check the filesystem. fsck -a
  5. Then resize the raw file
    qemu-img resize guest.raw 3G
  6. Then convert it back to qcow2
    qemu-img convert -O qcow2 guest.raw guest.img
  7. At this point you can resize the qcow2 virtual image size to add some space and this will not affect image file size. This may be needed to ensure the stability of the VM. qemu-img resize guest.img +1G
  8. Repeat steps 2,3 to allocate some more space to guest root partition if you did step 6.
Share:
14,612

Related videos on Youtube

Duncan Fairley
Author by

Duncan Fairley

Updated on September 18, 2022

Comments

  • Duncan Fairley
    Duncan Fairley almost 2 years

    I'm trying to get the virtual size of a qcow2 container down so that it can fit in a openstack flavor with 10GB disk.

    root@node-10:~# qemu-img info zztop.qcow 
    image: zztop.qcow
    file format: qcow2
    virtual size: 80G (85899345920 bytes)
    disk size: 2.6G
    cluster_size: 65536
    Format specific information:
    compat: 1.1
    lazy refcounts: false
    

    I follow the guide here and fill the end of the disk with zeros then run qemu-img convert -O qcow2. The resulting qcow2 however retains the same disk size and virtual size.

    How do I lower the virtual size of a qcow2 so that it can fit in a openstack flavor smaller than 80GB?

    • Sum1sAdmin
      Sum1sAdmin about 8 years
      you can boot from volume too, the openstack flavours only list the root ephemeral disk, boot from disk or snapshot.
    • Duncan Fairley
      Duncan Fairley about 8 years
      I'm trying to ensure my images/snapshots templates don't continue growing every time I update them.
    • Sum1sAdmin
      Sum1sAdmin about 8 years
      the second example in that link would work, "mv original_image.qcow2 original_image.qcow2_backup" qemu-img convert -O qcow2 original_image.qcow2_backup original_image.qcow2
    • Duncan Fairley
      Duncan Fairley about 8 years
      Virtual size remains 80GB. /dev/vtbd0p2 77G 2.3G 69G 3% /
  • Andy
    Andy about 4 years
    Would you mind posting the actual command?
  • David Corsalini
    David Corsalini about 4 years
    Did you try following the link?
  • Andy
    Andy about 4 years
    I did, Posting the actual code spares people following the link and skimming through all the documentation to find the important parts. It's recommended in the answering guidelines under Provide context for links also for the case when that site goes down or becomes unavailable.