1
0
IaC/packer/libvirt/arch/arch-minimal.build.pkr.hcl
2022-10-04 00:11:25 +13:00

148 lines
3.2 KiB
HCL

# -------------------------------------------------------------------------
# Name: vm-libvirt-arch-minimal
# Desc: Create a minimal Arch Linux VM install on a libvirt/kvm hypervisor.
# -------------------------------------------------------------------------
#--------------------
# Requirements block.
#--------------------
packer {
required_plugins {
libvirt = {
version = ">= 0.3.4"
source = "github.com/thomasklein94/libvirt"
}
}
}
# ----------------
# Variables block.
# ----------------
data "http" "image_checksum" {
url = var.checksum_url
}
locals {
image_checksum = split(" ",data.http.image_checksum)[0]
libvirt_uri = "qemu+ssh://${var.host_ssh_user}@${var.host_ssh_address}/system"
nvram_path = "${var.nvram_path_base}/${var.guest_hostname}_VARS.fd"
cidata_name = "${var.volume_name}_cidata"
}
#---------------
# Sources block.
#---------------
source "libvirt" "arch-minimal" {
libvirt_uri = local.libvirt_uri
domain_name = var.guest_hostname
vcpu = var.cpu_count
memory = var.memory
boot_devices = ["hd"]
shutdown_mode = "guest"
domain_type = var.domain_type
arch = var.arch
chipset = var.chipset
loader_type = var.loader_type
loader_path = var.loader_path
nvram_template = var.nvmram_template
nvram_path = local.nvram_path
secure_boot = var.secure_boot
volume {
alias = "artifact"
name = var.volume_name
pool = var.volume_pool
readonly = false
target_dev = "vda"
bus = "virtio"
format = "qcow2"
size = "2G"
capacity = var.volume_capacity
source {
type = "external"
urls = [var.image_url]
checksum = local.image_checksum
}
}
volume {
alias = "cidata"
name = local.cidata_name
pool = var.volume_pool
readonly = true
source {
type = "cloud-init"
meta_data = jsonencode({
"instance-id" = ${var.guest_hostname}
"hostname" = ${var.guest_hostname}
})
user_data = format("#cloud-config\n%s", jsonencode({
"packages" = [
"qemu-guest-agent",
"ufw"
]
"runcmd" = [
["systemctl", "enable", "--now", "qemu-guest-agent"],
["ufw", "enable"],
["ufw", "allow", "from", ${var.ssh_source}, "to", "port", "22", "proto", "tcp"],
["systemctl", "enable", "--now", "ufw"]
]
}))
network_config = jsonencode({
renderer = "networkd"
version = 2
ethernets = {
eth0 = {
match = {
name = "en*"
}
dhcp4 = false
addresses = [${var.network_address}]
gateway4 = ${var.network_gateway}
nameservers = {
addresses = [${var.network_nameserver}]
search = [${var.network_domain}]
}
}
}
})
}
}
artifact_volume_alias = "artifact"
network_interface {
type = "bridge"
bridge = var.bridge_name
model = "virtio"
alias = "default-network"
}
network_address_source = "agent"
graphics {
type = "spice"
port = 5900
}
communicator {
ssh_username = var.guest_ssh_user
ssh_port = var.guest_ssh_port
ssh_private_key = var.guest_ssh_private_key
}
communicator_interface = "default-network"
}
#-------------
# Build block.
#-------------
build {
sources = ["source.libvirt.arch-minimal"]
}