coreapp-dev: a Federated Core in a local KVM VM
A CoreDesktop sibling project. install.sh and update.sh install libvirt and QEMU/KVM and set up the coreapp VM network (10.123.0.0/24, open mode, with a libvirt hook for NAT and firewall rules, and ufw rules for DHCP, DNS and forwarding). scripts/create-coreserver-vm.sh creates a CoreServer VM with root's SSH keys from cloud-init, at 10.123.0.10, with systemd-resolved masked so the Core's DNS server survives reboots. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Executable
+103
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env bash
|
||||
# Create a local KVM VM from the Federated CoreServer QCOW2 image.
|
||||
# Usage: scripts/create-coreserver-vm.sh [name]
|
||||
# Env overrides: VCPUS, RAM_MB, DISK_GB, NETWORK, MAC
|
||||
#
|
||||
# Root's SSH keys (installed by cloud-init on first boot): coreapp-dev's own
|
||||
# key, plus every key in ~/.config/coreapp-dev/authorized_keys. Put the SSH
|
||||
# public key from Federated's dashboard (Cores) there before creating the VM,
|
||||
# so the Provisioner can log in.
|
||||
set -euo pipefail
|
||||
|
||||
NAME="${1:-coreserver}"
|
||||
VCPUS="${VCPUS:-4}"
|
||||
RAM_MB="${RAM_MB:-8192}"
|
||||
DISK_GB="${DISK_GB:-60}"
|
||||
NETWORK="${NETWORK:-coreapp}"
|
||||
# coreapp's DHCP gives this MAC 10.123.0.10, corerelay's default target
|
||||
if [[ "$NAME" == coreserver ]]; then MAC="${MAC:-52:54:00:c0:4e:10}"; fi
|
||||
MAC="${MAC:-}"
|
||||
URI="qemu:///system"
|
||||
|
||||
DATA="$HOME/.local/share/coreapp-dev"
|
||||
BASE="$DATA/images/coreserver-enterprise-x86-26-05-12.qcow2"
|
||||
# The image's accounts have locked passwords; root logs in with this key,
|
||||
# which cloud-init (NoCloud) installs on first boot.
|
||||
SSH_KEY="$DATA/ssh/id_ed25519"
|
||||
EXTRA_KEYS="$HOME/.config/coreapp-dev/authorized_keys"
|
||||
POOL="default"
|
||||
POOL_DIR="/var/lib/libvirt/images"
|
||||
VOL="$NAME.qcow2"
|
||||
DISK="$POOL_DIR/$VOL"
|
||||
|
||||
[[ -f "$BASE" ]] || { echo "Base image not found: $BASE" >&2; exit 1; }
|
||||
|
||||
if virsh -c "$URI" dominfo "$NAME" &>/dev/null; then
|
||||
echo "VM '$NAME' already exists." >&2; exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f "$SSH_KEY" ]]; then
|
||||
mkdir -p "$(dirname "$SSH_KEY")"
|
||||
ssh-keygen -q -t ed25519 -N "" -C "coreapp-dev" -f "$SSH_KEY"
|
||||
fi
|
||||
|
||||
# cloud-init user-data: root may log in, with these keys only
|
||||
USER_DATA="$(mktemp)"
|
||||
trap 'rm -f "$USER_DATA"' EXIT
|
||||
{
|
||||
echo "#cloud-config"
|
||||
echo "disable_root: false"
|
||||
echo "ssh_pwauth: false"
|
||||
# Federated Core's DNS server (pdns) needs port 53. Its installer disables
|
||||
# systemd-resolved, but this image's systemd-networkd starts it again at
|
||||
# boot (systemd-networkd-resolve-hook.socket), so pdns can't start after a
|
||||
# reboot. resolv.conf is a plain file (1.1.1.1), so nothing needs resolved.
|
||||
echo "bootcmd:"
|
||||
echo " - [systemctl, mask, --now, systemd-resolved.service]"
|
||||
echo "ssh_authorized_keys:"
|
||||
{ cat "$SSH_KEY.pub"; if [[ -f "$EXTRA_KEYS" ]]; then cat "$EXTRA_KEYS"; fi; } \
|
||||
| grep -E '^(ssh-|ecdsa-|sk-)' | sed 's/^/ - /'
|
||||
} > "$USER_DATA"
|
||||
echo "Root's SSH keys: $(grep -c '^ - ' "$USER_DATA") (coreapp-dev's, plus $EXTRA_KEYS)"
|
||||
|
||||
# libvirtd runs as root, so letting it manage the disk avoids sudo here.
|
||||
if ! virsh -c "$URI" pool-info "$POOL" &>/dev/null; then
|
||||
virsh -c "$URI" pool-define-as "$POOL" dir --target "$POOL_DIR" >/dev/null
|
||||
virsh -c "$URI" pool-build "$POOL" >/dev/null 2>&1 || true
|
||||
fi
|
||||
virsh -c "$URI" pool-start "$POOL" >/dev/null 2>&1 || true
|
||||
virsh -c "$URI" pool-autostart "$POOL" >/dev/null
|
||||
virsh -c "$URI" pool-refresh "$POOL" >/dev/null
|
||||
|
||||
if virsh -c "$URI" vol-info "$VOL" --pool "$POOL" &>/dev/null; then
|
||||
echo "Disk '$DISK' already exists; remove it or pick another name." >&2; exit 1
|
||||
fi
|
||||
|
||||
# Copy the pristine image (keeps the download reusable), then grow it.
|
||||
virsh -c "$URI" vol-create-as "$POOL" "$VOL" "${DISK_GB}G" --format qcow2 >/dev/null
|
||||
virsh -c "$URI" vol-upload "$VOL" "$BASE" --pool "$POOL" --sparse
|
||||
virsh -c "$URI" vol-resize "$VOL" "${DISK_GB}G" --pool "$POOL"
|
||||
|
||||
virsh -c "$URI" net-start "$NETWORK" 2>/dev/null || true
|
||||
virsh -c "$URI" net-autostart "$NETWORK" >/dev/null
|
||||
|
||||
virt-install --connect "$URI" \
|
||||
--name "$NAME" \
|
||||
--vcpus "$VCPUS" --cpu host-passthrough \
|
||||
--memory "$RAM_MB" \
|
||||
--disk "path=$DISK,format=qcow2,bus=virtio" \
|
||||
--import \
|
||||
--osinfo detect=on,require=off \
|
||||
--network "network=$NETWORK,model=virtio${MAC:+,mac=$MAC}" \
|
||||
--cloud-init "user-data=$USER_DATA" \
|
||||
--graphics vnc,listen=127.0.0.1 --console pty,target_type=serial \
|
||||
--noautoconsole
|
||||
|
||||
echo "Created '$NAME'. Waiting for an IP..."
|
||||
for _ in $(seq 1 60); do
|
||||
IP=$(virsh -c "$URI" domifaddr "$NAME" 2>/dev/null | awk '/ipv4/ {split($4,a,"/"); print a[1]; exit}')
|
||||
[[ -n "${IP:-}" ]] && { echo "IP: $IP"; echo "Log in: ssh -i $SSH_KEY root@$IP"; exit 0; }
|
||||
sleep 2
|
||||
done
|
||||
echo "No IP yet. Watch the screen: virt-viewer -c $URI $NAME (or virsh console $NAME)" >&2
|
||||
exit 1
|
||||
Reference in New Issue
Block a user