User Tools

Site Tools


wiki:start_headless_vm_at_boot

Background

The following steps will allow you to automatically save and restore your VirtualBox virtual machines during shutdown and reboot.

Ideal Prerequisites

Software you should have installed before attempting this task

  • Ubuntu Server 16.04.3 LTS (Older/Newer might work)
  • Latest VirtualBox (5.0.40 did not work)

Caveats

  • was not able to get this to work with the reboot command

Steps

Create a script called vbox_auto in init.d

sudo nano /etc/init.d/vbox_auto

Enter the following code into your new vbox_auto file

#! /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=origin
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



#! /bin/sh

### BEGIN INIT INFO
# Provides:             sshd
# Required-Start:       $remote_fs $syslog
# Required-Stop:        $remote_fs $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         
# Short-Description:    OpenBSD Secure Shell server
### END INIT INFO

Make the file exicutable

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

Make your script start at boot

sudo update-rc.d vbox_auto defaults

References

Discussion

Enter your comment. Wiki syntax is allowed:
If you can't read the letters on the image, download this .wav file to get them read to you.
 
wiki/start_headless_vm_at_boot.txt · Last modified: 2018/07/22 09:12 (external edit)