VirtualBox

Ticket #11856: vboxcontrol

File vboxcontrol, 3.5 KB (added by ED, 11 years ago)
Line 
1#! /bin/sh
2
3# vboxcontrol Startup script for VirtualBox Virtual Machines
4#
5# chkconfig: 345 99 01
6# description: Manages VirtualBox VMs
7# processname: vboxcontrol
8#
9# pidfile: /var/run/vboxcontrol/vboxcontrol.pid
10#
11### BEGIN INIT INFO
12# Provides: vboxcontrol
13# Required-Start: vboxdrv vboxautostart-service vboxballoonctrl-service vboxweb-service
14# Required-Stop: vboxdrv vboxautostart-service vboxballoonctrl-service vboxweb-service alsa-utils hdapsd
15# Default-Start: 2 3 4 5
16# Default-Stop: 0 1 6
17# Description: VirtualBox start vm's
18### END INIT INFO
19#
20# Version 20120317 by travisn000 based on:
21# Version 20090301 by Kevin Swanson <kswan.info> based on:
22# Version 2008051100 by Jochem Kossen <jochem.kossen@gmail.com>
23# [url]http://farfewertoes.com[/url]
24#
25# Released in the public domain
26#
27
28
29# Source function library.
30#if [ -f /etc/init.d/functions ] ; then
31#. /etc/init.d/functions
32#elif [ -f /etc/rc.d/init.d/functions ] ; then
33#. /etc/rc.d/init.d/functions
34#else
35#exit 1
36#echo exit 1
37#fi
38
39################################################################################
40# INITIAL CONFIGURATION
41VBOXDIR="/etc/virtualbox" #directory for enabled vm's at startup
42VM_USER="erik"
43USE_NAT="no"
44
45export PATH="${PATH:+$PATH:}/bin:/usr/bin:/usr/sbin:/sbin"
46
47#if [ -f $VBOXDIR/config ]; then
48#. $VBOXDIR/config
49#echo config
50#fi
51
52SU="su $VM_USER -c"
53VBOXMANAGE="VBoxManage -nologo"
54
55################################################################################
56# FUNCTIONS
57
58# Determine if USE_NAT is set to "yes"
59use_nat() {
60if [ "$USE_NAT" = "yes" ]; then
61return `true`
62else
63return `false`
64fi
65}
66
67log_failure_msg() {
68echo $1
69}
70
71log_action_msg() {
72echo $1
73}
74
75# Check for running machines every few seconds; return when all machines are
76# down
77wait_for_closing_machines() {
78RUNNING_MACHINES=`$SU "$VBOXMANAGE list runningvms" | wc -l`
79if [ $RUNNING_MACHINES != 0 ]; then
80sleep 5
81echo " ..waiting for VM shut-down to complete.."
82wait_for_closing_machines
83fi
84}
85
86################################################################################
87# RUN
88case "$1" in
89
90start)
91if [ -f /etc/virtualbox/machines_enabled ]; then
92
93cat /etc/virtualbox/machines_enabled | while read VM; do
94log_action_msg "Starting VM: $VM ..."
95$SU "$VBOXMANAGE startvm "$VM" -type vrdp"
96#$SU "$VBOXMANAGE startvm "$VM
97RETVAL=$?
98done
99touch /var/lock/vboxcontrol
100fi
101;;
102
103stop)
104## NOTE: this stops all running VM's. Not just the ones listed in the config
105## NOTE2: used controllvm 'savestate' instead of 'acpipowerbutton' to avoid hang
106## with guest OS "..are you sure?" GUI prompts with acpipowerbutton
107$SU "$VBOXMANAGE list runningvms" | cut -d\" -f2 | while read VM; do
108log_action_msg "Saving state and powering off VM: $VM ..."
109$SU "$VBOXMANAGE controlvm "$VM" savestate"
110done
111rm -f /var/lock/vboxcontrol
112wait_for_closing_machines
113
114;;
115
116start-vm)
117log_action_msg "Starting VM: $2 ..."
118$SU "$VBOXMANAGE startvm "$2" -type vrdp"
119;;
120
121stop-vm)
122log_action_msg "Stopping VM: $2 ..."
123$SU "$VBOXMANAGE controlvm "$2" acpipowerbutton"
124;;
125
126savestate-vm)
127log_action_msg "Saving state and powering off VM: $2 ..."
128$SU "$VBOXMANAGE controlvm "$2" savestate"
129;;
130
131poweroff-vm)
132log_action_msg "Powering off VM: $2 ..."
133$SU "$VBOXMANAGE controlvm "$2" poweroff"
134;;
135
136status)
137echo "The following virtual machines are currently running:"
138$SU "$VBOXMANAGE list runningvms" | while read VM; do
139echo -n "$VM ("
140echo -n `$SU "VBoxManage showvminfo ${VM%% *}|grep -m 1 Name:|sed -e 's/^Name:s*//g'"`
141echo ')'
142done
143;;
144
145*)
146echo "Usage: $0 {start|stop|status|start-vm <VM name>|stop-vm <VM name>|savestate-vm <VM name>|poweroff-vm <VM name>}"
147exit 3
148esac
149
150exit 0
151

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