Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Start a VirtualBox VM as a Service

  1. #1
    Join Date
    May 2006
    Location
    CT, USA
    Beans
    151

    Start a VirtualBox VM as a Service

    Hello All,

    I've been searching around trying to figure out how to run a VirtualBox virtual machine at boot. I found many articles on how to install and remove services. I've also found articles on running a VM from the command line, which is all well and good, but I haven't found how to run a command at boot.

    A similar question was asked here, in the forums, but the initial question on how to start on boot was never answered. Basically, all I want to do is run this command at boot:
    Code:
    VBoxVRDP -startvm "XP"
    The problem is that the command does not immediately drop back to the prompt, like a service/daemon does. So how does one go about something like this? Thanks in advance for your help.

    Ross

  2. #2
    Join Date
    Mar 2005
    Location
    Texas
    Beans
    1,675
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Start a VirtualBox VM as a Service

    I see two options here.

    One, make it run at boot. You'd have to create a startup script for it, and tell it to run on a different virtual terminal as a user (although i'm not sure how you'd get around having to log in), with its own X-server. This would feel pretty useless to me but could be done i think.

    I think more of what you're after is to have it start on login, running as your user, and available to run in seamless mode. In which case you would add the command you posted to whatever login script your window manager uses.

    For me, I added a button for that command to my menu. I don't use widnows much, but its right there if i need it.
    Last edited by Whiffle; February 19th, 2008 at 10:29 PM.
    100 buckets of bits on the bus,
    100 buckets of bits,
    Take one down, short it to ground,
    FF buckets of bits on the bus.

  3. #3
    Join Date
    May 2006
    Location
    CT, USA
    Beans
    151

    Re: Start a VirtualBox VM as a Service

    Thanks for your speedy reply.

    But, I want it run as a server on a headless server. VirtualBox gives you the ability to run virtual machines on a server without an X server, and to access the VM, you would use rdesktop (via the RDP protocol). Hence the name, VBoxVRDP.

    VMware Server gives you a similar option in its GUI to start the virtual machine at system boot. I'm basically looking for the same functionality in VirtualBox.

  4. #4
    Join Date
    Mar 2005
    Location
    Texas
    Beans
    1,675
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Start a VirtualBox VM as a Service

    Ooh okay. Makes more sense now.

    try looking at

    /etc/init.d/skeleton

    Its an init script skeleton, add your own commands and you can make your own init script.
    100 buckets of bits on the bus,
    100 buckets of bits,
    Take one down, short it to ground,
    FF buckets of bits on the bus.

  5. #5
    Join Date
    May 2006
    Location
    CT, USA
    Beans
    151

    Re: Start a VirtualBox VM as a Service

    I started looking at that, but wasn't clear on a couple of things. Hope you don't mind if I ask you to clear a couple things up for me

    First, making an init.d script from /etc/init.d/skeleton, you replace the values of the variables at the top of the file with the command you want to run, and that's all you need to touch?

    Second, This works for commands that block the terminal until the command completes, right?

    Third, I read somewhere that adding an ampersand to the end of a command would make it run like a service, is that true? Example: "VBoxVRDP -blah &" would return control of the terminal immediately and not block it until the vm was closed.

    I've never really messed with init scripts before and I haven't found a clear explanation of how to modify them to my needs, so I hope you don't mind helping me out with a couple questions.

    Thanks, Ross

  6. #6
    Join Date
    Mar 2005
    Location
    Texas
    Beans
    1,675
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Start a VirtualBox VM as a Service

    Well, I have yet to make an init script, but I'll try.


    It does look like all you have to change is the part at the top, usually. However, I don't think an XP vm will be willing to shutdown like a regular linux service. You might modify the do_stop and do_start functions to use the commands this guy did:
    http://forums.virtualbox.org/viewtop...366be569932be8

    It does appear to work with things that steal the terminal, ie dont automatically run as daemons. However, since it needs to be modified to make it work, it might take some fanagaling to get it working just right.

    Heres a blurb on the &

    If a command is terminated by the control operator ‘&’, the shell executes the command asynchronously in a subshell. This is known as executing the command in the background. The shell does not wait for the command to finish, and the return status is 0 (true). When job control is not active (see Job Control), the standard input for asynchronous commands, in the absence of any explicit redirections, is redirected from /dev/null.
    http://www.gnu.org/software/bash/man...ell-Parameters


    I'm actually interested in this idea too, so I'm going to try putting together an init script. I'll let you know if anything happens, although it might be a while.
    100 buckets of bits on the bus,
    100 buckets of bits,
    Take one down, short it to ground,
    FF buckets of bits on the bus.

  7. #7
    Join Date
    May 2006
    Location
    CT, USA
    Beans
    151

    Re: Start a VirtualBox VM as a Service

    Glad I peaked your interests I will also attempt making a init script and see what I can come up with. Surely, one of us should be able to make it work.

    Ross

  8. #8
    Join Date
    May 2006
    Location
    CT, USA
    Beans
    151

    Re: Start a VirtualBox VM as a Service

    Well, I got mine working, but I don't know how "clean" it is or what problems it may have. All I know is that it boots the VM when I say "start" and shuts the VM down when I say "stop". Feel free to correct anything, as this is my first init script and I have no idea what I'm doing

    Code:
    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides:          VirtualBox - XP-Radio
    # Required-Start:    $local_fs $remote_fs
    # Required-Stop:     $local_fs $remote_fs
    # Default-Start:     2 3 4 5
    # Default-Stop:      S 0 1 6
    # Short-Description: Handles the XP-Radio virtual machine
    # Description:       This file should be used to construct scripts to be
    #                    placed in /etc/init.d.
    ### END INIT INFO
    
    # Author: Ross Peoples <ubuntu.ross.peoples@gmail.com>
    
    # Do NOT "set -e"
    
    # PATH should only include /usr/* if it runs after the mountnfs.sh script
    PATH=/usr/sbin:/usr/bin:/sbin:/bin
    
    #
    # CHANGE THESE AS NEEDED
    #
    # Name given to the virtual machine
    VM_NAME="XP-Radio"
    
    # Name of this file, excluding /etc/init.d/
    THIS_FILE=vbox-xp-radio
    
    # Username to run as
    RUNAS_USERNAME=ross
    
    #
    # END CHANGE THESE AS NEEDED
    #
    
    DESC="VirtualBox - ${VM_NAME}"
    DAEMON=/usr/bin/VBoxVRDP
    DAEMON_ARGS="-startvm \"${VM_NAME}\""
    PIDFILE=/var/run/$THIS_FILE.pid
    SCRIPTNAME=/etc/init.d/$THIS_FILE
    
    # Exit if the package is not installed
    [ -x "$DAEMON" ] || exit 0
    
    # Read configuration variable file if it is present
    [ -r /etc/default/$THIS_FILE ] && . /etc/default/$THIS_FILE
    
    # Load the VERBOSE setting and other rcS variables
    [ -f /etc/default/rcS ] && . /etc/default/rcS
    
    # 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
    	#start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
    		#|| return 1
    	#start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
    		#$DAEMON_ARGS \
    		#|| return 2
    	# Add code here, if necessary, that waits for the process to be ready
    	# to handle requests from services started subsequently which depend
    	# on this one.  As a last resort, sleep for some time.
    
    	su $RUNAS_USERNAME -c "VBoxManage startvm ${VM_NAME} -type vrdp"	
    }
    
    #
    # Function that stops the daemon/service
    #
    do_stop()
    {
    	# Return
    	#   0 if daemon has been stopped
    	#   1 if daemon was already stopped
    	#   2 if daemon could not be stopped
    	#   other if a failure occurred
    	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $THIS_FILE
    	RETVAL="$?"
    	[ "$RETVAL" = 2 ] && return 2
    	# Wait for children to finish too if this is a daemon that forks
    	# and if the daemon is only ever run from this initscript.
    	# If the above conditions are not satisfied then add some other code
    	# that waits for the process to drop all resources that could be
    	# needed by services started subsequently.  A last resort is to
    	# sleep for some time.
    
    	su $RUNAS_USERNAME -c "VBoxManage controlvm ${VM_NAME} acpipowerbutton && sleep 10"
    
    	start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
    	[ "$?" = 2 ] && return 2
    	# Many daemons don't delete their pidfiles when they exit.
    	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
    	echo "Reload not supported"
    	return 0
    }
    
    case "$1" in
      start)
    	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$THIS_FILE"
    	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" "$THIS_FILE"
    	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 $?
    	#;;
      *)
    	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
    	echo "Usage: $SCRIPTNAME {start|stop}" >&2
    	exit 3
    	;;
    esac
    
    :

  9. #9
    Join Date
    Oct 2006
    Beans
    1

    Re: Start a VirtualBox VM as a Service

    Hi,

    Thanks for that scripts, that's what's I were looking for monthes ...
    Do you know if I can modify it to use it with vmware-server ?

    Regards,

    LpBv

    Edit : How can I add it to /etc/init.d/ script in hardy ? Thanks

  10. #10
    Join Date
    Oct 2008
    Location
    Boston
    Beans
    2
    Distro
    Ubuntu

    Re: Start a VirtualBox VM as a Service

    Here is my attempt at making an init.d script for a VirtualBox system. Mine suspends-to-disk during shutdown instead of acpishutdown, and more importantly, properly reports error codes (success; service is already in that mode; start/stop operation failed). Please modify it for yourself to suit your needs.

    I think you should also create "/etc/default/virtualbox" correctly according to the instructions found in "/etc/init.d/vboxdrv"; specify what users' vms to look for when shutting down vboxdrv, and what type of shutdown should be performed on the VM. I don't know if this is completely necessary, but I'm just covering my bases in case vboxdrv gets the shutdown command before my own init.d script.

    IMPORTANT! Fill in all the values marked with "!!" according to your local configuration.

    Code:
    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides:          virtualbox-!!SHORTNAME
    # Required-Start:    $local_fs $remote_fs vboxdrv vboxnet
    # Required-Stop:     $local_fs $remote_fs
    # Default-Start:     2 3 4 5
    # Default-Stop:      S 0 1 6
    # Short-Description: !!LONGNAME virtual machine
    # Description:       !!LONGNAME virtual machine hosted by VirtualBox
    ### END INIT INFO
     
    # Author: Brendan Kidwell <brendan@glump.net>
    #
    # Based on /etc/init.d/skeleton from Ubuntu 8.04.
     
    # Do NOT "set -e"
     
    # PATH should only include /usr/* if it runs after the mountnfs.sh script
    PATH=/usr/sbin:/usr/bin:/sbin:/bin
    DESC="!!LONGNAME virtual machine"
    NAME=virtualbox-!!SHORTNAME
    SCRIPTNAME=/etc/init.d/$NAME
     
    MANAGE_CMD=VBoxManage
    VM_OWNER=!!USERNAME
    VM_NAME="!!LONGNAME" #This has to be the name exactly as it appears in your VirtualBox GUI control panel.
     
    # Read configuration variable file if it is present
    [ -r /etc/default/$NAME ] && . /etc/default/$NAME
     
    # Load the VERBOSE setting and other rcS variables
    [ -f /etc/default/rcS ] && . /etc/default/rcS
     
    # 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
     
    	sudo -u $VM_OWNER $MANAGE_CMD showvminfo "$VM_NAME"|grep "^State:\s*running" >/dev/null && {
    	    echo "$VM_NAME" is already running.
    	    return 1
    	}
     
    	sudo -u $VM_OWNER $MANAGE_CMD startvm "$VM_NAME" -type vrdp >/dev/null || {
    	    echo Failed to start "$VM_NAME".
    	    return 2
    	}
     
    	echo "$VM_NAME" started or resumed.
    	return 0
    }
     
    #
    # Function that stops the daemon/service
    #
    do_stop()
    {
    	# Return
    	#   0 if daemon has been stopped
    	#   1 if daemon was already stopped
    	#   2 if daemon could not be stopped
    	#   other if a failure occurred
     
    	sudo -u $VM_OWNER $MANAGE_CMD showvminfo "$VM_NAME"|grep "^State:\s*running" >/dev/null || {
    	    echo "$VM_NAME" is already stopped.
    	    return 1
    	}
     
    	sudo -u $VM_OWNER $MANAGE_CMD controlvm "$VM_NAME" savestate || {
    	    echo Failed to stop "$VM_NAME".
    	    return 2
    	}
     
    	echo "$VM_NAME" suspended.
    	return 0
    }
     
    #
    # Display "State" field from showinfo action
    #
    do_status()
    {
    	sudo -u $VM_OWNER $MANAGE_CMD showvminfo "$VM_NAME"|grep "^State:\s*.*$"
    }
     
    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
    	;;
      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
    	;;
      status)
    	do_status
    	;;
      *)
    	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
    	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
    	exit 3
    	;;
    esac
     
    :
    (For more info, see my additional comments on VirtalBox as a Service in Linux.)

    I hope this helps some people.
    Last edited by brendankidwell; October 30th, 2008 at 06:02 PM. Reason: typo

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •