The Linux Page

Autostart Virtualbox VMs in Ubuntu 18.04

Virtualbox to create a Computer inside another Computer

Autostart Virtualbox VMs

So... certainly things evolve quickly and others lack behind. We've got systemd since 15.10 or so and it was the prominent change in 16.04. We actually made the switch for Snap! Websites and that help tremendously as it is very reliable since then (older versions were really bad in comparison.)

At first, I found a Stackoverflow answer describing the old way and it did not work which is why I decided to switch to systemd. I found this page about it and it's already pretty good! Just a couple of things I had to tweak on my system to make it all work properly.

Deleting the Old Way

Just to make sure it works properly, delete the files from the old environment if you created them. If you did edits to some of them, you probably want to undo those edits.

This is to make sure that the old way doesn't suddenly kick in and mess up your systemd setup.

$ rm -rf /etc/vbox

On my end, I edited the /etc/default/virtualbox file to add these two variables:

VBOXAUTOSTART_DB=/etc/vbox
VBOXAUTOSTART_CONFIG=/etc/vbox/autostart.cfg

So I went ahead and deleted those two lines.

However, I also changed the following and that I kept:

#SHUTDOWN=poweroff
SHUTDOWN=acpibutton

I prefer to have a good APCI shutdown and a plain power off. If it takes too long to shutdown, we'll anyway get a power off (a.k.a. a kill).

If you did anything else to try to make the old way work, please make sure to revert that too. You're on your own on that side of things...

Create Service File

Now you are ready to create a service file.

In mine, I have pretty much the same thing as the one from the page I referenced above, except for the vboxdrv.service. I'm not too sure where that came from, but under Ubuntu 18.04, there is no such thing. Though I remove that one reference.

The script includes two names you need to change:

vm1 — enter the name of your Virtualbox machine (this one appears three times)
alexis — unless you are also an alexis and your account is name like that, change that name

Then save the file under /etc/systemd/system/.

For example, if you use gvim to edit your files:

$ sudo gvim /etc/systemd/system/vm1.service

Then copy/paste the following and do the edits as expected:

[Unit]
Description=vm1
After=network.target virtualbox.service vboxdrv.service
Before=runlevel2.target shutdown.target
 
[Service]
User=alexis
Group=vboxusers
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
 
ExecStart=/usr/bin/VBoxManage startvm vm1 --type headless
ExecStop=/usr/bin/VBoxManage controlvm vm1 acpipowerbutton
 
[Install]
WantedBy=multi-user.target

Note: the vboxdrv.service comes from a comment (see below) and should help even further (i.e. I have had problems with my services starting once in a while, most of the time, I still had to restart them by hand—I'll have to confirm once I reboot that it works every time).

Once the file is ready, you need to tell systemd to reload everything including that new file. It may do so automatically, but just in case here is the command you need:

sudo systemctl daemon-reload

Now you are ready to use this service.

Note: To see a list of services you have running on your system, the cleanest way is to use the following command:

sudo systemctl

It automatically sends its output to less so you don't have to pipe manually. At the bottom of the list comes a brief explaination of what the columns stand for. Especially, if a column says "failed", then the service doesn't work as expected.

Enable/Start/Stop Service

Now that we created a service file, we want to enable and start it. This supposes that your VM is ready to be started as if you were rebooting your machine.

Note that in the script above, there is nothing to say that one service should be started before and/or after another. It's doable but you should ask yourself whether it makes sense that the computers should be started in a very specific order. i.e. if something fails on computer B when A isn't running, it should keep trying until it works...

First, you can test that systemd recognize your file with:

$ systemctl status vm1

The status command is also useful to check whether a service is running or not.

Now you can start the VM.

$ sudo systemctl start vm1

It should return on the very next line unless something goes wrong.

If you have the Virtualbox UI open, you should see that it gets started there. The icon changes with what appears on the screen as normal. However, because we use the headless feature, you don't see the actual GUI part. You can show that window at any time while the machine is running by selecting the " Show" menu (it's found in the Machine menu or when you right-click on the VM).

Finally, to make the VM run automatically on a reboot, you need to enable it. This is done using the enable command like so:

$ sudo systemctl enable vm1

You can verify the current status at any time. Note that the enable state shows on its own. There are two entries in the output. Both should be enabled. Here is an example of the output. I highlighted the enabled you are looking for in red:

$ systemctl status vm1
 virtualbox_finball2.service - finball2
   Loaded: loaded (/etc/systemd/system/vm1.service; enabled; vendor preset: enabled)
   Active: active (exited) since Sat 2019-11-23 20:13:36 PST; 34s ago
    Tasks: 0 (limit: 9830)
   CGroup: /system.slice/vm1.service

Nov 23 20:13:35 monster systemd[1]: Starting vm1...
Nov 23 20:13:36 monster VBoxManage[12050]: Waiting for VM "vm1" to power on...
Nov 23 20:13:36 monster VBoxManage[12050]: VM "vm1" has been successfully started.
Nov 23 20:13:36 monster systemd[1]: Started vm1.

The last few lines are logs (called journals in systemd) showing what happened. It should say that it started your Virtualbox system.

You can later disable the service like so:

$ sudo systemctl disable vm1

which will prevent it from auto-restarting on a reboot. Note that you can still start a VM marked as being disabled.

Renaming a VM

I use rather long names for my VM that include:

  1. The name of what I'm using the VM for
  2. The OS name (Ubuntu, Fedora, FreeBSD, SunOS, MacOS, etc.)
  3. The hostname of the computer running the VM (because when you run VMs on 3 or 4 different computers, it's not easy to know what's what otherwise)
  4. The processor (i86x or amd64)
  5. The sub-name of the computer (i.e. I create clusters that get one VM name + one sub-name such as front end, database, backend...)

I think such long names are great in the Virtualbox interface, but to type in your service file, it may cause problems (between dashes, spaces, parenthesis... and just the fact that such long names are hard to replicate without a few mistakes into them...)

So on my end, I had to rename the VMs. The cool thing, though, is that we now have grouping capabilities. So I can still keep my crazy naming convention for the group, and name each computer in the group something really simple (i.e. <cluster_name>_<sub-name> — for example wordpress_frontend and wordpress_database).

Where is My VM's Window?

The script is going to start the VM headless. This is actually important if you were to run the VM on a server without X-Windows. (Yes! It's doable, I'm just not too sure how you do the installation all by hand. It must be somewhat painful!)

Your VMs are going to start alongside the X-Windows environment and because of that, the X-Windows system may not be ready to open a window in the first place. So instead we start VMs headless.

When you are in the Virtualbox GUI, you see the VMs that are running. They have a green arrow. Right-click on those and select " Show" to open the window. Now you have full access to the VM's console. To hide the window again, you can of course minimize. However, if you want to actually close the X-Windows but leave the VM running, use the "Detach GUI" option found under the "Machine" menu.

In older versions (before 5.x), detaching the GUI was done in various ways:

  1. Machine » Detach GUI
  2. View » Close VM
  3. Close VM (as a button)

Various Errors

The virtual machine has terminated unexpectedly during startup with exit code 0 (0x0).

Looking into that one, I could not really see why one of my virtual machine wouldn't start, especially with an exit code of 0... First I checked that the machine still existed as expected (right location, etc.) It was there.

So next I searched the Internet and found a page which I won't even link here, it was totally useless except for one small detail: one of the message was one person asking the other what they saw in their log file. Yes. Why wouldn't I look at that for once.

Here are the last few messages:

00:00:01.005004 GIM: KVM: Resetting MSRs
00:00:01.186998 VMSetError: /build/virtualbox-p8NxA3/virtualbox-5.2.34-dfsg/src/VBox/VMM/VMMR3/VM.cpp(326) int VMR3Create(uint32_t, PCVMM2USERMETHODS, PFNVMATERROR, void*, PFNCFGMCONSTRUCTOR, void*, VM**, UVM**); rc=VERR_VMM_R0_VERSION_MISMATCH
00:00:01.187018 VMSetError: The VMMR0.r0 module version does not match VBoxVMM.dll/so/dylib. If you just upgraded VirtualBox, please terminate all VMs and make sure that neither VBoxNetDHCP nor VBoxNetNAT is running. Then try again. If this error persists, try re-installing VirtualBox.
00:00:01.188756 ERROR [COM]: aRC=NS_ERROR_FAILURE (0x80004005) aIID={872da645-4a9b-1727-bee2-5585105b9eed} aComponent={ConsoleWrap} aText={The VMMR0.r0 module version does not match VBoxVMM.dll/so/dylib. If you just upgraded VirtualBox, please terminate all VMs and make sure that neither VBoxNetDHCP nor VBoxNetNAT is running. Then try again. If this error persists, try re-installing VirtualBox. (VERR_VMM_R0_VERSION_MISMATCH)}, preserve=false aResultDetail=0
00:00:01.189069 Console: Machine state changed to 'PoweredOff'
00:00:01.194933 Power up failed (vrc=VERR_VMM_R0_VERSION_MISMATCH, rc=NS_ERROR_FAILURE (0X80004005))
00:00:01.695786 GUI: UIMachineViewNormal::resendSizeHint: Restoring guest size-hint for screen 0 to 800x600
00:00:01.695873 ERROR [COM]: aRC=E_ACCESSDENIED (0x80070005) aIID={76eed314-3c72-4bbb-95cf-5eb4947a4041} aComponent={DisplayWrap} aText={The console is not powered up}, preserve=false aResultDetail=0
00:00:01.695961 GUI: Aborting startup due to power up progress issue detected...

And interestingly enough, they give you the exact answer of what to do next (3rd line in the logs above):

  • Sstop all your VMs;
  • Quit VirtualBox;
  • Make sure all the VM services are down (reboot if you want to make 100% sure); and
  • Restart VirtualBox.

Now it should work and it did for me.

The Old Way

At first, I tried the old way, mainly because there are a large number of posts in link with that one. But I just could not find the Virtualbox autostart script anywhere. I tried that once and nothing happened so I would imagine that it's missing (was removed) in new versions (Ubuntu 18.04 at least).

These are a few points I wanted to make regarding that method.

I go the following error when trying to install a Virtualbox in auto-start mode and trying to fix the problem took me a little while...

VBoxManage: error: Adding machine 'my-virtual-box' to the autostart database failed with VERR_ACCESS_DENIED
VBoxManage: error: Details: code NS_ERROR_UNEXPECTED (0x8000ffff), component SessionMachine, interface IMachine, callee nsISupports
VBoxManage: error: Context: "COMSETTER(AutostartEnabled)(ValueUnion.f)" at line 3035 of file VBoxManageModifyVM.cpp

The fact is that once you added yourselves to the vboxusers group, you need to log out and back in properly or even just forget about that group for a moment and use your primary group instead.

In other words, the docs I've found such as this one:

https://askubuntu.com/questions/404665/how-to-start-virtual-box-machines-automatically-when-booting

tell you to use vboxusers but you could also do that:

chgrp <my-primary-group> /etc/vbox

(Note: in most cases, your primary group name is your username)

and the chmod is not even necessary. Of course, that one won't help much if you want many users to be able to use the autostart feature. The idea is just to have a way to do your setup before you log out and/or test with a full restart of your computer (in my case, I have a monster with 512Gb of RAM, two processors with a total of 64 threads, it takes forever to reboot so I prefer to avoid it if I can.)

I'll try again and see whether it works if I restart the computer and I have the correct group in there...

However, I just rebooted and it did not work, there is still something fishy about it under 18.04, I just can't see the vbox service. I can see the Virtualbox kernel module, just no the service. I probably need to install something else...

Re: Autostart Virtualbox VMs in Ubuntu 18.04

I'm running mint linux.

Noticed I was getting an error:

Oct 07 20:52:38 ShedPC VBoxManage[4984]: WARNING: The vboxdrv kernel module is not loaded. Either there is no module
Oct 07 20:52:38 ShedPC VBoxManage[4984]:          available for the current kernel (5.4.0-88-generic) or it failed >
Oct 07 20:52:38 ShedPC VBoxManage[4984]:          load. Please recompile the kernel module and install it by
Oct 07 20:52:38 ShedPC VBoxManage[4984]:            sudo /sbin/vboxconfig
Oct 07 20:52:38 ShedPC VBoxManage[4984]:          You will not be able to start VMs until this problem is fixed.

And then later on down the log, vboxdrv would load

Had to add the following lines to my config:

[Unit]
Description=FlexRadio
After=network.target virtualbox.service vboxdrv.service
Requires=virtualbox.service vboxdrv.service
Before=runlevel2.target shutdown.target

Don't know if I actually needed the Requires line, but seemed like a thing to do...

Many thanks for the webpage entry. It was of great help, easy to follow and well written.

Re: Autostart Virtualbox VMs in Ubuntu 18.04

I'm glad that my post helped you find out how to make it work on your machine.

On my end, if I run:

locate virtualbox.service

nothing is output. So I'm sure I do not have such a file and of course I can't wait on it being up/run if I want my boxes to start automatically. That being said, for me it works without that additional name.

That being said, there's a virtualbox script under /etc/init.d and it may be that your script will wait for that one to have run. That script makes sure that all the drivers are loaded before trying to start virtual boxes that are marked for auto-headless startup. But that did not work for me.


Update: The fact is that now I see the virtualbox.service file! I'm not too sure how/what changed, maybe I had to reboot or something. The fact is the last time I rebooted, my VMs did not start and since then I see that the virtualbox.service exists so I can depend on it. I updated my post accordingly.

Re: Autostart Virtualbox VMs in Ubuntu 18.04

Hi there, I followed your instructions, but found that adding network.target on the After line in /etc/systemd/system/vm1.service as you have suggested here was insufficient.

I found that without also adding an additional virtualbox.service entry on the After line my VMs were unable to find character device /dev/vboxdrv at boot and would fail to start.

As you have rightly point out in your article vboxdrv.service does not appear to exist on Ubuntu 18.04. However please be aware that Ubuntu DOES have the similarly named virtualbox.service available instead and we must wait for this to launch before loading any VMs.

In conclusion, in my own /etc/systemd/system/vm1.service file I have the following two lines which seems to work fine for me in Ubuntu 18.04:

After=network.target virtualbox.service
Before=runlevel2.target shutdown.target