Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am running two VirtualBox headless VMs on an Ubuntu 12.04 host. I would like these VMs to start automatically when the system boots.

I have two crontab entries set like this (under the user which owns the VMs):

@reboot /usr/bin/vboxheadless -startvm io
@reboot /usr/bin/vboxheadless -startvm pbx

But it seems that doesn't do the trick. If I run the command directly from the terminal, the machines start up without a hitch, but I can't seem to get them to start once the system starts. I'm thinking maybe the crontab is running before the VirtualBox program/library is loaded.

How do I start these VMs automatically?

share|improve this question

3 Answers 3

This is what I use. It starts the VMs on boot and saves their state on shutdown/reboot

#!/bin/bash
### BEGIN INIT INFO
# Provides:       vmboot
# Required-Start: vboxdrv
# Required-Stop:
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: Stop/Start VMs on System shutdown
### END INIT INFO

VBOXUSER=vboxuser
SU="sudo -H -u $VBOXUSER"
VBOXMANAGE=/usr/bin/VBoxManage
VBOXHEADLESS=/usr/bin/VBoxHeadless
RUNNINGVMS=$($SU $VBOXMANAGE --nologo list runningvms | sed -e 's/^".*".*{\(.*\)}/\1/')
ALLVMS=$($SU $VBOXMANAGE --nologo list vms | sed -e 's/^".*".*{\(.*\)}/\1/')

case $1 in
stop)
if [[ -n $RUNNINGVMS ]]; then
echo "Saving the state of all running VMs..."
for v in $RUNNINGVMS; do
    $SU $VBOXMANAGE --nologo controlvm $v savestate
done
fi
;;
start)
for v in $ALLVMS; do
if [[ -n $($SU $VBOXMANAGE --nologo showvminfo $v | grep saved) ]]; then
    echo "Restoring VMs..." && $SU $VBOXHEADLESS -s $v & > /dev/null
fi
done
;;
*)
echo "Usage: /etc/init.d/vmboot start | stop"; exit 1
;;
esac
exit 0

Just save it in /etc/init.d. I named mine vbox. Run update-rc.d <script name> defaults and you should be good to go.

share|improve this answer

I suspect that you're logged in with an account other than root when you start the VMs manually, while cron runs as root and thus doesn't know about your VMs. You can check that by running the command

sudo /usr/bin/vboxheadless -startvm io

If you get an error

Invalid machine name or UUID!

the VM is not registered with the root account. In that case create a shell script to run the commands under your account and schedule that script with cron:

#!/bin/sh

USERNAME=...

su -c "/usr/bin/vboxheadless -startvm io" - $USERNAME
su -c "/usr/bin/vboxheadless -startvm pbx" - $USERNAME
share|improve this answer
    
The cronjob is running as the user which the VMs are registered under. –  muncherelli Sep 6 '12 at 18:19
    
Then check /.../VMNAME/Logs/VBox.log. –  Ansgar Wiechers Sep 6 '12 at 18:45
    
The log file doesn't seem to show anything regarding the VM not starting up. All iterations of the log show a successful VM start. However I believe it might have something to do with the cron running before virtualbox has started up. –  muncherelli Sep 6 '12 at 23:16
    
Next step would be to check /var/log/syslog and the output of dmesg. –  Ansgar Wiechers Sep 7 '12 at 4:11

A secondary option also if your MB/CPU support hardware visualization is ProxMox. Its a self contained install of Linux, KVM and a web interface. it has much better performance over virtualbox as it runs at a kernel level and instead of "virtual" only drives like virtualbox uses you can actually connect a guest under KVM to a physical drive which uses a LOT less disk Io.

Quick check if your system supports hardware visualization:

egrep -c '(vmx|svm)' /proc/cpuinfo
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.