VirtualBox

Ticket #1037: vbox-tool

File vbox-tool, 2.9 KB (added by markba, 16 years ago)

vbox-tool

Line 
1#! /bin/bash
2#
3# vbox-tool: Utility to retrieve status and shutdown running sessions when desired
4#
5# Usage..: vbox-tool [shutdown]
6# Author.: Mark Baaijens (mark.baaijens@gmail.com)
7# Date...: 3 jan 2008
8#
9# Installation:
10# - copy to /usr/local/bin
11# - sudo chmod +x /usr/local/bin/vbox-tool
12# - modify /etc/init.d/vboxdrv:
13# - search for "log_daemon_msg "Stopping VirtualBox kernel module" "$MODNAME";"
14# - insert before that line: "vbox-tool shutdown > /dev/null"
15#
16
17# OSE-version uses a all lower case name, i.e. 'vboxmanage' so we
18# have to find out which executable is available.
19if [ -n $(whereis VBoxManage | awk 'BEGIN{FS="VBoxManage: "}{print $2}') ]
20then
21 vboxcommand='VBoxManage'
22else
23 if [ -n $(whereis vboxmanage | awk 'BEGIN{FS="vboxmanage: "}{print $2}') ]
24 then
25 vboxcommand='vboxmanage'
26 else
27 echo "Either 'VBoxManage' or 'vboxmanage' is not available, exiting."
28 exit
29 fi
30fi
31
32# Check for a commandline option
33option=$1 # Read first commandline parameter
34shutdown=0 # By default, no shutdown
35if [ -n "$option" ] # String is not null
36then
37 if [ $option != "shutdown" ] # There's only one option!
38 then
39 echo "Usage: vbox-tool [shutdown]"
40 exit
41 else
42 shutdown=1
43 fi
44fi
45
46# Several state constants
47state_running='running'
48state_saved='saved'
49state_powered_off='powered-off'
50state_aborted='aborted'
51state_unknown='unknown'
52
53#
54# Iterate over all registered vm's
55#
56# Beware: output from VBoxManage should be something like this
57# "UUID:<12 spaces><uuid>"
58for uuid in $($vboxcommand list vms | grep UUID: | awk 'BEGIN{FS="UUID: "}{print $2}')
59do
60 #
61 # Extract info from specific vm-session
62 #
63
64 # Beware: output from VBoxManage should be something like this
65 # "Name:<12 spaces><uuid>"
66 # "State:<11 spaces><uuid>"
67 name=$($vboxcommand showvminfo $uuid | grep "Name:" | awk 'BEGIN{FS="Name: "}{print $2}')
68 state_raw=$($vboxcommand showvminfo $uuid | grep "State:" | awk 'BEGIN{FS="State: "}{print $2}')
69
70 # Extract exact state from string state_raw
71 # Beware: output from VBoxManage should be exactly as the given strings,
72 # i.e. 'running', 'saved', etc.
73 echo "$state_raw" | grep -q "running"
74 if [ $? -eq 0 ]
75 then
76 state=$state_running
77 else
78 echo "$state_raw" | grep -q "saved"
79 if [ $? -eq 0 ]
80 then
81 state=$state_saved
82 else
83 echo "$state_raw" | grep -q "powered off"
84 if [ $? -eq 0 ]
85 then
86 state=$state_powered_off
87 else
88 echo "$state_raw" | grep -q "aborted"
89 if [ $? -eq 0 ]
90 then
91 state=$state_aborted
92 else
93 state=$state_unknown
94 fi
95 fi
96 fi
97 fi
98
99 # Show some output
100 echo "\"$name\": $state"
101
102 # Save running sessions
103 if [ $shutdown -eq 1 ]
104 then
105 if [ "$state" == "$state_running" ]
106 then
107 echo "Saving \"$name\""
108 $vboxcommand controlvm $uuid savestate
109 fi
110 fi
111done
112
113exit 0

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