Skip to content

qcow2: Decode a qcow2 image into a sparse raw file - #28

Open
saeedvft wants to merge 4 commits into
multikernel:mainfrom
saeedvft:qcow2-decoder
Open

saeedvft wants to merge 4 commits into
multikernel:mainfrom
saeedvft:qcow2-decoder

Conversation

@saeedvft

@saeedvft saeedvft commented Dec 1, 2026

Copy link
Copy Markdown

First step of #23: a standalone qcow2 -> raw decoder (src/kerf/qcow2.py). New files only, no change to existing code.

  • Pure Python (struct, zlib, plain file I/O), no subprocess.
  • Deflate clusters (raw deflate, no zlib header) and zstd clusters, using the zstandard module imported when an image needs it, like vmlinuz.py does.
  • Zero and unallocated clusters are skipped, so the output file is sparse.
  • Rejected with a clear error: backing files, encryption, external data files, extended L2 entries, unknown incompatible features, and offsets or sizes that point outside the file.

Testing:

  • tests/test_qcow2.py builds small qcow2 images in Python (no qemu-img needed).
  • I also compared the output byte for byte with qemu-img convert -O raw on images with plain, zeroed, zlib-compressed and zstd-compressed clusters (manual check, not part of the tests).
  • pylint is clean with the project's config, and the new tests pass on Python 3.9 and 3.13.

First step of loading a spawn from a VM image (multikernel#23). The decoder
is pure Python and handles deflate and zstd compressed clusters
and zero clusters.

Backing files, encryption, external data files and extended L2
entries are rejected with a clear error, since none of them can
be represented in the sparse raw output this produces.

Signed-off-by: saeednourian82 <saeednourian82@gmail.com>
Comment thread src/kerf/qcow2.py Resolved
Comment thread tests/test_qcow2.py Resolved
Comment thread src/kerf/qcow2.py Resolved
Raise Qcow2Error when a compressed cluster does not expand to exactly
cluster_size, for both deflate and zstd, and add tests. Also update the
copyright headers.
Refuse to convert an image onto itself, remove the output file when
decoding fails, and ignore L1 entries that lie past the end of the disk.
@saeedvft

saeedvft commented Dec 2, 2026

Copy link
Copy Markdown
Author

While fixing this I audited the rest of convert_to_raw for the same kind of silent failure and fixed three more: converting an image onto itself truncated the source, a failed conversion left a partial .raw behind, and L1 entries past the end of the disk were still validated. Each has a test that fails without the fix. Happy to split those into a separate PR if you prefer.

Comment thread src/kerf/qcow2.py Resolved
A failed conversion no longer touches a pre-existing dest.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants