[PATCH 08/18] selftests/liveupdate: Support a different command line for each kernel
From: David Matlack
Date: Tue Dec 01 2026 - 19:40:40 EST
Add TEST_CMDLINE_STAGE1 and TEST_CMDLINE_STAGE2 to vmtest.sh test
definitions, which are only passed to the first and the kexec'd kernel
respectively.
When either is set, vmtest.sh writes the kexec'd kernel's full command
line to /cmdline_stage2 in the first kernel's initrd, and init uses it
instead of the current command line. This lets tests check how the next
kernel handles preserved state that was set up under a different
configuration.
Signed-off-by: David Matlack <dmatlack@xxxxxxxxxx>
---
tools/testing/selftests/liveupdate/init.c | 33 ++++++++++++++------
tools/testing/selftests/liveupdate/vmtest.sh | 26 ++++++++++++---
2 files changed, 46 insertions(+), 13 deletions(-)
diff --git a/tools/testing/selftests/liveupdate/init.c b/tools/testing/selftests/liveupdate/init.c
index bcb401295801..45abf45c206c 100644
--- a/tools/testing/selftests/liveupdate/init.c
+++ b/tools/testing/selftests/liveupdate/init.c
@@ -21,6 +21,7 @@
#define TEST_BINARY "/test_binary"
#define TEST_ARGS "/test_args"
#define MAX_TEST_ARGS 32
+#define CMDLINE_STAGE2 "/cmdline_stage2"
static int mount_filesystems(void)
{
@@ -50,21 +51,22 @@ static long kexec_file_load(int kernel_fd, int initrd_fd,
cmdline, flags);
}
-static int kexec_load(void)
+/*
+ * Read the kexec'd kernel's command line from CMDLINE_STAGE2 if it exists, or
+ * reuse the current command line otherwise.
+ */
+static int read_cmdline_stage2(char *cmdline, size_t size)
{
- char cmdline[COMMAND_LINE_SIZE];
- int kernel_fd, initrd_fd, err;
ssize_t len;
int fd;
- fd = open("/proc/cmdline", O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "INIT: Failed to read /proc/cmdline\n");
-
+ fd = open(CMDLINE_STAGE2, O_RDONLY);
+ if (fd < 0)
+ fd = open("/proc/cmdline", O_RDONLY);
+ if (fd < 0)
return -1;
- }
- len = read(fd, cmdline, sizeof(cmdline) - 1);
+ len = read(fd, cmdline, size - 1);
close(fd);
if (len < 0)
return -1;
@@ -73,6 +75,19 @@ static int kexec_load(void)
if (len > 0 && cmdline[len - 1] == '\n')
cmdline[len - 1] = 0;
+ return 0;
+}
+
+static int kexec_load(void)
+{
+ char cmdline[COMMAND_LINE_SIZE];
+ int kernel_fd, initrd_fd, err;
+
+ if (read_cmdline_stage2(cmdline, sizeof(cmdline))) {
+ fprintf(stderr, "INIT: Failed to read the command line\n");
+ return -1;
+ }
+
strncat(cmdline, " luo_stage=2", sizeof(cmdline) - strlen(cmdline) - 1);
kernel_fd = open(KERNEL_IMAGE, O_RDONLY);
diff --git a/tools/testing/selftests/liveupdate/vmtest.sh b/tools/testing/selftests/liveupdate/vmtest.sh
index 937f10fc9592..102261892829 100755
--- a/tools/testing/selftests/liveupdate/vmtest.sh
+++ b/tools/testing/selftests/liveupdate/vmtest.sh
@@ -24,11 +24,15 @@ KEEP_WORKSPACE=0
# TEST_MODULES Kernel modules to place at /<module>.ko in the
# initrd, built from the Makefile's TEST_GEN_MODS_DIR
# TEST_QEMU_ARGS Extra QEMU arguments
+# TEST_CMDLINE_STAGE1 Extra command line for the first kernel only
+# TEST_CMDLINE_STAGE2 Extra command line for the kexec'd kernel only
# TEST_TIMEOUT Timeout for the QEMU run
# test_check() Host-side check, run after the test passes in the VM
TEST_ARGS=()
TEST_MODULES=()
TEST_QEMU_ARGS=()
+TEST_CMDLINE_STAGE1=""
+TEST_CMDLINE_STAGE2=""
TEST_TIMEOUT="30s"
function test_check() {
@@ -166,6 +170,7 @@ function mkinitrd() {
local build_dir=$1
local kernel_path=$2
local make_cmd=$3
+ local cmdline_stage2=$4
local output="$workspace_dir/build"
local init="$output/init"
local test_binary="$output/$TEST_BINARY"
@@ -219,6 +224,13 @@ file /initrd.img $workspace_dir/inner_initrd.cpio 0644 0 0
EOF
cat "$extra_files" >> "$workspace_dir/cpio_list"
+ # Only the first kernel needs the command line for the kexec'd kernel
+ if [ -n "$cmdline_stage2" ]; then
+ echo "$cmdline_stage2" > "$workspace_dir/cmdline_stage2"
+ echo "file /cmdline_stage2 $workspace_dir/cmdline_stage2 0644 0 0" \
+ >> "$workspace_dir/cpio_list"
+ fi
+
# Generate the final initrd
"$build_dir/scripts/gen_init_cpio" "$workspace_dir/cpio_list" > "$initrd"
}
@@ -229,8 +241,6 @@ function run_qemu() {
local kernel_path=$3
local serial="$workspace_dir/qemu.serial"
- cmdline="$cmdline liveupdate=on panic=-1"
-
echo "# Serial Log: $serial"
timeout "$TEST_TIMEOUT" \
$qemu_cmd -m 1G -smp 2 -no-reboot -nographic -nodefaults \
@@ -316,9 +326,17 @@ function main() {
build_kernel "$build_dir" "$make_cmd_build" "$KERNEL_IMAGE" "$target"
local final_kernel="$build_dir/arch/$arch/boot/$KERNEL_IMAGE"
- mkinitrd "$build_dir" "$final_kernel" "$make_cmd"
+ local cmdline="$KERNEL_CMDLINE liveupdate=on panic=-1"
+ local cmdline_stage2=""
+
+ # By default, the kexec'd kernel reuses the first kernel's command line
+ if [ -n "$TEST_CMDLINE_STAGE1$TEST_CMDLINE_STAGE2" ]; then
+ cmdline_stage2="$cmdline $TEST_CMDLINE_STAGE2"
+ fi
+
+ mkinitrd "$build_dir" "$final_kernel" "$make_cmd" "$cmdline_stage2"
- run_qemu "$QEMU_CMD" "$KERNEL_CMDLINE" "$final_kernel"
+ run_qemu "$QEMU_CMD" "$cmdline $TEST_CMDLINE_STAGE1" "$final_kernel"
ktap_test_pass "$test_name succeeded"
}
--
2.56.1.911.g883d8bcb20-goog