Repository navigation
Expand file tree
/
Copy pathbuild.pkr.hcl
More file actions
347 lines (296 loc) · 13.2 KB
/
Copy pathbuild.pkr.hcl
File metadata and controls
347 lines (296 loc) · 13.2 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# Build a custom SF Compute VM image with Packer.
#
# Two starting points, one build. Pick with -only:
#
# packer build -only='qemu.sfc_base' . # from an SF Compute base image
# packer build -only='qemu.ubuntu' . # from a stock Ubuntu cloud image
#
# Both produce a UEFI amd64 raw image under output/, which is what
# `sf images upload` accepts. Edit customize.sh -- that is the only file
# you need to touch.
packer {
# `packer init` and external required_plugins both arrived in 1.7. Ubuntu
# 22.04 still ships Packer 1.6.6, which would fail on `packer init` with an
# unknown-command error that says nothing about why; this turns that into a
# version message. See the README for where to get a current Packer.
required_version = ">= 1.9.0"
required_plugins {
qemu = {
source = "github.com/hashicorp/qemu"
# efi_boot / efi_firmware_* are used below and are not in 1.0.x's
# earliest releases.
version = ">= 1.0.10"
}
}
}
# ---------------------------------------------------------------------------
# Variables
# ---------------------------------------------------------------------------
variable "base_image_tag" {
type = string
default = ""
description = <<-EOT
Pin the build to one SF Compute base image release (qemu.sfc_base only).
Empty, the default, resolves whatever is current at build time. The
build prints the release it picked, so check the log if you need to
know which one you got.
Set this to a tag for a repeatable starting point. It does not make
the build bit-for-bit reproducible -- customize.sh still installs
whatever the Ubuntu archive has that day -- but it fixes the base
image, so a rebuild next month starts from the same driver, CUDA and
kernel. Tags look like v20260930.063326. latest.json carries the
current one and each build's output/sfc_base/manifest.json records
the one it used; there is no list of older tags, and releases
published before the index existed cannot be pinned.
EOT
}
variable "base_image_name" {
type = string
default = ""
description = <<-EOT
Which image within the release (qemu.sfc_base only).
Empty, the default, takes the image with the newest CUDA for
ubuntu_codename. A release carries one image per CUDA minor per
Ubuntu LTS and the set moves as NVIDIA and Canonical ship, so a
name written here goes stale; a rule does not.
Set this to build on a specific CUDA instead. Names look like
ubuntu-<lts>-cuda-<major.minor>, and
`curl -fsSL https://tiny-llama.sfcc.xyz/latest.json | jq -r '.images[].name'`
lists the ones in the current release.
EOT
}
variable "ubuntu_codename" {
type = string
default = "noble"
description = "Which Ubuntu to build on, for either starting point. noble = 24.04 LTS, resolute = 26.04 LTS. For qemu.ubuntu this names the cloud image; for qemu.sfc_base it picks the LTS when base_image_name is empty."
}
variable "nvidia_driver_branch" {
type = string
default = "590"
description = <<-EOT
NVIDIA server driver branch to install (qemu.ubuntu only -- the SF
Compute base images already ship a driver).
Must exist in the Ubuntu archive for your codename:
nvidia-headless-<branch>-server in <codename>-updates/restricted and
nvidia-fabricmanager-<branch> in <codename>-updates/multiverse. 590
is present for both noble and resolute.
Ubuntu may resolve the branch you name forward: as of this writing
the 590 packages are transitional and depend on their 595
counterparts, so a build asking for 590 installs 595. That is safe
rather than a mismatch, because the headless, utils and
fabric-manager packages are transitional together and land on the
same branch -- fabric manager refuses to start against a driver
from a different branch, so they have to move as a set. Check what
you actually got with `dpkg -l 'nvidia-headless-*'`.
EOT
}
variable "efi_firmware_code" {
type = string
default = ""
description = <<-EOT
Path to the OVMF (UEFI) firmware code file. Empty auto-detects.
Distributions disagree about the filename: Ubuntu 24.04 ships only
the 4MB variant (OVMF_CODE_4M.fd), 22.04 ships OVMF_CODE.fd, and
Homebrew's QEMU calls it edk2-x86_64-code.fd. Set this if
auto-detection doesn't find yours.
EOT
}
variable "efi_firmware_vars" {
type = string
default = ""
description = "Path to the OVMF UEFI variables file. Empty auto-detects. Must match the code file's variant -- pairing a 4M code file with a non-4M vars file does not boot."
}
variable "accelerator" {
type = string
default = "tcg"
description = "Set to kvm on Linux for a dramatically faster build. tcg (software emulation) is the portable default and is what you want on macOS."
}
variable "disk_size" {
type = string
default = "20G"
description = <<-EOT
Virtual disk size of the produced image.
QEMU can grow a disk but never shrink it, so this must be at least
as large as the source image: SF Compute base images are 12G, and
Ubuntu cloud images are smaller. The platform caps uploads at 75
GiB. The raw file is sparse, so the apparent size is the ceiling,
not what you actually store -- but note that uploaders reading
sequentially transfer the apparent size.
EOT
}
# The release index lists each image's sha256 beside its URL, so the digest
# below comes from the same document as the download and the two cannot
# disagree.
data "http" "sfc_release" {
url = var.base_image_tag == "" ? "https://tiny-llama.sfcc.xyz/latest.json" : "https://tiny-llama.sfcc.xyz/${var.base_image_tag}/manifest.json"
}
locals {
sfc_release = jsondecode(data.http.sfc_release.body)
# The images this build may pick from: the one named, or every image in
# the release for ubuntu_codename. Empty when the name or codename is not
# in the release, which fails the build here rather than downloading
# something else.
# The variant filter keeps a future non-base build published into the
# same release (a "-minimal", say) from tying with the base image on
# CUDA and winning on list order.
sfc_candidates = var.base_image_name != "" ? [
for i in local.sfc_release.images : i if i.name == var.base_image_name
] : [
for i in local.sfc_release.images : i
if i.ubuntu_codename == var.ubuntu_codename && try(i.variant, "base") == "base"
]
# Newest CUDA wins. Ranked numerically rather than by string, so a
# hypothetical 13.10 outranks 13.9 instead of sorting between 13.1 and
# 13.2. A version with no minor ranks as .0.
sfc_cuda_rank = [
for i in local.sfc_candidates :
parseint(split(".", i.cuda_version)[0], 10) * 1000 + try(parseint(split(".", i.cuda_version)[1], 10), 0)
]
sfc_image = local.sfc_candidates[index(local.sfc_cuda_rank, max(local.sfc_cuda_rank...))]
ubuntu_base_url = "https://cloud-images.ubuntu.com/${var.ubuntu_codename}/current"
# Probe for OVMF, newest layout first. Packer's own default is /usr/share/OVMF/
# OVMF_VARS.fd, which does not exist on Ubuntu 24.04 -- the build gets
# as far as booting and then dies on a missing file, so we resolve it
# here instead. If nothing matches, the fallback is the common Linux
# path; set -var efi_firmware_code=... -var efi_firmware_vars=... to point
# at yours (both must be the same variant -- a 4M code file needs 4M vars).
ovmf_code = var.efi_firmware_code != "" ? var.efi_firmware_code : try([
for p in [
"/usr/share/OVMF/OVMF_CODE_4M.fd",
"/usr/share/OVMF/OVMF_CODE.fd",
"/opt/homebrew/share/qemu/edk2-x86_64-code.fd", # Homebrew, Apple Silicon
"/usr/local/share/qemu/edk2-x86_64-code.fd", # Homebrew, Intel
] : p if fileexists(p)
][0], "/usr/share/OVMF/OVMF_CODE_4M.fd")
ovmf_vars = var.efi_firmware_vars != "" ? var.efi_firmware_vars : try([
for p in [
"/usr/share/OVMF/OVMF_VARS_4M.fd",
"/usr/share/OVMF/OVMF_VARS.fd",
"/opt/homebrew/share/qemu/edk2-i386-vars.fd", # Homebrew, Apple Silicon
"/usr/local/share/qemu/edk2-i386-vars.fd", # Homebrew, Intel
] : p if fileexists(p)
][0], "/usr/share/OVMF/OVMF_VARS_4M.fd")
# systemd-random-seed.service saves the seed to
# /var/lib/systemd/random-seed in its ExecStop, which runs during
# shutdown -- i.e. after every provisioner, so deleting the file in
# finalize.sh does not stick. Stopping the unit first runs that save
# once; the file we then delete stays deleted, because an inactive
# unit is not stopped again on the way down. Neither the Ubuntu cloud
# images nor the SF Compute base images ship this file, and an image
# that does gives every instance booted from it the same seed.
shutdown_command = "systemctl stop systemd-random-seed.service; rm -f /var/lib/systemd/random-seed; shutdown -P now"
}
# ---------------------------------------------------------------------------
# Sources
#
# The two blocks are deliberately near-identical: the only real
# differences are where the disk comes from and what it is called. Both
# boot the downloaded cloud image directly (disk_image = true) and hand
# it a cloud-init seed on a CD labelled "cidata", which is what opens
# the build-time SSH access the provisioners use.
# ---------------------------------------------------------------------------
source "qemu" "sfc_base" {
# The qcow2 rather than the .raw published alongside it: same image,
# ~3G instead of ~12G over the wire. format = "raw" below converts on
# the way out, so what lands in output/ is still raw.
iso_url = local.sfc_image.qcow2.url
iso_checksum = "sha256:${local.sfc_image.qcow2.sha256}"
# A fixed name, since the base image is resolved at build time: the
# upload command in the README stays copy-pasteable whatever the
# release picked. manifest.json beside it records which image that was.
vm_name = "sfc-base-custom.raw"
output_directory = "output/sfc_base"
disk_image = true
format = "raw"
disk_size = var.disk_size
accelerator = var.accelerator
# UEFI: the platform requires UEFI images, and booting the build VM
# the same way the instance will is the point of the exercise.
efi_boot = true
efi_firmware_code = local.ovmf_code
efi_firmware_vars = local.ovmf_vars
headless = true
disable_vnc = true
memory = 2048
cpus = 2
cd_files = ["./cloud-init/meta-data", "./cloud-init/user-data"]
cd_label = "cidata"
# Belt and braces for cloud-init's datasource detection: the CD label
# alone is usually enough, but the SMBIOS hint makes NoCloud
# unambiguous on a freshly generalized image.
qemuargs = [["-smbios", "type=1,serial=ds=nocloud"]]
ssh_username = "root"
ssh_password = "packer"
ssh_timeout = "20m"
shutdown_command = local.shutdown_command
}
source "qemu" "ubuntu" {
iso_url = "${local.ubuntu_base_url}/${var.ubuntu_codename}-server-cloudimg-amd64.img"
iso_checksum = "file:${local.ubuntu_base_url}/SHA256SUMS"
vm_name = "ubuntu-${var.ubuntu_codename}-custom.raw"
output_directory = "output/ubuntu"
disk_image = true
format = "raw"
disk_size = var.disk_size
accelerator = var.accelerator
# UEFI: the platform requires UEFI images, and booting the build VM
# the same way the instance will is the point of the exercise.
efi_boot = true
efi_firmware_code = local.ovmf_code
efi_firmware_vars = local.ovmf_vars
headless = true
disable_vnc = true
memory = 2048
cpus = 2
cd_files = ["./cloud-init/meta-data", "./cloud-init/user-data"]
cd_label = "cidata"
qemuargs = [["-smbios", "type=1,serial=ds=nocloud"]]
ssh_username = "root"
ssh_password = "packer"
ssh_timeout = "20m"
shutdown_command = local.shutdown_command
}
# ---------------------------------------------------------------------------
# Build
# ---------------------------------------------------------------------------
build {
sources = [
"source.qemu.sfc_base",
"source.qemu.ubuntu",
]
# Only the stock-Ubuntu path needs this: an SF Compute base image
# already ships the driver, fabric manager, CUDA and the DOCA-OFED
# stack that InfiniBand requires.
provisioner "shell" {
only = ["qemu.ubuntu"]
script = "install-nvidia.sh"
environment_vars = [
"DEBIAN_FRONTEND=noninteractive",
"NVIDIA_DRIVER_BRANCH=${var.nvidia_driver_branch}",
]
}
# Your changes. This is the file to edit.
provisioner "shell" {
script = "customize.sh"
environment_vars = ["DEBIAN_FRONTEND=noninteractive"]
}
# Must stay last: it removes the build-time SSH access and wipes
# per-machine identity, so nothing can run over SSH after it.
provisioner "shell" {
script = "finalize.sh"
environment_vars = ["DEBIAN_FRONTEND=noninteractive"]
}
# Otherwise the resolved tag and image survive only in terminal output.
# These are what you pass to base_image_tag and base_image_name to
# rebuild on the same base.
post-processor "manifest" {
only = ["qemu.sfc_base"]
output = "output/sfc_base/manifest.json"
custom_data = {
base_image_tag = local.sfc_release.tag
base_image_name = local.sfc_image.name
base_image_url = local.sfc_image.qcow2.url
base_image_sha256 = local.sfc_image.qcow2.sha256
}
}
}