trying to start Virtualbox VM at boot with systemd service file

Dear all,

I’m running openSUSE Leap 42.1 on a amd64 system and am trying to start a Virtualbox VM called Centos7 at boot with a systemd service file located in /etc/systemd/system

I have created the following vboxvmservice@.service file with this content:

[Unit]
Description=VBox Virtual Machine %i Service
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=robert
Group=vboxusers
ExecStart=/usr/bin/VBoxManage startvm %i --type gui
ExecStop=/usr/bin/VBoxManage controlvm %i acpipowerbutton

[Install]
WantedBy=multi-user.target

However, this doesn’t seem to work because

#systemctl --failed 

shows this output:

UNIT LOAD ACTIVE SUB DESCRIPTION
vboxvmservice@Centos7.service loaded failed failed VBox Virtual Machine Centos7 Service
vboxvmservice@vmservice@Centos7.service loaded failed failed VBox Virtual Machine vmservice@Centos7 Service

Can anyone assist me on this?
What should be fixed?

#uname -a

Linux opensuseleap42 4.1.34-33-default #1 SMP PREEMPT Thu Oct 20 08:03:29 UTC 2016 (fe18aba) x86_64 x86_64 x86_64 GNU/Linux

Hi,

You’re using the option

--type gui

Well for starters Imagine your vm starting with the gui option but your host does not have an X server running yet, does that make sense? :wink:

Hi,

Also did you enable the service?

If you want to instead follow the official Virtualbox documentation (Or, you should at least skim this info to know all that you need to do in your Unit file)

The main link that describes the vboxmanage commands
https://www.virtualbox.org/manual/ch08.html#vboxmanage-autostart

Before you can execute the vboxmanage commands, you first have to configure your Guest(s) to enable starting on boot
https://www.virtualbox.org/manual/ch09.html#autostart

To my eye comparing the official documentation with your proposed Unit file

  • You don’t define User permissions to access the Guest. This may be particularly relevant if you don’t enable “All Users” on your machine to be able to manage Guests in a default policy. On the other hand, AFAIK nowadays there are no restrictions for any User to manage a Guest, but that may still not be enough… A Guest may still require <some> User to be specified. To some degree, I’m comparing Virtualbox to how I understand “enterprise” virtualization technologies work, in those there is a fairly clear differentiation between core functionality and Userland tools. Virtualbox on the other hand seems to be designed to invoked, run and be managed entirely in a User security context so those differences need to be resolved, and may not be fully documented (in other words, where I don’t see full documentation, I’m guessing).
  • Your Unit file describes starting a specified Guest and “wants” network connectivity, but maybe that’s not right? Have you started the VBox application itself, which I would guess should provide at least minimal connectivity (Host-only, wouldn’t know about remote network functionality).

If what I suspect is correct, you should create another Unit file that sets up a “Virtualbox daemon service” to start and be running before you can execute the Unit file you created(with its vboxmanage commands), and you need to add a few parameters to your Unit file as well.

HTH,
TSU

Testing NNTP connectivity

Thanks dude,
this was very helpful and with this thread https://forums.virtualbox.org/viewtopic.php?f=7&t=51799 together wuth your explanation I managed to make it work.

My VM boots at startup now. For reference only I provide my files:

cat /etc/default/virtualbox shows:

virtualbox defaults file

VBOXAUTOSTART_DB=/etc/vbox
VBOXAUTOSTART_CONFIG=/etc/vbox/vboxcmd.txt

cat /etc/vbox/vboxcmd.txt shows:

Default policy is to deny starting a VM, the other option is “allow”.

#default_policy = deny

Create an entry for each user allowed to run autostart

#robert = {
#allow = true
#}
default_policy=allow

I then did this:

Set permissions on directory to the vboxuser group and make sure users can write to the directory as well as sticky bit.

chgrp vboxusers /etc/vbox

chmod 1775 /etc/vbox

Made sure my userid was in the vboxusers group

Then I did this:
VBoxManage setproperty autostartdbpath /etc/vbox

after which I did this:

VBoxManage modifyvm “Centos7” --autostart-enabled on

as I wanted to start my Centos7 VM at boot when my opensuse box starts

Then I enabled my systemd service with this command:
systemctl enable vboxvmservice@Centos7

The contents of my /etc/systemd/system/vboxvmservice@.service file is:

[Unit]
Description=VBox Virtual Machine %i Service
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=robert
Group=vboxusers
ExecStart=/usr/bin/VBoxManage startvm %i
ExecStop=/usr/bin/VBoxManage controlvm %i acpipowerbutton

[Install]
WantedBy=multi-user.target

Everything works as I was able to ssh to my just freshly started VM at boot.
Everybody thank you so much for this crash course:-)