5

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?

4
  • you can boot from volume too, the openstack flavours only list the root ephemeral disk, boot from disk or snapshot.
    – Sum1sAdmin
    Apr 27, 2016 at 22:06
  • I'm trying to ensure my images/snapshots templates don't continue growing every time I update them. Apr 27, 2016 at 22:10
  • 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
    – Sum1sAdmin
    Apr 27, 2016 at 22:24
  • Virtual size remains 80GB. /dev/vtbd0p2 77G 2.3G 69G 3% / Apr 27, 2016 at 22:37

5 Answers 5

2

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

3
  • Would you mind posting the actual command?
    – Andy
    Apr 5, 2020 at 22:29
  • Did you try following the link?
    – dyasny
    Apr 5, 2020 at 22:56
  • 3
    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.
    – Andy
    Apr 6, 2020 at 14:52
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
1

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.
0

Manipulating only from the host side, may leave your guest file system corrupted.

So to complete this answer, before any changes from the host you need to change the VM's storage partitions sizes with partitioning tools like gparted from inside of the VM. So that the total size of the virtual disk becomes less than desired size of the virtual storage. After that, you can do the first 3 steps.

0

1 - Disable swap file in fstab if there is one

2 - Mount gparted to your VM cd-rom

3 - start VM, boot gparted and resize the partition to a smaller size

4 - Delete swap partition and recreate, or make sure all partitions are aligned next to each other at the beginning of the disk. I had issues with the swap being at the end of the disk after shrinking the partition so this was the fix.

5 - shut down VM

6 - Locate qcow2 file and in same directory, run 'qemu-img create -f qcow2 -o preallocation=metadata new-disk.qcow2 80G' (where 80G is the new disk size)

7 - use virt-resize - 'virt-resize old-disk.qcow2 new-disk.qcow2'

8 - copt new-disk to correct file name for VM, start VM and re-enable swap ie: 'swapon /dev/sda5'

9 - edit fstab to add swap ie: '/dev/sda5 swap swap defaults 0 0'

DONE.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .