-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.pkr.hcl
95 lines (84 loc) · 2.43 KB
/
example.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
packer {
required_plugins {
qemu = {
version = ">= 1.0.10"
source = "github.com/hashicorp/qemu"
}
}
}
variables {
vm_name = "examplevm"
vm_pause = 0
vm_debug = 0
vm_noinstall = 0
qemu_unmap = false
qemu_ssh_forward = 20022
source_image = "./path/to/ubuntu-22-base.qcow2"
source_checksum = "none"
use_backing_file = true
output_directory = "/tmp/packer-out"
ssh_username = "student"
ssh_password = "student"
}
source "qemu" "examplevm" {
// VM Info:
vm_name = var.vm_name
headless = false
// Virtual Hardware Specs
memory = 1024
cpus = 2
disk_size = 8000
disk_interface = "virtio"
net_device = "virtio-net"
// disk usage optimizations (unmap zeroes as free space)
disk_discard = (var.qemu_unmap ? "unmap" : "")
disk_detect_zeroes = (var.qemu_unmap ? "unmap" : "")
// skip_compaction = true
// ISO & Output details
iso_url = var.source_image
iso_checksum = var.source_checksum
disk_image = var.use_backing_file
use_backing_file = var.use_backing_file
output_directory = var.output_directory
ssh_username = var.ssh_username
ssh_password = var.ssh_password
ssh_timeout = "30m"
host_port_min = var.qemu_ssh_forward
host_port_max = var.qemu_ssh_forward
shutdown_command = "sudo /sbin/shutdown -h now"
}
build {
sources = ["sources.qemu.examplevm"]
# create destination dirs + set privileges (`file` provisioner requires this...)
provisioner "shell" {
inline = [
"rm -rf /home/student/provision-scripts", "mkdir -p /home/student/provision-scripts",
"chown student:student /home/student/provision-scripts"
]
execute_command = "{{.Vars}} sudo -E -S bash -e '{{.Path}}'"
environment_vars = [
"VM_DEBUG=${var.vm_debug}"
]
}
# copy the provisioning scripts to student's home
provisioner "file" {
source = "scripts/"
destination = "/home/student/provision-scripts"
}
# run the scripts!
provisioner "shell" {
inline = [
"chmod +x /home/student/provision-scripts/install.sh",
"/home/student/provision-scripts/install.sh"
]
execute_command = "{{.Vars}} sudo -E -S bash -e '{{.Path}}'"
environment_vars = [
"VM_DEBUG=${var.vm_debug}",
]
}
# optionally, when PAUSE=1, keep the qemu VM open!
provisioner "breakpoint" {
disable = (var.vm_pause == 0)
note = "this is a breakpoint"
}
}