wireguard-setup/packer/qemu/start-vm.sh

46 lines
1.3 KiB
Bash
Raw Permalink Normal View History

2020-01-26 11:59:13 +01:00
#!/bin/sh
set -eu
export LC_ALL=C
2021-01-23 20:01:29 +01:00
SRC_DIR=$(CDPATH='' cd -- "$(dirname -- "$(dirname -- "${0:?}")")" && pwd -P)
2020-01-26 11:59:13 +01:00
TMP_DIR=$(mktemp -d)
2020-09-29 23:02:32 +02:00
ORIGINAL_DISK=${SRC_DIR:?}/dist/qemu/wireguard.qcow2
SNAPSHOT_DISK=${TMP_DIR:?}/snapshot.qcow2
2020-01-26 11:59:13 +01:00
2020-09-29 23:02:32 +02:00
USERDATA_YAML=${SRC_DIR:?}/qemu/http/seed/user-data
2020-09-29 23:12:19 +02:00
USERDATA_DISK=${TMP_DIR:?}/seed.img
2020-09-29 23:02:32 +02:00
# Remove temporary files on exit
2021-01-23 15:05:17 +01:00
# shellcheck disable=SC2154
trap 'ret="$?"; rm -rf -- "${TMP_DIR:?}"; trap - EXIT; exit "${ret:?}"' EXIT TERM INT HUP
2020-09-29 23:02:32 +02:00
# Create a snapshot image to preserve the original image
2021-11-08 20:06:40 +01:00
qemu-img create -f qcow2 -b "${ORIGINAL_DISK:?}" -F qcow2 "${SNAPSHOT_DISK:?}"
2020-01-26 11:59:13 +01:00
qemu-img resize "${SNAPSHOT_DISK:?}" +2G
# Create a seed image with metadata using cloud-localds
cloud-localds "${USERDATA_DISK:?}" "${USERDATA_YAML:?}"
# Remove keys from the known_hosts file
2020-09-29 23:02:32 +02:00
ssh-keygen -R '[127.0.0.1]:1122' 2>/dev/null
ssh-keygen -R '[localhost]:1122' 2>/dev/null
2020-01-26 11:59:13 +01:00
# hostfwd helper
hostfwd() { printf ',hostfwd=%s::%s-:%s' "$@"; }
2020-01-26 11:59:13 +01:00
# Launch VM
2021-01-22 17:51:27 +01:00
qemu-system-x86_64 \
2021-11-08 20:06:40 +01:00
-accel kvm -cpu host -smp 1 -m 512 \
2020-01-26 11:59:13 +01:00
-nographic -serial mon:stdio \
-device e1000,netdev=n0 \
-netdev user,id=n0"$(hostfwd \
tcp 1122 122 \
udp 51820 51820 \
udp 1053 53 \
tcp 1443 443 \
)" \
2020-01-26 11:59:13 +01:00
-drive file="${SNAPSHOT_DISK:?}",if=virtio,format=qcow2 \
-drive file="${USERDATA_DISK:?}",if=virtio,format=raw