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?
If you want to shrink the virtual size, you need to use
virt-resize
qemu-img convert -O raw guest.img guest.raw
qemu-img resize guest.raw 3G
qemu-img convert -O qcow2 -o compat=0.10 guest.raw guest.img
Improved answer of @user370956
I was able to successfully shrink my image actual size from 5.9G to 3.5G and v. size, too.
cp guest.img quest.backup
qemu-img convert -O raw guest.img guest.raw
-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
fsck -a
qemu-img resize guest.raw 3G
qemu-img convert -O qcow2 guest.raw guest.img
qemu-img resize guest.img +1G
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.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.