Currently vmtest-dmem.sh relies on the host's running kernel or a
pre-built one when booting the virtme-ng VM, with no option to
configure and build a local kernel tree directly.

This adds friction to the development cycle: the user must manually
run vng --kconfig with the correct config fragment, build the kernel,
and pass the result to the script.

Add a -b flag that automates this workflow.  When set, handle_build()
configures the kernel using vng --kconfig with the selftest config
fragment, builds it with make -j$(nproc), and run_vm() passes the
local tree to vng --run so the VM boots the freshly built kernel.

Signed-off-by: Eric Chanudet <[email protected]>
Signed-off-by: Albert Esteve <[email protected]>
---
 tools/testing/selftests/cgroup/vmtest-dmem.sh | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/cgroup/vmtest-dmem.sh 
b/tools/testing/selftests/cgroup/vmtest-dmem.sh
index b395b7153f635..415c1722b08e7 100755
--- a/tools/testing/selftests/cgroup/vmtest-dmem.sh
+++ b/tools/testing/selftests/cgroup/vmtest-dmem.sh
@@ -15,6 +15,7 @@ readonly KERNEL_CHECKOUT="$(realpath 
"${SCRIPT_DIR}"/../../../../)"
 
 source "${SCRIPT_DIR}"/../kselftest/ktap_helpers.sh
 
+BUILD=0
 QEMU="qemu-system-$(uname -m)"
 VERBOSE=0
 SHELL_MODE=0
@@ -26,6 +27,7 @@ function usage() {
        cat <<EOF
 $0 [OPTIONS]
 Options:
+       -b      Build kernel from source tree before booting
        -q      QEMU binary/path (default: ${QEMU})
        -s      Start interactive shell in VM instead of running tests
        -v      Verbose output (vng boot logs on stdout)
@@ -60,16 +62,33 @@ function check_deps() {
        done
 }
 
+function handle_build() {
+       [[ "${BUILD}" -eq 1 ]] || return 0
+
+       [[ -d "${KERNEL_CHECKOUT}" ]] || \
+               fail "-b requires vmtest-dmem.sh called from the kernel source 
tree"
+
+       pushd "${KERNEL_CHECKOUT}" &>/dev/null
+       vng --kconfig --config "${SCRIPT_DIR}"/config || \
+               fail "failed to generate .config for kernel source tree 
(${KERNEL_CHECKOUT})"
+       make O= KBUILD_OUTPUT= -j"$(nproc)" || \
+               fail "failed to build kernel from source tree 
(${KERNEL_CHECKOUT})"
+       popd &>/dev/null
+}
+
 # Run vng with common flags. Extra arguments are appended by the caller:
 #   --exec <script>  for automated test runs
 #   (nothing)        for interactive shell mode
 function run_vm() {
        local verbose_opt=""
+       local kernel_opt=""
 
        [[ "${VERBOSE}" -eq 1 ]] && verbose_opt="--verbose"
+       [[ "${BUILD}" -eq 1 ]] && kernel_opt="${KERNEL_CHECKOUT}"
 
        vng \
                --run \
+               ${kernel_opt:+"${kernel_opt}"} \
                ${verbose_opt:+"${verbose_opt}"} \
                --qemu="$(command -v "${QEMU}")" \
                --user root \
@@ -78,10 +97,11 @@ function run_vm() {
 }
 
 function main() {
-       while getopts ':hvq:s' opt; do
+       while getopts ':hvq:sb' opt; do
                case "${opt}" in
                v) VERBOSE=1 ;;
                q) QEMU="${OPTARG}" ;;
+               b) BUILD=1 ;;
                s) SHELL_MODE=1 ;;
                h) usage; exit 0 ;;
                *) usage; exit 1 ;;
@@ -89,6 +109,7 @@ function main() {
        done
 
        check_deps
+       handle_build
 
        if [[ "${SHELL_MODE}" -eq 1 ]]; then
                echo "Starting interactive shell in VM. Exit to stop VM."

-- 
2.53.0


Reply via email to