From 8e8079df2fce8b25fe0a069fee27c39d46ece575 Mon Sep 17 00:00:00 2001 From: Parham Fatemi <93884781+parhamfa@users.noreply.github.com> Date: Tue, 24 Nov 2026 13:29:58 +0330 Subject: [PATCH] Boot the staged writer with kexec handover disabled Ubuntu 26.04's 7.0 kernels enable kexec handover (KHO) by default. At boot KHO reserves three scratch areas, each twice the memory already in use, and hands them to the page allocator as CMA, which only movable pages may use. The staged initramfs carries the whole CHR image, so the reservation grows with it: with an 80 MB initramfs in 768 MiB, about 600 MB became movable-only and the kernel panicked out of memory about two seconds into boot, before the writer started. Whether the scratch areas fit depends on where KASLR places the kernel, so it failed 8 of 100 boots with KASLR and 45 of 45 with nokaslr. WriterCmdline now appends kho=off (and drops any inherited kho=). With nokaslr plus kho=off, 30 of 30 boots succeeded. The integration tests now build their writer command lines with WriterCmdline so they boot exactly what the installer stages, and the SCSI disk-probe VM gets kho=off for the same reason. Co-Authored-By: Claude Opus 5.5 --- internal/install/install_test.go | 10 +++++++--- internal/install/stage.go | 20 ++++++++++++++++---- internal/integration/writer_linux_test.go | 12 +++++++----- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/internal/install/install_test.go b/internal/install/install_test.go index 0b14f8a..ace18c9 100644 --- a/internal/install/install_test.go +++ b/internal/install/install_test.go @@ -109,9 +109,13 @@ func (*initramfsFixtureRunner) LookPath(name string) (string, error) { } func TestWriterCmdline(t *testing.T) { - got := writerCmdline("BOOT_IMAGE=/boot/vmlinuz root=/dev/sda1 ro quiet initrd=/boot/initrd chr.install=old") - if got != "root=/dev/sda1 ro quiet chr.install=1" { - t.Fatalf("writerCmdline() = %q", got) + for input, want := range map[string]string{ + "BOOT_IMAGE=/boot/vmlinuz root=/dev/sda1 ro quiet initrd=/boot/initrd chr.install=old": "root=/dev/sda1 ro quiet kho=off chr.install=1", + "root=/dev/vda ro kho=on kho_scratch=200%": "root=/dev/vda ro kho_scratch=200% kho=off chr.install=1", + } { + if got := WriterCmdline(input); got != want { + t.Fatalf("WriterCmdline(%q) = %q, want %q", input, got, want) + } } } diff --git a/internal/install/stage.go b/internal/install/stage.go index 9131e9d..fc1ca9e 100644 --- a/internal/install/stage.go +++ b/internal/install/stage.go @@ -150,7 +150,7 @@ func PrepareStage(ctx context.Context, runner command.Runner, disk model.Disk, i if err != nil { return Stage{}, err } - cmdline := writerCmdline(string(cmdlineData)) + cmdline := WriterCmdline(string(cmdlineData)) stage = Stage{Method: disk.Method, KernelPath: disk.KernelPath, Cmdline: cmdline} partialFiles := []string{} partialDirs := []string{} @@ -407,15 +407,27 @@ func kernelVersion(kernelPath string) string { return strings.TrimPrefix(filepath.Base(kernelPath), "vmlinuz-") } -func writerCmdline(input string) string { +// WriterCmdline derives the staged writer's kernel command line from the +// running host's command line. +// +// It always appends kho=off. Kernels built with +// CONFIG_KEXEC_HANDOVER_ENABLE_DEFAULT (Ubuntu 26.04's 7.0 kernels) reserve +// kexec-handover scratch areas at boot sized at twice the memory already in +// use, and hand them to the page allocator as CMA, which only movable pages +// may use. The staged initramfs carries the whole CHR image, so on a small +// host those areas can leave the kernel too little ordinary memory to finish +// booting, and it panics on out-of-memory before the writer starts. The +// writer boot never performs a kexec handover itself, so it does not need +// the scratch areas. +func WriterCmdline(input string) string { var result []string for _, field := range strings.Fields(input) { - if strings.HasPrefix(field, "BOOT_IMAGE=") || strings.HasPrefix(field, "initrd=") || strings.HasPrefix(field, "rdinit=") || strings.HasPrefix(field, "chr.install=") { + if strings.HasPrefix(field, "BOOT_IMAGE=") || strings.HasPrefix(field, "initrd=") || strings.HasPrefix(field, "rdinit=") || strings.HasPrefix(field, "chr.install=") || strings.HasPrefix(field, "kho=") { continue } result = append(result, field) } - result = append(result, "chr.install=1") + result = append(result, "kho=off", "chr.install=1") return strings.Join(result, " ") } diff --git a/internal/integration/writer_linux_test.go b/internal/integration/writer_linux_test.go index d1cfd03..94e2e79 100644 --- a/internal/integration/writer_linux_test.go +++ b/internal/integration/writer_linux_test.go @@ -107,7 +107,7 @@ func TestStagedWriterQEMU(t *testing.T) { arguments := []string{ "-machine", "q35,accel=tcg", "-cpu", writerQEMUCPU, "-m", "768", "-nographic", "-no-reboot", "-kernel", kernel, "-initrd", stagedInitrd, - "-append", "console=ttyS0 root=" + targetDisk.Path + " rootfstype=ext4 ro panic=-1 chr.install=1", + "-append", install.WriterCmdline("console=ttyS0 root=" + targetDisk.Path + " rootfstype=ext4 ro panic=-1"), } arguments = append(arguments, diskArguments...) process := exec.CommandContext(ctx, "qemu-system-x86_64", arguments...) @@ -252,10 +252,10 @@ menuentry 'Fallback Linux' --id 'fallback-linux' { } menuentry 'CHR Install Writer' --id 'chr-install-writer' { search --no-floppy --fs-uuid --set=root %s - linux /boot/vmlinuz console=ttyS0 root=/dev/vda rootfstype=ext4 ro panic=-1 chr.install=1 + linux /boot/vmlinuz %s initrd /boot/initrd.img } -`, uuid) +`, uuid, install.WriterCmdline("console=ttyS0 root=/dev/vda rootfstype=ext4 ro panic=-1")) grubDirectory := filepath.Join(bootDirectory, "grub") if err := os.WriteFile(filepath.Join(grubDirectory, "grub.cfg"), []byte(configuration), 0o600); err != nil { t.Fatal(err) @@ -434,7 +434,7 @@ PREREQ="" prereqs() { echo "$PREREQ"; } case "${1:-}" in prereqs) prereqs; exit 0 ;; esac echo "CHR-KEXEC-LAUNCH" >/dev/console -/sbin/kexec -l /chr-kexec/vmlinuz --initrd=/chr-kexec/writer-initrd.img --append="console=ttyS0 root=/dev/vda rootfstype=ext4 ro panic=-1 chr.install=1" +/sbin/kexec -l /chr-kexec/vmlinuz --initrd=/chr-kexec/writer-initrd.img --append="` + install.WriterCmdline("console=ttyS0 root=/dev/vda rootfstype=ext4 ro panic=-1") + `" sync exec /sbin/kexec -e ` @@ -705,7 +705,9 @@ exec /sbin/chr-disk-probe %s >/dev/console 2>&1 arguments := []string{ "-machine", "q35,accel=tcg", "-cpu", writerQEMUCPU, "-m", "512", "-nographic", "-no-reboot", "-kernel", kernel, "-initrd", probeInitrd, - "-append", "console=ttyS0 root=" + targetPath + " rootfstype=ext4 ro panic=-1", + // kho=off for the same reason as install.WriterCmdline: this probe + // boots a large initramfs in 512 MiB. + "-append", "console=ttyS0 root=" + targetPath + " rootfstype=ext4 ro panic=-1 kho=off", } arguments = append(arguments, diskArguments...) process := exec.CommandContext(ctx, "qemu-system-x86_64", arguments...)