diff --git a/test/vhost/common/common.sh b/test/vhost/common/common.sh index fccd025fd..9a0835eb3 100644 --- a/test/vhost/common/common.sh +++ b/test/vhost/common/common.sh @@ -362,15 +362,24 @@ function vm_kill() fi } +# List all VM numbers in VM_BASE_DIR +# +function vm_list_all() +{ + local vms="$(shopt -s nullglob; echo $VM_BASE_DIR/[0-9])" + if [[ ! -z "$vms" ]]; then + basename --multiple $vms + fi +} + # Kills all VM in $VM_BASE_DIR # function vm_kill_all() { - shopt -s nullglob - for vm in $VM_BASE_DIR/[0-9]*; do - vm_kill $(basename $vm) + local vm + for vm in $(vm_list_all); do + vm_kill $vm done - shopt -u nullglob } # Shutdown all VM in $VM_BASE_DIR @@ -380,8 +389,10 @@ function vm_shutdown_all() local shell_restore_x="$( [[ "$-" =~ x ]] && echo 'set -x' )" set +x - shopt -s nullglob - for vm in $VM_BASE_DIR/[0-9]*; do + local vms=$(vm_list_all) + local vm + + for vm in $vms; do vm_shutdown $(basename $vm) done @@ -389,7 +400,7 @@ function vm_shutdown_all() timeo=15 while [[ $timeo -gt 0 ]]; do all_vms_down=1 - for vm in $VM_BASE_DIR/[0-9]*; do + for vm in $vms; do if [[ -r $vm/qemu.pid ]] && pkill -0 -F "$vm/qemu.pid"; then all_vms_down=0 break @@ -398,7 +409,6 @@ function vm_shutdown_all() if [[ $all_vms_down == 1 ]]; then notice "All VMs successfully shut down" - shopt -u nullglob $shell_restore_x return 0 fi @@ -406,8 +416,9 @@ function vm_shutdown_all() ((timeo-=1)) sleep 1 done - shopt -u nullglob + $shell_restore_x + error "Timout waiting for some VMs to shutdown" return 1 } @@ -696,8 +707,10 @@ function vm_setup() function vm_run() { - local OPTIND optchar a + local OPTIND optchar vm local run_all=false + local vms_to_run="" + while getopts 'a-:' optchar; do case "$optchar" in a) run_all=true ;; @@ -708,11 +721,8 @@ function vm_run() esac done - local vms_to_run="" - if $run_all; then - shopt -s nullglob - vms_to_run=$VM_BASE_DIR/[0-9]* + vms_to_run="$(vm_list_all)" else shift $((OPTIND-1)) for vm in $@; do @@ -721,18 +731,18 @@ function vm_run() error "VM$vm not defined - setup it first" return 1 fi - vms_to_run+=" $VM_BASE_DIR/$vm" + vms_to_run+=" $vm" done fi for vm in $vms_to_run; do - if vm_is_running $(basename $vm); then - warning "VM$(basename $vm) ($vm) already running" + if vm_is_running $vm; then + warning "VM$vm ($VM_BASE_DIR/$vm) already running" continue fi - notice "running $vm/run.sh" - if ! $vm/run.sh; then + notice "running $VM_BASE_DIR$vm/run.sh" + if ! $VM_BASE_DIR/$vm/run.sh; then error "FAILED to run vm $vm" return 1 fi