| 1 | #! /bin/bash
|
|---|
| 2 |
|
|---|
| 3 | Command=$1
|
|---|
| 4 | VM_Name=$2
|
|---|
| 5 | Timeout=$3
|
|---|
| 6 | if [ -z "$Timeout" ]; then ShutdownWait="90"; else ShutdownWait="$Timeout"; fi
|
|---|
| 7 | Log=/home/linuxutil/VM-Utils.log
|
|---|
| 8 | Default_VM="VB-WinXP"
|
|---|
| 9 | DefaultUser="todd"
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | logger -p user.notice -t VM-Utils " whoami <`whoami`>; calling run string <$0 $1 $2 $3 $4>"
|
|---|
| 14 |
|
|---|
| 15 | # note: run sting for calling from other users:
|
|---|
| 16 | # su root -c "VM-Utils status"
|
|---|
| 17 |
|
|---|
| 18 | WhoAmI="`/usr/bin/whoami`"
|
|---|
| 19 | if [ "$WhoAmI" != "root" ] && [ "$WhoAmI" != "$DefaultUser" ]; then
|
|---|
| 20 | echo ""
|
|---|
| 21 | echo 'Dude! You must be root or '$DefaultUser' to do this.'
|
|---|
| 22 | echo 'Exiting. Bummer ...'
|
|---|
| 23 | echo ""
|
|---|
| 24 | exit 1
|
|---|
| 25 | fi
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | # Change this as you get more Windows Servers!
|
|---|
| 30 | if [ "$VM_Name" = "" ]; then VM_Name=$Default_VM; fi
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | if [ "$WhoAmI" = "$DefaultUser" ]; then
|
|---|
| 34 | NotFound="`/usr/bin/VBoxManage showvminfo $VM_Name | grep -i -e "Could not find"`"
|
|---|
| 35 | else
|
|---|
| 36 | NotFound="`su $DefaultUser -c "/usr/bin/VBoxManage showvminfo $VM_Name" | grep -i -e "Could not find"`"
|
|---|
| 37 | fi
|
|---|
| 38 |
|
|---|
| 39 | if [ -n "$NotFound" ]; then
|
|---|
| 40 | echo "$VM_Name not running. Did you mis-spell it?"
|
|---|
| 41 | logger -p user.notice -t vm-utils " $VM_Name not running. Did you mis-spell it?"
|
|---|
| 42 | exit 1
|
|---|
| 43 | fi
|
|---|
| 44 |
|
|---|
| 45 | case "$Command" in
|
|---|
| 46 | start)
|
|---|
| 47 | Running="`ps ax | grep -v grep | grep -e "$VM_Name" | grep -v VM-Utils | grep -v showvminfo`"
|
|---|
| 48 | if [ -n "$Running" ]; then
|
|---|
| 49 | echo "$VM_Name is already running"
|
|---|
| 50 | exit 1
|
|---|
| 51 | fi
|
|---|
| 52 |
|
|---|
| 53 | # Some VirtualBox housekeeping (should also be done in rc.local)
|
|---|
| 54 | chmod 0666 /dev/net/tun
|
|---|
| 55 | chmod 0777 /dev/fd0
|
|---|
| 56 | chmod 0777 /dev/scd0
|
|---|
| 57 | chmod 2777 $Log
|
|---|
| 58 |
|
|---|
| 59 | echo "Starting $VM_Name"
|
|---|
| 60 | if [ "$WhoAmI" = "$DefaultUser" ]; then
|
|---|
| 61 | /usr/bin/VBoxHeadless --startvm $VM_Name & > $Log 2>&1
|
|---|
| 62 | else
|
|---|
| 63 | su $DefaultUser -c "/usr/bin/VBoxHeadless --startvm $VM_Name &" > $Log 2>&1
|
|---|
| 64 | fi
|
|---|
| 65 | Status=$?
|
|---|
| 66 | # Let the log catch up
|
|---|
| 67 | sleep 2
|
|---|
| 68 |
|
|---|
| 69 | if [ -n "`cat $Log | grep -i -e "failed to start"`" ] || [ -n "`cat $Log | grep -i -e "ERROR"`" ] ; then
|
|---|
| 70 | Status=1
|
|---|
| 71 | cat $Log
|
|---|
| 72 | logger -p user.notice -t VM-Utils " starting $VM_Name. Error Message <`cat $Log`>"
|
|---|
| 73 | fi
|
|---|
| 74 |
|
|---|
| 75 | if [ "$Status" = "0" ]; then
|
|---|
| 76 | logger -p user.notice -t VM-Utils " starting $VM_Name. Start status <$Status>"
|
|---|
| 77 | fi
|
|---|
| 78 |
|
|---|
| 79 | sleep 2
|
|---|
| 80 | ;;
|
|---|
| 81 |
|
|---|
| 82 | stop)
|
|---|
| 83 | Running="`ps ax | grep -v grep | grep -e "$VM_Name" | grep -v VM-Utils | grep -v showvminfo`"
|
|---|
| 84 | if [ -z "$Running" ]; then
|
|---|
| 85 | echo "$VM_Name is not running"
|
|---|
| 86 | logger -p user.notice -t VM-Utils " $VM_Name is not running"
|
|---|
| 87 | exit 1
|
|---|
| 88 | fi
|
|---|
| 89 |
|
|---|
| 90 | # wait for VBoxSVC to exit after the "VBoxManage showvminfo" command
|
|---|
| 91 | # SvcDelay=10
|
|---|
| 92 | SvcDelay=1
|
|---|
| 93 | echo "Waiting $SvcDelay seconds for VBoxManage showvminf's VBoxSVC to exit"
|
|---|
| 94 | I=0; while [ "$I" -lt "$SvcDelay" ]; do
|
|---|
| 95 | I=`expr $I + 1`
|
|---|
| 96 | echo -n "$I "
|
|---|
| 97 | sleep 1
|
|---|
| 98 | done
|
|---|
| 99 | echo ""
|
|---|
| 100 |
|
|---|
| 101 | echo "Shutting down $VM_Name. Waiting $ShutdownWait seconds"
|
|---|
| 102 |
|
|---|
| 103 | SvcFound="`ps alx | grep -v grep | grep VBoxSVC`"
|
|---|
| 104 | if [ -z "$SvcFound" ]; then
|
|---|
| 105 | echo "VBoxSVC process not found. Starting it"
|
|---|
| 106 | if [ "$WhoAmI" = "$DefaultUser" ]; then
|
|---|
| 107 | /usr/lib/virtualbox/VBoxSVC --automate &
|
|---|
| 108 | else
|
|---|
| 109 | su $DefaultUser -c "/usr/lib/virtualbox/VBoxSVC --automate &"
|
|---|
| 110 | fi
|
|---|
| 111 | sleep 4
|
|---|
| 112 | fi
|
|---|
| 113 |
|
|---|
| 114 | if [ "$WhoAmI" = "$DefaultUser" ]; then
|
|---|
| 115 | /usr/bin/VBoxManage controlvm $VM_Name acpipowerbutton
|
|---|
| 116 | else
|
|---|
| 117 | su $DefaultUser -c "/usr/bin/VBoxManage controlvm $VM_Name acpipowerbutton"
|
|---|
| 118 | fi
|
|---|
| 119 | Status=$?
|
|---|
| 120 | logger -p user.notice -t VM-Utils " stopping $VM_Name. Stop status <$Status>"
|
|---|
| 121 |
|
|---|
| 122 | echo -n "Waiting for $VM_Name to shutdown (max wait $ShutdownWait seconds) "
|
|---|
| 123 | I=0
|
|---|
| 124 | while [ "$I" -lt "$ShutdownWait" ]
|
|---|
| 125 | do
|
|---|
| 126 | I=`expr $I + 1`
|
|---|
| 127 | echo -n "$I "
|
|---|
| 128 | sleep 1
|
|---|
| 129 |
|
|---|
| 130 | # Running="`ps ax | grep -v grep | grep -e "$VM_Name" | grep -v VM-Utils | grep -v linuxutil`"
|
|---|
| 131 | Running="`ps ax | grep -v grep | grep -e "$VM_Name" | grep -e "--startvm" | grep -v showvminfo`"
|
|---|
| 132 | # echo $Running
|
|---|
| 133 | if [ -z "$Running" ]; then I=2000; fi
|
|---|
| 134 | done
|
|---|
| 135 | if [ "$I" -eq "$ShutdownWait" ]; then echo -n "TIMEOUT"; fi
|
|---|
| 136 | echo ""
|
|---|
| 137 | ;;
|
|---|
| 138 |
|
|---|
| 139 | status)
|
|---|
| 140 | if [ "$WhoAmI" = "$DefaultUser" ]; then
|
|---|
| 141 | /usr/bin/VBoxManage list runningvms
|
|---|
| 142 | else
|
|---|
| 143 | su $DefaultUser -c "/usr/bin/VBoxManage list runningvms"
|
|---|
| 144 | fi
|
|---|
| 145 | ;;
|
|---|
| 146 |
|
|---|
| 147 | *)
|
|---|
| 148 | echo "Usage: $0 start|stop|status VM_Name Timeout_seconds (case sensitive)"
|
|---|
| 149 | echo ""
|
|---|
| 150 | exit 1
|
|---|
| 151 | esac
|
|---|
| 152 |
|
|---|
| 153 | exit $Status
|
|---|