Autostart of VMs on boot

In bsc#1107769, J Merkel reported that autostarting VirtualBox virtual machines on system boot no longer work since the switch to systemd.

It took a while, but a fix is now available, which is similar to the Oracle mechanism, and different from the one previously used by openSUSE. Once the updated code reaches the appropriate repository (Tumbleweed, Leap 15 Update, or Leap 42.3 Update) using this feature will only require the following steps:

  1. As root, edit file /etc/vbox/autostart.cfg and add the following:
    default_policy = deny

    Create an entry for each user allowed to use autostart

    myusername = {
    allow = true
    }

  2. As root, edit file /etc/default/virtualbox and add the following (set directory and file as you wish):
    VBOXAUTOSTART_DB=/etc/vbox
    VBOXAUTOSTART_CONFIG=/etc/vbox/autostart.cfg

  3. For each VM you want to autostart, as your regular user, enter a line such as
    VBoxManage modifyvm <uuid|vmname> --autostart-enabled on

  4. To turn off autostart:VBoxManage modifyvm <uuid|vmname> --autostart-enabled off

I just created a .service file in /etc/systemd/system

[Unit]
Description = VirtualBox LAMP Server

[Service]
Type = idle
User=dos
Group=vboxusers
ExecStart = /usr/bin/VBoxHeadless --startvm "LAMP"
ExecStop =  /usr/bin/VBoxManage controlvm "LAMP" poweroff
ExecStopPost = /usr/bin/sleep 40
Restart = on-abort
StartLimitInterval = 60
StartLimitBurst = 10

[Install]
WantedBy = multi-user.target


I then enabled the service and can start/stop using the normal systemd commands. I used a 40 second delay on stop so that the vm does a clean shutdown when I reboot my computer.