aboutsummaryrefslogtreecommitdiffstats
path: root/openbsd.pkr.hcl
blob: bba0327086ffb5b38bd2dc9df46e2f5e8b42bf8f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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 "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
  validation {
    condition     = contains(["amd64", "arm64"], var.arch)
    error_message = "The arch must be one of: amd64, arm64."
  }
}

variable "admin_user" {
  type        = string
  description = "Username of the admin user on the VM"
  default     = "admin"
}

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     = 4096
}

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-amd64" {
  vm_name              = "openbsd-${var.version}-amd64.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-x86_64"
  machine_type         = "q35"
  cpu_model            = "max"
  accelerator          = "tcg"
  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          = "30m"

  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"
  headless            = false
  qemuargs = [["-display", "cocoa"]]
}

source "qemu" "openbsd-arm64" {
  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-amd64", "source.qemu.openbsd-arm64"]

  provisioner "file" {
    source      = "configs/doas.conf"
    destination = "/etc/doas.conf"
  }

  provisioner "shell" {
    script = "scripts/setup.sh"
    environment_vars = ["ADMIN_USER=${var.admin_user}"]
  }

  provisioner "shell" {
    script = "scripts/harden-root.sh"
  }
}