1#!/usr/bin/env bash 2# SPDX-License-Identifier: BSD-3-Clause 3# Copyright (C) 2018 Intel Corporation 4# All rights reserved. 5# 6testdir=$(readlink -f $(dirname $0)) 7rootdir=$(readlink -f $testdir/../../..) 8source $rootdir/test/common/autotest_common.sh 9source $rootdir/test/vhost/common.sh 10source $testdir/migration-tc1.sh 11source $testdir/migration-tc2.sh 12 13vms=() 14declare -A vms_os 15declare -A vms_raw_disks 16declare -A vms_ctrlrs 17declare -A vms_ctrlrs_disks 18 19# By default use Guest fio 20fio_bin="" 21function usage() { 22 [[ -n $2 ]] && ( 23 echo "$2" 24 echo "" 25 ) 26 echo "Shortcut script for doing automated test of live migration." 27 echo "Usage: $(basename $1) [OPTIONS]" 28 echo 29 echo " --os ARGS VM configuration. This parameter might be used more than once:" 30 echo " --fio-bin=FIO Use specific fio binary (will be uploaded to VM)" 31 echo "-x set -x for script debug" 32} 33 34for param in "$@"; do 35 case "$param" in 36 --help | -h) 37 usage $0 38 exit 0 39 ;; 40 --os=*) os_image="${param#*=}" ;; 41 --fio-bin=*) fio_bin="${param}" ;; 42 -x) set -x ;; 43 -v) SPDK_VHOST_VERBOSE=true ;; 44 *) 45 usage $0 "Invalid argument '$param'" 46 exit 1 47 ;; 48 esac 49done 50 51vhosttestinit 52 53trap 'error_exit "${FUNCNAME}" "${LINENO}"' INT ERR EXIT 54 55function vm_monitor_send() { 56 local vm_num=$1 57 local cmd_result_file="$2" 58 local vm_dir="$VM_DIR/$1" 59 local vm_monitor_port 60 vm_monitor_port=$(cat $vm_dir/monitor_port) 61 62 [[ -n "$vm_monitor_port" ]] || fail "No monitor port!" 63 64 shift 2 65 nc 127.0.0.1 $vm_monitor_port "$@" > $cmd_result_file 66} 67 68# Migrate VM $1 69function vm_migrate() { 70 local from_vm_dir="$VM_DIR/$1" 71 local target_vm_dir 72 local target_vm 73 local target_vm_migration_port 74 target_vm_dir="$(readlink -e $from_vm_dir/vm_migrate_to)" 75 target_vm="$(basename $target_vm_dir)" 76 target_vm_migration_port="$(cat $target_vm_dir/migration_port)" 77 if [[ -n "$2" ]]; then 78 local target_ip=$2 79 else 80 local target_ip="127.0.0.1" 81 fi 82 83 # Sanity check if target VM (QEMU) is configured to accept source VM (QEMU) migration 84 if [[ "$(readlink -e ${target_vm_dir}/vm_incoming)" != "$(readlink -e ${from_vm_dir})" ]]; then 85 fail "source VM $1 or destination VM is not properly configured for live migration" 86 fi 87 88 timing_enter vm_migrate 89 notice "Migrating VM $1 to VM "$(basename $target_vm_dir) 90 echo -e \ 91 "migrate tcp:$target_ip:$target_vm_migration_port\n" \ 92 "info migrate\n" \ 93 "quit" | vm_monitor_send $1 "$from_vm_dir/migration_result" 94 95 # Post migration checks: 96 if ! grep "Migration status: completed" $from_vm_dir/migration_result -q; then 97 cat $from_vm_dir/migration_result 98 fail "Migration failed:\n" 99 fi 100 101 # Don't perform the following check if target VM is on remote server 102 # as we won't have access to it. 103 # If you need this check then perform it on your own. 104 if [[ "$target_ip" == "127.0.0.1" ]]; then 105 if ! vm_os_booted $target_vm; then 106 cat $target_vm $target_vm_dir/cont_result 107 fail "VM$target_vm is not running" 108 fi 109 fi 110 111 notice "Migration complete" 112 timing_exit vm_migrate 113} 114 115function is_fio_running() { 116 xtrace_disable 117 118 if vm_exec $1 'kill -0 $(cat /root/fio.pid)'; then 119 local ret=0 120 else 121 local ret=1 122 fi 123 124 xtrace_restore 125 return $ret 126} 127 128run_test "vhost_migration_tc1" migration_tc1 129run_test "vhost_migration_tc2" migration_tc2 130 131trap - SIGINT ERR EXIT 132 133vhosttestfini 134