QCOW2 virtual size lowering
14,612
Solution 1
If you want to shrink the virtual size, you need to use virt-resize
Solution 2
- You need to convert the qcow2 image to raw
qemu-img convert -O raw guest.img guest.raw
- Then resize the raw file
qemu-img resize guest.raw 3G
- Then convert it back to qcow2
qemu-img convert -O qcow2 -o compat=0.10 guest.raw guest.img
- 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.
- Backup your image
cp guest.img quest.backup
- You need to convert the qcow2 image to raw
qemu-img convert -O raw guest.img guest.raw
- Boot up some live iso mounted as
-cdrom
and yourguest.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
- 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
- Then resize the raw file
qemu-img resize guest.raw 3G
- Then convert it back to qcow2
qemu-img convert -O qcow2 guest.raw guest.img
- 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
- Repeat steps 2,3 to allocate some more space to guest root partition if you did step 6.
Related videos on Youtube
Author by
Duncan Fairley
Updated on September 18, 2022Comments
-
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 about 8 yearsyou can boot from volume too, the openstack flavours only list the root ephemeral disk, boot from disk or snapshot.
-
Duncan Fairley about 8 yearsI'm trying to ensure my images/snapshots templates don't continue growing every time I update them.
-
Sum1sAdmin about 8 yearsthe 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 about 8 yearsVirtual size remains 80GB.
/dev/vtbd0p2 77G 2.3G 69G 3% /
-
-
Andy about 4 yearsWould you mind posting the actual command?
-
David Corsalini about 4 yearsDid you try following the link?
-
Andy about 4 yearsI 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.