Last active
October 12, 2015 15:47
-
-
Save sasasin/4049821 to your computer and use it in GitHub Desktop.
VirtualBoxのVM起動停止スクリプト。自宅の運用方針に合うように、かつ、Ubuntu用に、改変を行ったもの。ベースは→ http://www.pclinuxos.com/forum/index.php?topic=103651.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# vboxcontrol Startup script for VirtualBox Virtual Machines | |
# | |
# chkconfig: 345 99 01 | |
# description: Manages VirtualBox VMs | |
# processname: vboxcontrol | |
# | |
# pidfile: /var/run/vboxcontrol/vboxcontrol.pid | |
# | |
### BEGIN INIT INFO | |
# | |
### END INIT INFO | |
# | |
# Version 20120317 by travisn000 based on: | |
# Version 20090301 by Kevin Swanson <kswan.info> based on: | |
# Version 2008051100 by Jochem Kossen <[email protected]> | |
# [url]http://farfewertoes.com[/url] | |
# | |
# Released in the public domain | |
# | |
# Source function library. | |
if [ -f /etc/init.d/functions ] ; then | |
. /etc/init.d/functions | |
elif [ -f /etc/rc.d/init.d/functions ] ; then | |
. /etc/rc.d/init.d/functions | |
elif [ -f /lib/lsb/init-functions ];then | |
. /lib/lsb/init-functions | |
else | |
exit 1 | |
fi | |
################################################################################ | |
# INITIAL CONFIGURATION | |
#VBOXDIR="$VBOXDIR" | |
VM_USER="vboxadm" | |
VBOXDIR="/home/$VM_USER" | |
USE_NAT="no" | |
export PATH="${PATH:+$PATH:}/bin:/usr/bin:/usr/sbin:/sbin" | |
if [ -f $VBOXDIR/config ]; then | |
. $VBOXDIR/config | |
fi | |
SU="su $VM_USER -c" | |
VBOXMANAGE="VBoxManage " | |
################################################################################ | |
# FUNCTIONS | |
# Determine if USE_NAT is set to "yes" | |
use_nat() { | |
if [ "$USE_NAT" = "yes" ]; then | |
return `true` | |
else | |
return `false` | |
fi | |
} | |
log_failure_msg() { | |
echo $1 | |
} | |
log_action_msg() { | |
echo $1 | |
} | |
# Check for running machines every few seconds; return when all machines are | |
# down | |
wait_for_closing_machines() { | |
RUNNING_MACHINES=`$SU "$VBOXMANAGE list runningvms" | wc -l` | |
if [ $RUNNING_MACHINES != 0 ]; then | |
sleep 5 | |
echo " ..waiting for VM shut-down to complete.." | |
wait_for_closing_machines | |
fi | |
} | |
################################################################################ | |
# RUN | |
case "$1" in | |
start) | |
if [ -f $VBOXDIR/machines_enabled ]; then | |
/etc/init.d/vboxdrv setup | |
cat $VBOXDIR/machines_enabled | while read VM; do | |
log_action_msg "Starting VM: $VM ..." | |
$SU "$VBOXMANAGE startvm "$VM" --type headless" | |
RETVAL=$? | |
done | |
touch /var/lock/vboxcontrol | |
fi | |
;; | |
start-without-compile) | |
if [ -f $VBOXDIR/machines_enabled ]; then | |
cat $VBOXDIR/machines_enabled | while read VM; do | |
log_action_msg "Starting VM: $VM ..." | |
$SU "$VBOXMANAGE startvm "$VM" --type headless" | |
RETVAL=$? | |
done | |
touch /var/lock/vboxcontrol | |
fi | |
;; | |
stop) | |
## NOTE: this stops all running VM's. Not just the ones listed in the config | |
## NOTE2: used controllvm 'savestate' instead of 'acpipowerbutton' to avoid hang | |
## with guest OS "..are you sure?" GUI prompts with acpipowerbutton | |
$SU "$VBOXMANAGE list runningvms" | cut -d\" -f2 | while read VM; do | |
log_action_msg "Saving state and powering off VM: $VM ..." | |
$SU "$VBOXMANAGE controlvm "$VM" savestate" | |
done | |
rm -f /var/lock/vboxcontrol | |
wait_for_closing_machines | |
;; | |
start-vm) | |
log_action_msg "Starting VM: $2 ..." | |
$SU "$VBOXMANAGE startvm "$2" --type headless" | |
;; | |
stop-vm) | |
log_action_msg "Stopping VM: $2 ..." | |
$SU "$VBOXMANAGE controlvm "$2" poweroff" | |
;; | |
savestate-vm) | |
log_action_msg "Saving state and powering off VM: $2 ..." | |
$SU "$VBOXMANAGE controlvm "$2" savestate" | |
;; | |
poweroff-vm) | |
log_action_msg "Powering off VM: $2 ..." | |
$SU "$VBOXMANAGE controlvm "$2" poweroff" | |
;; | |
status) | |
echo "The following virtual machines are currently running:" | |
$SU "$VBOXMANAGE list runningvms" | while read VM; do | |
echo -n "$VM (" | |
echo -n `$SU "VBoxManage showvminfo ${VM%% *}|grep -m 1 Name:|sed -e 's/^Name:s*//g'"` | |
echo ')' | |
done | |
;; | |
*) | |
echo "Usage: $0 {start|stop|status|start-vm <VM name>|stop-vm <VM name>|savestate-vm <VM name>|poweroff-vm <VM name>}" | |
exit 3 | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment