-
Notifications
You must be signed in to change notification settings - Fork 2
/
vyos-image2-1.3.pkr.hcl
270 lines (222 loc) · 7.25 KB
/
vyos-image2-1.3.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
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
packer {
required_plugins {
qemu = {
version = "~> 1"
source = "github.com/hashicorp/qemu"
}
}
}
# dont edit those vars below, customize in local.auto.pkrvars.hcl using local.example.pkrvars.hcl
variable "vm_name" {
default = "vyos-image-1.3.6"
}
variable "numvcpus" {
default = "4"
}
variable "memsize" {
default = "2048"
}
variable "disk_size" {
default = "1024"
}
variable "iso_checksum" {
default = "file:iso/SHA256SUM"
}
variable "ssh_username" {
default = "vyos"
}
variable "ssh_password" {
default = "vyos"
}
variable "boot_wait" {
default = "5s"
}
# - qemu: will build qcow2 image
# - none: not supported
variable "platform" {
type = string
default = "qemu"
}
# cloud-init values:
# debian - will install/replace cloud-init packages
# vyos - will keep cloud-init packages from vyos
variable "cloud_init" {
type = string
default = "vyos"
}
# equuleus: debian 11 (branch 1.3.*)
# sagitta: debian 12 (branch 1.4.*)
# circinus: debian 12 (branch 1.5.*)
# current: debian 12 (branch 1.5.*)
variable "vyos_release" {
default = "equuleus"
}
# build will fail if headless is false, only use headless false if you prepared X11/vnc setup
variable "headless" {
default = true
}
variable "host_port_min" {
default = env("HOST_PORT_FIXED") != "" ? env("HOST_PORT_FIXED") : 2222
}
variable "host_port_max" {
default = env("HOST_PORT_FIXED") != "" ? env("HOST_PORT_FIXED") : 4444
}
variable "vnc_port_min" {
default = env("VNC_PORT_FIXED") != "" ? env("VNC_PORT_FIXED") : 5900
}
variable "vnc_port_max" {
default = env("VNC_PORT_FIXED") != "" ? env("VNC_PORT_FIXED") : 6000
}
variable "sleep_before_shutdown" {
default = env("SLEEP_BEFORE_SHUTDOWN") != "" ? env("SLEEP_BEFORE_SHUTDOWN") : 0
}
# this is actually boot time between grub and user login. Need to be increased if your system in heavy load. A wait time too long will increase build time.
variable "sleep_after_grub" {
default = "45" # in seconds
}
# set grub_serial=1 to turn grub default=1, ie: use serial console. it is need to adjust on hypervisor
variable "grub_serial" {
type = string
default = 1
}
# which kind of datasource should be used
# nocloud_configdrive => use this as default, will turn on NoCloud, ConfigDrive on cloud-init datasource_list
# blank - don't set default datasource_list
variable "cloud_init_datasource" {
default = "nocloud_configdrive"
}
locals {
iso_path = "iso/${var.vm_name}-build2.qcow2" # not used at all since qemuargs -drive override it
output_dir = "output/vyos-image2/${regex_replace(timestamp(), "[: ]", "-")}"
}
source "qemu" "vyos" {
boot_command = [
"<wait2s><enter>",
"<wait${var.sleep_after_grub}s>",
"${var.ssh_username}<enter><wait>",
"${var.ssh_password}<enter><wait>",
"configure<enter><wait>",
"set interfaces ethernet eth0 address 'dhcp'<enter><wait>",
"set system name-server '8.8.8.8'<enter><wait>",
"set service ssh port '22'<enter><wait>",
"commit<enter><wait>",
"save<enter><wait>",
"exit<enter><wait10s>", # wait 10 seconds before reboot (dev purposes)
]
accelerator = "kvm"
iso_checksum = var.iso_checksum
iso_url = local.iso_path # not used at all since qemuargs -drive override it
boot_wait = var.boot_wait
http_directory = "http"
shutdown_command = "sleep ${var.sleep_before_shutdown}; sudo shutdown -P now"
communicator = "ssh"
host_port_min = var.host_port_min
host_port_max = var.host_port_max
ssh_username = var.ssh_username
ssh_password = var.ssh_password
ssh_timeout = "30m"
memory = var.memsize
cpus = var.numvcpus
disk_size = var.disk_size
vnc_port_min = var.vnc_port_min
vnc_port_max = var.vnc_port_max
headless = var.headless
output_directory = "${local.output_dir}"
net_device = "virtio-net"
disk_interface = "virtio"
qemuargs = [
["-m", "2048"],
["-smp", "4"],
["-cpu", "host"],
["-netdev", "user,id=user.0,", "hostfwd=tcp::{{ .SSHHostPort }}-:22"],
["-device", "virtio-net,netdev=user.0"],
#["-drive", "file=iso/${var.vm_name}.qcow2,if=virtio,cache=writeback,discard=ignore,format=qcow2"]
#["-drive", "file=iso/${var.vm_name}.qcow2,if=none,id=drive-virtio0,format=qcow2,cache=writeback,aio=io_uring,detect-zeroes=on"]
["-drive", "file=iso/${var.vm_name}-build2.qcow2,if=virtio,cache=writeback,format=qcow2,aio=io_uring,detect-zeroes=on"]
]
}
build {
name = "vyos"
source "source.qemu.vyos" {
name = "vyos_qemu_qcow2"
vm_name = "${var.vm_name}-${source.name}.qcow2"
format = "qcow2"
}
provisioner "shell-local" {
inline = [
"mkdir -p ${local.output_dir}"
]
}
# preparing provisioner
provisioner "shell" {
execute_command = "VYOS_RELEASE='${var.vyos_release}' {{ .Vars }} sudo -E bash '{{ .Path }}'"
scripts = [
"scripts/vyos/init.sh",
]
}
# prepare os - general customizations here
provisioner "shell" {
execute_command = "VYOS_RELEASE='${var.vyos_release}' {{ .Vars }} sudo -E bash '{{ .Path }}'"
scripts = [
"scripts/vyos/osprep.sh",
]
}
# configure vyos
provisioner "shell" {
execute_command = "VYOS_RELEASE='${var.vyos_release}' {{ .Vars }} sudo -E bash '{{ .Path }}'"
scripts = [
"scripts/vyos/configure.sh",
]
}
# installing apt repos and custom packages
provisioner "shell" {
execute_command = "VYOS_RELEASE='${var.vyos_release}' CLOUD_INIT='${var.cloud_init}' {{ .Vars }} sudo -E bash '{{ .Path }}'"
scripts = [
"scripts/vyos/apt-repo-debian.sh",
"scripts/vyos/apt-repo-vyos.sh",
"scripts/vyos/apt-install.sh",
]
}
# preparing cloud-init
provisioner "shell" {
execute_command = "VYOS_RELEASE='${var.vyos_release}' CLOUD_INIT='${var.cloud_init}' CLOUD_INIT_DATASOURCE='${var.cloud_init_datasource}' {{ .Vars }} sudo -E bash '{{ .Path }}'"
scripts = [
"scripts/vyos/cloud-init-debian.sh",
"scripts/vyos/cloud-init-vyos.sh",
"scripts/vyos/cloud-init-datasource.sh",
]
}
# if PLATFORM=qemu will install qemu packages
provisioner "shell" {
execute_command = "VYOS_RELEASE='${var.vyos_release}' PLATFORM='${var.platform}' {{ .Vars }} sudo -E bash '{{ .Path }}'"
scripts = [
"scripts/vyos/platform-qemu.sh"
]
}
# if grub_serial=1 change grub default to serial
provisioner "shell" {
execute_command = "VYOS_RELEASE='${var.vyos_release}' GRUB_SERIAL='${var.grub_serial}' {{ .Vars }} sudo -E bash '{{ .Path }}'"
scripts = [
"scripts/vyos/grub-serial.sh"
]
}
# image cleanup
provisioner "shell" {
execute_command = "VYOS_RELEASE='${var.vyos_release}' {{ .Vars }} sudo -E bash '{{ .Path }}'"
scripts = [
"scripts/vyos/cleanup-vyos-configure.sh",
"scripts/vyos/cleanup.sh",
]
}
# copy qcow2 to final destination
post-processors {
post-processor "shell-local" {
inline = [
"cp 'iso/${var.vm_name}-build2.qcow2' iso/${var.vm_name}.img",
"cd iso/ && sha256sum ${var.vm_name}.img > ${var.vm_name}.img.checksum && cd ../" ,
"cat iso/*.checksum > iso/SHA256SUM",
"rm -rf '${local.output_dir}'"
]
}
}
}