From 08d55c4f22d8237b36e537ed3d338a8f6b9dc239 Mon Sep 17 00:00:00 2001 From: michael Date: Mon, 3 Oct 2022 15:19:24 +1300 Subject: [PATCH] begin arch packer template --- packer/README.md | 0 .../libvirt/arch/arch-minimal.build.pkr.hcl | 141 ++++++++++++++++++ packer/libvirt/arch/arch-minimal.pkrvars.hcl | 19 +++ .../arch/arch-minimal.variables.pkr.hcl | 41 +++++ 4 files changed, 201 insertions(+) delete mode 100644 packer/README.md create mode 100644 packer/libvirt/arch/arch-minimal.build.pkr.hcl create mode 100644 packer/libvirt/arch/arch-minimal.pkrvars.hcl create mode 100644 packer/libvirt/arch/arch-minimal.variables.pkr.hcl diff --git a/packer/README.md b/packer/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/packer/libvirt/arch/arch-minimal.build.pkr.hcl b/packer/libvirt/arch/arch-minimal.build.pkr.hcl new file mode 100644 index 0000000..5c2b57d --- /dev/null +++ b/packer/libvirt/arch/arch-minimal.build.pkr.hcl @@ -0,0 +1,141 @@ +# ------------------------------------------------------------------------- +# 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}@${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 = true + + volume { + alias = "artifact" + name = var.template_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({ + version = 2 + ethernets = { + eth = { + match = { + name = "en*" + } + dhcp4 = true + } + } + }) + + } + } + + 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"] +} \ No newline at end of file diff --git a/packer/libvirt/arch/arch-minimal.pkrvars.hcl b/packer/libvirt/arch/arch-minimal.pkrvars.hcl new file mode 100644 index 0000000..e89f3d8 --- /dev/null +++ b/packer/libvirt/arch/arch-minimal.pkrvars.hcl @@ -0,0 +1,19 @@ +image_url = "https://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-cloudimg.qcow2" +checksum_url = "https://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-cloudimg.qcow2.SHA256" +host_ssh_address = "server.balsillie.net" +host_ssh_user = "michael" +guest_hostname = "arch-minimal-template" +cpu_count = 2 +memory = 2048 +type = "kvm" +arch = "x86_64" +chipset = "pc-q35-6.1" +loader_type = "pflash" +loader_path = "/usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd" +secure_boot = true +nvram_template = "/usr/share/edk2-ovmf/x64/OVMF_VARS.fd" +nvram_path_base = "/var/lib/libvirt/qemu/nvram" +guest_ssh_user = "arch" +guest_ssh_port = 22 +guest_ssh_private_key = "~/.ssh/conf.d/home/arch@arch_template.key" +guest_ssh_public_key = "~/.ssh/conf.d/home/arch@arch_template.key.pub" diff --git a/packer/libvirt/arch/arch-minimal.variables.pkr.hcl b/packer/libvirt/arch/arch-minimal.variables.pkr.hcl new file mode 100644 index 0000000..9a65de0 --- /dev/null +++ b/packer/libvirt/arch/arch-minimal.variables.pkr.hcl @@ -0,0 +1,41 @@ +variable "image_url" { + type = string + description = "The URL to retrieve the backing image from." +} + +variable "checksum_url" { + type = string + description = "The URL to retrieve the checksum value of the backing image from." +} + +variable "hostname" { + type = string + description = "The hostname of the virtual machine" +} + +variable "host_ssh_address" { + type = string + description = "The address of the hypervisor, used to construct the libvirt URI." +} + +variable "host_ssh_user" { + type = string + description = "The user to connect to the hypervisor as, used to construct the libvirt URI." +} + +variable "arch" { + type = string + description = "Domain architecture." + default = "x86_64" +} + +variable "chipset" { + type = string + description = "Libvirt Machine Type Value for domain XML's machine type." +} + +variable "guest_ssh_pass" { + type = string + sensitive = true + description = "Default password for the cloud-init image" +}