Skip to content

vmlinuz: Decompress zstd and LZ4 kernels without external tools - #26

Merged
congwang-mk merged 1 commit into
mainfrom
vmlinuz-python-decompress
Nov 30, 2026
Merged

congwang-mk merged 1 commit into
mainfrom
vmlinuz-python-decompress

Conversation

@congwang-mk

Copy link
Copy Markdown
Contributor

Extracting the vmlinux from a bzImage was the one place where kerf ran an external program at runtime:

  • lz4 -d -c for an LZ4 kernel, always.
  • zstd -d -c for a zstd kernel when the zstandard module was missing, which is any plain install, since the module was never a dependency.

This removes _decompress_cli and uses the zstandard and lz4 Python modules instead. After this, kerf runs no external command at runtime.

Changes

  • zstd: zstandard only, with no fallback to the zstd binary.
  • LZ4: the kernel build compresses with lz4 -l, the legacy format, which the frame API of the lz4 module does not read. vmlinuz.py walks the legacy blocks (at most 8 MB each, behind a compressed size) and decompresses each with lz4.block. The standard frame format goes through lz4.frame.
  • Appended size: the kernel build appends the vmlinux size to the compressed stream. All three decompressors stop at the end of the stream and ignore it, as the CLI tools did.
  • Dependencies: zstandard and lz4 are added to pyproject.toml, to the deb and rpm dependencies in packaging/nfpm.yaml, and to the README.

The modules are imported only when a kernel needs them. Without one, kerf load or kerf sign of such a kernel fails with a message that names the missing module, and every other command keeps working.

Testing

  • New tests cover a multi-block legacy LZ4 payload, an LZ4 frame payload, a corrupt LZ4 payload, and the error for a missing module.
  • Files written by the real lz4 -l, lz4 and zstd tools from a 27 MB input decompress to identical bytes, with and without an appended size.
  • The full suite passes locally (699 passed, 1 skipped) and lint is clean.
  • Not tested against a real kernel image.

To check

The package names python3-zstandard and python3-lz4 in nfpm.yaml are unversioned and were not verified against specific distro releases.

🤖 Generated with Claude Code

Extracting the vmlinux of a bzImage was the one place where kerf ran
another program: lz4 for an LZ4 kernel, and zstd for a zstd kernel
where the zstandard module was missing, which is any plain install as
the module was never a dependency. A missing binary was only found out
at kerf load, and its output was trusted whatever it wrote to stderr.

Use the zstandard and lz4 modules instead and make them dependencies,
of the wheel and of the deb and rpm packages.

The kernel build compresses with lz4 -l, the legacy format, which the
frame API of the lz4 module does not read. It is a magic number and
then blocks of at most 8 MB, each behind its compressed size, so walk
them and decompress each as a raw block. The frame format is still
taken, through the frame API.

The build appends the size of the vmlinux to the compressed stream.
The tools complained about it and decompressed anyway; here it reads
as a block that the payload has no room for, which ends the legacy
format, and the frame and zstd decompressors stop at the end of their
stream.

The modules are imported when a kernel needs them, so that a machine
without one can still run every command but the load of such a kernel,
which says which module is missing.

Signed-off-by: Cong Wang <cwang@multikernel.io>
@congwang-mk
congwang-mk merged commit 17b6f7f into main Nov 30, 2026
8 checks passed
@congwang-mk
congwang-mk deleted the vmlinuz-python-decompress branch November 30, 2026 00:26
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.

1 participant