Skip to content

Instantly share code, notes, and snippets.

@nhoriguchi
Last active August 29, 2015 13:57
Show Gist options
  • Save nhoriguchi/9425387 to your computer and use it in GitHub Desktop.
Save nhoriguchi/9425387 to your computer and use it in GitHub Desktop.
virsh_start_wait.sh
#!/bin/bash
TMPF=$(mktemp)
while getopts c: OPT
do
case $OPT in
"c" ) export LIBVIRT_DEFAULT_URI=$OPTARG ;;
esac
done
shift $[OPTIND - 1]
VM=$1
[ ! "$1" ] && echo "need to give domain name" && exit 1
cat <<EOF > ${TMPF}.vm_start_wait.exp
#!/usr/bin/expect
set timeout 1
set timecount 100
log_file -noappend ${TMPF}.vm_start_wait.log
spawn virsh console $VM
expect "Escape character is"
send "\n"
while {\$timecount > 0} {
send "\n"
expect "login:" {
send_user "\nVM start finished\n"
break
}
set timecount [expr \$timecount-1]
if {[expr \$timecount] == 0} {
send_user "\nVM start timeout\n"
}
}
send -- "^]"
interact
EOF
echo "starting domain $VM ..."
virsh start $VM > /dev/null 2>&1
expect ${TMPF}.vm_start_wait.exp > /dev/null 2>&1
[ ! -e ${TMPF}.vm_start_wait.log ] && echo "expect failed." && return 1
grep "VM start finished" ${TMPF}.vm_start_wait.log > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment