Starting virtualbox VM (headless) at boot time

Hi folks
I’m setting up my new system based on Leap 15 Virtualbox SW as supplied by Opensuse (5.2.18-lp150.4.14.1). I’ve addedVBOXAUTOSTART_DB=/etc/vbox/
VBOXAUTOSTART_CONFIG=/etc/vbox/vbox.cfg

to /etc/default/virtualbox

# /etc/default/virtualbox
#
# -------------------------------------------------------------------------------------------------
# In the "SHUTDOWN_USERS" list all users for which a check for runnings VMs should be done during
# shutdown of "vboxdrv" resp. the server:
# SHUTDOWN_USERS="foo bar"
#
# Set "SHUTDOWN" to one of "poweroff", "acpibutton" or "savestate" depending on which of the
# shutdown methods for running VMs are wanted:
# SHUTDOWN="poweroff"
# SHUTDOWN="acpibutton"
# SHUTDOWN="savestate"
# -------------------------------------------------------------------------------------------------
#
#SHUTDOWN_USERS="foo bar"
#SHUTDOWN="savestate"

# -------------------------------------------------------------------------------------------------
# By default, vboxdrv creates a file /etc/udev/rules.d/60-vboxdrv.rules every time, it is started, 
# that allows full control of this hosts usb devices in virtual machines (given, you allow accessing
# them with the GUI).
# In order to control this ability yourself, copy the generated /etc/udev/rules.d/60-vboxdrv.rules
# file to another name and modify to your needs, e.g. comment out all lines beginning with SUBSYSTEM,
# which will make this hosts usb devices inaccessible AND enable the next line.
#INSTALL_NO_UDEV=1

# Autostart added 

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

Then I applied*chmod 1775 /etc/vbox
chgrp vboxusers /etc/vbox
*
*resulting in:
*drwxrwxr-t 1 root vboxusers 36 7. Sep 14:24 vbox

Of course the user “user” owning the machine is also member of vboxusers.

vbox.cfg in that directory looks like

#settings for vboxwebsrn
VBOXWEB_USER=root

# Added
# 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
user = {
allow = true
}

Added the autostart path to Virtualbox:


user@server:/root> VBoxManage list systemproperties
API version:                     5_2
Minimum guest RAM size:          4 Megabytes
Maximum guest RAM size:          2097152 Megabytes
Minimum video RAM size:          0 Megabytes
Maximum video RAM size:          256 Megabytes
Maximum guest monitor count:     64
Minimum guest CPU count:         1
Maximum guest CPU count:         32
Virtual disk limit (info):       2199022206976 Bytes
Maximum Serial Port count:       4
Maximum Parallel Port count:     2
Maximum Boot Position:           4
Maximum PIIX3 Network Adapter count:   8
Maximum ICH9 Network Adapter count:   36
Maximum PIIX3 IDE Controllers:   1
Maximum ICH9 IDE Controllers:    1
Maximum IDE Port count:          2
Maximum Devices per IDE Port:    2
Maximum PIIX3 SATA Controllers:  1
Maximum ICH9 SATA Controllers:   8
Maximum SATA Port count:         30
Maximum Devices per SATA Port:   1
Maximum PIIX3 SCSI Controllers:  1
Maximum ICH9 SCSI Controllers:   8
Maximum SCSI Port count:         16
Maximum Devices per SCSI Port:   1
Maximum SAS PIIX3 Controllers:   1
Maximum SAS ICH9 Controllers:    8
Maximum SAS Port count:          255
Maximum Devices per SAS Port:    1
Maximum NVMe PIIX3 Controllers:  1
Maximum NVMe ICH9 Controllers:   8
Maximum NVMe Port count:         255
Maximum Devices per NVMe Port:   1
Maximum PIIX3 Floppy Controllers:1
Maximum ICH9 Floppy Controllers: 1
Maximum Floppy Port count:       1
Maximum Devices per Floppy Port: 2
Default machine folder:          /home/user/VirtualBox VMs
Raw-mode Supported:              yes
Exclusive HW virtualization use: on
Default hard disk format:        VDI
VRDE auth library:               VBoxAuth
Webservice auth. library:        VBoxAuth
Remote desktop ExtPack:          Oracle VM VirtualBox Extension Pack
Log history count:               3
Default frontend:                
Default audio driver:            Pulse
**Autostart database path:         /etc/vbox**
Default Guest Additions ISO:     
Logging Level:                   all


and switched it on:


user@server:/root> VBoxManage showvminfo ........
Name:            Win 7 Neu
Groups:          /
Guest OS:        Windows 7 (64-bit)
.
.
.
.
Autostart Enabled: on
Autostart Delay: 0
.
.

The file /etc/vbox/user.start was created just containing a “1”

restarting vboxes.sh returns:

server:/ # service vboxes restart
server:/ # service vboxes status
vboxes.service - Autostart Headless Virtual Box VMs
Loaded: loaded (/usr/lib/virtualbox/vboxes.sh; enabled; vendor preset: disabled)
Active: active (exited) since Fri 2018-09-07 18:11:32 CEST; 1s ago
Process: 4740 ExecStop=/usr/lib/virtualbox/vboxes.sh stop (code=exited, status=0/SUCCESS)
Process: 4742 ExecStart=/usr/lib/virtualbox/vboxes.sh start (code=exited, status=0/SUCCESS)
Main PID: 4742 (code=exited, status=0/SUCCESS)

Sep 07 18:11:32 server systemd[1]: Starting Autostart Headless Virtual Box VMs...
Sep 07 18:11:32 server vboxes.sh[4742]: Starting Virtualbox machines: no virtual machines configured
Sep 07 18:11:32 server systemd[1]: Started Autostart Headless Virtual Box VMs.

Any ideas why vboxes does not see the virtual machine to start?
Did I miss to configure something?

br

JM

First,
to enable easier identification of each item you posted, each should be preceded by a short line containing the file name or command that generated the output.
At the moment for example, I can’t identify which if any is or is from your vboxes.sh.

In any case,
I’m looking for the exact mechanism you’re using to start a specific Guest and don’t see anything that does that…

Are you following a specific guide, if so you should post a link to it so that can be inspected.

TSU

Hi

I followed this:

https://www.virtualbox.org/manual/ch09.html#autostart-linux

and as this was leaving some points open

https://www.notesit.net/2017/08/16/rtfm-oder-virtualbox-headless-und-autostart/

plus amendments in:

http://yakupkorkmaz.info/?p=146

br

JM

and of course

The references you point to look like they should work, but as you describe for some reason the Virtualbox documentation seems to be oddly incomplete…

And,
Your references describe the “mechanism” I don’t see clearly described in the steps you did… Either you did something and posted results of whatever you did or the step is missing altogether.

From your reference
http://yakupkorkmaz.info/?p=146
The following step seems to be missing in your steps

VBoxManage modifyvm Ubuntu-VM –autostart-enabled on

From your reference
https://www.notesit.net/2017/08/16/rtfm-oder-virtualbox-headless-und-autostart/
The following step seems to be missing in your steps

*VboxManage modifyvm <VM name> -autostart-enabled on*

TSU

Hi

the result of this switch command is in a box of my initial mail showing the excerpt of the VBoxManage showvminfo … command.

In the meantime i dug into the underlying script (yikes! last time i did shell scripting was surely two decades ago ;))
The vboxes.sh script does not fit the rest and the behavior of VBoxManage which creates and deletes nicely those user.start files on switch on/off the autostart flag in the VM.
It seems not the right version. Just see the config file it wants to read, which is not supplied by the package. It further assumes “VM;user” pairs in a file to start them.


server:/usr/lib/virtualbox # 
cat vboxes.sh 
#!/bin/sh
#
# description: Starts and stops vbox autostart VMs.
# Based on
# http://www.amiryan.org/2009/11/04/virtualbox-init-d-service-autostart-scriptu
#
# By Richard Bos <rbos at opensuse dot org> - May 2010
#
# Converted to standalone script for systemd environments
#
# By Hans-Peter jansen <hpj at urpla dot net> - July 2017
#

PRG=$(basename $0)
SERVICE="Virtualbox machines"

VBOXMGR_BIN=/usr/lib/virtualbox/VBoxManage
if  ! -x $VBOXMGR_BIN ]]; then
  echo "$VBOXMGR_BIN does not exist"
  if  "$1" = "stop" ]; then
    exit 0
  else
    exit 6
  fi
fi

**# read config file
 -r /etc/sysconfig/vbox ] && . /etc/sysconfig/vbox**

start() {

  N=1
  for VBOX in $VBOX_AUTOSTART; do

    if grep -q \; <<< "$VBOX"; then
      VBOX_NAME$N]=$(cut -d\; -f1 <<< "$VBOX")
      VBOX_USER$N]=$(cut -d\; -f2 <<< "$VBOX")
    else
      VBOX_NAME$N]="$VBOX"
      VBOX_USER$N]=""
    fi
    N=$(($N+1)) 
  done



Being fed up and not want to waste more time on this piece of excellent software, I decided to do my own thing and created, based on vboxes.sh, my own dedicated service.

I’ve created in usr/lib/virtualbox vivotek.sh


#!/bin/sh
#
# 
#

PRG=$(basename $0)
SERVICE="Virtualbox machines"

VBOXMGR_BIN=/usr/lib/virtualbox/VBoxManage
if  ! -x $VBOXMGR_BIN ]]; then
  echo "$VBOXMGR_BIN does not exist"
  if  "$1" = "stop" ]; then
    exit 0
  else
    exit 6
  fi
fi

start() {

              su user -c "$VBOXMGR_BIN -q startvm 'Win 7 Neu' -type headless" &> /tmp/$PRG.$$
              RETVAL=$?

              if  $RETVAL != 0 ]]; then
                echo "Starting virtual machine: Win 7 Neu  (user: user) failed. "
                cat /tmp/$PRG.$$
              fi

}

stop() {

    echo "Shutting down: Win 7 Neu"
    su user -c "$VBOXMGR_BIN -q controlvm 'Win 7 Neu' savestate"
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|force-reload)
        stop
        start
        ;;
  *)
        echo "Usage: $PRG {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac


in server:/usr/lib/systemd/system I created vivotek.service

 
# Autostart configured Virtual Box VMs

[Unit]
SourcePath=/usr/lib/virtualbox/vivotek.sh
Description=Autostart Headless Virtual Box VMs
Before=multi-user.target graphical.target
After=network-online.target vboxdrv.service 
Wants=network-online.target vboxdrv.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/lib/virtualbox/vivotek.sh start
ExecStop=/usr/lib/virtualbox/vivotek.sh stop

[Install]
WantedBy=multi-user.target

Which I symlinked to /usr/lib/systemd/system/multi-user.target.wants and activated and started it

Works nicely - and will not be affected by opensuse fixes to the bug I will file tomorrow
Hope I haven’t forgotten anything!

br

JM

and the Bug report

https://bugzilla.opensuse.org/show_bug.cgi?id=1107769

Hi,

I never used any autoboot tools/service when it comes to ‘*starting virtual machines during system boot.’
*I always disable any service or manually link /dev/null to it or use the systemctl mask option to disable the autostart feature.
and i use systemd-vboxinit :wink: