Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
Did you upgrade Virtual Box to PUEL to include VRDP support (needed for headless mode)? –  Takkat Aug 15 '11 at 19:02
    
@Takkat: yes, i should have mentioned: headless mode works great for me... i just can't get it to run on startup. –  capdragon Aug 15 '11 at 19:04
1  
What if you tried VBoxManage startvm --type headless "UbuntuServer" instead of VBoxHeadless command? –  Takkat Aug 15 '11 at 19:15
    
You may also need to be root to get it running like su -c 'VBoxHeadless --startvm <MACHINE>'. –  Takkat Aug 15 '11 at 19:18
    
@Takkat: no dice. But again, i can get VBoxHeadless working just fine with the command i'm using. It's ONLY not working when i do it in a startup script. –  capdragon Aug 15 '11 at 19:36

4 Answers 4

up vote 19 down vote accepted

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

share|improve this answer
1  
doesnt the VRDP add extra overhead, wouldnt VBoxManage startvm --type headless be better ? –  mojzis Sep 7 '12 at 21:09

FYI, This works on Opensuse without nohup.

VBoxHeadless -s "OpenSuSE 11.4 64bit" &
share|improve this answer
    
So i use the same script i have except leave out the nohup? –  capdragon Nov 7 '11 at 17:21
    
Got rid of hohup, still doesn't work. I must be doing something else wrong. Are you sure we're using the same script? Please edit your answer and include your script so i can try it. –  capdragon Nov 7 '11 at 22:19

Since you mention that VBoxHeadless works fine if you type it from the command line of a terminal but does not start any VM when launched from a init script, my guess is that it is looking for some environment variable which is defined when you run a shell in your terminal but undefined in the init script environment (which is basically empty, except for parameters given on the kernel command line).

Can you try replacing the VBoxHeadless invocations in the script with this?

env USER=username HOME=/path/to/user/homedir VBoxHeadless ...same options as before...

Here "username" and the home directory path should be changed to match those of the user you use to start the VM.

If that works, you might want to use su -l to run VBoxHeadless instead of this env hack.

share|improve this answer
    
+1 for su -l - that's a good point! –  Takkat Aug 15 '11 at 20:14
    
nope... still doesn't work. I decided to just use a cron job –  capdragon Aug 15 '11 at 20:26

This is working ok with Ubuntu server 12.04 and VirtualBox 4.2.20.

#! /bin/sh
# /etc/init.d/StartVM
#
#Edit these variables!
VMUSER=username
case "$1" in
  start)
    echo "Starting VirtualBox VM SMARTHOST ..."
    sudo -u $VMUSER VBoxManage startvm SMARTHOST --type headless
    echo "Starting VirtualBox VM wxp-acceso ..."
    sudo -u $VMUSER VBoxManage startvm wxp-acceso --type headless
    echo "Starting VirtualBox VM wmmaq_edi ..."
    sudo -u $VMUSER VBoxManage startvm vmmaq_edi --type headless
    ;;
  stop)
    echo "Saving state of Virtualbox VM SMARTHOST ..."
    sudo -u $VMUSER VBoxManage controlvm SMARTHOST savestate
    echo "Saving state of Virtualbox VM wxp-acceso ..."
    sudo -u $VMUSER VBoxManage controlvm wxp-acceso savestate
    echo "Saving state of Virtualbox VM vmmaq_edi ..."
    sudo -u $VMUSER VBoxManage controlvm vmmaq_edi savestate
    ;;
  *)
    echo "Usage: /etc/init.d/StartVM {start|stop}"
    exit 1
    ;;
esac

exit 0

And

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

and

sudo update-rc.d StartVM defaults 99 01

as indicated at a previous answer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.