Install VirtualBox VM Service Script

Script ini saya modifikasi dari script nya Kevin Swanson yang ditampilkan disini.

Buat config file untuk vboxcontrol init.d script

# mkdir /etc/virtualbox
# touch /etc/virtualbox/machines_enabled

file machines_enabled ini berisi daftar nama dari Guest VM yang akan dijalankan secara otomatis. Daftarnya bisa kita dapatkan dari perintah :

$VBoxManage list vms
"Mikrotik3.10" {1e8dfce2-2068-4922-9ce1-86925e31b5cf}
"Ubuntu 8.04" {c4050afb-7077-4c60-83e5-898d4ff8508e}
"HS-Win" {75d80d99-d9c5-41cf-9e88-1d1c3ef5dd4c}

Sehingga isi file machines_enabled adalah

#nano /etc/virtualbox/machines_enabled
"Mikrotik3.10"
"Ubuntu 8.04"
"HS-Win"

Buat VirtualBox VM init.d script
Buat script init.d baru dan beri permition untuk di eksekusi

# touch /etc/init.d/vboxcontrol
# chmod 755 /etc/init.d/vboxcontrol

Kopi script berikut ke /etc/init.d/vboxcontrol

#   Startup script for VirtualBox Virtual Machines
#
# update-rc.d defaults 98 02
# chkconfig: 345 98 02
# description: Manages VirtualBox VMs
# processname: vboxcontrol
#
# pidfile: /var/run/vboxcontrol/vboxcontrol.pid
#
### BEGIN INIT INFO
#
### END INIT INFO
#
# Version 20120302 by ahmad basuki  base on:
# Version 20090301 by Kevin Swanson  based on:
# Version 2008051100 by Jochem Kossen
# http://farfewertoes.com
#
# Released in the public domain
#
# This file came with a README file containing the instructions on how
# to use this script.
#

################################################################################
# INITIAL CONFIGURATION
VBOXDIR="/etc/virtualbox"
VM_USER="vmadmin"
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
wait_for_closing_machines
fi
}

################################################################################
# RUN
case "$1" in
start)
if [ -f /etc/virtualbox/machines_enabled ]; then

cat /etc/virtualbox/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
$SU "$VBOXMANAGE list runningvms" |awk '{ print $1 }' | while read VM; do
log_action_msg "Shutting down 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" 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 Name:|sed -e 's/^Name:s*//g'"`
echo ')'
done
;;
*)
echo "Usage: $0 {start|stop|status|start-vm |stop-vm |poweroff-vm }"
exit 3
esac

exit 0

tambahkan vboxcontrol ke daftar startup service

# update-rc.d vboxcontrol defaults 98 02

Cara penggunaan:

Menjalankan semua virtual machine yang terdaftar dalam file machines_enabled

#/etc/init.d/vboxcontrol start

Mematikan semua virtual machine yang sedang running

#/etc/init.d/vboxcontrol stop

Melihat status virtual maschine yang running

#/etc/init.d/vboxcontrol status

Menyalakan virtual machine tertentu

#/etc/init.d/vboxcontrol start-vm

Mematikan virtual machine tertentu

#/etc/init.d/vboxcontrol stop-vm

Mematikan power virtual machine tertentu

#/etc/init.d/vboxcontrol poweroff-vm

Pada script asli option stop akan mengirimkan sinyal acpipowerbutton, sedangkan pada host saya option tersebut tidak jalan, sehingga saya menggantinya dengan option savestate.

 

tambahan script :

#!/bin/bash

# This script starts and stops the VirtualBox virtual machine winxpvb11.
# Use a RDP client to connect to the virtual machine.

case “$1” in

‘start’)
/usr/bin/VBoxHeadless –startvm “Windows Server 2008 SP2 x86” &
;;

‘stop’) /usr/bin/VirtualBox/VBoxManage controlvm “Windows Server 2008 SP2 x86” poweroff
;;

*)
echo “Usage: $0 { start | stop }”
exit 1
;;
esac

Leave a Reply