aboutsummaryrefslogtreecommitdiffstats
path: root/openbsd-image
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-09-22 17:00:53 -0700
committerEugene Triguba <hello@eugenetriguba.com>2026-10-11 18:33:35 -0700
commitc9cfdbf86f2bf620c986abb2f67dcaabf5b01047 (patch)
tree23be01d9eb5cee38e1919e83d633ceff977f8f78 /openbsd-image
parentRemove need for explicit memory var override (diff)
downloadsite-server-images-c9cfdbf86f2bf620c986abb2f67dcaabf5b01047.zip
Add support for hcloud openbsd image snapshot
Diffstat (limited to 'openbsd-image')
-rw-r--r--openbsd-image/Makefile73
-rw-r--r--openbsd-image/configs/doas.conf1
-rw-r--r--openbsd-image/locals.pkr.hcl4
-rw-r--r--openbsd-image/main.pkr.hcl23
-rw-r--r--openbsd-image/openbsd-amd64.pkr.hcl42
-rw-r--r--openbsd-image/openbsd-arm64.pkr.hcl51
-rwxr-xr-xopenbsd-image/scripts/harden-root.sh12
-rwxr-xr-xopenbsd-image/scripts/setup.sh32
-rw-r--r--openbsd-image/templates/install.conf.pkrtpl.hcl16
-rw-r--r--openbsd-image/variables.pkr.hcl55
10 files changed, 309 insertions, 0 deletions
diff --git a/openbsd-image/Makefile b/openbsd-image/Makefile
new file mode 100644
index 0000000..0c3ae00
--- /dev/null
+++ b/openbsd-image/Makefile
@@ -0,0 +1,73 @@
+# TODO: Replace with run-openbsd-image.sh and add ARM support to build-openbsd-image.sh
+SHELL := /bin/sh
+
+ARCH ?= arm64
+BUILD_DIR := build
+EDK2_DIR := /opt/homebrew/share/qemu
+SSH_PORT := 2222
+SSH_USER := admin
+SSH_KEY := ~/.ssh/id_rsa
+CPU := 8
+MEMORY := 32768M
+PACKER_LOG := 1
+
+VERSION := 7.9
+VERSION_SHORT := 79
+QEMU_ARCH := $(if $(filter arm64,$(ARCH)),aarch64,x86_64)
+VARS_FILE := openbsd$(VERSION_SHORT)-$(ARCH).pkvars.hcl
+DISK := $(BUILD_DIR)/openbsd-$(VERSION)-$(QEMU_ARCH).qcow2
+EFIVARS_RUN := $(BUILD_DIR)/efivars-run.fd
+EDK2_CODE := $(EDK2_DIR)/edk2-aarch64-code.fd
+EDK2_VARS := edk2-arm-vars.fd
+
+export PACKER_LOG
+
+.PHONY: all init build run ssh clean
+
+all: build
+
+init:
+ packer init .
+
+build: init clean
+ifeq ($(ARCH),arm64)
+ cp $(EDK2_DIR)/$(EDK2_VARS) ./$(EDK2_VARS)
+endif
+ packer build -only qemu.openbsd-$(ARCH) -var-file=$(VARS_FILE) .
+
+# Run the headless VM (SSH via port-forwarded localhost:$(SSH_PORT))
+run:
+ifeq ($(ARCH),arm64)
+ cp ./$(EDK2_VARS) $(EFIVARS_RUN)
+ qemu-system-aarch64 \
+ -machine type=virt,accel=hvf \
+ -cpu host \
+ -m $(MEMORY) \
+ -smp $(CPU) \
+ -nographic \
+ -device qemu-xhci,id=xhci \
+ -device usb-kbd,bus=xhci.0 \
+ -device virtio-gpu-pci \
+ -netdev user,id=net0,hostfwd=tcp::$(SSH_PORT)-:22 \
+ -device virtio-net,netdev=net0 \
+ -drive file=$(DISK),if=virtio,cache=writeback,discard=ignore,format=qcow2 \
+ -drive file=$(EDK2_CODE),if=pflash,unit=0,format=raw,readonly=on \
+ -drive file=$(EFIVARS_RUN),if=pflash,unit=1,format=raw
+else
+ qemu-system-x86_64 \
+ -machine type=q35,accel=tcg \
+ -cpu max \
+ -m $(MEMORY) \
+ -smp $(CPU) \
+ -nographic \
+ -netdev user,id=net0,hostfwd=tcp::$(SSH_PORT)-:22 \
+ -device virtio-net,netdev=net0 \
+ -drive file=$(DISK),if=virtio,cache=writeback,discard=ignore,format=qcow2
+endif
+
+ssh:
+ ssh -i $(SSH_KEY) -p $(SSH_PORT) $(SSH_USER)@127.0.0.1
+
+clean:
+ rm -rf $(BUILD_DIR)
+
diff --git a/openbsd-image/configs/doas.conf b/openbsd-image/configs/doas.conf
new file mode 100644
index 0000000..96cf24d
--- /dev/null
+++ b/openbsd-image/configs/doas.conf
@@ -0,0 +1 @@
+permit nopass keepenv :wheel
diff --git a/openbsd-image/locals.pkr.hcl b/openbsd-image/locals.pkr.hcl
new file mode 100644
index 0000000..f88100f
--- /dev/null
+++ b/openbsd-image/locals.pkr.hcl
@@ -0,0 +1,4 @@
+locals {
+ version_short = replace(var.version, ".", "")
+ root_password = "temp"
+}
diff --git a/openbsd-image/main.pkr.hcl b/openbsd-image/main.pkr.hcl
new file mode 100644
index 0000000..43d01dd
--- /dev/null
+++ b/openbsd-image/main.pkr.hcl
@@ -0,0 +1,23 @@
+packer {
+ required_plugins {
+ qemu = { source = "github.com/hashicorp/qemu", version = ">= 1.1.0" }
+ }
+}
+
+build {
+ sources = ["source.qemu.openbsd-amd64", "source.qemu.openbsd-arm64"]
+
+ provisioner "file" {
+ source = "configs/doas.conf"
+ destination = "/etc/doas.conf"
+ }
+
+ provisioner "shell" {
+ script = "scripts/setup.sh"
+ environment_vars = ["ADMIN_USER=${var.admin_user}"]
+ }
+
+ provisioner "shell" {
+ script = "scripts/harden-root.sh"
+ }
+}
diff --git a/openbsd-image/openbsd-amd64.pkr.hcl b/openbsd-image/openbsd-amd64.pkr.hcl
new file mode 100644
index 0000000..5d2c9f7
--- /dev/null
+++ b/openbsd-image/openbsd-amd64.pkr.hcl
@@ -0,0 +1,42 @@
+source "qemu" "openbsd-amd64" {
+ vm_name = "openbsd-${var.version}-amd64.raw"
+ iso_url = "${var.mirror}/pub/OpenBSD/${var.version}/${var.arch}/install${local.version_short}.iso"
+ iso_checksum = "file:${var.mirror}/pub/OpenBSD/${var.version}/${var.arch}/SHA256"
+ output_directory = "build"
+ qemu_binary = "qemu-system-x86_64"
+ machine_type = "q35"
+ cpu_model = "max"
+ accelerator = "tcg"
+ memory = var.memory
+ disk_interface = "virtio"
+ disk_size = "${var.disk_size}G"
+ disk_compression = true
+ format = "raw"
+ ssh_username = "root"
+ ssh_password = local.root_password
+ ssh_timeout = "30m"
+
+ http_content = {
+ "/install.conf" = templatefile("${path.root}/templates/install.conf.pkrtpl.hcl", {
+ hostname = var.hostname
+ user = var.admin_user
+ ssh_key = var.ssh_public_key
+ root_password = local.root_password
+ mirror = var.mirror
+ version = var.version
+ version_short = local.version_short
+ arch = var.arch
+ })
+ }
+
+ boot_command = [
+ "<wait>S<enter><wait>",
+ "set tty com0<enter><wait>",
+ "autoinstall<enter><wait>",
+ "http://{{ .HTTPIP }}:{{ .HTTPPort }}/install.conf<enter>",
+ ]
+ boot_wait = "20s"
+ shutdown_command = "shutdown -p now"
+ headless = false
+ qemuargs = [["-display", "cocoa"]]
+}
diff --git a/openbsd-image/openbsd-arm64.pkr.hcl b/openbsd-image/openbsd-arm64.pkr.hcl
new file mode 100644
index 0000000..dbe5833
--- /dev/null
+++ b/openbsd-image/openbsd-arm64.pkr.hcl
@@ -0,0 +1,51 @@
+source "qemu" "openbsd-arm64" {
+ vm_name = "openbsd-${var.version}-aarch64.qcow2"
+ iso_url = "${var.mirror}/pub/OpenBSD/${var.version}/${var.arch}/install${local.version_short}.iso"
+ iso_checksum = "file:${var.mirror}/pub/OpenBSD/${var.version}/${var.arch}/SHA256"
+ output_directory = "build"
+ qemu_binary = "qemu-system-aarch64"
+ machine_type = "virt"
+ cpu_model = "host"
+ accelerator = "hvf"
+ memory = var.memory
+ disk_interface = "virtio"
+ disk_size = "${var.disk_size}G"
+ disk_compression = true
+ format = "qcow2"
+ ssh_username = "root"
+ ssh_password = local.root_password
+ ssh_timeout = "10m"
+
+ http_content = {
+ "/install.conf" = templatefile("${path.root}/templates/install.conf.pkrtpl.hcl", {
+ hostname = var.hostname
+ user = var.admin_user
+ ssh_key = var.ssh_public_key
+ root_password = local.root_password
+ mirror = var.mirror
+ version = var.version
+ version_short = local.version_short
+ arch = var.arch
+ })
+ }
+
+ boot_command = [
+ "<wait>S<enter><wait>",
+ "set tty com0<enter><wait>",
+ "autoinstall<enter><wait>",
+ "http://{{ .HTTPIP }}:{{ .HTTPPort }}/install.conf<enter>",
+ ]
+ boot_wait = "20s"
+ shutdown_command = "shutdown -p now"
+ use_default_display = true
+ headless = false
+ qemuargs = [
+ ["-device", "qemu-xhci,id=xhci"],
+ ["-device", "usb-kbd,bus=xhci.0"],
+ ["-device", "virtio-gpu-pci"],
+ ["-display", "cocoa"],
+ ]
+ efi_firmware_code = "/opt/homebrew/share/qemu/edk2-aarch64-code.fd"
+ efi_firmware_vars = "./edk2-arm-vars.fd"
+}
+
diff --git a/openbsd-image/scripts/harden-root.sh b/openbsd-image/scripts/harden-root.sh
new file mode 100755
index 0000000..c8e267a
--- /dev/null
+++ b/openbsd-image/scripts/harden-root.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+set -e
+
+main() {
+ chown root:wheel /etc/doas.conf
+ chmod 0640 /etc/doas.conf
+ usermod -p '*' root
+ sed -i 's/^PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
+ rcctl reload sshd
+}
+
+main "$@"
diff --git a/openbsd-image/scripts/setup.sh b/openbsd-image/scripts/setup.sh
new file mode 100755
index 0000000..320d93d
--- /dev/null
+++ b/openbsd-image/scripts/setup.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+set -e
+
+: "${ADMIN_USER:?ADMIN_USER must be set}"
+
+main() {
+ update_pkgs
+ install_system_maintenance_cron_jobs "$ADMIN_USER"
+ allow_user_non_root_shutdown "$ADMIN_USER"
+}
+
+update_pkgs() {
+ # Ensure the time is in sync. If it is not, things like upgrading the system
+ # packages via pkg_add -u fail.
+ rdate -n pool.ntp.org
+ pkg_add -u
+ fw_update
+}
+
+install_system_maintenance_cron_jobs() {
+ (
+ echo "MAILTO=$1" # Send mail about root cron jobs to the admin user.
+ crontab -l 2>/dev/null # Print existing cron file; redirect stderr if empty cron file error.
+ echo "10 12 * * * /usr/sbin/syspatch -c" # Install any outstanding patches everyday at 12:10
+ ) | crontab -
+}
+
+allow_user_non_root_shutdown() {
+ usermod -G _shutdown "$1"
+}
+
+main "$@"
diff --git a/openbsd-image/templates/install.conf.pkrtpl.hcl b/openbsd-image/templates/install.conf.pkrtpl.hcl
new file mode 100644
index 0000000..9df18ce
--- /dev/null
+++ b/openbsd-image/templates/install.conf.pkrtpl.hcl
@@ -0,0 +1,16 @@
+System hostname = ${hostname}
+Setup a user = ${user}
+SSH key for user = ${ssh_key}
+Password for user = none
+Password for root = ${root_password}
+Allow root ssh login = yes
+Which disk is the root disk = sd0
+Use (W)hole disk or (E)dit the MBR = whole
+Location of sets = http
+HTTP proxy = none
+HTTP Server = ${mirror}
+Server directory = pub/OpenBSD/${version}/${arch}
+Set name(s) = base comp man -game*.tgz -x*.tgz
+Directory does not contain SHA256.sig. Continue without verification = no
+Change the default console to com0 = yes
+Which speed should com0 use = 115200
diff --git a/openbsd-image/variables.pkr.hcl b/openbsd-image/variables.pkr.hcl
new file mode 100644
index 0000000..4bc0860
--- /dev/null
+++ b/openbsd-image/variables.pkr.hcl
@@ -0,0 +1,55 @@
+variable "version" {
+ type = string
+ description = "Version of OpenBSD to build"
+ validation {
+ condition = can(regex("^\\d\\.\\d$", var.version))
+ error_message = "The version must be a single-digit followed by a period followed by another single digit."
+ }
+}
+
+variable "arch" {
+ type = string
+ validation {
+ condition = contains(["amd64", "arm64"], var.arch)
+ error_message = "The arch must be one of: amd64, arm64."
+ }
+}
+
+variable "admin_user" {
+ type = string
+ description = "Username of the admin user on the VM"
+ default = "admin"
+}
+
+variable "ssh_public_key" {
+ type = string
+ description = "Public SSH key to associate with the admin user for login"
+}
+
+variable "mirror" {
+ type = string
+ description = "URL basename for where to download the OpenBSD ISO."
+ default = "https://cdn.openbsd.org"
+ validation {
+ condition = can(regex("^(https?)://", var.mirror)) && !endswith(var.mirror, "/")
+ error_message = "URL must start with http(s):// and must not have a trailing slash."
+ }
+}
+
+variable "hostname" {
+ type = string
+ description = "Hostname for the VM"
+ default = "openbsd-vm"
+}
+
+variable "memory" {
+ type = number
+ description = "Memory to allocate to the VM in MB while building the ISO"
+ default = 4096
+}
+
+variable "disk_size" {
+ type = number
+ description = "Size of the disk in GB while building the ISO"
+ default = 16
+}