#!/usr/bin/env bash # name: proxmox-vm # summary: Clone an op-enabled Proxmox template into a tailnet-joined VM # # Run ON a Proxmox node, as root: # proxmox-vm --name mybox --vmid 142 [--template 9001] [--cores 2] # [--memory 4096] [--disk 48G] [--storage local-lvm] # [--ts-authkey tskey-...] [--no-op] # [--metrics-push user:password] [--metrics-env dev|prod] [--no-metrics] # # Metrics: at first boot the VM runs install.xaritomi.xyz/metrics-agent, which # ships host and container metrics to the estate's Prometheus. It reads the # push credential from the vault (VMs given the op token) or, on a node with no # vault credential such as zerene, from --metrics-push, read on a host with op: # echo "$(op read op://mimir/observability-push-basicauth/username):$(op read op://mimir/observability-push-basicauth/password)" # --no-metrics leaves the VM unmonitored. # # --ts-authkey injects a tailnet key directly, so the Proxmox node itself never # needs a 1Password token. Read it from the vault on a host that has op: # op read op://mimir/tailscale/authkey \033[0m %s\n' "$*"; } die() { printf '\033[1;31merror:\033[0m %s\n' "$*" >&2; exit 1; } usage() { sed -n '2,/^set -euo/p' "$0" | sed 's/^# \{0,1\}//; /^set -euo/d'; exit 0; } while [ $# -gt 0 ]; do case "$1" in --name) NAME="${2:?}"; shift ;; --vmid) VMID="${2:?}"; shift ;; --template) TEMPLATE="${2:?}"; shift ;; --cores) CORES="${2:?}"; shift ;; --memory) MEMORY="${2:?}"; shift ;; --disk) DISK="${2:?}"; shift ;; --storage) STORAGE="${2:?}"; shift ;; --ts-authkey) TS_AUTHKEY="${2:?}"; shift ;; --no-op) WITH_OP=0 ;; --metrics-push) METRICS_PUSH="${2:?}"; shift ;; --metrics-env) METRICS_ENV="${2:?}"; shift ;; --no-metrics) WITH_METRICS=0 ;; --no-start) START=0 ;; -h|--help) usage ;; *) die "unknown argument: $1 (try --help)" ;; esac shift done [ "$(id -u)" -eq 0 ] || die "must run as root on a Proxmox node" command -v qm >/dev/null 2>&1 || die "qm not found - is this a Proxmox node?" [ -n "$NAME" ] || die "--name is required" case "$METRICS_ENV" in dev|prod) ;; *) die "--metrics-env must be dev or prod" ;; esac case "$METRICS_PUSH" in ""|*:*) ;; *) die "--metrics-push must be user:password" ;; esac qm config "$TEMPLATE" >/dev/null 2>&1 || die "template $TEMPLATE does not exist" # next free vmid from 140 up, if not given if [ -z "$VMID" ]; then for i in $(seq 140 199); do qm config "$i" >/dev/null 2>&1 || { VMID=$i; break; } done [ -n "$VMID" ] || die "no free VMID between 140 and 199" log "picked VMID $VMID" fi qm config "$VMID" >/dev/null 2>&1 && die "VMID $VMID is already in use" SNIPPET="$SNIPPET_DIR/vm-${VMID}.yaml" mkdir -p "$SNIPPET_DIR" { echo "#cloud-config" echo "# Per-VM vendor data for ${NAME} (VMID ${VMID}). Generated by proxmox-vm." echo "package_update: true" echo "packages:" echo " - qemu-guest-agent" # A VM needs at most one of these; both are accepted. estate-bootstrap # prefers the vault (op) and falls back to the bare key. if { [ "$WITH_OP" -eq 1 ] && [ -r "$OP_ENV" ]; } || [ -n "$TS_AUTHKEY" ] || { [ "$WITH_METRICS" -eq 1 ] && [ -n "$METRICS_PUSH" ]; }; then echo "write_files:" fi if [ "$WITH_OP" -eq 1 ] && [ -r "$OP_ENV" ]; then echo " - path: /etc/op-service-account.env" echo " owner: 'root:root'" echo " permissions: '0600'" echo " content: |" sed 's/^/ /' "$OP_ENV" fi if [ -n "$TS_AUTHKEY" ]; then echo " - path: /etc/ts-authkey" echo " owner: 'root:root'" echo " permissions: '0600'" echo " content: |" printf ' %s\n' "$TS_AUTHKEY" fi if [ "$WITH_METRICS" -eq 1 ] && [ -n "$METRICS_PUSH" ]; then echo " - path: /etc/metrics-push.env" echo " owner: 'root:root'" echo " permissions: '0600'" echo " content: |" printf ' PUSH_USER=%s\n PUSH_PASSWORD=%s\n' "${METRICS_PUSH%%:*}" "${METRICS_PUSH#*:}" fi echo "runcmd:" echo " - systemctl enable --now qemu-guest-agent" echo " - /usr/local/sbin/estate-bootstrap" # After the tailnet join; fetched at boot, so it improves without a template rebuild. if [ "$WITH_METRICS" -eq 1 ]; then echo " - [ bash, -c, 'curl -fsSL https://install.xaritomi.xyz/metrics-agent | HOST_ENV=${METRICS_ENV} METRICS_DOCKER=install bash' ]" fi } > "$SNIPPET" chmod 600 "$SNIPPET" if [ "$WITH_OP" -eq 1 ] && [ ! -r "$OP_ENV" ] && [ -z "$TS_AUTHKEY" ]; then log "WARNING: $OP_ENV not readable - the VM will boot without vault access" log " and will not join the tailnet until given a credential" fi log "cloning template $TEMPLATE -> $VMID ($NAME)" qm clone "$TEMPLATE" "$VMID" --name "$NAME" --full 1 --storage "$STORAGE" >/dev/null qm set "$VMID" \ --cores "$CORES" --memory "$MEMORY" --agent enabled=1 --onboot 1 \ --cicustom "vendor=${SNIPPET_STORE}:snippets/vm-${VMID}.yaml" \ --description "${NAME} - created $(date -u +%Y-%m-%d) by proxmox-vm from template ${TEMPLATE}" \ >/dev/null [ -n "$DISK" ] && { log "resizing disk to $DISK"; qm resize "$VMID" scsi0 "$DISK" >/dev/null; } [ "$START" -eq 0 ] && { log "created $VMID ($NAME), not started"; exit 0; } log "starting $VMID" qm start "$VMID" log "waiting for the guest agent" for _ in $(seq 1 60); do qm guest cmd "$VMID" ping >/dev/null 2>&1 && break sleep 5 done IP=$(qm guest cmd "$VMID" network-get-interfaces 2>/dev/null \ | grep -o '"ip-address" *: *"[0-9.]*"' \ | grep -o '[0-9][0-9.]*' | grep -v '^127\.' | head -1 || true) log "done: $NAME (VMID $VMID)${IP:+ at $IP}" log "tailnet: estate-bootstrap runs at first boot; check with 'qm guest exec $VMID -- tailscale status'"