diff --git a/LICENSE b/LICENSE index 2071b23..49d0cbc 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) +Copyright (c) 2022 FollieHiyuki Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0d01e60 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +all: alpine-qemu.pkr.hcl + +alpine-qemu.pkr.hcl: + @echo "==> Validating $@ ..." + @packer validate $@ + @echo "==> Building VM box for $@ ..." + @packer build -on-error=abort $@ + +format: + @packer fmt -diff -recursive . + +clean: + @find ./artifacts/* -prune -exec rm -rfv {} \; + +.PHONY: all alpine-qemu.pkr.hcl format clean diff --git a/README.md b/README.md index 5772524..7fe0398 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ # packer-templates -Messing around with packer and vagrant \ No newline at end of file +This repository holds Packer's VM templates I use. Most (if not all) of them run [AlpineLinux](https://alpinelinux.org). + +## VM boxes + +- **[alpine-qemu.pkr.hcl](./alpine-qemu.pkr.hcl)**: libvirt-compatible VM on local machine running Alpine edge. + +## License + +MIT diff --git a/alpine-qemu.pkr.hcl b/alpine-qemu.pkr.hcl new file mode 100644 index 0000000..d70b0c6 --- /dev/null +++ b/alpine-qemu.pkr.hcl @@ -0,0 +1,99 @@ +variable "headless" { + type = bool + default = true +} + +variable "iso_url" { + type = string + default = "https://dl-cdn.alpinelinux.org/alpine/v3.16/releases/x86_64/alpine-standard-3.16.2-x86_64.iso" +} + +variable "iso_checksum" { + type = string + default = "sha512:b4dcc47b28390c20348d14643863ce41fb9ad55755a1ec00af0117c3dd09c9d03c2c722b005534abece67baa2a017bd78a65c18d2dccd525491d679fe13cd615" +} + +variable "disk_size" { + type = string + default = "20G" +} + +variable "ram" { + type = string + default = "2048M" +} + +variable "cpus" { + type = string + default = "2" +} + +variable "rootfstype" { + type = string + default = "btrfs" +} + +variable "ssh_username" { + type = string + default = "kawaii" +} + +variable "ssh_password" { + type = string + default = "kawaii" +} + +source "qemu" "alpine-qemu" { + accelerator = "kvm" + boot_command = [ + "root", + "ifconfig eth0 up && udhcpc -i eth0", + "wget -qO answers http://{{.HTTPIP}}:{{.HTTPPort}}/answers/alpine-qemu", + "echo 'USEROPTS=\"-a -u ${var.ssh_username}\"' >> answers", + "ERASE_DISKS=/dev/vda ROOTFS=${var.rootfstype} setup-alpine -ef answers", + "reboot", + "root", + "echo '${var.ssh_username}:${var.ssh_password}' | chpasswd", + "sed -i 's/^permit persist/permit nopass/' /etc/doas.d/doas.conf", + "apk add dropbear-scp", + "passwd -l root", + "reboot" + ] + boot_wait = "30s" + disk_cache = "none" + disk_compression = true + disk_discard = "unmap" + disk_interface = "virtio" + disk_size = var.disk_size + format = "qcow2" + headless = var.headless + http_directory = "." + iso_checksum = var.iso_checksum + iso_url = var.iso_url + net_device = "virtio-net" + output_directory = "artifacts/qemu" + qemu_binary = "/usr/bin/qemu-system-x86_64" + qemuargs = [ + ["-m", "${var.ram}"], + ["-display", "none"], + ["-smp", "${var.cpus}"] + ] + shutdown_command = "doas /sbin/poweroff" + ssh_password = var.ssh_password + ssh_username = var.ssh_username + ssh_wait_timeout = "30m" + vm_name = "packer-alpine-edge-qemu-x86_64" +} + +build { + sources = ["source.qemu.alpine-qemu"] + + provisioner "shell" { + execute_command = "doas sh {{.Path}}" + inline_shebang = "/bin/sh -e" + inline = [ + "apk add qemu-guest-agent", + "rc-update add qemu-guest-agent" + ] + } +} diff --git a/answers/alpine-qemu b/answers/alpine-qemu new file mode 100644 index 0000000..b90e844 --- /dev/null +++ b/answers/alpine-qemu @@ -0,0 +1,33 @@ +# Answer file for alpine-qemu.pkr.hcl template + +KEYMAPOPTS="us us" + +HOSTNAMEOPTS="alpine-qemu" + +DEVDOPTS="mdev" + +INTERFACESOPTS="auto lo +iface lo inet loopback + +auto eth0 +iface eth0 inet dhcp + hostname alpine-qemu +" + +DNSOPTS="" + +TIMEZONEOPTS="UTC" + +PROXYOPTS="none" + +APKREPOSOPTS="https://download.nus.edu.sg/mirror/alpine/edge/main +https://download.nus.edu.sg/mirror/alpine/edge/community +https://download.nus.edu.sg/mirror/alpine/edge/testing +" + +SSHDOPTS="dropbear" + +NTPOPTS="busybox" + +DISKOPTS="-s 0 -m sys /dev/vda" + diff --git a/artifacts/.gitignore b/artifacts/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/artifacts/.gitignore @@ -0,0 +1 @@ +* diff --git a/update_iso_checksum.sh b/update_iso_checksum.sh new file mode 100644 index 0000000..9b5b93c --- /dev/null +++ b/update_iso_checksum.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +set -e + +if [ $# -ne 1 ]; then + echo "Required only 1 argument." + exit 1 +fi + +filename="$1" +if [ ! -r "$filename" ]; then + echo "File $filename cannot be read." + exit 1 +fi + +iso_url=$(hcl2json "$filename" | jq -r '.variable.iso_url[].default') + +printf "Checking sha512sum of \033[1;34m%s\033[0m ...\n" "$iso_url" +sha512_sum=$(wget -qO- "${iso_url}".sha512 | awk '{print $1}') +sed -i -E "s|(default = \"sha512:).*\"|\\1$sha512_sum\"|g" "$filename" && echo "ISO checksum updated."