aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--LICENSE15
-rw-r--r--README.md74
-rw-r--r--configs/doas.conf1
-rw-r--r--openbsd.pkr.hcl147
-rw-r--r--openbsd79-arm64-macos.pkvars.hcl9
-rw-r--r--scripts/harden-root.sh12
-rw-r--r--templates/install.conf.pkrtpl.hcl16
8 files changed, 276 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..cd31339
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/build
+/edk2-arm-vars.fd
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..885612d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,15 @@
+ISC License
+
+Copyright (c) 2026 Eugene Triguba
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c9e85ab
--- /dev/null
+++ b/README.md
@@ -0,0 +1,74 @@
+# OpenBSD Packer Template
+
+This repository contains a template for building OpenBSD boxes for QEMU using Packer.
+
+It may work for other configurations with additional variables added and some
+variables changed. However, I only use it for running OpenBSD on an ARM Mac, so
+that is the only configuration that is actively tested.
+
+## Installation
+
+You can also build the box yourself with the following:
+
+```sh
+brew install qemu
+cp /opt/homebrew/share/qemu/edk2-arm-vars.fd ./edk2-arm-vars.fd
+export PACKER_LOG=1
+packer init
+packer build -var-file=openbsd79-arm64-macos.pkvars.hcl
+```
+
+## Usage
+
+Once your image is built, you can run it.
+
+First, the efivars are stateful. So, make a copy of the ones you used during the
+build to use for your particular run.
+
+```sh
+cp build/efivars.fd build/efivars-run.fd
+```
+
+Then, run the following command:
+
+```sh
+qemu-system-aarch64 \
+ -machine type=virt,accel=hvf \
+ -cpu host \
+ -m 4096M \
+ -smp 1 \
+ -nographic \
+ -device qemu-xhci,id=xhci \
+ -device usb-kbd,bus=xhci.0 \
+ -device virtio-gpu-pci \
+ -netdev user,id=net0,hostfwd=tcp::2222-:22 \
+ -device virtio-net,netdev=net0 \
+ -drive file=build/openbsd-7.9-aarch64.qcow2,if=virtio,cache=writeback,discard=ignore,format=qcow2 \
+ -drive file=/opt/homebrew/share/qemu/edk2-aarch64-code.fd,if=pflash,unit=0,format=raw,readonly=on \
+ -drive file=build/efivars-run.fd,if=pflash,unit=1,format=raw
+```
+
+If you want a display (rather than a headless VM over SSH), replace `-nographic`
+with `-display cocoa`.
+
+Once the system is up, you can SSH in using your admin user and private key:
+
+```sh
+> ssh -p 2222 eugene@127.0.0.1
+The authenticity of host '[127.0.0.1]:2222 ([127.0.0.1]:2222)' can't be established.
+ED25519 key fingerprint is: SHA256:GnVug9cxjtROdVCbDsaxG7Co/E1j060B9U+bmY2lqOk
+This key is not known by any other names.
+Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
+Warning: Permanently added '[127.0.0.1]:2222' (ED25519) to the list of known hosts.
+OpenBSD 7.9 (GENERIC) #211: Wed May 6 17:30:57 MDT 2026
+
+Welcome to OpenBSD: The proactively secure Unix-like operating system.
+
+Please use the sendbug(1) utility to report bugs in the system.
+Before reporting a bug, please try to reproduce it with the latest
+version of the code. With bug reports, please try to ensure that
+enough information to reproduce the problem is enclosed, and if a
+known fix for it exists, include that as well.
+
+openbsd-vm$
+```
diff --git a/configs/doas.conf b/configs/doas.conf
new file mode 100644
index 0000000..d2423e7
--- /dev/null
+++ b/configs/doas.conf
@@ -0,0 +1 @@
+permit nopass :wheel
diff --git a/openbsd.pkr.hcl b/openbsd.pkr.hcl
new file mode 100644
index 0000000..9bf7dc0
--- /dev/null
+++ b/openbsd.pkr.hcl
@@ -0,0 +1,147 @@
+packer {
+ required_plugins {
+ qemu = { source = "github.com/hashicorp/qemu", version = ">= 1.1.0" }
+ }
+}
+
+variable "version" {
+ type = string
+ 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 "admin_user" {
+ type = string
+ description = "Username of the admin user on the VM"
+}
+
+variable "ssh_public_key" {
+ type = string
+ description = "Public SSH key to associate with the admin user for login"
+}
+
+variable "ssh_private_key_file" {
+ type = string
+ description = "Private SSH key to associate with the admin user for login"
+}
+
+variable "arch" {
+ type = string
+ default = "arm64"
+ validation {
+ condition = contains(["arm64"], var.arch)
+ error_message = "The arch must be one of: arm64."
+ }
+}
+
+variable "accelerator" {
+ type = string
+ default = "hvf"
+ description = <<-EOT
+ The accelerator type to use when running the VM. See the following for more information:
+ https://developer.hashicorp.com/packer/integrations/hashicorp/qemu/latest/components/builder/qemu#optional
+ EOT
+ validation {
+ condition = contains(["none", "kvm", "tcg", "hax", "hvf", "whpx", "xen"], var.accelerator)
+ error_message = "Must be one of: 'none', 'kvm', 'tcg', 'hax', 'hvf', 'whpx', 'xen'."
+ }
+}
+
+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"
+ default = 2048
+}
+
+variable "disk_size" {
+ type = number
+ description = "Size of the disk in GB"
+ default = 16
+}
+
+locals {
+ version_short = replace(var.version, ".", "")
+ root_password = "temp"
+}
+
+source "qemu" "openbsd" {
+ 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"
+}
+
+build {
+ sources = ["source.qemu.openbsd"]
+
+ provisioner "file" {
+ source = "configs/doas.conf"
+ destination = "/etc/doas.conf"
+ }
+
+ provisioner "shell" {
+ script = "scripts/harden-root.sh"
+ }
+}
diff --git a/openbsd79-arm64-macos.pkvars.hcl b/openbsd79-arm64-macos.pkvars.hcl
new file mode 100644
index 0000000..c8df031
--- /dev/null
+++ b/openbsd79-arm64-macos.pkvars.hcl
@@ -0,0 +1,9 @@
+version = "7.9"
+arch = "arm64"
+memory = 4096
+disk_size = 16
+admin_user = "eugene"
+ssh_public_key = <<-EOF
+ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC31SiCeUZSJN3CueH6/+7UQvxwxkT/QkaDK6ZGdnkdpCWxRVp+0CTO4WG3u0PVEVd9DvSd81CEqrFaKxtKyiSh+4Va0A/mqY27tX7NZzEQLM/zLQrdESmaHY6EyQM0wlCtBKfNzuQqCa9QEw4TwkyMLOJzyI+bRsfeSHNCfxqnqGIMKKFiaF0ygakvQz57nWuKLOgqWsWkzFTuJKGI0OhefFBcZ4VzoCLV8i1zbB2AebhsmbyMza9OUF3px44atSM3o33Jto3gy7LTWtxU83NZU+wLEqY6IvcWx9BbSQZxAwEwXxUFmnzcX+FTkTiOs/Q2nXiLl2ggsGJffAq9Z1SGMCNGxT+Syyta5UtfBVVQdn6xtUxpppHwrdGC+TR8C5F9U12gsZRzvKkz0SSt5M4WHG2gna5rDgTisDbrEAsrqpmUqY9iSBzgAyk5xOi6fdI+r67gpjRSs+PNvDle3gImfIkPnEQg4eWJ54iKjfH+yd8vBk25T/ahu3J9rGgxp18= eugene@laptop
+EOF
+ssh_private_key_file = "~/.ssh/id_rsa"
diff --git a/scripts/harden-root.sh b/scripts/harden-root.sh
new file mode 100644
index 0000000..c8e267a
--- /dev/null
+++ b/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/templates/install.conf.pkrtpl.hcl b/templates/install.conf.pkrtpl.hcl
new file mode 100644
index 0000000..9df18ce
--- /dev/null
+++ b/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