-
Notifications
You must be signed in to change notification settings - Fork 0
/
vbox-initd
executable file
·91 lines (80 loc) · 1.99 KB
/
vbox-initd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/sh
# Maintained at: [email protected]:dareni/shellscripts.git
#
# Usage: Copy and rename the script containing the user and vm name ie:
#
# cp shellscripts/vbox_initd /etc/rc.d/vbox-root-hal
# Edit the 'PROVIDES:' tag and set to the hostname.
#
### BEGIN INIT INFO
# Provides: [sethostname]
# Required-Start: nfs-kernel-server virtualbox
# Required-Stop: nfs-kernel-server virtualbox
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Virtualbox 4.1 vm server start-up/shutdown
# Description: Virtualbox 4.1 vm server start-up/shutdown
### END INIT INFO
# chkconfig: 5 99 99
# description: VirtualBox vbox ctrl
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin
VBOXMANAGE=/usr/bin/vboxmanage
VBOXUSER=`echo $0 |awk -F- '{print $(NF-1)}'`
VBOXVM=`echo $0 |awk -F- '{print $(NF)}'`
prog="${0##*/}"
do_start()
{
RUNNING=`status`
if [ "$RUNNING" = "running" ]; then
/bin/echo -e "\n$VBOXVM already running: $RUNNING"
else
echo -n "Enabling $VBOXVM:"
su -l -c "$VBOXMANAGE startvm $VBOXVM --type headless &" $VBOXUSER
sleep 1;
fi
}
do_stop()
{
RUNNING=`status`
if [ "$RUNNING" = "running" ]; then
/bin/echo -e "\nStopping $VBOXVM:"
su -l -c "$VBOXMANAGE controlvm $VBOXVM acpipowerbutton" $VBOXUSER
sleep 1;
else
/bin/echo -e "\n$VBOXVM already off: $RUNNING"
fi
while [ "$RUNNING" = "running" ]; do
cnt=$((cnt+1))
RUNNING=`status`
echo "Wait for $VBOXVM shutdown ... $cnt $RUNNING"
sleep 1
if [ $cnt -gt 120 ]; then
su -l -c "$VBOXMANAGE controlvm $VBOXVM poweroff" $VBOXUSER
/bin/echo "Force power off $VBOXVM"
fi;
done;
}
status()
{
su -l -c "$VBOXMANAGE showvminfo cephalox |grep ^State: |awk '{print \$2}'" $VBOXUSER
}
case "$1" in
restart)
do_stop
do_start
;;
start)
do_start
;;
stop)
do_stop
;;
status)
status
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 3
;;
esac