| 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 | Default_VM="WS08-1"
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 | logger -p user.notice -t VM-Utils " whoami <`whoami`>; calling run string <$0 $1 $2 $3 $4>"
|
|---|
| 12 |
|
|---|
| 13 | # note: run sting for calling from other users:
|
|---|
| 14 | # su root -c "VM-Utils status"
|
|---|
| 15 |
|
|---|
| 16 | WhoAmI="`/usr/bin/whoami`"
|
|---|
| 17 | if [ "$WhoAmI" != "root" ]; then
|
|---|
| 18 | echo ""
|
|---|
| 19 | echo 'Dude! You must be root or todd to do this.'
|
|---|
| 20 | echo 'Exiting. Bummer ...'
|
|---|
| 21 | echo ""
|
|---|
| 22 | exit 1
|
|---|
| 23 | fi
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | # Change this as you get more Windows Servers!
|
|---|
| 28 | if [ "$VM_Name" = "" ]; then VM_Name=$Default_VM; fi
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | NotFound="`su todd -c "/usr/bin/VBoxManage showvminfo $VM_Name" | grep -i -e "Could not find"`"
|
|---|
| 32 | if [ -n "$NotFound" ]; then
|
|---|
| 33 | echo "$VM_Name not running. Did you mis-spell it?"
|
|---|
| 34 | logger -p user.notice -t vm-utils " $VM_Name not running. Did you mis-spell it?"
|
|---|
| 35 | exit 1
|
|---|
| 36 | fi
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | case "$Command" in
|
|---|
| 40 | start)
|
|---|
| 41 | Running="`ps ax | grep -v grep | grep -e "$VM_Name" | grep -v VM-Utils`"
|
|---|
| 42 | if [ -n "$Running" ]; then
|
|---|
| 43 | echo "$VM_Name is already running"
|
|---|
| 44 | exit 1
|
|---|
| 45 | fi
|
|---|
| 46 | echo "Starting $VM_Name"
|
|---|
| 47 | su todd -c "/usr/bin/VBoxHeadless --startvm $VM_Name &"
|
|---|
| 48 | Status=$?
|
|---|
| 49 | logger -p user.notice -t VM-Utils " starting $VM_Name. Start status <$Status>"
|
|---|
| 50 | sleep 2
|
|---|
| 51 | ;;
|
|---|
| 52 |
|
|---|
| 53 | stop)
|
|---|
| 54 | Running="`ps ax | grep -v grep | grep -e "$VM_Name" | grep -v VM-Utils`"
|
|---|
| 55 | if [ -z "$Running" ]; then
|
|---|
| 56 | echo "$VM_Name is not running"
|
|---|
| 57 | logger -p user.notice -t VM-Utils " $VM_Name is not running"
|
|---|
| 58 | exit 1
|
|---|
| 59 | fi
|
|---|
| 60 |
|
|---|
| 61 | # wait for VBoxSVC to exit after the "VBoxManage showvminfo" command
|
|---|
| 62 | SvcDelay=10
|
|---|
| 63 | echo "Waiting $SvcDelay seconds for VBoxManage showvminf's VBoxSVC to exit"
|
|---|
| 64 | I=0; while [ "$I" -lt "$SvcDelay" ]; do
|
|---|
| 65 | I=`expr $I + 1`
|
|---|
| 66 | echo -n "$I "
|
|---|
| 67 | sleep 1
|
|---|
| 68 | done
|
|---|
| 69 | echo ""
|
|---|
| 70 |
|
|---|
| 71 | echo "Shutting down $VM_Name. Waiting $ShutdownWait seconds"
|
|---|
| 72 |
|
|---|
| 73 | SvcFound="`ps alx | grep -v grep | grep VBoxSVC`"
|
|---|
| 74 | if [ -z "$SvcFound" ]; then
|
|---|
| 75 | echo "VBoxSVC process not found. Starting it"
|
|---|
| 76 | su todd -c "/usr/lib/virtualbox/VBoxSVC --automate &"
|
|---|
| 77 | sleep 4
|
|---|
| 78 | fi
|
|---|
| 79 |
|
|---|
| 80 | su todd -c "/usr/bin/VBoxManage controlvm $VM_Name acpipowerbutton"
|
|---|
| 81 | Status=$?
|
|---|
| 82 | logger -p user.notice -t VM-Utils " stopping $VM_Name. Stop status <$Status>"
|
|---|
| 83 |
|
|---|
| 84 | # sleep $ShutdownWait
|
|---|
| 85 | echo -n "Waiting for $VM_Name to shutdown (max wait $ShutdownWait seconds) "
|
|---|
| 86 | I=0
|
|---|
| 87 | while [ "$I" -lt "$ShutdownWait" ]
|
|---|
| 88 | do
|
|---|
| 89 | I=`expr $I + 1`
|
|---|
| 90 | echo -n "$I "
|
|---|
| 91 | sleep 1
|
|---|
| 92 |
|
|---|
| 93 | # Running="`ps ax | grep -v grep | grep -e "$VM_Name" | grep -v VM-Utils | grep -v linuxutil`"
|
|---|
| 94 | Running="`ps ax | grep -v grep | grep -e "$VM_Name" | grep -e "--startvm"`"
|
|---|
| 95 | # echo $Running
|
|---|
| 96 | if [ -z "$Running" ]; then I=2000; fi
|
|---|
| 97 | done
|
|---|
| 98 | if [ "$I" -eq "$ShutdownWait" ]; then echo -n "TIMEOUT"; fi
|
|---|
| 99 | echo ""
|
|---|
| 100 | ;;
|
|---|
| 101 |
|
|---|
| 102 | status)
|
|---|
| 103 | su todd -c "/usr/bin/VBoxManage list runningvms"
|
|---|
| 104 | ;;
|
|---|
| 105 |
|
|---|
| 106 | *)
|
|---|
| 107 | echo "Usage: $0 start|stop|status VM_Name Timeout_seconds (case sensitive)"
|
|---|
| 108 | echo ""
|
|---|
| 109 | exit 1
|
|---|
| 110 | esac
|
|---|
| 111 |
|
|---|
| 112 | exit 0
|
|---|