windows icon indicating copy to clipboard operation
windows copied to clipboard

[Question]: Image compression

Open stelara opened this issue 3 months ago • 4 comments

Is your question not already answered in the FAQ?

  • [x] I made sure the question is not listed in the FAQ.

Is this a general question and not a technical issue?

  • [x] I am sure my question is not about a technical issue.

Question

Is it possible to compress data.img ? Because raw is huge. Same question for qemus/qemu (linux). If "Yes" please provide user guide. Thanks in advance.

stelara avatar Aug 31 '26 07:08 stelara

By default, it does not allocate the empty space inside the raw disk image (unless you set ALLOCATE=Y). So that file only looks huge, but in reality it is not taking 64 GB of disk space, but only the space actually used. You can set it to 500 terrabyte and it will still work, no matter that your drive is much smaller than that. So if you just look at the file size its misleading, since its a sparse file. So no compression is needed.

I think with this command you can see the REAL size:

du -h data.img

The only problem with this design is if you wanted to copy that file to another machine for example. Then it will fill that empty zeroes with literal zeroes during the copy, so the target file is no longer sparse and the copy will take a very long time. You can workaround this by zipping the file first before transferring it, as that will compress these zeroes, then you are not needlessly copying so much empty data over the network.

Another solution if you really dont like sparse files is to set DISK_FMT=qcow2, this uses a different format to store the disk, and this .qcow2 file will have the real size that is identical to the amount of space used.

But as long as you dont need to transfer or copy the disk, then I would just leave everything as it is.

kroese avatar Aug 31 '26 07:08 kroese

Oh Yes thanks for detailed answer. I have moved the file to other machine (backup) . And now I have get it back to my local machine. What I have to do now because the file is 130 GB ;-) and it is already installed and configured OS. Is there any way to convert it or ... in order to remove 0000....?

stelara avatar Aug 31 '26 07:08 stelara

If you set it to the qcow2 format in your compose file like I showed, then when you boot the container it will first start a conversion from raw to qcow2. After the conversion is finished you shutdown the container again. Then you transfer the folder with the qcow2 file to your other machine, and when you start the container there it will automatically convert it back to .img because that other compose file does not have the qcow2 setting, but that doesnt matter because the zeroes will not take space anymore. Or if you dont want to do two conversions then add that setting to the other machine too so that it stays in qcow2 and does not perform the conversion.

kroese avatar Aug 31 '26 08:08 kroese

To reclaim the zeroed space after moving the file back, you can run qemu-img convert -O qcow2 data.img data.qcow2 on your host machine to compress it outside the container environment. Once the conversion is complete, ensure you update your compose file to point to the new .qcow2 file so the container handles the sparse format correctly.

ronniromeo26 avatar Sep 21 '26 14:09 ronniromeo26