Path: blob/master/tools/testing/selftests/kho/vmtest.sh
26302 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.023set -ue45CROSS_COMPILE="${CROSS_COMPILE:-""}"67test_dir=$(realpath "$(dirname "$0")")8kernel_dir=$(realpath "$test_dir/../../../..")910tmp_dir=$(mktemp -d /tmp/kho-test.XXXXXXXX)11headers_dir="$tmp_dir/usr"12initrd_dir="$tmp_dir/initrd"13initrd="$tmp_dir/initrd.cpio"1415source "$test_dir/../kselftest/ktap_helpers.sh"1617function usage() {18cat <<EOF19$0 [-d build_dir] [-j jobs] [-t target_arch] [-h]20Options:21-d) path to the kernel build directory22-j) number of jobs for compilation, similar to -j in make23-t) run test for target_arch, requires CROSS_COMPILE set24supported targets: aarch64, x86_6425-h) display this help26EOF27}2829function cleanup() {30rm -fr "$tmp_dir"31ktap_finished32}33trap cleanup EXIT3435function skip() {36local msg=${1:-""}3738ktap_test_skip "$msg"39exit "$KSFT_SKIP"40}4142function fail() {43local msg=${1:-""}4445ktap_test_fail "$msg"46exit "$KSFT_FAIL"47}4849function build_kernel() {50local build_dir=$151local make_cmd=$252local arch_kconfig=$353local kimage=$45455local kho_config="$tmp_dir/kho.config"56local kconfig="$build_dir/.config"5758# enable initrd, KHO and KHO test in kernel configuration59tee "$kconfig" > "$kho_config" <<EOF60CONFIG_BLK_DEV_INITRD=y61CONFIG_KEXEC_HANDOVER=y62CONFIG_TEST_KEXEC_HANDOVER=y63CONFIG_DEBUG_KERNEL=y64CONFIG_DEBUG_VM=y65$arch_kconfig66EOF6768make_cmd="$make_cmd -C $kernel_dir O=$build_dir"69$make_cmd olddefconfig7071# verify that kernel confiration has all necessary options72while read -r opt ; do73grep "$opt" "$kconfig" &>/dev/null || skip "$opt is missing"74done < "$kho_config"7576$make_cmd "$kimage"77$make_cmd headers_install INSTALL_HDR_PATH="$headers_dir"78}7980function mkinitrd() {81local kernel=$18283mkdir -p "$initrd_dir"/{dev,debugfs,proc}84sudo mknod "$initrd_dir/dev/console" c 5 18586"$CROSS_COMPILE"gcc -s -static -Os -nostdinc -I"$headers_dir/include" \87-fno-asynchronous-unwind-tables -fno-ident -nostdlib \88-include "$test_dir/../../../include/nolibc/nolibc.h" \89-o "$initrd_dir/init" "$test_dir/init.c" \9091cp "$kernel" "$initrd_dir/kernel"9293pushd "$initrd_dir" &>/dev/null94find . | cpio -H newc --create > "$initrd" 2>/dev/null95popd &>/dev/null96}9798function run_qemu() {99local qemu_cmd=$1100local cmdline=$2101local kernel=$3102local serial="$tmp_dir/qemu.serial"103104cmdline="$cmdline kho=on panic=-1"105106$qemu_cmd -m 1G -smp 2 -no-reboot -nographic -nodefaults \107-accel kvm -accel hvf -accel tcg \108-serial file:"$serial" \109-append "$cmdline" \110-kernel "$kernel" \111-initrd "$initrd"112113grep "KHO restore succeeded" "$serial" &> /dev/null || fail "KHO failed"114}115116function target_to_arch() {117local target=$1118119case $target in120aarch64) echo "arm64" ;;121x86_64) echo "x86" ;;122*) skip "architecture $target is not supported"123esac124}125126function main() {127local build_dir="$kernel_dir/.kho"128local jobs=$(($(nproc) * 2))129local target="$(uname -m)"130131# skip the test if any of the preparation steps fails132set -o errtrace133trap skip ERR134135while getopts 'hd:j:t:' opt; do136case $opt in137d)138build_dir="$OPTARG"139;;140j)141jobs="$OPTARG"142;;143t)144target="$OPTARG"145;;146h)147usage148exit 0149;;150*)151echo Unknown argument "$opt"152usage153exit 1154;;155esac156done157158ktap_print_header159ktap_set_plan 1160161if [[ "$target" != "$(uname -m)" ]] && [[ -z "$CROSS_COMPILE" ]]; then162skip "Cross-platform testing needs to specify CROSS_COMPILE"163fi164165mkdir -p "$build_dir"166local arch=$(target_to_arch "$target")167source "$test_dir/$arch.conf"168169# build the kernel and create initrd170# initrd includes the kernel image that will be kexec'ed171local make_cmd="make ARCH=$arch CROSS_COMPILE=$CROSS_COMPILE -j$jobs"172build_kernel "$build_dir" "$make_cmd" "$QEMU_KCONFIG" "$KERNEL_IMAGE"173174local kernel="$build_dir/arch/$arch/boot/$KERNEL_IMAGE"175mkinitrd "$kernel"176177run_qemu "$QEMU_CMD" "$KERNEL_CMDLINE" "$kernel"178179ktap_test_pass "KHO succeeded"180}181182main "$@"183184185