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
|
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-1.3.6"
}
variable "numvcpus" {
default = "4"
}
variable "memsize" {
default = "2048"
}
variable "disk_size" {
default = "10240"
}
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 = "60" # 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
}
locals {
iso_path = "iso/${var.vm_name}.iso"
output_dir = "output/vyos-image1/${regex_replace(timestamp(), "[: ]", "-")}"
}
source "qemu" "vyos" {
boot_command = [
"<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><wait>",
"install image<enter><wait3s>",
"Yes<enter><wait3s>",
"Auto<enter><wait3s>",
"<enter><wait3s>", # vda
"Yes<enter><wait5s>",
"<enter><wait15s>", #disk size
"${var.vm_name}<enter><wait10s>",
"<enter><wait2s>",
"${var.ssh_password}<enter><wait>",
"${var.ssh_password}<enter><wait>",
"<enter><wait10s>", # wait 10 seconds before reboot (dev purposes)
]
accelerator = "kvm"
iso_checksum = var.iso_checksum
iso_url = local.iso_path
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"]
]
}
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 iso/",
"mkdir -p ${local.output_dir}"
]
}
# checksum
post-processors {
post-processor "checksum" {
checksum_types = ["sha256"]
keep_input_artifact = true
}
post-processor "shell-local" {
inline = [
"mv packer_vyos_qemu_sha256.checksum iso/${var.vm_name}-build1.qcow2.checksum.tmp",
"awk '{print $1, \" ${var.vm_name}-build1.qcow2\"}' iso/${var.vm_name}-build1.qcow2.checksum.tmp > iso/${var.vm_name}-build1.qcow2.checksum",
"rm -f iso/${var.vm_name}-build1.qcow2.checksum.tmp",
"cat iso/*.checksum > iso/SHA256SUM",
"echo '${var.vm_name}' > .vm_name"
]
}
}
# copy from output to iso/ for vyos-image2.pkr.hcl customize
post-processors {
post-processor "shell-local" {
inline = [
"cp '${local.output_dir}/${var.vm_name}-${source.name}.qcow2' iso/${var.vm_name}-build1.qcow2",
"rm -rf '${local.output_dir}'"
]
}
}
}
|