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?

Duncan Fairley
  • 63
  • 1
  • 1
  • 10

5 Answers5

2

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

dyasny
  • 18,482
  • 6
  • 48
  • 63
  • Would you mind posting the actual command? – Andy Apr 05 '20 at 22:29
  • Did you try following the link? – dyasny Apr 05 '20 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](https://serverfault.com/help/how-to-answer) under _Provide context for links_ also for the case when that site goes down or becomes unavailable. – Andy Apr 06 '20 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
Mark Riddell
  • 1,103
  • 1
  • 6
  • 10
user370956
  • 21
  • 1
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.

Datis
  • 1
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.

Greg King
  • 1
  • 2