Ubuntu 18.04 Start Stop VirtualBox

pete_c

Guru
Here have been running a Windows VB on my main automation server which is running Ubuntu 18.04 64 bit and recently upgraded to VirtualBox 6.0.
 
I have had issues autostarting the VB using standard virtual box autostart. 
 
Found a new way to do this this morning and it works well.
 
Documenting here on a post.
 
Get rid of vboxautostart-service
 
1 - sudo systemctl disable vboxautostart-service
2 - sudo rm -rf /lib/systemd/system/vboxautostart-service.service
3 - sudo rm -rf /etc/vbox
 
User permissions for Virtualbox
 
1 - type groups to see if you are in the vboxusers group
 
IE:
pete@ICS-HS3Pro:~$ groups
pete adm cdrom sudo dip plugdev lpadmin sambashare vboxusers
 
2 - if not put yourself in the vboxusers group typing:
sudo usermod -a -G vboxusers pete
 
3 - type groups again to make sure you are in the group vboxusers
 
New Unit File for systemd
 
1 - Create a new Unit file
sudo nano /etc/systemd/system/vm_autostart_name.service
IE: sudo nano /etc/systemd/system/vm_autostart_W7.service
 
2 - use this text as a template: (I used vm_name=W7)

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

 
Reload systemd daemon
 
sudo systemctl daemon-reload
 
Using the service
 
1 - Enable the service
 
sudo systemctl enable vm_autostart_name
 
2 - Start, Stop and Status of service
 
sudo systemctl start vm_autostart_name
sudo systemctl stop vm_autostart_name
sudo systemctl status vm_autostart_name
 
That's it.  Works great over here.
 
 
 
 
 
How do you add a delay to this. I need time for PfSense to load or it times out. 
 
i did up the timeout to 600
 
[Unit]
Description=VM jin
After=network.target vboxdrv.service
Before=runlevel2.target shutdown.target
 
[Service]
User=sam
Group=vboxusers
Type=forking
Restart=no
TimeoutSec=600
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
 
ExecStart=/usr/bin/VBoxManage startvm jin --type headless
ExecStop=/usr/bin/VBoxManage controlvm jin acpipowerbutton
 
but still get :
 
 

vm_autostart_jin.service - VM jin
   Loaded: loaded (/etc/systemd/system/vm_autostart_jin.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2020-02-09 14:09:26 EST; 1min 44s ago
  Process: 882 ExecStart=/usr/bin/VBoxManage startvm jin --type headless (code=exited, status=1/FAILURE)
    Tasks: 0 (limit: 4915)
   CGroup: /system.slice/vm_autostart_jin.service
 
Feb 09 14:09:25 pms systemd[1]: Starting VM jin...
Feb 09 14:09:26 pms VBoxManage[882]: VBoxManage: error: Nonexistent host networking interface, name 'vmnet2' (VERR_INTERNAL_ERROR)
Feb 09 14:09:26 pms VBoxManage[882]: VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole
Feb 09 14:09:26 pms VBoxManage[882]: Waiting for VM "jin" to power on...
Feb 09 14:09:26 pms systemd[1]: vm_autostart_jin.service: Control process exited, code=exited status=1
Feb 09 14:09:26 pms systemd[1]: vm_autostart_jin.service: Failed with result 'exit-code'.
Feb 09 14:09:26 pms systemd[1]: Failed to start VM jin.

 
after the failure i can:
 
sudo systemctl start vm_autostart_jin.service
 
and then all is good:
 

vm_autostart_jin.service - VM jin
   Loaded: loaded (/etc/systemd/system/vm_autostart_jin.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-02-09 14:13:32 EST; 26s ago
  Process: 4212 ExecStart=/usr/bin/VBoxManage startvm jin --type headless (code=exited, status=0/SUCCESS)
    Tasks: 43 (limit: 4915)
   CGroup: /system.slice/vm_autostart_jin.service
           ├─4226 /usr/lib/virtualbox/VBoxXPCOMIPCD
           ├─4232 /usr/lib/virtualbox/VBoxSVC --auto-shutdown
           └─4278 /usr/lib/virtualbox/VBoxHeadless --comment jin --startvm ea8a1c26-3e90-43f5-acd3-78e7784122f9 --vrde config
 
Feb 09 14:13:31 pms systemd[1]: Starting VM jin...
Feb 09 14:13:32 pms VBoxManage[4212]: Waiting for VM "jin" to power on...
Feb 09 14:13:32 pms VBoxManage[4212]: VM "jin" has been successfully started.
Feb 09 14:13:32 pms systemd[1]: Started VM jin

 
any helpl woud be appreciated.
 
Welcome to the Cocoontech forum Stuach.
 
Add the following (this is a guess) to the service section:
 
allow = true
startup_delay = 10
 
 
Here just running Windows 7 embedded on the Ubuntu Desktop box for Microsoft SAPI speech and running the command line executable for starting the Virtual box.
 
/usr/bin/virtualboxvm --startvm W7
 
 
This thread was extremely helpful, but there was a small change I had to make because I was getting this error:
------------------------------------------------------------
root@server1:/etc/systemd/system# systemctl status vbox-roundcube.service
● vbox-roundcube.service - VM roundcube
     Loaded: loaded (/etc/systemd/system/vbox-roundcube.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Mon 2020-06-15 16:09:21 CDT; 16h ago

Jun 15 16:09:20 server1 systemd[1]: Starting VM roundcube...
Jun 15 16:09:21 server1 VBoxManage[848]: VBoxManage: error: The virtual machine 'Roundcube' has terminated unexpectedly during startup with exit code 1 (0x1)
Jun 15 16:09:21 server1 VBoxManage[848]: VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component MachineWrap, interface IMachine
Jun 15 16:09:21 server1 VBoxManage[848]: Waiting for VM "Roundcube" to power on...
Jun 15 16:09:21 server1 systemd[1]: vbox-roundcube.service: Control process exited, code=exited, status=1/FAILURE
Jun 15 16:09:21 server1 systemd[1]: vbox-roundcube.service: Failed with result 'exit-code'.
Jun 15 16:09:21 server1 systemd[1]: Failed to start VM roundcube.
-----------------------------
 
so, in my /etc/systemd/system/vbox-hmail.service file, I added virtualbox.service, like this:
 
After=network.target vboxdrv.service virtualbox.service

and now it works perfectly!  I didn't have to use the extra startup_delay.
 
p.s.  please fix your https on this site, all the security warnings almost made me give up joining!
 
Welcome to the Cocoontech forum shippj. 
 
 
Thank you for the fix. One automation server has been doing well auto starting.  I have not looked at the service configuration in a while now.
 
I am though having one issue where the Windows 7 virtual box does lock up after a couple of weeks and I have to force Virtual Box off or kill the job to get a new session going.  It is running a few instances of a MS SAPI application and wondering if that piece is hitting the utilization of the virtual box where it shuts down?
 
Relating to security here on the forum I am no longer using Firefox as it's use appears to have been blocked such that I use Chrome these days (which I do not really like).  So try using Chrome, new Edge or Firefox (in safe mode).
 
Signed up specifically to say thanks for these steps! I am running an Ubuntu server with VMs for pfsense and Home Assistant that I was struggling to get to autostart on boot.  After a lot of googling and frustration I was close to giving up but these worked a treat.
 
Thank you!
 
Welcome to the Cocoontech forum @mmckerman.
 
Another way that works is just running a one liner in cron of a virtual box on start up.
 
sleep 120 && exec /usr/lib/virtualbox/VBoxHeadless --comment "W7" --startvm "{a67ee324-9558-42dc-bd16-b8d29f96277e}"
 
Ha! Touche! Works even better with the one liner!
 
Originally I was going to stick with the first method but in adding a Windows 10 VM to the system I was having trouble getting the VMs sequenced the way I wanted them to start.  Using the one liner version and adjusting the timings solved that quick smart.
 
Thanks again!
 
Good news @mmckernan!!!
 
Here over the years found that WIndows 2016 server is a bit thinner than Windows 10 desktop.  This is similiar to switching over from Windows XP to Windows 2003 many many years ago.  IE: relating to automation servers stuff.
 
I have my laptops here dual booting Ubuntu and Windows and have noticed that Windows 10 keeps getting larger and slower.  
 
Back
Top