From 127682c4e6f24124583f2df26a70cc679d5cb0d7 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sun, 17 Jun 2018 13:30:40 -0300 Subject: Run guest inside a screen session to be more immune to hangups --- kvmx | 136 +++++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 79 insertions(+), 57 deletions(-) diff --git a/kvmx b/kvmx index 1c8d875..8acd1b2 100755 --- a/kvmx +++ b/kvmx @@ -426,6 +426,8 @@ function kvmx_up { net_opts="$net_opts,dns=$net_dns" fi + net_opts="-net $net_opts" + if [ -z "$spice" ] || [ "$spice" == "1" ]; then spice_opts="-spice port=$PORT,addr=127.0.0.1,disable-ticketing,streaming-video=off,jpeg-wan-compression=never,playback-compression=off,zlib-glz-wan-compression=never,image-compression=off" spice_opts="$spice_opts -device virtio-serial-pci" @@ -441,24 +443,42 @@ function kvmx_up { sound_opts="-soundhw $sound" fi - # Run virtual machine + # Run virtual machine, nohup approach # See https://en.wikipedia.org/wiki/Nohup#Overcoming_hanging - nohup kvm -m $memory -name $VM \ - -drive file=$image,if=$drive_interface $graphics $shared \ - $spice_opts \ - -chardev "socket,id=monitor,path=$MONITORFILE,server,nowait" -mon chardev=monitor,mode=readline \ - -chardev "socket,id=serial0,path=$CONSOLEFILE,server,nowait" -device isa-serial,chardev=serial0 \ - -smp $smp -cpu host -balloon virtio \ - $sound_opts \ - $cdrom_opts \ - $boot_opts \ - -net $net_opts \ - $qemu_opts &> $LOGFILE < /dev/null & - - PID="$!" + #nohup kvm -m $memory -name $VM \ + # -drive file=$image,if=$drive_interface $graphics $shared \ + # -chardev "socket,id=monitor,path=$MONITORFILE,server,nowait" -mon chardev=monitor,mode=readline \ + # -chardev "socket,id=serial0,path=$CONSOLEFILE,server,nowait" -device isa-serial,chardev=serial0 \ + # -smp $smp -cpu host -balloon virtio \ + # $sound_opts \ + # $cdrom_opts \ + # $boot_opts \ + # $spice_opts \ + # $net_opts \ + # $qemu_opts &> $LOGFILE < /dev/null & + + # Run virtual machine, screen approach + # This is more immune to hangups + screen -S kvmx-qemu-$VM -d -m kvm -m $memory -name $VM \ + -drive file=$image,if=$drive_interface $graphics $shared \ + -chardev "socket,id=monitor,path=$MONITORFILE,server,nowait" -mon chardev=monitor,mode=readline \ + -chardev "socket,id=serial0,path=$CONSOLEFILE,server,nowait" -device isa-serial,chardev=serial0 \ + -smp $smp -cpu host -balloon virtio \ + $spice_opts \ + $sound_opts \ + $cdrom_opts \ + $boot_opts \ + $net_opts \ + -pidfile $PIDFILE \ + -D $LOGFILE \ + $qemu_opts + + # Only if nohup approach is being used + #PID="$!" # Save state - echo $PID > $PIDFILE + # Save PID here only if nohup approach is being used + #echo $PID > $PIDFILE echo $PORT > $PORTFILE echo $SSH > $SSHFILE echo $GUEST_DISPLAY > $DISPLAYFILE @@ -744,7 +764,7 @@ function kvmx_suspended { if ! kvmx_running; then return 1 else - if ps -p $PID -o stat --no-headers | grep -q 'Tl'; then + if ps -p $PID -o stat --no-headers | grep -q 'T'; then return 0 else return 1 @@ -1773,7 +1793,7 @@ function kvmx_version { # Shell function kvmx_shell { - local tail="" + #local tail="" local restricted="$1" local restricted_actions=":status:start:stop:poweroff:suspend:resume:console:monitor" restricted_actions="$restricted_actions:wipe:shred:app_base:version:list_image:kill:" @@ -1796,52 +1816,54 @@ function kvmx_shell { echo "Running in restricted shell mode." echo "Allowed commands are only `echo $restricted_actions | tr ':' ' '`" else - if [ "${STDIN[0]}" == "up" ] || [ "${STDIN[0]}" == "start" ] || [ "${STDIN[0]}" == "restart" ]; then - # Process command, nohup version, stdout and stderr are redirected to a file - #nohup $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} < /dev/null >> $LOG_DIR/nohup 2>&1 & - - # Process command, disown version, stdout are preserved - #$APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} & - $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} < /dev/null >> $LOG_DIR/nohup 2>&1 & - disown -h - - sleep 1 - if [ -e "$LOG_DIR/nohup" ] && [ -z "$tail" ]; then - tail -F $LOG_DIR/nohup & - tail="$1" - fi - sleep 1 - else - $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} - fi - fi - else - if [ "${STDIN[0]}" == "up" ] || [ "${STDIN[0]}" == "start" ] || [ "${STDIN[0]}" == "restart" ]; then - # Process command, nohup version - #nohup $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} < /dev/null >> $LOG_DIR/nohup 2>&1 & - - # Process command, disown version - #$APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} & - $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} < /dev/null >> $LOG_DIR/nohup 2>&1 & - disown -h - - sleep 1 - if [ -e "$LOG_DIR/nohup" ] && [ -z "$tail" ]; then - tail -F $LOG_DIR/nohup & - tail="$1" - fi - sleep 1 - else + #if [ "${STDIN[0]}" == "up" ] || [ "${STDIN[0]}" == "start" ] || [ "${STDIN[0]}" == "restart" ]; then + # # Process command, nohup version, stdout and stderr are redirected to a file + # #nohup $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} < /dev/null >> $LOG_DIR/nohup 2>&1 & + + # # Process command, disown version, stdout are preserved + # #$APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} & + # $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} < /dev/null >> $LOG_DIR/nohup 2>&1 & + # disown -h + + # sleep 1 + # if [ -e "$LOG_DIR/nohup" ] && [ -z "$tail" ]; then + # tail -F $LOG_DIR/nohup & + # tail="$1" + # fi + # sleep 1 + #else + # $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} + #fi $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} fi + else + #if [ "${STDIN[0]}" == "up" ] || [ "${STDIN[0]}" == "start" ] || [ "${STDIN[0]}" == "restart" ]; then + # # Process command, nohup version + # #nohup $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} < /dev/null >> $LOG_DIR/nohup 2>&1 & + + # # Process command, disown version + # #$APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} & + # $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} < /dev/null >> $LOG_DIR/nohup 2>&1 & + # disown -h + + # sleep 1 + # if [ -e "$LOG_DIR/nohup" ] && [ -z "$tail" ]; then + # tail -F $LOG_DIR/nohup & + # tail="$1" + # fi + # sleep 1 + #else + # $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} + #fi + $APP_BASE/kvmx ${STDIN[0]} $VM ${STDIN[@]:1} fi fi done - if [ ! -z "$tail" ]; then - kill $tail &> /dev/null - #rm -f $LOG_DIR/nohup - fi + #if [ ! -z "$tail" ]; then + # kill $tail &> /dev/null + # #rm -f $LOG_DIR/nohup + #fi } # Xrandr integration -- cgit v1.2.3