From 001300ff54d826696f2d7438063c09d2e8c9afd8 Mon Sep 17 00:00:00 2001
From: Kristian Klausen <kristian@klausen.dk>
Date: Sun, 27 Oct 2024 14:52:30 +0100
Subject: [PATCH] gitlab_runner: Fix broken "Running on ..." in the
 libvirt-executor

The prepare stage runs "echo "Running on $(hostname)...""[1], resulting
in "bash: line 7: hostname: command not found" and it outputting
"Running on ..." as the hostname command is provided by inetutils, which
is not installed.

Fix it by "monkey patching" it to use "hostnamectl hostname" and inject
the hostname with SMBIOS[2][3]. Injecting creds with SMBIOS may also be
useful in the future, e.g. for injecting an ephemeral SSH public key.

[1] https://gitlab.com/gitlab-org/gitlab-runner/-/blob/v17.5.2/shells/bash.go?ref_type=tags#L452-L456
[2] https://systemd.io/CREDENTIALS/
[3] https://github.com/systemd/systemd/pull/30814
---
 roles/gitlab_runner/files/libvirt-executor                | 8 +++++++-
 .../files/libvirt-executor-update-base-image              | 4 +++-
 roles/gitlab_runner/templates/domain_template.xml.j2      | 6 ++++++
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/roles/gitlab_runner/files/libvirt-executor b/roles/gitlab_runner/files/libvirt-executor
index c39d15b8b..a51a14f69 100755
--- a/roles/gitlab_runner/files/libvirt-executor
+++ b/roles/gitlab_runner/files/libvirt-executor
@@ -69,7 +69,13 @@ prepare() {
 run() {
   local ip
   ip="$(vm_ip "$(vm_name)")"
-  ssh "${ip}" bash < "${1}" || exit "${BUILD_FAILURE_EXIT_CODE:-1}"
+  if [[ ${2} == prepare_script ]]; then
+    # TODO: Get this fixed upstream or perhaps we should just install inetutils?
+    # https://gitlab.com/gitlab-org/gitlab-runner/-/blob/v17.5.2/shells/bash.go?ref_type=tags#L452-L456
+    ssh "${ip}" bash < <(sed 's/$(hostname)/$(hostnamectl hostname)/' "${1}") || exit "${BUILD_FAILURE_EXIT_CODE:-1}"
+  else
+    ssh "${ip}" bash < "${1}" || exit "${BUILD_FAILURE_EXIT_CODE:-1}"
+  fi
 }
 
 # https://docs.gitlab.com/runner/executors/custom.html#cleanup
diff --git a/roles/gitlab_runner/files/libvirt-executor-update-base-image b/roles/gitlab_runner/files/libvirt-executor-update-base-image
index 87ce385e1..1f96ecaff 100755
--- a/roles/gitlab_runner/files/libvirt-executor-update-base-image
+++ b/roles/gitlab_runner/files/libvirt-executor-update-base-image
@@ -43,7 +43,9 @@ sed 's/^\(GRUB_CMDLINE_LINUX=".*\)"$/\1 lockdown=confidentiality"/' -i mnt/etc/d
 arch-chroot mnt /usr/bin/grub-mkconfig -o /boot/grub/grub.cfg
 install -d -m0700 mnt/root/.ssh
 install -m0600 /etc/libvirt-executor/id_ed25519.pub mnt/root/.ssh/authorized_keys
-rm -f mnt/etc/machine-id
+# We want to use the transient hostname
+# https://github.com/systemd/systemd/pull/30814
+rm -f mnt/etc/machine-id mnt/etc/hostname
 
 cp -a mnt/boot/{initramfs-linux-fallback.img,initramfs-linux.img}
 
diff --git a/roles/gitlab_runner/templates/domain_template.xml.j2 b/roles/gitlab_runner/templates/domain_template.xml.j2
index 6ce09333d..16fc01ea9 100644
--- a/roles/gitlab_runner/templates/domain_template.xml.j2
+++ b/roles/gitlab_runner/templates/domain_template.xml.j2
@@ -2,8 +2,14 @@
   <name>$vm_name</name>
   <memory unit='MiB'>{{ gitlab_runner_libvirt_vm_memory }}</memory>
   <vcpu>4</vcpu>
+  <sysinfo type='smbios'>
+    <oemStrings>
+      <entry>io.systemd.credential:system.hostname=$vm_name</entry>
+    </oemStrings>
+  </sysinfo>
   <os>
     <type arch='x86_64' machine='q35'>hvm</type>
+    <smbios mode='sysinfo'/>
   </os>
   <features>
     <acpi/>
-- 
GitLab