Re: [kvm-unit-tests PATCH v3 10/16] scripts: Add default arguments for kvmtool

Andrew Jones
2025-05-07T16:43:19Z

On Wed, May 07, 2025 at 04:12:50PM +0100, Alexandru Elisei wrote:
> kvmtool, unless told otherwise, will do its best to make sure that a kernel
> successfully boots in a virtual machine. It does things like automatically
> creating a rootfs and adding extra parameters to the kernel command line.
> This is actively harmful to kvm-unit-tests, because some tests parse the
> kernel command line and they will fail if they encounter the options added
> by kvmtool.
>
> Fortunately for us, kvmtool commit 5613ae26b998 ("Add --nodefaults command
> line argument") addded the --nodefaults kvmtool parameter which disables
> all the implicit virtual machine configuration that cannot be disabled by
> using other parameters, like modifying the kernel command line. So always
> use --nodefaults to allow a test to run.
>
> kvmtool can also be too verbose when running a virtual machine, and this is
> controlled by several parameters. Add those to the default kvmtool command
> line to reduce this verbosity to a minimum.
>
> Before:
>
> $ vm run arm/selftest.flat --cpus 2 --mem 256 --params "setup smp=2 mem=256"
> Info: # lkvm run -k arm/selftest.flat -m 256 -c 2 --name guest-5035
> Unknown subtest
>
> EXIT: STATUS=127
> Warning: KVM compatibility warning.
> virtio-9p device was not detected.
> While you have requested a virtio-9p device, the guest kernel did not initialize it.
> Please make sure that the guest kernel was compiled with CONFIG_NET_9P_VIRTIO=y enabled in .config.
> Warning: KVM compatibility warning.
> virtio-net device was not detected.
> While you have requested a virtio-net device, the guest kernel did not initialize it.
> Please make sure that the guest kernel was compiled with CONFIG_VIRTIO_NET=y enabled in .config.
> Info: KVM session ended normally.
>
> After:
>
> $ vm run arm/selftest.flat --nodefaults --network mode=none --loglevel=warning --cpus 2 --mem 256 --params "setup smp=2 mem=256"
> PASS: selftest: setup: smp: number of CPUs matches expectation
> INFO: selftest: setup: smp: found 2 CPUs
> PASS: selftest: setup: mem: memory size matches expectation
> INFO: selftest: setup: mem: found 256 MB
> SUMMARY: 2 tests
>
> EXIT: STATUS=1
>
> Note that KVMTOOL_DEFAULT_OPTS can be overwritten by an environment
> variable with the same name, but it's not documented in the help string for
> run_tests.sh. This has been done on purpose, since overwritting
> KVMTOOL_DEFAULT_OPTS should only be necessary for debugging or development
> purposes.
>
> Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
> ---
> scripts/common.bash | 10 +++++-----
> scripts/vmm.bash | 13 +++++++++++++
> 2 files changed, 18 insertions(+), 5 deletions(-)
>
> diff --git a/scripts/common.bash b/scripts/common.bash
> index 0645235d8baa..ee0ae71948c2 100644
> --- a/scripts/common.bash
> +++ b/scripts/common.bash
> @@ -56,7 +56,7 @@ function for_each_unittest()
> # because qemu interprets the first argument after
> # -append as a kernel parameter.
> test_args=""
> - opts=""
> + opts="${vmm_opts[$TARGET:default_opts]}"
> groups=""
> arch=""
> machine=""
> @@ -70,13 +70,13 @@ function for_each_unittest()
> elif [[ $line =~ ^test_args\ *=\ *(.*)$ ]]; then
> test_args="${vmm_opts[$TARGET:args]} ${BASH_REMATCH[1]}"
> elif [[ $TARGET = "qemu" ]] && [[ $line =~ ^(extra_params|qemu_params)\ *=\ *'"""'(.*)$ ]]; then
> - opts=$(parse_opts ${BASH_REMATCH[2]}$'\n' $fd)
> + opts="${vmm_opts[$TARGET:default_opts]} $(parse_opts ${BASH_REMATCH[2]}$'\n' $fd)"
> elif [[ $TARGET = "qemu" ]] && [[ $line =~ ^(extra_params|qemu_params)\ *=\ *(.*)$ ]]; then
> - opts=${BASH_REMATCH[2]}
> + opts="${vmm_opts[$TARGET:default_opts]} ${BASH_REMATCH[2]}"
> elif [[ $TARGET = "kvmtool" ]] && [[ $line =~ ^kvmtool_params\ *=\ *'"""'(.*)$ ]]; then
> - opts=$(parse_opts ${BASH_REMATCH[1]}$'\n' $fd)
> + opts="${vmm_opts[$TARGET:default_opts]} $(parse_opts ${BASH_REMATCH[1]}$'\n' $fd)"
> elif [[ $TARGET = "kvmtool" ]] && [[ $line =~ ^kvmtool_params\ *=\ *(.*)$ ]]; then
> - opts=${BASH_REMATCH[1]}
> + opts="${vmm_opts[$TARGET:default_opts]} ${BASH_REMATCH[1]}"
> elif [[ $line =~ ^groups\ *=\ *(.*)$ ]]; then
> groups=${BASH_REMATCH[1]}
> elif [[ $line =~ ^arch\ *=\ *(.*)$ ]]; then
> diff --git a/scripts/vmm.bash b/scripts/vmm.bash
> index 20968f2e6b10..d24a4c4b8713 100644
> --- a/scripts/vmm.bash
> +++ b/scripts/vmm.bash
> @@ -1,5 +1,16 @@
> source config.mak
>
> +# The following parameters are enabled by default when running a test with
> +# kvmtool:
> +# --nodefaults: suppress VM configuration that cannot be disabled otherwise
> +# (like modifying the supplied kernel command line). Tests that
> +# use the command line will fail without this parameter.
> +# --network mode=none: do not create a network device. kvmtool tries to help the
> +# user by automatically create one, and then prints a warning
> +# when the VM terminates if the device hasn't been initialized.
> +# --loglevel=warning: reduce verbosity
> +: "${KVMTOOL_DEFAULT_OPTS:="--nodefaults --network mode=none --loglevel=warning"}"
> +
> ##############################################################################
> # qemu_fixup_return_code translates the ambiguous exit status in Table1 to that
> # in Table2. Table3 simply documents the complete status table.
> @@ -87,12 +98,14 @@ declare -A vmm_opts=(
> [qemu:kernel]='-kernel'
> [qemu:args]='-append'
> [qemu:initrd]='-initrd'
> + [qemu:default_opts]=''
> [qemu:fixup_return_code]=qemu_fixup_return_code
>
> [kvmtool:nr_cpus]='--cpus'
> [kvmtool:kernel]='--kernel'
> [kvmtool:args]='--params'
> [kvmtool:initrd]='--initrd'
> + [kvmtool:default_opts]="$KVMTOOL_DEFAULT_OPTS"
> [kvmtool:fixup_return_code]=kvmtool_fixup_return_code
> )
>
> --
> 2.49.0

Reviewed-by: Andrew Jones <andrew.jones@linux.dev>