Created
August 5, 2016 23:02
-
-
Save allyunion/36013eb2f44da576647c93ef93a68b7c to your computer and use it in GitHub Desktop.
VirtualBox startup script
This file contains hidden or 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 | |
### BEGIN INIT INFO | |
# Provides: vbox_server | |
# Required-Start: $local_fs $network $named $time $syslog | |
# Required-Stop: $local_fs $network $named $time $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Description: Starts a VirtualBox instance named 'vbox_server' | |
### END INIT INFO | |
# /etc/init.d/vbox_server | |
. /lib/lsb/init-functions | |
VMUSER=<YOUR_USERNAME> | |
VMNAME='vbox_server' | |
VBOXNET_IPADDRESS='192.168.56.#' | |
COUNT=0 | |
case "$1" in | |
start) | |
log_daemon_msg "Starting VirtualBox VM [$VMNAME]..." | |
su - $VMUSER -c "/usr/bin/VBoxManage startvm $VMNAME --type headless &>/dev/null" | |
until /bin/ping -c 1 -i 1 -W 1 $VBOXNET_IPADDRESS &>/dev/null | |
do | |
sleep 1 | |
COUNT=$(( $COUNT + 1 )) | |
if [ $COUNT -eq 300 ]; | |
then | |
log_end_msg 1 | |
exit 1 | |
fi | |
done | |
log_end_msg 0 | |
;; | |
stop) | |
log_daemon_msg "Shutting VirtualBox VM [$VMNAME]..." | |
su - $VMUSER -c "/usr/bin/VBoxManage controlvm $VMNAME acpipowerbutton &> /dev/null" | |
until ! su - $VMUSER -c "/usr/bin/VBoxManage list runningvms" | grep $VMNAME &> /dev/null | |
do | |
sleep 1 | |
COUNT=$(( $COUNT + 1 )) | |
if [ $COUNT -eq 300 ]; | |
then | |
su - $VMUSER -c "/usr/bin/VBoxManage controlvm $VMNAME acpipowerbutton &> /dev/null" | |
elif [ $COUNT -eq 600 ]; | |
then | |
su - $VMUSER -c "/usr/bin/VBoxManage controlvm $VMNAME acpipowerbutton &> /dev/null" | |
elif [ $COUNT -eq 900 ]; | |
then | |
log_end_msg 1 | |
exit 1 | |
fi | |
done | |
log_end_msg 0 | |
;; | |
restart) | |
$0 stop | |
$0 start | |
;; | |
reset) | |
log_daemon_msg "Restarting VirtualBox VM [$VMNAME]..." | |
su - $VMUSER -c "/usr/bin/VBoxManage controlvm $VMNAME reset &>/dev/null" | |
sleep 60 | |
until /bin/ping -c 1 -i 1 -W 1 $VBOXNET_IPADDRESS &>/dev/null | |
do | |
sleep 1 | |
COUNT=$(( $COUNT + 1 )) | |
if [ $COUNT -eq 300 ]; | |
then | |
log_end_msg 1 | |
exit 1 | |
fi | |
done | |
log_end_msg 0 | |
;; | |
status) | |
log_daemon_msg "VirtualBox VM [$VMNAME] is..." | |
if [ su - $VMUSER -c "/usr/bin/VBoxManage list runningvms" | grep $VMNAME &> /dev/null ]; | |
then | |
if log_use_fancy_output; then | |
RED=$( $TPUT setaf 1) | |
YELLOW=$( $TPUT setaf 3) | |
NORMAL=$( $TPUT op) | |
else | |
RED='' | |
YELLOW='' | |
NORMAL='' | |
fi | |
/bin/echo -e " ${RED}not running!${NORMAL}" || true | |
else | |
/bin/echo -e " running!" || true | |
fi | |
;; | |
*) | |
echo "Usage: /etc/init.d/vbox_server {start|stop|restart|reset|status}" | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment