tinfoil-hat.net-relaunch/content/posts/vbox-autostart.md
2024-05-31 00:57:15 +02:00

152 lines
5.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Vbox Autostart"
date: 2024-05-31T00:38:31+02:00
draft: false
tags: ["Linux","Server","Technology"]
categories: "Technology"
---
In this guide, we are going to learn how to autostart VirtualBox VMs on system boot on Linux. On a Linux system with VirtualBox installed, you can start VMs automatically during system boot.
# AutoStart VirtualBox VMs on System Boot on Linux
There are multiple ways in which you can configure your VirtualBox vms to automatically start on system boot.
A few of these ways that we will discuss in this guide include;
1. Using VirtualBox Autostart Service
2. Using Systemd service unit
3. Using system cronjobs
## Using VirtualBox Autostart Service
VirtualBox comes with a service called vboxautostart-service that makes it easy to automatically start virtual machines during system reboot.
In order to configure a VirtualBox VM to start on system boot on Linux, you need to activate the autostart service. The autostart service can be activated by setting two variables in /etc/default/virtualbox;
- **VBOXAUTOSTART_DB** which defines the absolute path to the autostart database directory, usually the /etc/vbox.
- **VBOXAUTOSTART_CONFIG** defines the path to the virtual machine autostart configuration.
These variables can be defined as;
```bash
VBOXAUTOSTART_DB=/etc/vbox
VBOXAUTOSTART_CONFIG=/etc/vbox/autostartvm.cfg
```
To place these variables in the /etc/default/virtualbox, run the command below;
```bash
echo -e "VBOXAUTOSTART_DB=/etc/vbox\nVBOXAUTOSTART_CONFIG=/etc/vbox/autostartvm.cfg" | sudo tee /etc/default/virtualbox
```
Define the virtual machine autostart configuration settings. The autostart configuration file contains options that controls how the virtual machine is auto started.
```sudo vim /etc/vbox/autostartvm.cfg```
```bash
default_policy = deny
kifarunix = {
allow = true
startup_delay = 10
}
```
- **default_policy** defines whether to allow or deny the virtual machine autostart by default. In our example above, we denied any one from auto-starting the VM and explicitly allow one user, amos.
- **username** (kifarunix) with the default deny policy, you can define the specific users that are allowed to autostart the virtual machine (allow = trues). You can also define how long to delay the VM startup. 10 seconds is used in this demo.
### Set Ownership of Database directory
The database directory, /etc/vbox, should be writable by the user to be used to start VMs automatically. To make it easy, you can simply add your user to vboxusers group and set the group ownership of the database directory to vboxusers group. After that, set the write permissions for the group. In this case, amos user is to be used to automatically start the virtual machine.
```sudo usermod -aG vboxusers kifarunix```
```sudo chgrp vboxusers /etc/vbox```
Assign the group write permissions on the autostart database directory.
```sudo chmod g+w /etc/vbox```
To shield the directory from being modified or deleted by other users except the owner or the root user, set sticky bit.
```sudo chmod +t /etc/vbox```
### Enable Virtual Machine Autostart
As a user, you can enable autostart for individual machines. This requires that you define the path to the database directory first.
```bash
VBoxManage setproperty autostartdbpath /etc/vbox/
```
Once that is done, you can now setup the virtual machine to automatically start on system boot.
```bash
vboxmanage modifyvm fedora30 --autostart-enabled on
```
Where fedora30 is the name of your virtual machine. You can also use UUID instead.
You can get the names/UUIDs of the vms by running the command below;
vboxmanage list vms
Restart vboxautostart-service
The configuration is now done. To effect the settings, you need to restart the vboxautostart-service.
```bash
sudo systemctl restart vboxautostart-service
```
Testing the Virtual Machine autostart
To test that your virtual machine can actually autostart on system boot, reboot you system and check. When system boots, your VM should now be running.
If you which to disable the virtual machine autostart;
```bash
vboxmanage modifyvm fedora30 --autostart-enabled off
```
## Using Systemd service unit
Similarly, you can auto-start a VirtualBox by a creating a systemd service that starts the respective VM when system reboots.
To use this approach, just create the systemd unit service as follows (replace the name of the vm accordingly);
```bash
sudo vim /etc/systemd/system/fedora30-vm.service
```
```bash
[Unit]
Description=Autostart VirtualBox VM
After=network.target vboxdrv.service
[Service]
User=kifarunix
ExecStart=/usr/bin/vboxheadless -s fedora30
ExecStop=/usr/bin/VBoxManage controlvm fedora30 acpipowerbutton
[Install]
WantedBy=multi-user.target
```
Replace the User=kifarunix, with your correct username.
Reload the systemd daemon to read the new unit file:
```bash
sudo systemctl daemon-reload
```
Enable the service to start automatically on boot:
```bash
sudo systemctl enable vmname.service
```
Reboot the system and confirm if all is good.
## Autostart VirtualBox VM using System Cronjob
It is also possible to use cronjob to automatically start a virtualbox machine on system reboot.
Simply, as user with which you want to start the VirtualBox vm as, create a cronjob as follows;
```bash
crontab -e
```
Enter this line to start your VM. Replace the names accordingly;
```bash
@reboot sleep 60 && vboxheadless --startvm "fedora30" &
```
The command simply starts the vm 1 minute after the system boots.
That is all! You have learnt how to autostart VirtualBox VMs on system boot on Linux using the following methods;
Credits: https://kifarunix.com/autostart-virtualbox-vms-on-system-boot-on-linux