How can I get vboxweb to start properly at boot

After some more googling and mucking about, I finally came up with a solution:

1) I concluded that the /etc/defaults/virtualbox file did not seem to have any effect on anything (just a remnant from the init.d days?). So the user and group settings in that file was wasted.

Solution: Added User=vbox and Group=vboxusers in the [Service] section of the vboxweb.service file.

2) Some of the problems I had was due to the vboxweb.config file referring to a pid-file in the /run folder, and vbox has no write access to that folder. First I tried to make a subfolder, owned by vbox, in which to store the pid file, but I learned that the /run folder gets wiped at boot time, including all subfolders.

Solution: Created a subfolder in vbox homedir, and updated vboxweb.config file with that location.

3) For some reason (which still eludes me), referring to the actual IP address, 192.168.1.12, in config.php (in the phpvirtualbox folder) and in the vboxweb.config file (as mentioned in the original post), did not work at boot time. (A later restart of the service would work though.)

Solution: Updated both config.php and vboxweb.config with IP address 127.0.0.1 instead of 192.168.1.12.

Case closed! :o) ...apart for all the questions about why things are the way they are, but I can live with that :o)

BR, Rolf


In my install version there were some built-in bugs in the services:

$ dpkg -l | grep -i virtualbox
ii  virtualbox                                 6.1.10-dfsg-1~ubuntu1.20.04.1         amd64        x86 virtualization solution - base binaries
ii  virtualbox-dkms                            6.1.10-dfsg-1~ubuntu1.20.04.1         amd64        x86 virtualization solution - kernel module sources for dkms
ii  virtualbox-qt                              6.1.10-dfsg-1~ubuntu1.20.04.1         amd64        x86 virtualization solution - Qt based user interface

Plus you have to define at least one parameter as per this:

On Linux, the web service can be automatically started during host boot by adding appropriate parameters to the file /etc/default/virtualbox. There is one mandatory parameter, VBOXWEB_USER, which must be set to the user which will later start the VMs.

Let's suppose this user is pjfinn. Here are the fix/mods. Add the user to the vboxusers group:

$ sudo usermod -a -G vboxusers pjfinn

Create the directory run under pjfinn's home:

$ mkdir /home/pjfinn/run

Then apply the following patches.

Patch #1:

*** /etc/default/virtualbox-orig  2020-12-07 17:02:51.567517505 +0100
--- /etc/default/virtualbox 2020-12-07 18:23:58.436155855 +0100
***************
*** 21,23 ****
--- 21,26 ----
  #   30 seconds for the VMs to shutdown
  SHUTDOWN_USERS=""
  SHUTDOWN=poweroff
+ 
+ # 2020/12 pj_finnegan fix - define mandatory parameter
+ VBOXWEB_USER=pjfinn

Patch #2:

*** /lib/systemd/system/vboxweb.service-orig 2020-12-07 16:13:43.776834092 +0100
--- /lib/systemd/system/vboxweb.service   2020-12-07 19:24:07.352940488 +0100
***************
*** 1,11 ****
  [Unit]
  Description=VirtualBox Web Service
! After=network.target
  
  [Service]
  Type=forking
  ExecStart=/usr/lib/virtualbox/vboxweb-service.sh start
! PIDFile=/run/vboxweb.pid
  
  [Install]
  WantedBy=multi-user.target
--- 1,13 ----
  [Unit]
  Description=VirtualBox Web Service
! Requires=virtualbox.service
! After=network.target virtualbox.service
  
  [Service]
  Type=forking
  ExecStart=/usr/lib/virtualbox/vboxweb-service.sh start
! ExecStop=/usr/lib/virtualbox/vboxweb-service.sh stop
! PIDFile=/home/pjfinn/run/vboxweb.pid
  
  [Install]
  WantedBy=multi-user.target

Patch #3:

*** /usr/lib/virtualbox/vboxweb-service.sh-orig 2020-12-07 17:00:04.901783182 +0100
--- /usr/lib/virtualbox/vboxweb-service.sh  2020-12-07 18:08:43.586981699 +0100
***************
*** 29,34 ****
--- 29,37 ----
  # X-Required-Target-Start: network-online
  ### END INIT INFO
  
+ # 2020/10 pj_finnegan fix - save binary PIDFILE received from systemd
+ VBOXWEB_PIDFILE="$PIDFILE"
+ 
  PATH=$PATH:/bin:/sbin:/usr/sbin
  SCRIPTNAME=vboxweb-service.sh
  
***************
*** 135,140 ****
--- 138,148 ----
          [ -n "$VBOXWEB_ROTATE" ]         && PARAMS="$PARAMS -R $VBOXWEB_ROTATE"
          [ -n "$VBOXWEB_LOGSIZE" ]        && PARAMS="$PARAMS -S $VBOXWEB_LOGSIZE"
          [ -n "$VBOXWEB_LOGINTERVAL" ]    && PARAMS="$PARAMS -I $VBOXWEB_LOGINTERVAL"
+ 
+         # 2020/12 pj_finnegan fix - avoid error:
+         #  Can't open PID file /run/vboxweb.pid (yet?) after start: Operation not permitted
+         [ -n "$VBOXWEB_PIDFILE" ]        && PARAMS="$PARAMS -P $VBOXWEB_PIDFILE"
+ 
          # set authentication method + password hash
          if [ -n "$VBOXWEB_AUTH_LIBRARY" ]; then
              su - "$VBOXWEB_USER" -c "$vboxmanage setproperty websrvauthlibrary \"$VBOXWEB_AUTH_LIBRARY\""

Then execute the commands:

$ sudo systemctl daemon-reload

Finally the admin commands:

$ sudo systemctl start vboxweb.service
$ systemctl status vboxweb.service
● vboxweb.service - VirtualBox Web Service
     Loaded: loaded (/lib/systemd/system/vboxweb.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2020-12-07 18:13:42 CET; 2s ago
    Process: 8997 ExecStart=/usr/lib/virtualbox/vboxweb-service.sh start (code=exited, status=0/SUCCESS)
   Main PID: 9023 (vboxwebsrv)
      Tasks: 20 (limit: 18850)
     Memory: 15.0M
     CGroup: /system.slice/vboxweb.service
             ├─9023 /usr/lib/virtualbox/vboxwebsrv --background -P /home/pjfinn/run/vboxweb.pid
             ├─9025 /usr/lib/virtualbox/VBoxXPCOMIPCD
             └─9031 /usr/lib/virtualbox/VBoxSVC --auto-shutdown

Dec 07 18:13:42 yourhost systemd[1]: Starting VirtualBox Web Service...
Dec 07 18:13:42 yourhost vboxweb-service.sh[8997]: vboxweb-service.sh: Starting VirtualBox web service.
Dec 07 18:13:42 yourhost vboxweb-service.sh[9014]: Starting VirtualBox web service.
Dec 07 18:13:42 yourhost systemd[1]: Started VirtualBox Web Service.

$ sudo systemctl stop vboxweb.service
$ systemctl status vboxweb.service
● vboxweb.service - VirtualBox Web Service
     Loaded: loaded (/lib/systemd/system/vboxweb.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Mon 2020-12-07 18:19:45 CET; 2s ago
    Process: 8997 ExecStart=/usr/lib/virtualbox/vboxweb-service.sh start (code=exited, status=0/SUCCESS)
    Process: 9135 ExecStop=/usr/lib/virtualbox/vboxweb-service.sh stop (code=exited, status=0/SUCCESS)
   Main PID: 9023 (code=exited, status=0/SUCCESS)

Dec 07 18:13:42 yourhost systemd[1]: Starting VirtualBox Web Service...
Dec 07 18:13:42 yourhost vboxweb-service.sh[8997]: vboxweb-service.sh: Starting VirtualBox web service.
Dec 07 18:13:42 yourhost vboxweb-service.sh[9014]: Starting VirtualBox web service.
Dec 07 18:13:42 yourhost systemd[1]: Started VirtualBox Web Service.
Dec 07 18:19:39 yourhost systemd[1]: Stopping VirtualBox Web Service...
Dec 07 18:19:39 yourhost vboxweb-service.sh[9135]: vboxweb-service.sh: Stopping VirtualBox web service.
Dec 07 18:19:45 yourhost systemd[1]: vboxweb.service: Succeeded.
Dec 07 18:19:45 yourhost systemd[1]: Stopped VirtualBox Web Service.

$ sudo systemctl restart vboxweb.service

should all work fine, and the errors at boot should disappear.


rolfhsp's answer is really helpful.

In addition, if vboxweb.service is modified to run as some other user which is non root, another failure would occur. In this situation, the cause is logged as "Failed to open “/dev/vboxdrvu”, errno=13, rc=VERR_VM_DRIVER_NOT_ACCESSIBLE", which means the user has no permission to access /dev/vboxdrvu.

By default, /dev/vboxdrvu along with /dev/vboxnetctl and /dev/vboxdru are all permitted for root only (crw-------). for non-root users to access it, the permission must be modified. According to this blog, it is archived by simply putting a "rules" file into /lib/udev/ with content below:

/lib/udev/rules.d/20-virtualbox2.rules:

KERNEL=="vboxdrv", NAME="vboxdrv", OWNER="root", GROUP="vboxusers", MODE="0660"
KERNEL=="vboxdrvu", NAME="vboxdrvu", OWNER="root", GROUP="vboxusers", MODE="0660"
KERNEL=="vboxnetctl", NAME="vboxnetctl", OWNER="root",GROUP="vboxusers", MODE="0660"

Then restart the system, after reboot, those files have new permissions:

crw-------. 1 root root      10, 54 Mar 20 00:24 /dev/vboxdrv
crw-rw----. 1 root vboxusers 10, 53 Mar 20 00:24 /dev/vboxdrvu
crw-rw----. 1 root vboxusers 10, 52 Mar 20 00:24 /dev/vboxnetctl

Although vboxdru is still unchanged, vboxdrvu already has good permission for a non-root (in vboxusers group) to access. After this modification, vboxweb.service could be run by a non-root user successfully.