Path: blob/master/tools/testing/selftests/kho/vmtest.sh
29268 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_TEST_KEXEC_HANDOVER=y62CONFIG_DEBUG_KERNEL=y63CONFIG_DEBUG_VM=y64$arch_kconfig65EOF6667make_cmd="$make_cmd -C $kernel_dir O=$build_dir"68$make_cmd olddefconfig6970# verify that kernel confiration has all necessary options71while read -r opt ; do72grep "$opt" "$kconfig" &>/dev/null || skip "$opt is missing"73done < "$kho_config"7475$make_cmd "$kimage"76$make_cmd headers_install INSTALL_HDR_PATH="$headers_dir"77}7879function mkinitrd() {80local kernel=$18182"$CROSS_COMPILE"gcc -s -static -Os -nostdinc -nostdlib \83-fno-asynchronous-unwind-tables -fno-ident \84-I "$headers_dir/include" \85-I "$kernel_dir/tools/include/nolibc" \86-o "$tmp_dir/init" "$test_dir/init.c"8788cat > "$tmp_dir/cpio_list" <<EOF89dir /dev 0755 0 090dir /proc 0755 0 091dir /debugfs 0755 0 092nod /dev/console 0600 0 0 c 5 193file /init $tmp_dir/init 0755 0 094file /kernel $kernel 0644 0 095EOF9697"$build_dir/usr/gen_init_cpio" "$tmp_dir/cpio_list" > "$initrd"98}99100function run_qemu() {101local qemu_cmd=$1102local cmdline=$2103local kernel=$3104local serial="$tmp_dir/qemu.serial"105106cmdline="$cmdline kho=on panic=-1"107108$qemu_cmd -m 1G -smp 2 -no-reboot -nographic -nodefaults \109-accel kvm -accel hvf -accel tcg \110-serial file:"$serial" \111-append "$cmdline" \112-kernel "$kernel" \113-initrd "$initrd"114115grep "KHO restore succeeded" "$serial" &> /dev/null || fail "KHO failed"116}117118function target_to_arch() {119local target=$1120121case $target in122aarch64) echo "arm64" ;;123x86_64) echo "x86" ;;124*) skip "architecture $target is not supported"125esac126}127128function main() {129local build_dir="$kernel_dir/.kho"130local jobs=$(($(nproc) * 2))131local target="$(uname -m)"132133# skip the test if any of the preparation steps fails134set -o errtrace135trap skip ERR136137while getopts 'hd:j:t:' opt; do138case $opt in139d)140build_dir="$OPTARG"141;;142j)143jobs="$OPTARG"144;;145t)146target="$OPTARG"147;;148h)149usage150exit 0151;;152*)153echo Unknown argument "$opt"154usage155exit 1156;;157esac158done159160ktap_print_header161ktap_set_plan 1162163if [[ "$target" != "$(uname -m)" ]] && [[ -z "$CROSS_COMPILE" ]]; then164skip "Cross-platform testing needs to specify CROSS_COMPILE"165fi166167mkdir -p "$build_dir"168local arch=$(target_to_arch "$target")169source "$test_dir/$arch.conf"170171# build the kernel and create initrd172# initrd includes the kernel image that will be kexec'ed173local make_cmd="make ARCH=$arch CROSS_COMPILE=$CROSS_COMPILE -j$jobs"174build_kernel "$build_dir" "$make_cmd" "$QEMU_KCONFIG" "$KERNEL_IMAGE"175176local kernel="$build_dir/arch/$arch/boot/$KERNEL_IMAGE"177mkinitrd "$kernel"178179run_qemu "$QEMU_CMD" "$KERNEL_CMDLINE" "$kernel"180181ktap_test_pass "KHO succeeded"182}183184main "$@"185186187