From de40b1e69eaabac3ca01774fa3a4e7abc463ac7c Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 3 Oct 2026 08:52:49 -0700 Subject: Customize the disk layout via disklabel templates --- .shellcheckrc | 8 ++ build-hcloud-snapshot.sh | 26 ++++++- build-openbsd-image.sh | 44 ++++++++--- hcloud-snapshot/snapshot.pkr.hcl | 2 +- lib.sh | 81 ++++++++++++++++++++ openbsd-image/Makefile | 73 ------------------ openbsd-image/disk-layouts/cgit.disklabel | 10 +++ openbsd-image/locals.pkr.hcl | 1 + openbsd-image/openbsd-amd64.pkr.hcl | 32 +++++--- openbsd-image/openbsd-arm64.pkr.hcl | 32 +++++--- openbsd-image/templates/install.conf.pkrtpl.hcl | 6 +- openbsd-image/variables.pkr.hcl | 6 ++ run-openbsd.image.sh | 98 +++++++++++++++++++++++++ 13 files changed, 306 insertions(+), 113 deletions(-) create mode 100644 .shellcheckrc create mode 100644 lib.sh delete mode 100644 openbsd-image/Makefile create mode 100644 openbsd-image/disk-layouts/cgit.disklabel create mode 100755 run-openbsd.image.sh 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 <&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 = [ "S", 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 = [ "S", 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 "$@" -- cgit v1.2.3