Skip to content

perf(convert): reduce rootfs copies from 3 to 1 - #164

Merged
imlk0 merged 11 commits into
masterfrom
convert-overlay-rework
Oct 30, 2026
Merged

imlk0 merged 11 commits into
masterfrom
convert-overlay-rework

Conversation

@imlk0

@imlk0 imlk0 commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Replace the intermediate rootfs.img file and the final qemu-img convert step with a qcow2 overlay/snapshot chain, reducing full rootfs copies from 3 to 1: the source image is modified through a writable overlay (source-mod), then forked into two independent snapshots (source-read for verity hashing and the final rootfs dd, source-write for the dracut chroot), while the output image is built directly on a fresh qcow2 over NBD.
  • Measured on the same aliyun_3_x64 20G input in the same container: 247 s vs 313 s wall (~21% faster), and the output image is ~0.7 GiB smaller.
  • Remove --device / operate_on_device support: file-based conversion (--in/--out) only. Overlay chains require file backing, so in-place device conversion cannot be supported under the new architecture. The flag is still accepted and exits with a clear error pointing at the file-based flow, so existing scripts fail legibly instead of hitting unexpected argument.
  • Remove --wipe-freed-space: meaningless under the new architecture, since freed space beyond the shrunk filesystem is never copied. The flag is still accepted, prints a warning, and is ignored.
  • VHD output is preserved: qcow2 outputs are built in place; .vhd outputs get one final qemu-img convert -O vpc pass.
  • Fix the concurrency guard: master's flock invocation was invalid (command-less form always exits 64), which silently made the guard always delete leftover files without ever detecting a concurrent instance. The script now holds an exclusive lock on the source-mod overlay for its whole lifetime, so a second instance on the same input aborts with a clear error.
  • Fix RPM version detection: rpm -q prints its error to stdout on uninstalled packages, which was captured as the version string. The guest package is now pinned with %{VERSION}-%{RELEASE}.
  • Fix a latent dependency gap: udevadm is required by the partition-table rescan settling but was missing from both tool package lists.
  • All NBD disconnects now go through sync + blockdev --flushbufs + nbd_disconnect_and_wait, so qemu's image lock is released before any subsequent image operation.

Supersedes #110 (that branch mixed this rework with Ubuntu cloud image support and accumulated debug workarounds; Ubuntu support will follow as a separate PR on top of this one).

Test plan

  • End-to-end grub-mode conversion in an alinux4 container (aliyun_3_x64 20G input): all 12 steps pass, output qcow2 has the expected BIOS-boot/EFI/boot/LVM layout, EFI contains shim/grub/BOOTX64.EFI plus the freshly generated initramfs, show-reference-value produces the full SHA-384 measurement set (247 s vs master's 313 s on the same input).
  • End-to-end UKI-mode conversion in the same container with --uki --uki-stub-version 258.
  • VHD smoke: --out disk.vhd produces a file format: vpc image.
  • Concurrency: a second instance against a locked source-mod aborts with the "locked by another process" error; a stale leftover overlay from a crashed run is detected and removed.
  • bash -n, shellcheck, cargo fmt --check, make clippy, cargo build all clean.
  • CI: the 12 test-convert matrix jobs (6 uki as regression gate, 6 grub as acceptance gate) plus the full build/test matrix.

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,您的请求已接收,请耐心等待结果。

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,未检测到有镜像需要构建,如需重新检测请评论 /start 。

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,您的请求已接收,请耐心等待结果。

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,未检测到有镜像需要构建,如需重新检测请评论 /start 。

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,您的请求已接收,请耐心等待结果。

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,未检测到有镜像需要构建,如需重新检测请评论 /start 。

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,您的请求已接收,请耐心等待结果。

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,未检测到有镜像需要构建,如需重新检测请评论 /start 。

imlk0 added 11 commits October 30, 2026 11:06
…ion detection

File-based conversion (--in/--out) only. --wipe-freed-space is meaningless
once the output is built directly instead of via qemu-img convert, since
freed space beyond the shrunk filesystem is never copied.

Initialize boot_part to empty at detection time when the source has no boot
partition, so expansions of ${boot_part} in later steps are safe under set -u.

Assisted-by: Claude Code:qwen3.8-flash[1M]
Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
…ate output

Step 1 now wraps the input in a <input>.source-mod qcow2 overlay and
pre-creates <output>.build.qcow2 at the input's virtual size, so later
steps can write to the output through a dedicated NBD device.

Drop the cp fallback of the old work file: qcow2 backing chains only
work with image files, so overlay creation cannot fall back to a raw
copy. Extend the flock staleness guard to all three overlay names
(source-mod, source-read, source-write): a leftover file that can be
locked is stale and deleted, a file held by another process aborts the
run. This also corrects the guard's inverted branches on master, which
treated a successfully acquired nonblocking lock as "locked by another
process". The guard uses flock with "-c true" because the command-less
form reads its file argument as a descriptor number and always fails
with rc 64 on util-linux 2.32.1/2.39.3, making every leftover look
locked.

Port disk::nbd_connect from the perf branch: it allocates a free NBD
device, connects the image, registers the disconnect hook, and assigns
the device path to the caller's variable. The device alias keeps
pointing at the source-mod NBD so steps 2-10 are untouched; steps 10-11
still convert from the overlay until the output-device write path
lands.

Assisted-by: Claude Code:qwen3.8-flash[1M]
Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
Step 4 now only marks the rootfs read-only, runs e2fsck and resize2fs
-M, and reports the shrunk geometry. The dd extraction to rootfs.img
and the parted rm of the rootfs partition are dropped: source-read and
source-write snapshots keep the original partition layout, so the
shrunk partition stays the data source for the later steps and the
output partition table is built from it.

The rootfs_file_path global assignment is removed with the dd block;
main() call sites that still reference it are rewired to NBD
partitions in follow-up steps.

Assisted-by: Claude Code:qwen3.8-flash[1M]
Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
…ut partition layout

Step 5 disconnects the source-mod NBD and forks two qcow2 snapshots that
share it as backing file: source-read freezes the shrunk source for the
remaining copy and verity reads, source-write takes later writes. The
source partition table is copied onto the pre-created output image with
sfdisk. For sources that keep /boot inside the rootfs, the output rootfs
partition is replaced by a boot partition sized from the extracted
boot.img plus a rootfs filling the rest of the disk; the rootfs partition
UUID is saved and restored around the table edits so fstab and grub
references survive. The UKI path gets no resize: the copied table already
matches what current images need.

Step 6 dd's the EFI partition, and the boot partition from source-read
when the source had one. Otherwise boot.img content is copied into the
formatted output boot partition and its filesystem UUID is set to match
boot.img, with e2fsck before tune2fs since tune2fs requires a freshly
checked filesystem.

setup_lvm moves the lvm flag plus pvcreate/vgcreate/vgchange onto the
output rootfs partition, keeping the autoactivation and activation flags
from the old create_lvm_part. Steps 7-9 still reference the removed
rootfs.img path and are rewired in follow-up commits.

gdisk is added to the host tool lists because the partition UUID restore
uses sgdisk.

Assisted-by: Claude Code:qwen3.8-flash[1M]
Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
…ootfs.img

The LV setup steps now take no file argument: they size the rootfs from
dumpe2fs on the source-read NBD partition (the shrunk filesystem lives
inside a full-size partition, so partition size is not usable) and dd
only the filesystem bytes into the new LVs. veritysetup gains a
--data-blocks limit derived from the filesystem size for the same
reason. The no-encryption path drops the 16MB LUKS slack since lvcreate
rounds up to the physical extent boundary anyway.

Assisted-by: Claude Code:qwen3.8-flash[1M]
Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
Step 9 now chroots the source-write snapshot with /boot and /boot/efi
mounted from the output partitions, so the generated initrd and UKI land
directly on the output image while the rootfs, already verity-formatted
from source-read, stays untouched. The read-only compat flag set during
shrink is cleared on the snapshot before mounting it rw.
setup_chroot_mounts gains optional boot/EFI override arguments because
the boot_part and efi_part globals still point at the source-mod NBD,
which is disconnected in step 5.

Step 10 tears down dm targets under the output device (the LVs now live
there), flushes buffers on the output, source-read and source-write
devices, then disconnects all three NBDs with nbd_disconnect_and_wait so
the build file lock is released before any image operation. The same
retarget applies to the dm-remove hooks and the step 8 teardown, which
referenced the stale source device. Step 11 renames the build qcow2 into
place for qcow2 output and converts it to vpc for VHD output, restoring
the VHD path PR #110 dropped, and a guard fails the run when no output
file was produced.

Assisted-by: Claude Code:qwen3.8-flash[1M]
Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
cryptpilot-convert no longer supports --device or in-place disk
conversion. Drop the real-system-disk example and the --device
variant of the KBS example, and renumber the remaining examples.
Prerequisites and troubleshooting now describe file-based conversion
only; supported input formats are qcow2 and VHD. Trim the stale
"or system disks" wording from the fde READMEs.

Assisted-by: Claude Code:qwen3.8-flash[1M]
Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
cryptpilot-convert.sh runs udevadm settle after partition table
rescans, but neither step-0 package list installed a package
providing udevadm. CI containers ship it, minimal hosts do not, so
conversion died at the first settle call. udevadm is in udev on the
Debian family and in systemd-udev on the yum family.

Signed-off-by: Kun Lai <laikun@linux.alibaba.com>

Assisted-by: Claude Code:qwen3.8-flash
…he snapshot fork

A second cryptpilot instance could treat a live run's source-mod overlay
as a stale leftover: qemu-nbd guards its image with an fcntl record lock,
which flock(2) does not see, and the script itself held no lock at all.
The step 1 probe then deleted an in-use overlay and corrupted the running
conversion. Right after creating source-mod, open it on a persistent fd
and take an exclusive flock on it, held until the process exits. That
restores the stale check's discrimination: a locked file means a live
instance, an unlocked one a crashed leftover.

The step 5 snapshot fork dropped source-mod with a bare qemu-nbd -d and
a fixed sleep 3, racing the daemon exiting and releasing its write lock
on the backing file. Flush and wait like the step 11 disconnects do:
sync, blockdev --flushbufs, then disk::nbd_disconnect_and_wait.

Signed-off-by: Kun Lai <laikun@linux.alibaba.com>

Assisted-by: Claude Code:qwen3.8-flash
The source-mod flock guard keeps the image open for the run, so the step 5
disconnect-and-wait saw the script itself as a holder on every poll: the
lsof wait always burned its 30 attempts and logged a misleading "still in
use" warning before forking the snapshots.

Filtering just $$ is not enough: lsof scans while its sibling subshell and
lsof itself (bash execs a bare command substitution) still carry inherited
copies of the guard fd, and each shows up as a holder. Probe from a
subshell that closes the inherited descriptors first, with "|| true"
keeping it alive so bash does not exec into lsof, then drop our own pid.
A genuine other holder, such as a qemu-nbd daemon still exiting or a
converting sibling process, still keeps the wait going; the step 11 call
sites are unchanged because no script fd points at those images.

Signed-off-by: Kun Lai <laikun@linux.alibaba.com>

Assisted-by: Claude Code:qwen3.8-flash
--device exits with a clear message pointing at the file-based flow
instead of the generic unexpected-argument failure, so existing scripts
fail legibly. --wipe-freed-space is obsolete rather than removed: freed
space beyond the shrunk filesystem is never copied into the output image,
so the flag is accepted, warns, and is ignored.

Assisted-by: Claude Code:glm-5.3
Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
@imlk0
imlk0 force-pushed the convert-overlay-rework branch from 7320046 to a520e7b Compare October 30, 2026 03:06
@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,您的请求已接收,请耐心等待结果。

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,未检测到有镜像需要构建,如需重新检测请评论 /start 。

@imlk0
imlk0 merged commit a520e7b into master Oct 30, 2026
10 of 14 checks passed
@imlk0
imlk0 deleted the convert-overlay-rework branch October 30, 2026 03:07
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