Alas, no but I see @iwalker has come to the rescue.
I haven’t used
qemu-img convert … but I notice there is nother option
qemu-img resize <img> --shrink <decrease eg -10G> … but again, haven’t used it and not entirely clear how it works (see below)
Avoiding unnecessary vd space on host initially
qemu-img create -f qcow2 myVitualDisk 1000204886016 # 1TB vd, no preallocation - a few MB on host
If you install OS to that it should only be as big as necessary when ur done - there shouldn’t be any space to recover. Whereas
qemu-img create −−preallocation=full -f qcow2 myVitualDisk 1000204886016
Would use up 1TB on the host media and have space to recover.
The efficient way to do an install, have a 1TB vd and not waste host space would seem to be
qemu-img create -f qcow2 -o preallocation=full myVitualDisk 10G
qemu-img resize myVitualDisk 1000204886016
… so the backing space etc is in place (1TB vd, 10G used on host).
However, the install will grow it beyond 10G (prob because it sees several G of that as used for swap).
So in reality its best to just not allocate anything at the start and you end up with a smaller img:
qemu-img create -f qcow2 myVitualDisk 1000204886016 and do install as normal.
qemu-img resize <img> --shrink <decrease eg -10G or new smaller size>
Before using this command to shrink a disk image, you MUST use file system and partitioning tools inside the VM to reduce allocated file systems and partition sizes accordingly. Failure to do so will result in data loss!
When shrinking images, the “−−shrink” option must be given. This informs qemu-img that the user acknowledges all loss of data beyond the truncated image’s end.
I suspect this will shrink the img in place on the host if the new size is smaller than the initial on-host size but also shrink the vd size. … dunno how original preallocation would play into it.
so I would try
- Backup my vd (img)
- Find out how much space my vd file systems are using
- Shrink my file systems (depends on fs / eg. not possible with xfs)
- Shrink my partition(s) - again depends on setup/tools
- Shutdown my vm
- qemu-img resize <img> --shrink <decrease eg -10G or new smaller size>
- qemu-img resize <img> <desired vd size>
Alternatively you can work on the vd in the host by mounting as a loop device for some img types or as an nbd for qcow2:
qemu-nbd -c /dev/nbd0 -f qcow2 <img>
then cryptsetup open if its luks and mount the volume.
after unmounting and cryptsetup closing;
qemu-nbd -d /dev/nbd0
Though your mileage may vary.
EDIT (re mounting vd in host)
Just remembered !
If your vd contains vgs (volume groups) with the same name as any of those active on the host, you will not be able to activate them. (and this is likely as you will probably have used the default values to install an OS on your host and your guest where they are the same OS).
There is a way around this - you need to rename the vgs on the vd and then name them back again when ur done. This involves vgcfgbackup to get a cfg file , then changing the name in that file and then applying it with vgcfgrestore … but I seem to have lost the details and there is the question of WHICH rl-root is which… I’ll post again when I get round to redoing/documenting it - the exact syntax for vgcfgrestore is not entirely obvious and a little scary as it could be construed as amending the wrong vg.
See here for instructions.