++2nd openbsd sketch

Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
Nico Schottelius 2026-06-29 09:29:54 +02:00
commit 9cdcacaeac

121
opennebula-images/openbsd-v2.sh Executable file
View file

@ -0,0 +1,121 @@
#!/bin/sh
set -euo pipefail
# --- Configuration ---
RELEASE="7.9"
ARCH="amd64"
MIRROR="https://cdn.openbsd.org/pub/OpenBSD/${RELEASE}/${ARCH}"
DISK_IMG="openbsd79.qcow2"
DISK_SIZE="10G"
echo "=== 1. Downloading OpenBSD ${RELEASE} Boot Images ==="
# We need the install ISO and the file distribution verification key if needed
wget -cN "${MIRROR}/install79.iso"
echo "=== 2. Creating empty QCOW2 Disk ==="
qemu-img create -f qcow2 "${DISK_IMG}" "${DISK_SIZE}"
echo "=== 3. Creating Autoinstall Configuration ==="
# OpenBSD's installer natively looks for an 'install.conf' exposed via an HTTP server
# to achieve a completely unattended interactive installation.
mkdir -p www
cat << 'EOF' > www/install.conf
Choose your keyboard layout = us
System hostname = openbsd-cloud
Which network interface = vio0
IPv4 address for vio0 = dhcp
IPv6 address for vio0 = none
Password for root = **********
Public ssh key for root = none
Start sshd by default = yes
Change the default console to com0 = yes
Which speed should com0 use = 115200
Setup a user = none
Allow root ssh login = yes
What timezone are you in = UTC
Location of sets = cd0
EOF
echo "=== 4. Launching Local HTTP Server for Auto-Install ==="
# We spin up a background python web server so OpenBSD's autoinstall can fetch the file
python3 -m http.server 8080 --directory www &
HTTP_PID=$!
# Ensure the web server dies when this script exits
trap 'kill $HTTP_PID 2>/dev/null || true' EXIT
# echo "=== 5. Running Unattended Installation via QEMU ==="
# # We pass 'auto' via the serial console instruction to tell the bootloader to run autoinstall
# qemu-system-x86_64 \
# -m 2048 \
# -smp 2 \
# -nographic \
# -drive file="${DISK_IMG}",format=qcow2,if=virtio \
# -cdrom install79.iso \
# -netdev user,id=n1,hostname=openbsd-cloud,bootfile=http://10.0.2.2:8080/install.conf \
# -device virtio-net-pci,netdev=n1 \
# -boot d
echo "=== 5. Running Unattended Installation via QEMU ==="
# We pass instructions to the OpenBSD 'boot>' prompt:
# 1. 'set tty com0' switches the kernel console output to serial.
# 2. 'auto' kicks off the unattended installer layout.
(
sleep 2;
echo "set tty com0";
sleep 1;
echo "auto";
) | qemu-system-x86_64 \
-m 2048 \
-smp 2 \
-nographic \
-drive file="${DISK_IMG}",format=qcow2,if=virtio \
-cdrom install79.iso \
-netdev user,id=n1,hostname=openbsd-cloud,bootfile=http://10.0.2.2:8080/install.conf \
-device virtio-net-pci,netdev=n1 \
-boot d \
-serial stdio
echo "=== 6. Base Installation Finished! ==="
echo "Cleaning up installer webserver..."
kill $HTTP_PID || true
echo "=== 7. Provisioning Packages (bash & cloud-agent) ==="
# Boot the newly created image to run a quick post-install step over SSH or embedded execution.
# To keep this completely automated, we pass a script to install packages via QEMU serial.
# Alternatively, we can boot with user networking forwarded to run a quick ssh loop.
echo "Booting VM to apply packages..."
qemu-system-x86_64 \
-m 2048 \
-smp 2 \
-nographic \
-drive file="${DISK_IMG}",format=qcow2,if=virtio \
-netdev user,id=n1,hostfwd=tcp::2222-:22 \
-device virtio-net-pci,netdev=n1 &
VM_PID=$!
echo "Waiting for OpenBSD to boot SSH..."
until nc -z localhost 2222; do sleep 2; done
echo "Installing bash and cloud-agent..."
# OpenBSD's native cloud-agent handles provisioning across cloud hypervisors natively.
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p 2222 root@localhost << 'EOF'
# Set the package mirror
echo "https://cdn.openbsd.org/pub/OpenBSD/%m" > /etc/installurl
# Install requested packages
pkg_add bash cloud-agent
# Enable the cloud-agent to run on boot
rcctl enable cloud_agent
# Clean up unique identifiers so the image behaves as a proper golden template
rm -f /etc/ssh/ssh_host_*
rm -f /var/db/dhcpleased/*
echo "Done!"
halt -p
EOF
wait $VM_PID
echo "=== Success! Final Image Built: ${DISK_IMG} ==="