diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2026-11-28 19:47:53 -0800 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2026-11-28 19:47:53 -0800 |
| commit | 060a1067031e699cb77c36c47573a315fd782e75 (patch) | |
| tree | 355fc6afe2edb2a89c3bbe3abd72cc020d2135da | |
| parent | Add support for custom disk format (diff) | |
| download | site-server-images-060a1067031e699cb77c36c47573a315fd782e75.zip | |
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | README.md | 39 | ||||
| -rwxr-xr-x | build-openbsd-image.sh | 18 | ||||
| -rwxr-xr-x | run-openbsd.image.sh | 108 |
4 files changed, 133 insertions, 34 deletions
@@ -1,2 +1,2 @@ /openbsd-image/build -/openbsd-iamge/edk2-arm-vars.fd +/openbsd-image/edk2-arm-vars.fd @@ -8,8 +8,9 @@ This repository contains a template for: ## Building an OpenBSD Image -First, ensure you have QEMU installed: +First, ensure you have QEMU installed. +On macOS, you could do this via homebrew: ```sh brew install qemu ``` @@ -20,18 +21,44 @@ Then, you can build the box with the following: ./build-openbsd-image.sh ``` -If you have a custom disk layout you want, you can pass in a disklabel template. -e.g. +The image build may be customized. Use the `--help` flag to see the options. + +For example, if you have a custom disk layout you want, you can pass in +a disklabel template, such as: ```sh ./build-openbsd-image.sh --disklabel-template ./openbsd-image/disk-layouts/cgit.disklabel ``` +## Running an OpenBSD Image + +Once you've built your image, you can run the image in `openbsd-image/build` via the `run-openbsd-image.sh` script: + +```sh +./run-openbsd-image.sh --arch <your-built-image-arch> --disk-format <your-built-image-disk-format> --display +``` + +Once it is started, you can SSH in via `ssh -p 2222 admin@localhost`. + ## Creating a Hetzner Snapshot from your OpenBSD Image -Once you've built your image, you can now create a Hetzner snapshot for that -image. Hetzner doesn't support OpenBSD out of the box, so this allows you to -spin up new servers on demand using your snapshot of this OpenBSD image. +Once you've built your image, you may create a Hetzner snapshot for that image. +Hetzner doesn't support OpenBSD out of the box, so this allows you to spin up +new servers on demand using your snapshot of this OpenBSD image. + +If your plan is to build the image for creating a Hetzner snapshot, you'll want +to keep in mind the following when you run `./build-openbsd-image.sh`: + +- `--arch` must match the architecture of the instances you'll be creating. +- `--disk-format` must be `raw`. +- `--disk-size-gb` must be slightly smaller than your instance disk size. I use + a 2GB buffer e.g. for a 40GB disk size host, I build an image with a 38GB + disk size. The reason for this is because we need to transfer a compressed + version of this image over to the host in rescue mode and wipe it with our + image. The compressed image we transfer over it takes up between 1-2GB. +- If you have a custom disk layout you need for your use case, make sure you + pass a `--disklabel-template`. See `./openbsd-image/disk-layouts/cgit.disklabel` + as an example. To get started, you'll need to: diff --git a/build-openbsd-image.sh b/build-openbsd-image.sh index 7a0d634..ca140b9 100755 --- a/build-openbsd-image.sh +++ b/build-openbsd-image.sh @@ -8,12 +8,13 @@ set -o nounset set -o errexit -_SCRIPT_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd) -. "$_SCRIPT_DIR/lib.sh" +SCRIPT_DIR="$(realpath "$(dirname "$0")")" +. "$SCRIPT_DIR/lib.sh" VERSION=8.0 ARCH=amd64 -DISK_SIZE_GB=38 +DISK_SIZE_GB=64 +DISK_FORMAT=qcow2 SSH_KEY="$HOME/.ssh/id_rsa.pub" DISKLABEL_TEMPLATE="" @@ -21,11 +22,14 @@ export PACKER_LOG=1 main() ( parse_args "$@" + cd openbsd-image/ require_readable_file "$SSH_KEY" + if [ -n "$DISKLABEL_TEMPLATE" ]; then require_readable_file "$DISKLABEL_TEMPLATE" fi + if [ -d build/ ]; then printf 'Build directory exists. Must delete before re-building. Proceed? (y/N)' read -r answer @@ -40,6 +44,12 @@ main() ( ;; esac fi + + if [ "$ARCH" = "arm64" ]; then + rm -f "$SCRIPT_DIR/openbsd-image/edk2-arm-vars.fd" + cp /opt/homebrew/share/qemu/edk2-arm-vars.fd "$SCRIPT_DIR/openbsd-image/edk2-arm-vars.fd" + fi + packer init . packer build \ -only "qemu.openbsd-$ARCH" \ @@ -48,7 +58,7 @@ main() ( -var "version=$VERSION" \ -var "disk_format=$DISK_FORMAT" \ -var "ssh_key=$(cat "$SSH_KEY")" \ - -var "cpus=$(get_ncpus)" \ + -var "cpus=2" \ ${DISKLABEL_TEMPLATE:+-var "disklabel_template=$(cat "$DISKLABEL_TEMPLATE")"} \ . ) diff --git a/run-openbsd.image.sh b/run-openbsd.image.sh index 2b578c1..8226380 100755 --- a/run-openbsd.image.sh +++ b/run-openbsd.image.sh @@ -2,8 +2,8 @@ set -o nounset set -o errexit -_SCRIPT_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd) -. "$_SCRIPT_DIR/lib.sh" +SCRIPT_DIR="$(realpath "$(dirname "$0")")" +. "$SCRIPT_DIR/lib.sh" CPU="$(get_ncpus)" MEM=$(($(get_memsize 'mb') / 2)) @@ -12,23 +12,22 @@ TMP='' _ARCH_X86="x86_64" _ARCH_ARM="arm64" -ARCH="$_ARCH_X86" +ARCH="$_ARCH_ARM" -_DISKFMT_RAW="raw" -_DISKFMT_QCOW2="qcow2" -DISKFMT="$_DISKFMT_RAW" +_DISK_FORMAT_RAW="raw" +_DISK_FORMAT_QCOW2="qcow2" +DISK_FORMAT="$_DISK_FORMAT_QCOW2" + +VERSION="8.0" +EDK2_DIR="/opt/homebrew/share/qemu" +SSH_PORT="2222" +WORKSPACE_DIR="$SCRIPT_DIR/openbsd-image" +BUILD_DIR="$WORKSPACE_DIR/build" +EFIVARS_RUN="$BUILD_DIR/efivars.fd" +EDK2_CODE="$EDK2_DIR/edk2-aarch64-code.fd" main() { parse_args "$@" - - _ARCH_LABEL=amd64 - [ "$ARCH" = "$_ARCH_ARM" ] && _ARCH_LABEL=arm64 - if [ "$DISKFMT" = "$_DISKFMT_RAW" ]; then - DRIVE="file=$_SCRIPT_DIR/build/openbsd-8.0-$_ARCH_LABEL.raw,if=virtio,format=raw" - elif [ "$DISKFMT" = "$_DISKFMT_QCOW2" ]; then - DRIVE="file=$_SCRIPT_DIR/build/openbsd-8.0-$_ARCH_LABEL.qcow2,if=virtio,format=qcow2" - fi - if [ "$ARCH" = "$_ARCH_X86" ]; then # shellcheck disable=SC2086 qemu-system-x86_64 \ @@ -37,10 +36,25 @@ main() { -m "$MEM" \ -smp "$CPU" \ ${TMP:+-snapshot} \ - ${DISPLAY:+-display cocoa} \ - -netdev user,id=net0,hostfwd=tcp::2222-:22 \ + -netdev user,id=net0,hostfwd=tcp::${SSH_PORT}-:22 \ -device virtio-net,netdev=net0 \ - -drive $DRIVE + $(get_display_flag) \ + $(get_drive_flags) + elif [ "$ARCH" = "$_ARCH_ARM" ]; then + # shellcheck disable=SC2086 + qemu-system-aarch64 \ + -machine type=virt,accel=hvf \ + -cpu host \ + -m "$MEM" \ + -smp "$CPU" \ + ${TMP:+-snapshot} \ + -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 \ + $(get_display_flag) \ + $(get_drive_flags) fi } @@ -63,8 +77,8 @@ parse_args() { ARCH="$2" shift 2 ;; - --disk-fmt) - DISKFMT="$2" + --disk-format) + DISK_FORMAT="$2" shift 2 ;; --display) @@ -88,11 +102,59 @@ parse_args() { 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 + _parse_args_disk_format_options="$_DISK_FORMAT_RAW $_DISK_FORMAT_QCOW2" + if ! validate_enum "$DISK_FORMAT" "$_parse_args_disk_format_options"; then + echo "[ERROR] Expected one of: '$_parse_args_disk_format_options'. Got '$DISK_FORMAT'." >&2 return 1 fi } +usage() { + cat <<EOF +Usage: $(basename "$0") [OPTIONS] + +Run a built OpenBSD image with QEMU. + +Options: + --mem MEMORY Memory in MB to allocate to the VM (default: $MEM) + --cpu NUM Num of CPUs to allocate to the VM (default: $CPU) + --arch ARCH Target architecture (must match built image): amd64 or arm64 (default: $ARCH) + --disk-format FORMAT Format of the VM image (default: $DISK_FORMAT) + --display Show a GUI display + --tmp Use a temporary disk that is wiped when the VM is stopped + -h, --help Show this help message +EOF +} + +get_display_flag() { + if [ -n "$DISPLAY" ]; then + echo "-display cocoa" + else + echo "-display none" + fi +} + +get_drive_flags() ( + base="$BUILD_DIR/openbsd-$VERSION-$(get_arch_label)" + + case "$DISK_FORMAT" in + "$_DISK_FORMAT_RAW") + echo "-drive file=$base.raw,if=virtio,format=raw" + ;; + "$_DISK_FORMAT_QCOW2") + echo "-drive file=$base.qcow2,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" + ;; + esac +) + +get_arch_label() { + if [ "$ARCH" = "$_ARCH_ARM" ]; then + echo "aarch64" + else + echo "amd64" + fi +} + main "$@" |