Install a one-shot service to initialize hostid

This commit is contained in:
Stefan Bethke 2026-03-31 19:55:02 +02:00
commit 7ebf94cc23
3 changed files with 28 additions and 2 deletions

View file

@ -9,12 +9,13 @@
: "${STORAGE:=local-zfs}" : "${STORAGE:=local-zfs}"
: "${OS:=debian-13}" : "${OS:=debian-13}"
: "${NAME:=chaos-${OS}-tmpl-$(date -u +%Y-%m-%d)}" : "${NAME:=chaos-${OS}-tmpl-$(date -u +%Y-%m-%d)}"
: "${BRIDGE:=vmbr0,tag=208}" : "${BRIDGE:=vmbr0,tag=2}"
: "${AUTHORIZED_KEYS_URL:=https://git.hamburg.ccc.de/CCCHH/infrastructure-authorized-keys/raw/branch/trunk/authorized_keys}" : "${AUTHORIZED_KEYS_URL:=https://git.hamburg.ccc.de/CCCHH/infrastructure-authorized-keys/raw/branch/trunk/authorized_keys}"
set -eE set -eE
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH
here="$(pwd)"
tempfolder=$(mktemp -d /tmp/cloudinit-XXXXX) tempfolder=$(mktemp -d /tmp/cloudinit-XXXXX)
pushd $tempfolder pushd $tempfolder
@ -44,7 +45,11 @@ virt-customize -a disk.raw \
--uninstall openipmi \ --uninstall openipmi \
--run-command 'systemctl enable qemu-guest-agent' \ --run-command 'systemctl enable qemu-guest-agent' \
--run-command 'systemctl enable fstrim.timer' \ --run-command 'systemctl enable fstrim.timer' \
--run-command 'echo -n >/etc/machine-id' --run-command 'echo -n >/etc/machine-id' \
--copy-in ${here}/initialize-host-id:/usr/local/sbin \
--run-command 'chmod +x /usr/local/sbin/initialize-host-id' \
--copy-in ${here}/initialize-host-id.service:/etc/systemd/system \
--run-command 'systemctl enable initialize-host-id.service'
qm destroy ${VMID} || true qm destroy ${VMID} || true
qm create ${VMID} --name "creating-vm" --memory 2048 --net0 virtio,bridge=${BRIDGE} qm create ${VMID} --name "creating-vm" --memory 2048 --net0 virtio,bridge=${BRIDGE}

8
initialize-host-id Normal file
View file

@ -0,0 +1,8 @@
#!/bin/sh
#
# Initialize /etc/hostid if it doesn't exist or is empty
#
if [ ! -f /etc/hostid -o "$(cat /etc/hostid 2>/dev/null)" = "" ]; then
hexdump -n 4 -e '"%02x"' /dev/urandom >/etc/hostid
fi

View file

@ -0,0 +1,13 @@
[Unit]
Description=Initialize /etc/hostid
Documentation=https://git.hamburg.ccc.de/CCCHH/pve-template-vm
After=sysinit.target
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/initialize-host-id
User=root
Restart=never
[Install]
WantedBy=sysinit.target