Recommended way to start/stop a VB on a Ubuntu host?

Discussions related to using VirtualBox on Linux hosts.
Post Reply
JonCage
Posts: 10
Joined: 11. Mar 2009, 04:09

Recommended way to start/stop a VB on a Ubuntu host?

Post by JonCage »

As the title suggests, I have a guest OS (Windows XP) running as a guest under Ubuntu 8.10 server (running headless). Thus far I've been configuring things and as a result I've been running it by calling:

sudo -u vboxuser VBoxHeadless -startvm "Windows-XP-Virtual-Server"

...but I'd like to use a /etc/init.d/ script to start and stop the server at bootup / shutdown of the host. I've been googling around this area but haven't found anything that looks like an answer to what surely must have been tried and solved before(?)... any advice or links would be very gratefully received!

Cheers,
Jon
srimalik
Posts: 7
Joined: 12. Mar 2009, 09:38

Re: Recommended way to start/stop a VB on a Ubuntu host?

Post by srimalik »

Try this
1) create a file in /etc/init.d (say startwinXpGuest) and give it execute permissions for root
the contents:

Code: Select all

#!/usr/bin/sh
su - vboxuser VBoxHeadless -startvm "Windows-XP-Virtual-Server"
then go to /etc/rc5.d and create a link to this file, the link should have a name of format SXXstartwinXpGuest where XX is a number say 99 (this will start vbox as the last process in runlevel 5)

Code: Select all

ln -sf /etc/init.d/startwinXpGuest S99startwinXpGuest
Now go to /etc/rc0.d and create another link by name K99startwinXpGuest which will point to /etc/init.d/startwinXpGuest. Create a K file in /etc/rc6.d also.

Hope this helps

-Sri
TerryE
Volunteer
Posts: 3572
Joined: 28. May 2008, 08:40
Primary OS: Ubuntu other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Ubuntu 10.04 & 11.10, both Svr&Wstn, Debian, CentOS
Contact:

Post by TerryE »

Alternatively do a man update-rc.d and follow the instructions. Clone one of the other simpler /etc/init.d scripts as a template. You should include at a minimum the start and stop entries. You can do a VBoxManage controlvm savestate on the stop entry.
Read the Forum Posting Guide
Google your Q site:VirtualBox.org or search for the answer before posting.
JonCage
Posts: 10
Joined: 11. Mar 2009, 04:09

Re: Recommended way to start/stop a VB on a Ubuntu host?

Post by JonCage »

Thanks Terry, I've been crafting something along those lines for most of this evening (been a long time since I did any bash scripting, and I was never all that good at it ;)). Here's my first stab:

Code: Select all

#! /bin/sh
### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Author: Jon Cage <Jon@digital-explosion.co.uk>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.

# Do NOT "set -e"

#set -x

# PATH should only include /usr/* if it runs after the mountnfs.sh script

MACHINE_TO_START="Windows-XP-Virtual-Server"

DESC="Virtual Box"
NAME=$MACHINE_TO_START
DAEMON=/usr/bin/VBoxManage
USER=vboxuser 

DAEMON_ARGS="startvm $MACHINE_TO_START -type vrdp"
PATH=/sbin:/usr/sbin:/bin:/usr/bin
PIDFILE=/var/run/$NAME.pid
SCRIPT_NAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || {
	echo "Couldn't find '$DAEMON'"
	exit 0;
}

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
VERBOSE=yes

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started

	# Check to make sure we're not already running
	if [ -f $PIDFILE ]; then
		echo "...$DAEMON is already running!"
		return 1
	fi

	# Do the command
	START_COMMAND="start-stop-daemon --start --chuid $USER --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS"
	#echo "====$START_COMMAND===="
	TEXT_RETURNED=`$START_COMMAND`
	#echo "====\n$TEXT_RETURNED\n===="

	# Check the result
	RESULT=`echo $TEXT_RETURNED | grep -q "success"`
	if !($RESULT); then
		echo "...$DAEMON failed to start:\n**********\n\n$TEXT_RETURNED\n\n**********";
		return 2;
	fi

	# If we made it this far, all went well so lets create the PID file	
	PID=`ps -ef | awk '/Windows-XP-Virtual-Server/ && !/awk/ {print $2}'`
	echo $PID > ${PIDFILE}

	return 0;
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Tell the machine to shut down
	#STOP_COMMAND="sudo -u vboxuser VBoxManage -nologo controlvm Windows-XP-Virtual-Server poweroff"
	STOP_COMMAND="sudo -u vboxuser VBoxManage -nologo controlvm Windows-XP-Virtual-Server savestate"
	
	#echo "====$STOP_COMMAND===="
	TEXT_RETURNED=`$STOP_COMMAND`
	echo "====\n$TEXT_RETURNED\n===="

	RESULT=`echo $TEXT_RETURNED | grep -q "100%"`
	if !($RESULT) ; then
		echo "...$DAEMON failed to shut down:\n**********\n$TEXT_RETURNED\n**********"
		RETVAL=2
	else
		echo "SHUTDOWN"
		RETVAL=0
	fi

	rm -f $PIDFILE

	return $RETVAL
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	#
	# If the daemon can reload its configuration without
	# restarting (for example, when it is sent a SIGHUP),
	# then implement that here.
	#
	start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --NAME $NAME
	return 0
}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  #reload|force-reload)
	#
	# If do_reload() is not implemented then leave this commented out
	# and leave 'force-reload' as an alias for 'restart'.
	#
	#log_daemon_msg "Reloading $DESC" "$NAME"
	#do_reload
	#log_end_msg $?
	#;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	#echo "Usage: $SCRIPTSCRIPT_NAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTSCRIPT_NAME {start|stop|restart|force-reload}" >&2
	exit 3
	;;
esac
The start up works fine, but the stop doesn't detect a correct shutdown properly yet. Needs some more work, but seems like it could be a lot simpler / cleaner than most of the other solutions I've seen out and about on t'internet...
Jose Catre-Vandis

Re: Recommended way to start/stop a VB on a Ubuntu host?

Post by Jose Catre-Vandis »

You'll have trouble trying to shutdown a GUI VM using rcd scripts, because VBoxManage started VMs need the GUI to enable shutdown. Headless VMs don't need a GUI for rcd script shutdown. I fought with this for ages before coming up with a script to run that savestated open VMs and then shutdown the PC. See this post:
Jose Catre-Vandis

Re: Recommended way to start/stop a VB on a Ubuntu host?

Post by Jose Catre-Vandis »

Jose Catre-Vandis wrote:You'll have trouble trying to shutdown a GUI VM using rcd scripts, because VBoxManage started VMs need the GUI to enable shutdown. Headless VMs don't need a GUI for rcd script shutdown. I fought with this for ages before coming up with a script to run that savestated open VMs and then shutdown the PC. See this post:
Sorry here is link:

http://forums.virtualbox.org/viewtopic. ... ful#p61338
Post Reply