shrinking qcow2 image virtual size

skraw

Well-Known Member
Aug 13, 2019
97
1
48
59
Hello all,

I have to move around some physical boxes and virtualise them later on. So I came across this question about qcow2 images and cannot find a conveniant solution for it.
Lets assume having a qcow2 disk image with a big virtual size and a relatively small partition size inside. How can I shrink the image to contain only this/these partition(s)? The problem with qemu-img resize is that you have to give it exact resize numbers. But since I want to make the sizes fitting exactly I would have to "calculate for the byte". Isn't there some way to tell a resize tool "shrink the unused space of a disk at the end" ?
--
Regards
 
Hello skraw,

there is no built-in “auto shrink to used partitions” command in QEMU / Proxmox VE because disk images are treated as raw block devices and QEMU does not know which part of the disk is unused. You should be able to script this, but I currently don't have the time to look into this, sorry.

Best,
Erik
 
Then use a ZFS Dataset and import your disk, with compression it will compress the data "on the fly".
I got a Win7 Clone of a 500 GB Disk. It use now 68 GB disk space.
 
Ok, i found another thread with quite the same problem showing up:


Only he tries to enlarge the disk.
With the hints he gave I found that qemu-img resize simply destroys the partition table, his partly, mine completely. And thats about the difference.
So if you save the table with

sfdisk --dump /dev/nbd0 > disk.dump
(I use this to acess the file as block device),

then

qemu-img resize --shrink blabla.qcow2 <somesize>

and then restore the partition table from the dump with

sfdisk --no-reread --no-tell-kernel -f --wipe never /dev/nbd0 < disk.dump

you win. if you are interested look at the partition table after the resize with sfdisk and find it completely destroyed
Contrary to the above thread here no lvm or complex other stuff is involved. I'd say qemu-img resize is really a bad implementation of what it should do.