First VM box

This commit is contained in:
Hoang Nguyen 2022-08-29 01:56:11 +07:00
parent 175619f3b4
commit 1432187a8f
No known key found for this signature in database
GPG Key ID: 813CF484F4993419
7 changed files with 178 additions and 2 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) <year> <copyright holders>
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:

15
Makefile Normal file
View File

@ -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

View File

@ -1,3 +1,11 @@
# packer-templates
Messing around with packer and vagrant
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

99
alpine-qemu.pkr.hcl Normal file
View File

@ -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<enter>",
"ifconfig eth0 up && udhcpc -i eth0<enter><wait10s>",
"wget -qO answers http://{{.HTTPIP}}:{{.HTTPPort}}/answers/alpine-qemu<enter><wait5s>",
"echo 'USEROPTS=\"-a -u ${var.ssh_username}\"' >> answers<enter>",
"ERASE_DISKS=/dev/vda ROOTFS=${var.rootfstype} setup-alpine -ef answers<enter><wait10m>",
"reboot<enter><wait30s>",
"root<enter>",
"echo '${var.ssh_username}:${var.ssh_password}' | chpasswd<enter>",
"sed -i 's/^permit persist/permit nopass/' /etc/doas.d/doas.conf<enter>",
"apk add dropbear-scp<enter>",
"passwd -l root<enter>",
"reboot<enter>"
]
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"
]
}
}

33
answers/alpine-qemu Normal file
View File

@ -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"

1
artifacts/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*

20
update_iso_checksum.sh Normal file
View File

@ -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."