Auto-Start VirtualBox before user login

Hello everyone.

I am a newbie in Mabox Linux. I have created a virtual machine with virtual box to run homeAssistant in my Mabox installation, since I use that computer for general stuff.
I am trying to make VirtualBox auto-start at boot in the event of a power outage.
For that I created a simple script and placed it under
/etc/X11/xinit/xinitrc.d/51-startHAos.sh
with the bellow code:
VBoxManage startvm "HomeAssistant" --type headless

Well… it has a small problem: it only starts to run after I login to my session.

Is there a way to automatically start this process even before I login?

If not, maybe the alternative would be to auto-login, start the script and immediately after lock the screen/session.
I would prefer the first approach. Also, I’m not sure how to auto-login without password.

Thanks for the support

Hello @Jtradamus and welcome to the forum :slight_smile:

I think that running it via systemd will be a more elegant solution in this case.

https://www.baeldung.com/linux/virtualbox-vm-start-on-boot

1 Like

Thank you very much for your help.
It works perfectly now .

Steps followed:

  1. get the name of the home assistant image that we want to run in virtual box
    VBoxManage list vms

This will list all the images currently installed. In my case the image is called HomeAssistant

  1. created the file
    /etc/systemd/system/vboxvmservice@.service

  2. added the following to the vboxvmservice@.service

[Unit]
Description=VBox Virtual Machine %i Service
Requires=systemd-modules-load.service
After=systemd-modules-load.service

[Service]
User=USERNAME_ADDED_HERE
Group=vboxusers
ExecStart=/usr/bin/VBoxHeadless -s %i
ExecStop=/usr/bin/VBoxManage controlvm %i savestate

[Install]
WantedBy=multi-user.target
  1. enable the systemd script

sudo systemctl enable vboxvmservice@HomeAssistant.service

Thank you for the help.

1 Like