aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-10-03 08:52:49 -0700
committerEugene Triguba <hello@eugenetriguba.com>2026-10-11 18:33:35 -0700
commitde40b1e69eaabac3ca01774fa3a4e7abc463ac7c (patch)
tree9e6ba70dd1f982f851c84a1aa542c1a28a55010f
parentAdd prompt to remove build directory if it exists (diff)
downloadsite-server-images-de40b1e69eaabac3ca01774fa3a4e7abc463ac7c.zip
Customize the disk layout via disklabel templates
-rw-r--r--.shellcheckrc8
-rwxr-xr-xbuild-hcloud-snapshot.sh26
-rwxr-xr-xbuild-openbsd-image.sh44
-rw-r--r--hcloud-snapshot/snapshot.pkr.hcl2
-rw-r--r--lib.sh81
-rw-r--r--openbsd-image/Makefile73
-rw-r--r--openbsd-image/disk-layouts/cgit.disklabel10
-rw-r--r--openbsd-image/locals.pkr.hcl1
-rw-r--r--openbsd-image/openbsd-amd64.pkr.hcl32
-rw-r--r--openbsd-image/openbsd-arm64.pkr.hcl32
-rw-r--r--openbsd-image/templates/install.conf.pkrtpl.hcl6
-rw-r--r--openbsd-image/variables.pkr.hcl6
-rwxr-xr-xrun-openbsd.image.sh98
13 files changed, 306 insertions, 113 deletions
diff --git a/.shellcheckrc b/.shellcheckrc
new file mode 100644
index 0000000..0c220e0
--- /dev/null
+++ b/.shellcheckrc
@@ -0,0 +1,8 @@
+shell=sh
+
+# Allow ShellCheck to follow `source something.sh`
+external-sources=true
+
+# where to look for sourced files
+source-path=SCRIPTDIR
+
diff --git a/build-hcloud-snapshot.sh b/build-hcloud-snapshot.sh
index 00ec32d..aff6c1c 100755
--- a/build-hcloud-snapshot.sh
+++ b/build-hcloud-snapshot.sh
@@ -12,7 +12,7 @@ set -o nounset
set -o errexit
export PACKER_LOG=1
-SCRIPTDIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
+_SCRIPT_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
VERSION=7.9
ARCH=amd64
@@ -20,9 +20,9 @@ SSH_KEY="default-key"
main() (
parse_args "$@"
- img_path="${SCRIPTDIR}/openbsd-image/build/openbsd-${VERSION}-${ARCH}.raw"
+ img_path="${_SCRIPT_DIR}/openbsd-image/build/openbsd-${VERSION}-${ARCH}.raw"
build_compressed_image "$img_path"
- cd "$SCRIPTDIR/hcloud-snapshot/"
+ cd "$_SCRIPT_DIR/hcloud-snapshot/"
packer init .
packer build \
-var "hcloud_token=${HCLOUD_TOKEN:?}" \
@@ -36,6 +36,10 @@ main() (
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
+ -h | --help)
+ usage
+ exit 0
+ ;;
--version)
VERSION="$2"
shift 2
@@ -56,6 +60,22 @@ parse_args() {
done
}
+usage() {
+ cat <<EOF
+Usage: $(basename "$0") [OPTIONS]
+
+Create an OpenBSD snapshot in Hetzner Cloud.
+Requires HCLOUD_TOKEN to be set in the environment.
+Run ./build-openbsd-image.sh first to produce the local image.
+
+Options:
+ --version VERSION OpenBSD version (must match the built image) (default: $VERSION)
+ --arch ARCH Target architecture: amd64 or arm64 (default: $ARCH)
+ --ssh-key NAME Hetzner SSH key name to add to the snapshot (default: $SSH_KEY)
+ -h, --help Show this help message
+EOF
+}
+
build_compressed_image() (
if [ ! -e "$1.zst" ]; then
zstd -T0 "$1" -o "$1.zst"
diff --git a/build-openbsd-image.sh b/build-openbsd-image.sh
index faa6bc1..0e14e54 100755
--- a/build-openbsd-image.sh
+++ b/build-openbsd-image.sh
@@ -8,16 +8,22 @@
set -o nounset
set -o errexit
+_SCRIPT_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
+. "$_SCRIPT_DIR/lib.sh"
+
VERSION=7.9
ARCH=amd64
DISK_SIZE_GB=36
-PUBLIC_SSH_KEY="$HOME/.ssh/id_rsa.pub"
+SSH_KEY="$HOME/.ssh/id_rsa.pub"
+DISKLABEL_TEMPLATE=""
export PACKER_LOG=1
main() (
parse_args "$@"
cd openbsd-image/
+ require_readable_file "$SSH_KEY"
+ require_readable_file "$DISKLABEL_TEMPLATE"
if [ -d build/ ]; then
printf 'Build directory exists. Must delete before re-building. Proceed? (y/N)'
read -r answer
@@ -38,14 +44,19 @@ main() (
-var "arch=$ARCH" \
-var "disk_size=$DISK_SIZE_GB" \
-var "version=$VERSION" \
- -var "ssh_key=$(cat "$PUBLIC_SSH_KEY")" \
+ -var "ssh_key=$(cat "$SSH_KEY")" \
-var "cpus=$(get_ncpus)" \
+ ${DISKLABEL_TEMPLATE:+-var "disklabel_template=$(cat "$DISKLABEL_TEMPLATE")"} \
.
)
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
+ -h | --help)
+ usage
+ exit 0
+ ;;
--version)
VERSION="$2"
shift 2
@@ -58,8 +69,12 @@ parse_args() {
DISK_SIZE_GB="$2"
shift 2
;;
- --public-ssh-key)
- PUBLIC_SSH_KEY="$2"
+ --ssh-key)
+ SSH_KEY="$2"
+ shift 2
+ ;;
+ --disklabel-template)
+ DISKLABEL_TEMPLATE="$(realpath "$2")"
shift 2
;;
*)
@@ -70,12 +85,21 @@ parse_args() {
done
}
-get_ncpus() {
- if [ "$(uname -s)" = 'Darwin' ]; then
- sysctl -n hw.ncpu
- else
- nproc
- fi
+usage() {
+ cat <<EOF
+Usage: $(basename "$0") [OPTIONS]
+
+Create an OpenBSD image with Packer and QEMU.
+
+Options:
+ --version VERSION OpenBSD version to build (default: $VERSION)
+ --arch ARCH Target architecture: amd64 or arm64 (default: $ARCH)
+ --disk-size-gb SIZE Disk size in GB (default: $DISK_SIZE_GB)
+ --ssh-key PATH Path to SSH key (default: $SSH_KEY)
+ --disklabel-template PATH Path to a custom disklabel autopartition template.
+ If omitted, uses OpenBSD's default auto layout.
+ -h, --help Show this help message
+EOF
}
main "$@"
diff --git a/hcloud-snapshot/snapshot.pkr.hcl b/hcloud-snapshot/snapshot.pkr.hcl
index cabf65d..ba6376f 100644
--- a/hcloud-snapshot/snapshot.pkr.hcl
+++ b/hcloud-snapshot/snapshot.pkr.hcl
@@ -6,7 +6,7 @@ source "hcloud" "openbsd" {
location = "nbg1"
ssh_username = "root"
ssh_keys = [var.ssh_key]
- snapshot_name = "openbsd-${var.version}-${var.arch}-golden"
+ snapshot_name = "openbsd-${var.version}-${var.arch}"
snapshot_labels = {
"os": "openbsd",
"version": var.version,
diff --git a/lib.sh b/lib.sh
new file mode 100644
index 0000000..7a8ad17
--- /dev/null
+++ b/lib.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+set -o nounset
+set -o errexit
+
+# Get the number of cpus of the current system.
+#
+# STDOUT:
+# The number of CPU cores
+#
+# RETURNS:
+# 1 on failure
+get_ncpus() {
+ _get_ncpus_os="$(uname -s)"
+ if [ "$_get_ncpus_os" = 'Darwin' ]; then
+ sysctl -n hw.ncpu
+ else
+ return 1
+ fi
+}
+
+# Get the total amount of memory on the current system.
+#
+# ARGS:
+# $1 (Optional): The units to retrieve the memory size in.
+# Options are: 'b', 'mb', 'kb', 'gb'. Defaults to 'b' (bytes).
+#
+# STDOUT:
+# The total amount of memory.
+#
+# RETURNS:
+# 1 on failure
+get_memsize() {
+ _get_memsize_unit="${1:-b}"
+ _get_memsize_unit_spec="b mb kb gb"
+ if ! validate_enum "$_get_memsize_unit" "$_get_memsize_unit_spec"; then
+ echo "[ERROR] Expected one of: '$_get_memsize_unit_spec'. Got '$_get_memsize_unit'." >&2
+ return 1
+ fi
+
+ _get_memsize_os="$(uname -s)"
+ if [ "$_get_memsize_os" = 'Darwin' ]; then
+ _get_memsize_mem=$(sysctl -n hw.memsize)
+ else
+ return 1
+ fi
+
+ case "$_get_memsize_unit" in
+ b) _get_memsize_divisor=1 ;;
+ kb) _get_memsize_divisor=1024 ;;
+ mb) _get_memsize_divisor=1048576 ;;
+ gb) _get_memsize_divisor=1073741824 ;;
+ esac
+
+ echo $((_get_memsize_mem / _get_memsize_divisor))
+}
+
+validate_enum() {
+ _validate_enum_val=$1
+ _validate_enum_spec=$2
+
+ set -f # Temporarily disable pathname expansion e.g. foo*
+ # See `set` in `man bash`:
+ # > Any arguments remaining after option
+ # > processing are treated as values for the positional parameters and
+ # > are assigned, in order, to $1, $2, ..., $n.
+ # shellcheck disable=SC2086
+ set -- $_validate_enum_spec
+ set +f
+
+ for item in "$@"; do
+ [ "$_validate_enum_val" = "$item" ] && return 0
+ done
+ return 1
+}
+
+require_readable_file() {
+ if [ ! -f "$1" ] || [ ! -r "$1" ]; then
+ echo "File not found or not readable: $1" >&2
+ exit 1
+ fi
+}
diff --git a/openbsd-image/Makefile b/openbsd-image/Makefile
deleted file mode 100644
index 0c3ae00..0000000
--- a/openbsd-image/Makefile
+++ /dev/null
@@ -1,73 +0,0 @@
-# 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/disk-layouts/cgit.disklabel b/openbsd-image/disk-layouts/cgit.disklabel
new file mode 100644
index 0000000..b02585b
--- /dev/null
+++ b/openbsd-image/disk-layouts/cgit.disklabel
@@ -0,0 +1,10 @@
+/ 200M-1G 10%
+swap 1G-2G 0%
+/tmp 256M 0%
+/var 1G-3G 10%
+/var/log 200M-1G 0%
+/var/www 200M-5G 15%
+/usr 1G-4G 10%
+/usr/local 200M-4G 5%
+/home 100M-500M 0%
+/var/git 1G-* 50%
diff --git a/openbsd-image/locals.pkr.hcl b/openbsd-image/locals.pkr.hcl
index f88100f..a398219 100644
--- a/openbsd-image/locals.pkr.hcl
+++ b/openbsd-image/locals.pkr.hcl
@@ -1,4 +1,5 @@
locals {
version_short = replace(var.version, ".", "")
root_password = "temp"
+ http_port = 8900
}
diff --git a/openbsd-image/openbsd-amd64.pkr.hcl b/openbsd-image/openbsd-amd64.pkr.hcl
index 2fef98b..e19b208 100644
--- a/openbsd-image/openbsd-amd64.pkr.hcl
+++ b/openbsd-image/openbsd-amd64.pkr.hcl
@@ -17,18 +17,26 @@ source "qemu" "openbsd-amd64" {
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_key
- root_password = local.root_password
- mirror = var.mirror
- version = var.version
- version_short = local.version_short
- arch = var.arch
- })
- }
+ http_port_min = local.http_port
+ http_port_max = local.http_port
+
+ http_content = merge(
+ {
+ "/install.conf" = templatefile("${path.root}/templates/install.conf.pkrtpl.hcl", {
+ hostname = var.hostname
+ user = var.admin_user
+ ssh_key = var.ssh_key
+ root_password = local.root_password
+ mirror = var.mirror
+ version = var.version
+ version_short = local.version_short
+ arch = var.arch
+ disklabel_template = var.disklabel_template
+ http_port = local.http_port
+ })
+ },
+ var.disklabel_template != "" ? { "/disklabel" = var.disklabel_template } : {}
+ )
boot_command = [
"<wait>S<enter><wait>",
diff --git a/openbsd-image/openbsd-arm64.pkr.hcl b/openbsd-image/openbsd-arm64.pkr.hcl
index a304426..b8845ff 100644
--- a/openbsd-image/openbsd-arm64.pkr.hcl
+++ b/openbsd-image/openbsd-arm64.pkr.hcl
@@ -17,18 +17,26 @@ source "qemu" "openbsd-arm64" {
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_key
- root_password = local.root_password
- mirror = var.mirror
- version = var.version
- version_short = local.version_short
- arch = var.arch
- })
- }
+ http_port_min = local.http_port
+ http_port_max = local.http_port
+
+ http_content = merge(
+ {
+ "/install.conf" = templatefile("${path.root}/templates/install.conf.pkrtpl.hcl", {
+ hostname = var.hostname
+ user = var.admin_user
+ ssh_key = var.ssh_key
+ root_password = local.root_password
+ mirror = var.mirror
+ version = var.version
+ version_short = local.version_short
+ arch = var.arch
+ disklabel_template = var.disklabel_template
+ http_port = local.http_port
+ })
+ },
+ var.disklabel_template != "" ? { "/disklabel" = var.disklabel_template } : {}
+ )
boot_command = [
"<wait>S<enter><wait>",
diff --git a/openbsd-image/templates/install.conf.pkrtpl.hcl b/openbsd-image/templates/install.conf.pkrtpl.hcl
index 9df18ce..62a205a 100644
--- a/openbsd-image/templates/install.conf.pkrtpl.hcl
+++ b/openbsd-image/templates/install.conf.pkrtpl.hcl
@@ -12,5 +12,7 @@ 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
+Change the default console to com0 = no
+%{ if disklabel_template != "" ~}
+URL to autopartitioning template for disklabel = http://10.0.2.2:${http_port}/disklabel
+%{ endif ~}
diff --git a/openbsd-image/variables.pkr.hcl b/openbsd-image/variables.pkr.hcl
index e8340a9..0577253 100644
--- a/openbsd-image/variables.pkr.hcl
+++ b/openbsd-image/variables.pkr.hcl
@@ -59,3 +59,9 @@ variable "disk_size" {
description = "Size of the disk in GB while building the ISO"
default = 16
}
+
+variable "disklabel_template" {
+ type = string
+ description = "Specify a custom disklabel template. If empty (default), we'll use OpenBSD's default automatic layout."
+ default = ""
+}
diff --git a/run-openbsd.image.sh b/run-openbsd.image.sh
new file mode 100755
index 0000000..7f16897
--- /dev/null
+++ b/run-openbsd.image.sh
@@ -0,0 +1,98 @@
+#!/bin/sh
+set -o nounset
+set -o errexit
+
+_SCRIPT_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
+. "$_SCRIPT_DIR/lib.sh"
+
+CPU="$(get_ncpus)"
+MEM=$(($(get_memsize 'mb') / 2))
+DISPLAY=''
+TMP=''
+
+_ARCH_X86="x86_64"
+_ARCH_ARM="arm64"
+ARCH="$_ARCH_X86"
+
+_DISKFMT_RAW="raw"
+_DISKFMT_QCOW2="qcow2"
+DISKFMT="$_DISKFMT_RAW"
+
+main() {
+ parse_args "$@"
+
+ _ARCH_LABEL=amd64
+ [ "$ARCH" = "$_ARCH_ARM" ] && _ARCH_LABEL=arm64
+ if [ "$DISKFMT" = "$_DISKFMT_RAW" ]; then
+ DRIVE="file=$_SCRIPT_DIR/build/openbsd-7.9-$_ARCH_LABEL.raw,if=virtio,format=raw"
+ elif [ "$DISKFMT" = "$_DISKFMT_QCOW2" ]; then
+ DRIVE="file=$_SCRIPT_DIR/build/openbsd-7.9-$_ARCH_LABEL.qcow2,if=virtio,format=qcow2"
+ fi
+
+ if [ "$ARCH" = "$_ARCH_X86" ]; then
+ # shellcheck disable=SC2086
+ qemu-system-x86_64 \
+ -machine type=q35,accel=tcg \
+ -cpu max \
+ -m "$MEM" \
+ -smp "$CPU" \
+ ${TMP:+-snapshot} \
+ ${DISPLAY:+-display cocoa} \
+ -netdev user,id=net0,hostfwd=tcp::2222-:22 \
+ -device virtio-net,netdev=net0 \
+ -drive $DRIVE
+ fi
+}
+
+parse_args() {
+ while [ $# -gt 0 ]; do
+ case "$1" in
+ -h | --help)
+ usage
+ exit 0
+ ;;
+ --mem)
+ MEM="$2"
+ shift 2
+ ;;
+ --cpu)
+ CPU="$2"
+ shift 2
+ ;;
+ --arch)
+ ARCH="$2"
+ shift 2
+ ;;
+ --disk-fmt)
+ DISKFMT="$2"
+ shift 2
+ ;;
+ --display)
+ DISPLAY=1
+ shift 1
+ ;;
+ --tmp)
+ TMP=1
+ shift 1
+ ;;
+ *)
+ echo "Unknown option: $1"
+ shift
+ ;;
+ esac
+ done
+
+ _parse_args_arch_options="$_ARCH_X86 $_ARCH_ARM"
+ if ! validate_enum "$ARCH" "$_parse_args_arch_options"; then
+ echo "[ERROR] Expected one of: '$_parse_args_arch_options'. Got '$ARCH'." >&2
+ return 1
+ fi
+
+ _parse_args_diskfmt_options="$_DISKFMT_RAW $_DISKFMT_QCOW2"
+ if ! validate_enum "$DISKFMT" "$_parse_args_diskfmt_options"; then
+ echo "[ERROR] Expected one of: '$_parse_args_diskfmt_options'. Got '$DISKFMT'." >&2
+ return 1
+ fi
+}
+
+main "$@"