# Virtualbox, run at reboot



## drmike (Aug 25, 2013)

Someone a ways back pointed me at Virtualbox on here.  Using it for a few virtual containers mainly for appliances and VPS-like use locally.

Little problem I have now is sustaining reboots.   Need Virtualbox to launch the instances on system reboot.

Anyone know/use/familiar with how to accomplish this even if it a commandline I can fire off via CRON?


----------



## hcjake (Aug 25, 2013)

Should be able to use VBoxManage for that, http://www.virtualbox.org/manual/ch08.html


----------



## drmike (Aug 25, 2013)

Arrgh! VBoxManager likes to vomit and complain at first attempt:



> VBoxManage startvm "deleteme"
> VBoxManage: error: Could not find a registered machine named 'deleteme'
> VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
> Context: "FindMachine(Bstr(pszVM).raw(), machine.asOutParam())" at line 544 of file VBoxManageMisc.cpp


The fix for that:


VBoxManage registervm /home/user/VirtualBox\ VMs/deleteme/deleteme.vbox
Then try again:


VBoxManage startvm deleteme


Now we get:



> Waiting for VM "deleteme" to power on...
> 
> VM "deleteme" has been successfully started.


Hey it works!

Time to shove that in CRON to spawn at reboot.

Thanks @hcjake for impressing upon me the commandline tool.  Ran into same bugs before, but knowing right tool made me press on to resolve matter.


----------



## drmike (Aug 25, 2013)

BTW: same *registervm *step seems to be need for for every container you have.    A little annoying, but I can deal with it


----------



## peterw (Aug 26, 2013)

I use this script for it


```
#!/bin/bash

### BEGIN INIT INFO
# Provides: virtualboxstarter
# Required-Start: vboxweb-service
# Required-Stop: vboxweb-service
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: Starter for virtual box
### END INIT INFO

set -e

VBOXMANAGE=/usr/bin/VBoxManage
FIND=/usr/bin/find
SU=/bin/su
PRINTF=/usr/bin/printf

VBOXUSER=vbox
VMDIR=/export/vbox-vms
VMS_TO_START=(Windows Debian Test)

export -n DISPLAY
umask 022
. /lib/lsb/init-functions

if [[ "$UID" -ne 0 ]]; then
echo 'Run as root!'
exit 1
fi

case "$1" in
  start)
log_daemon_msg "Starting virtual boxes"
VMLIST="$($PRINTF " '%s'" "${VMS_TO_START[@]}")"
$SU "$VBOXUSER" -s /bin/bash <<EOF
$FIND "$VMDIR" -type f -name '*.vbox' \
-exec $VBOXMANAGE registervm '{}' ';' > /dev/null 2>&1
for VM in $VMLIST; do
$VBOXMANAGE startvm "\$VM" --type headless
done
EOF
log_end_msg 0
;;
  stop)

;;

  status)
;;

  *)
log_action_msg "Usage: $0 {start|stop|status}"
exit 1
esac

exit 0
```


----------

