Virtualbox Autostart a VM

From chriesibaum wiki
Jump to navigation Jump to search

Follow the steps described as follows. A more detailed description can be found here:


First disable the Oracle vboxautostart-service and remove its config folder:

sudo systemctl disable vboxautostart-service
sudo rm -rf /etc/vbox


Append vboxusers group to your list of existing groups:

groups
sudo usermod -a -G vboxusers <user>


Create a new service:

sudo nano /etc/systemd/system/<vm_autostart_name.service>


And add the following content for the new service to start a virtualbox machine:

[Unit]
Description=<vm name>
After=network.target vboxdrv.service
Before=runlevel2.target shutdown.target

[Service]
User=<user to run the vm>
Group=vboxusers
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes

ExecStart=/usr/bin/VBoxManage startvm <vm_name> --type headless
ExecStop=/usr/bin/VBoxManage controlvm >vm_name> acpipowerbutton

[Install]
WantedBy=multi-user.target


Reload systemd daemon:

sudo systemctl daemon-reload


Enable the new service:

sudo systemctl enable <vm_autostart_name>


Finally, test the service:

sudo systemctl start <vm_autostart_name>
sudo systemctl stop <vm_autostart_name>
sudo systemctl status <vm_autostart_name>