-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathZFS-root_local.pkr.hcl
157 lines (135 loc) · 4.82 KB
/
ZFS-root_local.pkr.hcl
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
packer {
required_plugins {
qemu = {
version = ">= 1.1.0"
source = "github.com/hashicorp/qemu"
}
}
}
# Show the VM console gui ?
# Set to true when running under CI/CD pipelines
# Set to false when running manually/locally
variable "headless" {
type = bool
default = true
}
# Output dir prefix
# Set to "" for running in CI/CD pipelines
# Set to some location for running locally
# Be SURE to include trailing slash
variable "output_prefix" {
type = string
default = ""
}
variable "ubuntu_version" {
type = string
default = "24.04.1"
}
# Used to select the remote dir for the live iso image
# For https://releases.ubuntu.com it will be the actual version
# For local iso image it can be any additional dir to add to ubuntu_live_iso_src
# Note: Make SURE this has a trailing slash when referencing an https://releases.ubuntu.com location
variable "ubuntu_version_dir" {
type = string
default = "24.04.1/"
}
# Source for ubuntu live iso image
# Can be pulled from Ubuntu, or reference a local copy in some local dir
# For example file:///home/myuser/ISOs
variable "ubuntu_live_iso_src" {
type = string
default = "https://releases.ubuntu.com"
}
locals {
output_dir = "packer_zfsroot_${local.timestamp}"
timestamp = formatdate("YYYY-MM-DD-hhmm", timestamp())
ubuntu_live_iso = "${var.ubuntu_live_iso_src}/${var.ubuntu_version_dir}ubuntu-${var.ubuntu_version}-live-server-amd64.iso"
}
source "qemu" "ubuntu" {
vm_name = "packer-zfsroot-${local.timestamp}"
iso_url = "${local.ubuntu_live_iso}"
iso_checksum = "file:https://releases.ubuntu.com/${var.ubuntu_version}/SHA256SUMS"
# iso_checksum = "10f19c5b2b8d6db711582e0e27f5116296c34fe4b313ba45f9b201a5007056cb" # 22.04.1
cpus = 2
memory = 2048
accelerator = "kvm"
qemuargs = [
["-enable-kvm"],
["-machine", "pc"],
["-cpu", "host,+nx,+pae"]
]
efi_firmware_code = "/usr/share/OVMF/OVMF_CODE.fd"
efi_firmware_vars = "/usr/share/OVMF/OVMF_VARS.fd"
efi_boot = true
# NOTE: output_prefix MUST have trailing slash in var definition
output_directory = "${var.output_prefix}${local.output_dir}"
# virtio-scsi needed to populate /dev/disk/by-id
# virtio alone does not populate that
disk_interface = "virtio-scsi"
disk_size = 10000
format = "qcow2"
http_directory = "./"
net_device = "virtio-net"
ssh_username = "ubuntu-server"
ssh_password = "packer"
ssh_wait_timeout = "30m"
shutdown_command = "sudo poweroff -f" # force to avoid "remove installation media" msg
headless = "${var.headless}" # NOTE: set this to true when using in CI Pipelines
boot_wait = "10s"
# Trigger the "Try Ubuntu" right away, then wait 60secs to get to installer
# ctrl-z the installer into background to get shell, then
# need to set a password so packer can ssh in to provision.
# Could also curl ZFS-root.sh/.conf then run script right here
boot_command = [
"<wait><enter><wait60>",
"<leftCtrlOn>z<leftCtrlOff>",
"<wait><enter><wait>",
"ls -la /dev/vd* /dev/disk/by-id<enter><wait>",
"echo ubuntu-server:packer | chpasswd<enter>"
]
}
build {
sources = ["source.qemu.ubuntu"]
# Get the ZFS-root.sh script and packer config into place
provisioner "file" {
source = "ZFS-root.sh"
destination = "/tmp/ZFS-root.sh"
}
provisioner "file" {
source = "ZFS-root-packerci.conf"
destination = "/tmp/ZFS-root-packerci.conf"
}
# Actually run the ZFS-root.sh script to build the system as root
# Put the debug output somewhere that ubuntu-server user can reach
provisioner "shell" {
execute_command = "echo 'packer' | sudo -S sh -c '{{ .Vars }} {{ .Path }}'"
inline = [
"cd /tmp",
"./ZFS-root.sh packerci",
"mv /root/ZFS-setup.log /tmp/ZFS-setup-packerci.log"
]
}
# Push the debug output back to host machine
provisioner "file" {
source = "/tmp/ZFS-setup-packerci.log"
destination = "${var.output_prefix}${local.output_dir}/ZFS-setup-packerci.log"
direction = "download"
}
post-processor "manifest" {
output = "${var.output_prefix}${local.output_dir}/manifest.json"
strip_path = true
}
post-processor "artifice" {
files = [
"${var.output_prefix}${local.output_dir}/ZFS-setup-packerci.log",
"${var.output_prefix}${local.output_dir}/manifest.json",
"${var.output_prefix}${local.output_dir}/packer-zfsroot-${local.timestamp}.qcow2"
]
}
# Finally Generate a Checksum (SHA256) which can be used for further stages in the `output` directory
post-processor "checksum" {
checksum_types = [ "sha256" ]
output = "${var.output_prefix}${local.output_dir}/packer-zfsroot-${local.timestamp}.qcow2.{{.ChecksumType}}"
keep_input_artifact = true
}
}