Path: blob/master/tools/testing/selftests/kho/vmtest.sh
49639 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="$tmp_dir/initrd.cpio"1314source "$test_dir/../kselftest/ktap_helpers.sh"1516function usage() {17cat <<EOF18$0 [-d build_dir] [-j jobs] [-t target_arch] [-h]19Options:20-d) path to the kernel build directory21-j) number of jobs for compilation, similar to -j in make22-t) run test for target_arch, requires CROSS_COMPILE set23supported targets: aarch64, x86_6424-h) display this help25EOF26}2728function cleanup() {29rm -fr "$tmp_dir"30ktap_finished31}32trap cleanup EXIT3334function skip() {35local msg=${1:-""}3637ktap_test_skip "$msg"38exit "$KSFT_SKIP"39}4041function fail() {42local msg=${1:-""}4344ktap_test_fail "$msg"45exit "$KSFT_FAIL"46}4748function build_kernel() {49local build_dir=$150local make_cmd=$251local arch_kconfig=$352local kimage=$45354local kho_config="$tmp_dir/kho.config"55local kconfig="$build_dir/.config"5657# enable initrd, KHO and KHO test in kernel configuration58tee "$kconfig" > "$kho_config" <<EOF59CONFIG_BLK_DEV_INITRD=y60CONFIG_KEXEC_HANDOVER=y61CONFIG_KEXEC_HANDOVER_DEBUGFS=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=$18283"$CROSS_COMPILE"gcc -s -static -Os -nostdinc -nostdlib \84-fno-asynchronous-unwind-tables -fno-ident \85-I "$headers_dir/include" \86-I "$kernel_dir/tools/include/nolibc" \87-o "$tmp_dir/init" "$test_dir/init.c"8889cat > "$tmp_dir/cpio_list" <<EOF90dir /dev 0755 0 091dir /proc 0755 0 092dir /debugfs 0755 0 093nod /dev/console 0600 0 0 c 5 194file /init $tmp_dir/init 0755 0 095file /kernel $kernel 0644 0 096EOF9798"$build_dir/usr/gen_init_cpio" "$tmp_dir/cpio_list" > "$initrd"99}100101function run_qemu() {102local qemu_cmd=$1103local cmdline=$2104local kernel=$3105local serial="$tmp_dir/qemu.serial"106107cmdline="$cmdline kho=on panic=-1"108109$qemu_cmd -m 1G -smp 2 -no-reboot -nographic -nodefaults \110-accel kvm -accel hvf -accel tcg \111-serial file:"$serial" \112-append "$cmdline" \113-kernel "$kernel" \114-initrd "$initrd"115116grep "KHO restore succeeded" "$serial" &> /dev/null || fail "KHO failed"117}118119function target_to_arch() {120local target=$1121122case $target in123aarch64) echo "arm64" ;;124x86_64) echo "x86" ;;125*) skip "architecture $target is not supported"126esac127}128129function main() {130local build_dir="$kernel_dir/.kho"131local jobs=$(($(nproc) * 2))132local target="$(uname -m)"133134# skip the test if any of the preparation steps fails135set -o errtrace136trap skip ERR137138while getopts 'hd:j:t:' opt; do139case $opt in140d)141build_dir="$OPTARG"142;;143j)144jobs="$OPTARG"145;;146t)147target="$OPTARG"148;;149h)150usage151exit 0152;;153*)154echo Unknown argument "$opt"155usage156exit 1157;;158esac159done160161ktap_print_header162ktap_set_plan 1163164if [[ "$target" != "$(uname -m)" ]] && [[ -z "$CROSS_COMPILE" ]]; then165skip "Cross-platform testing needs to specify CROSS_COMPILE"166fi167168mkdir -p "$build_dir"169local arch=$(target_to_arch "$target")170source "$test_dir/$arch.conf"171172# build the kernel and create initrd173# initrd includes the kernel image that will be kexec'ed174local make_cmd="make ARCH=$arch CROSS_COMPILE=$CROSS_COMPILE -j$jobs"175build_kernel "$build_dir" "$make_cmd" "$QEMU_KCONFIG" "$KERNEL_IMAGE"176177local kernel="$build_dir/arch/$arch/boot/$KERNEL_IMAGE"178mkinitrd "$kernel"179180run_qemu "$QEMU_CMD" "$KERNEL_CMDLINE" "$kernel"181182ktap_test_pass "KHO succeeded"183}184185main "$@"186187188