Ubuntu – Start VBoxHeadless VM at startup

startupvirtualbox

I cannot seem to get my VM to run at startup.

I tried the "startup applications" and update-rc.d with no luck.

sudo update-rc.d startvms defaults 99 10

This created all the appropriate symbolic links for the different run levels but the VM still does not start.

Here's my startvms script:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          startvms
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start my VMs at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO
case "$1" in
  start)
        echo "Starting"
        nohup VBoxHeadless --startvm "UbuntuServer" &
        ;;
  stop)
        echo "Stopping $DESC"
        VBoxManage controlvm "UbuntuServer" poweroff
        ;;

  restart|force-reload)
        echo "Restarting $DESC"
        VBoxManage controlvm "UbuntuServer" poweroff
        nohup VBoxHeadless --startvm "UbuntuServer" &
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

What am i doing wrong?

Best Answer

THIS is what finally worked!

1) Create the startup script file

in /etc/init.d - sudo nano /etc/init.d/StartVM.

Copy Paste the following in the file and replace "My VM Name" for your vm name:

#! /bin/sh
# /etc/init.d/StartVM
#

#Edit these variables!
VMUSER=spode
VMNAME="My VM Name"

case "$1" in
  start)
    echo "Starting VirtualBox VM..."
    sudo -H -b -u $VMUSER /usr/bin/VBoxVRDP -s "$VMNAME"
    ;;
  stop)
    echo "Saving state of Virtualbox VM..."
    sudo -H -u  $VMUSER /usr/bin/VBoxManage controlvm "$VMNAME" savestate
    ;;
  *)
    echo "Usage: /etc/init.d/StartVM {start|stop}"
    exit 1
    ;;
esac

exit 0

2) Give the script executable permission

with sudo chmod +x /etc/init.d/StartVM.

3) Tell script to run at startup.

tell the script be the first to shutdown and the last to startup.

sudo update-rc.d StartVM defaults 99 01