From 86011931283eb1140a037d5238cbba2557ff9b04 Mon Sep 17 00:00:00 2001
From: Michael Prokop <mprokop@sipwise.com>
Date: Tue, 18 Jul 2023 15:05:22 +0200
Subject: [PATCH] MT#57453 vagrant_configuration: fake systemd presence

As of git rev 6c960afee4af we're using the
virtualbox-guest-additions-iso from bookworm.

Previous versions of VBoxGuestAdditions had a simple test to check for
present of systemd, quoting from
/opt/VBoxGuestAdditions-6.1.22/routines.sh:

| use_systemd()
| {
|     test ! -f /sbin/init || test -L /sbin/init
| }

Now in more recent versions of VBoxGuestAdditions[1], the systemd check
was modified, quoting from /opt/VBoxGuestAdditions-7.0.6/routines.sh:

| use_systemd()
| {
|     # First condition is what halfway recent systemd uses itself, and the
|     # other two checks should cover everything back to v1.
|     test -e /run/systemd/system || test -e /sys/fs/cgroup/systemd || test -e /cgroup/systemd
| }

So if we're running inside a chroot as with our deployment.sh, it looks
like a non-systemd system for VBoxGuestAdditions's installer, and we end
up with installation and presence of /etc/init.d/vboxadd, leading to:

| root@spce:~# ls -lah /run/systemd/generator.late/
| total 4.0K
| drwxr-xr-x  4 root root 100 Jul 18 00:20 .
| drwxr-xr-x 23 root root 580 Jul 18 00:20 ..
| drwxr-xr-x  2 root root  60 Jul 18 00:20 graphical.target.wants
| drwxr-xr-x  2 root root  60 Jul 18 00:20 multi-user.target.wants
| -rw-r--r--  1 root root 537 Jul 18 00:20 vboxadd.service
|
| root@spce:~# systemctl cat vboxadd.service
| # /run/systemd/generator.late/vboxadd.service
| # Automatically generated by systemd-sysv-generator
|
| [Unit]
| Documentation=man:systemd-sysv-generator(8)
| SourcePath=/etc/init.d/vboxadd
| Description=LSB: VirtualBox Linux Additions kernel modules
| Before=multi-user.target
| Before=multi-user.target
| Before=multi-user.target
| Before=graphical.target
| Before=display-manager.service
|
| [Service]
| Type=forking
| Restart=no
| TimeoutSec=5min
| IgnoreSIGPIPE=no
| KillMode=process
| GuessMainPID=no
| RemainAfterExit=yes
| SuccessExitStatus=5 6
| ExecStart=/etc/init.d/vboxadd start
| ExecStop=/etc/init.d/vboxadd stop

We don't expect any init scripts to be present, as all our services must
have systemd unit files. Therefore we check for absence of systemd's
/run/systemd/generator.late in our system-tests, which started to fail
with the upgrade to VBoxGuestAdditions-v7.0.6 due to the systemd
presence detection mentioned above.

Let's fake presence of systemd before invoking VBoxGuestAdditions's
installer, to avoid ending up with unexpected vbox* init scripts.

[1] See svn rev 92682:
    https://www.virtualbox.org/browser/vbox/trunk/src/VBox/Installer/linux/routines.sh?rev=92682
    https://www.virtualbox.org/changeset?old=92681&old_path=vbox%2Ftrunk%2Fsrc%2FVBox%2FInstaller%2Flinux%2Froutines.sh&new=92682&new_path=vbox%2Ftrunk%2Fsrc%2FVBox%2FInstaller%2Flinux%2Froutines.sh

Change-Id: Ifd11460e3a8fd4f4c1269453a9b8376065861b8e
---
 templates/scripts/includes/deployment.sh | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/templates/scripts/includes/deployment.sh b/templates/scripts/includes/deployment.sh
index 9be1d70..3668adf 100755
--- a/templates/scripts/includes/deployment.sh
+++ b/templates/scripts/includes/deployment.sh
@@ -1099,6 +1099,11 @@ vagrant_configuration() {
     FAKE_UNAME='/tmp/fake-uname.so'
   fi
 
+#  if ! [ -d "${TARGET}/run/systemd/system" ] ; then
+    echo "Creating /run/systemd/system for systemd detection within VBoxLinuxAdditions"
+    mkdir -p "${TARGET}/run/systemd/system"
+#  fi
+
   grml-chroot "${TARGET}" env \
     UTS_RELEASE="${KERNELVERSION}" LD_PRELOAD="${FAKE_UNAME}" \
     /media/cdrom/VBoxLinuxAdditions.run --nox11 || true