If you start a virtual machine VirtualBox on the server, you’ll likely want them to remain running, even after an unexpected reboot of the server.
In this small article we will consider how to configure the startup of the virtual machine VirtualBox using init system systemd, which is now used everywhere.
Autostart a virtual machine in VirtualBox
Let’s create a file of the unit, which will manage the startup. To do this, run the following command:
sudo systemctl edit [email protected] --full --force
In the opened editor, paste this text:
Description=Virtual Box Guest %I
ExecStart=/usr/bin/VBoxManage startvm %i --type headless
ExecStop=/usr/bin/VBoxManage controlvm %i acpipowerbutton
With this unit we are going to do autostart virtualbox linux. In this example, I run a virtual machine as a root user, but you need to run it from that user, to which the added virtual machine and on whose behalf VirtualBox usually works.
Save the changes to the file and run the following command to the system initialization read configuration files from disk:
In our unit file, we have provided the ability to use the parameter that is passed the name or UUID of the virtual machine ID. If the machine name contains a space, it is better to use UUID, but otherwise you can name.
See the list of virtual machines:
VBoxManage list vms
Now let’s run the machine with UUID Ubutnu1804 ed0ce7dd-0ddb-46bf-913c-876671c4d1f1 using systemd:
sudo systemctl start [email protected]
If during startup an error has occurred, you can watch her perform:
In this case, the machine had already been started. I stopped it and launched again, is already using systemd. Look at the status of the virtual machine:
sudo systemctl status [email protected]
If everything is good, all that’s left is to add this service in startup:
sudo systemctl enable [email protected]
In this article, we understand how to make the startup a virtual machine virtualbox. As you can see, everything is very simple and convenient. Now your virtual machine will always be running.