Creating a modifiable gzipped disk image
rwmj.wordpress.com
rwmj.wordpress.com
• pigz (https://github.com/madler/pigz) has -0 for no-compression compression;
• you can concat multiple gzip files together.
$ echo foo | gzip > foobar.gz
$ echo bar | pigz -0 >> foobar.gz
$ echo baz | gzip >> foobar.gz
$ hd foobar.gz | grep -C1 -w bar
00000010 a8 65 32 7e 04 00 00 00 1f 8b 08 00 70 bf 8c 63 |.e2~........p..c|
00000020 00 03 01 04 00 fb ff 62 61 72 0a e9 b3 a2 04 04 |.......bar......|
00000030 00 00 00 1f 8b 08 00 00 00 00 00 00 03 4b 4a ac |.............KJ.|
$ gzip -d < foobar.gz
foo
bar
baz
This also solves the CRC problem, because pigz computes the checksum for you.Another thing I found out while researching this earlier is the pigz -i / --independent option. It lets you create a seekable gzip file, albeit one which cannot easily be indexed except by brute force searching over the whole file for the block boundaries (so not as useful as seekable xz like we use here: https://libguestfs.org/nbdkit-xz-filter.1.html)
checksum ^= crc_of_removed_bytes
checksum ^= crc_of_added_bytes
To be honest the tool as posted is just a hack to demonstrate the point that it is possible. I have an idea for a somewhat more refined tool which would let you mark small stretches of data (not whole partitions) for modification, and provide additional tooling to do the mods and update the checksums.
As well as implementing the whole thing again for xz & zstd, which is another large chunk of work.
The data layout within the file can be queried with "qemu-img map file.qcow2" so you know the offsets where uncompressed data can be overwritten (if you want to patch it from the host instead of from inside a virtual machine).
- Clouds don't universally support qcow2 (or even worse, use their own implementation which isn't feature-complete)
- Probably a per-cluster compressed qcow2 isn't ever going to be as small as a qcow2.xz file.
[Edit: deleted a bogus third point here about making these files, no special tools are needed to create or modify compressed qcow2 files]
But oh well, the author works at Red Hat so I'm sure they have their reasons :p