diff options
Diffstat (limited to 'openbsd.pkr.hcl')
| -rw-r--r-- | openbsd.pkr.hcl | 147 |
1 files changed, 147 insertions, 0 deletions
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" + } +} |