VirtualBox

Ticket #8913: vboxhstart.sh

File vboxhstart.sh, 4.2 KB (added by Markus Duft, 13 years ago)

improved startscript. now sets rdesktop window title to vm name and port.

Line 
1#!/usr/bin/env bash
2
3####
4# VirtualBox headless VM start script, copyright (c) Markus Duft
5# .. do with this what you want (public domain), no warranty ... :)
6####
7
8zenity=$(type -p zenity)
9notify=$(type -p notify-send)
10
11[[ -z ${zenity} || -z ${notify} ]] && \
12 {
13 echo "zenity and notify-send are required for this script!"
14 exit 1
15 }
16
17function error() { echo "$@"; exit 1; }
18
19# gather together all the vms.
20declare -a vms
21declare -a running
22declare -a vrdes
23
24function update_all_vms() {
25 vms=()
26 eval $(VBoxManage list vms | sed -e 's,"\([^"]*\)" {\([^}]*\)},vms[${#vms[@]}]="\2|\1",g')
27}
28
29function update_running_vms() {
30 running=()
31 eval $(VBoxManage list runningvms | sed -e 's,"\([^"]*\)" {\([^}]*\)},running[${#running[@]}]="\2",g')
32}
33
34function update_vrde() {
35 vrdes=()
36 update_all_vms
37 update_running_vms
38
39 for vm in "${running[@]}"; do
40 eval $(VBoxManage showvminfo ${vm%|*} --machinereadable | grep -E '(\<vrde\>|\<vrdeport\>)')
41
42 if [[ ${vrdeport} == "-1" ]]; then
43 zenity --warning --text "The virtual machine ${vm%|*} has an invalid VRDE port. This is most probably a conflict with another machine."
44 continue
45 fi
46
47 if [[ ${vrde} == on ]]; then
48 vrdes[${#vrdes[@]}]="${vrdeport}|${vm#*|}"
49 fi
50 done
51}
52
53function name_from_uuid() {
54 for vm in "${vms[@]}"; do
55 if [[ ${vm%|*} == $1 ]]; then
56 echo "${vm#*|}"
57 return
58 fi
59 done
60}
61
62function run_manager() {
63 update_all_vms
64 update_running_vms
65
66 local cmd="zenity --list --title \"Choose VM\" --text \"Choose a Virtual Machine you want to start or stop\" --width=350 --height=250 --hide-column=1 --column \"UUID\" --column \"Name\" --column \"State\" "
67
68 for vm in "${vms[@]}"; do
69 local state=stopped
70 [[ ${running[@]} == *${vm%|*}* ]] && state=started
71
72 cmd="${cmd} \"${vm%|*}\" \"${vm#*|}\" \"${state}\""
73 done
74
75 local response=$(eval "${cmd}")
76
77 if [[ -n ${response} ]]; then
78 if [[ ${running[@]} == *${response}* ]]; then
79 VBoxManage controlvm "${response}" savestate | zenity --progress --pulsate --auto-close --no-cancel --text="Saving state"
80 if [[ $? == 0 ]]; then
81 notify-send -t 4000 -i info "Virtual Machine state saved" "Virtual Machine $(name_from_uuid "${response}") was saved successfully"
82 else
83 notify-send -t 4000 -i error "Virtual Machine Error" "Virtual Machine $(name_from_uuid "${response}") could not be stopped!"
84 fi
85 else
86 VBoxManage startvm "${response}" --type headless | zenity --progress --pulsate --auto-close --no-cancel --text="Starting or restoring"
87 if [[ $? == 0 ]]; then
88 notify-send -t 4000 -i info "Virtual Machine starting" "Virtual Machine $(name_from_uuid "${response}") is starting ..."
89 else
90 notify-send -t 4000 -i error "Virtual Machine Error" "Virtual Machine $(name_from_uuid "${response}") could not be started!"
91 fi
92 fi
93 fi
94}
95
96function run_vrde() {
97 update_vrde
98
99 local cmd="zenity --list --title \"Choose VM\" --text \"Choose a Virtual Machine you want to connect to\" --width=350 --height=250 --hide-column=1 --column \"Port\" --column \"Name\" "
100
101 for vm in "${vrdes[@]}"; do
102 cmd="${cmd} \"${vm%|*}\" \"$(name_from_uuid "${vm#*|}")\""
103 done
104
105 echo $cmd
106
107 local response=$(eval ${cmd})
108
109 if [[ -n ${response} ]]; then
110 for vm in "${vrdes[@]}"; do
111 if [[ ${response} == ${vm%|*} ]]; then
112 break
113 fi
114
115 vm=
116 done
117
118 [[ -n ${vm} ]] && vm=$(name_from_uuid "${vm#*|}")
119
120 rdesktop -b localhost:${response} -T "VM: ${vm} (${response})" &
121 local _id=$!
122
123 sleep 1
124 local ok=$(ps -ef | grep "\<${_id}\>" | grep -v '\<grep\>' | wc -l)
125
126 if [[ ${ok} != 1 ]]; then
127 notify-send -t 4000 -i error "Remote Desktop Error" "Could not connect to Virtual Machine on port ${response}..."
128 fi
129 fi
130}
131
132case "$1" in
133manager) run_manager || error "error while running machine manager" ;;
134vrdemanager) run_vrde || error "error while running vrde manager" ;;
135*) error "invalid mode. pass 'manager' or 'vrdemanager'" ;;
136esac

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy