commit b405e35ea84da85192dd5c93f2f575fae741cbe2 Author: David Young Date: Thu Dec 3 11:27:23 2026 -0700 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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b3c81bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +images/ +*.qcow2 diff --git a/PLAN.md b/PLAN.md new file mode 100644 index 0000000..bd435fe --- /dev/null +++ b/PLAN.md @@ -0,0 +1,73 @@ +# Plan + +coreapp-dev is a software development kit for building apps that run as +Docker containers on Federated Core. It is a CoreDesktop sibling project: +people install it with `sibling add `, and CoreDesktop's `update` keeps +it current (format: `~/Projects/coredesktop/skills/sibling-project-format.md`). +Delete an item once it's done; git history keeps the record. + +## Now: a working Core in a local VM + +What we know about the CoreServer image (2026-12-02): it's Arch Linux, boots +with BIOS or UEFI, and needs a display device (it stalls at "Loading initial +ramdisk" with `--graphics none`). All passwords are locked; cloud-init +(NoCloud) installs root's SSH keys. `/` is a fixed 10 GiB btrfs partition; +the extra disk space goes to `/federated`, which the image's own +`grow-federated.service` grows on first boot. + +The Core `dev.fedcom.net` was provisioned on 2026-12-03 in the VM +(10.123.0.10). It's reachable from the internet at 77.216.180.150 +through corerelay, which also carries its outbound traffic. + +What we learned provisioning (2026-12-02/03): +- Federated Core finds its public IP with `dig @resolver4.opendns.com + myip.opendns.com` and publishes it in DNS, so the Core's outbound traffic + must leave through the relay (first attempt published the home IP) +- The Core is the authoritative DNS server for its domain (ns1/ns2. + point at `ip`), so 53 tcp/udp must be forwarded, plus 25 143 465 587 993 + 4190 9980 besides 22 80 443 +- The domain is picked round-robin (dev.federatedcomputer.cloud, + dev.federatedcomputer.net, dev.fedcom.net, ...) +- The progress stream from core-create is server-sent events; it takes 5+ + minutes, a full install about 40 +- Deleting a Core makes the platform SSH into its IP and tear it down, so + never put a new VM behind an IP whose old Core still exists. A failed Core + has no delete button; Federated has to remove it +- The Core's containers use 192.168.0.0/16 and send replies for any + 192.168.x.x address onto their own bridge, so the VM network is + 10.123.0.0/24 (moved there on 2026-12-03 from 192.168.123.0/24) +- libvirt's DHCP `` reservations are ignored on this workstation: + libvirt writes the hosts file readable by root only and dnsmasq reads it as + `nobody`. The reservation is a `dnsmasq:option` in the network instead +- Before core-create on a fresh VM: `systemctl is-active docker`, opendns + sees the VPS IP, and the hairpin (`/dev/tcp//22` from the VM) + +- [ ] The Core shows as Active in the dashboard +- [ ] Tell Federated (2026-12-03): + - On this image, disabling systemd-resolved isn't enough: networkd's + `systemd-networkd-resolve-hook.socket` starts it at boot, it takes + port 53 and pdns can't start after a reboot. coreapp-dev masks it + through cloud-init; the installer should mask it too + - `upgrade-federated.timer` has `Requires=upgrade-federated.service`, so + the upgrade runs at every boot, not just at 5:00. It fails there + (exit 2), and on the first reboot it removed the pdns container that + couldn't start + - `/federated/bin/upgrade` has an API token in it, readable by anyone + with the image + +## SDK packaging + +- [ ] `install.sh`: check when the system was last upgraded, as `sibling` + does (`upgrade_if_stale`), so running it by hand doesn't hit stale-mirror + 404s +- [ ] Add coreapp-dev's `uninstall.sh`: the hook, the `coreapp` network, + the ufw rules +- [ ] Make the VM script a command in `~/.local/bin` with a `coreapp-` prefix + (e.g. `coreapp-vm create`). Never use CoreDesktop's names: configure, + update, refresh, sibling, version, audit, report, webapp +- [ ] Have that command download the CoreServer image into + `~/.local/share/coreapp-dev/images/` when it's missing. Image: + `https://nginx.core-x86builder.federatedcomputer.cloud/coreserver-enterprise-x86-26-05-12.qcow2` + (no published checksum; our download's SHA-256 is + `8c3a64e9be12121b71e1a3ad3dd7c6de686734b83ee77f3852d80cee1b6dd4a4`) +- [ ] Tag the first release once install and VM creation work end to end diff --git a/README.md b/README.md new file mode 100644 index 0000000..085f3e4 --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +# coreapp-dev + +A software development kit for building apps that run as Docker containers on +[Federated Core](https://documentation.federated.computer/self-hosted/cores/provisioning-a-core/). +It runs a real Core in a KVM virtual machine on your own computer, so you can +build and test apps against it. + +It's a CoreDesktop sibling project: CoreDesktop installs it and keeps it up to +date. + +## Install + +On a CoreDesktop computer with hardware virtualization (VT-x/AMD-V) turned on: + +``` +sibling add https://gitea.federated.computer/Federated_Computer/coreapp-dev.git +``` + +This installs libvirt and QEMU/KVM, adds you to the `libvirt` group (log out +and back in afterwards), and sets up the `coreapp` VM network +(10.123.0.0/24). CoreDesktop's `update` keeps them current. + +## A Core in a VM + +1. Download the CoreServer image to + `~/.local/share/coreapp-dev/images/coreserver-enterprise-x86-26-05-12.qcow2` + (see [PLAN.md](PLAN.md) for the URL). +2. Put the SSH public key from Federated's dashboard (Cores) in + `~/.config/coreapp-dev/authorized_keys`, so the Provisioner can log in. +3. Create the VM: + ``` + scripts/create-coreserver-vm.sh + ``` + It gets 10.123.0.10. Log in with + `ssh -i ~/.local/share/coreapp-dev/ssh/id_ed25519 root@10.123.0.10`. +4. Federated's Provisioner has to reach the VM over SSH, and the Core must be + reachable from the internet. Behind a home router, use + [corerelay](https://gitea.federated.computer/Federated_Computer/corerelay) + to give it a VPS's public IP. +5. Provision the Core with `core-create` + ([Provisioning a Core](https://documentation.federated.computer/self-hosted/cores/provisioning-a-core/)), + with the public IP as `ip`. + +## Files + +| Path | What it is | +|---|---| +| `install.sh`, `update.sh` | Run by CoreDesktop's `sibling add` and `update` | +| `libvirt/network-hook.sh` | NAT and firewall rules for the `coreapp` network | +| `scripts/create-coreserver-vm.sh` | Creates a CoreServer VM | +| [PLAN.md](PLAN.md), [TEST.md](TEST.md) | What's next, and how to test it | diff --git a/TEST.md b/TEST.md new file mode 100644 index 0000000..efc804b --- /dev/null +++ b/TEST.md @@ -0,0 +1,120 @@ +# Testing + +Manual checks for coreapp-dev. The scripts call `sudo`, `pacman`/`yay`, +`systemctl` and `virsh`, so they change the real system even when `HOME` is +a temporary folder. Run these on a machine or VM you don't mind changing. + +## Static checks + +```bash +cd ~/Projects/coreapp-dev +for f in install.sh update.sh scripts/*.sh; do bash -n "$f"; done +shellcheck -x install.sh update.sh scripts/*.sh +``` + +## install.sh + +1. Bring the system up to date first: `update`. Otherwise pacman may ask for + package versions the mirrors have deleted, and the download fails with a 404. +2. `bash install.sh`. It should: + - install whichever of `libvirt qemu-base virt-install dnsmasq edk2-ovmf + dmidecode` are missing + - enable and start `libvirtd.service` + - add you to the `libvirt` group + - define the `default` network if it's missing, set it to start at boot, + and start it + - print "coreapp-dev installed.", and the log-out note if your group + membership is new +3. Log out and back in, then check: + ```bash + id -nG | grep -w libvirt + systemctl is-active libvirtd + virsh -c qemu:///system net-info default # Active: yes, Autostart: yes + ls ~/.local/share/coreapp-dev/images + ``` +4. Run `bash install.sh` again. It must succeed, print "already enabled" and + change nothing. +5. Error paths: + - With CoreDesktop missing: `HOME=$(mktemp -d) bash install.sh` should say + it needs CoreDesktop and exit 1. + +## update.sh + +1. `bash update.sh` with everything already installed: no package installs, + no prompts apart from sudo, exits 0. +2. Stop the network (`virsh -c qemu:///system net-destroy default`) and run + `bash update.sh`: it should start the network again. + +## Through CoreDesktop's sibling command + +Once the repo is in git: + +```bash +export HOME="$(mktemp -d)" +mkdir -p "$HOME/.local/share" +ln -s ~/.local/share/coredesktop "$HOME/.local/share/coredesktop" +bash "$HOME/.local/share/coredesktop/sibling" add ~/Projects/coreapp-dev +bash "$HOME/.local/share/coredesktop/sibling" update +bash "$HOME/.local/share/coredesktop/sibling" add ~/Projects/coreapp-dev # re-run is safe +``` + +## The coreapp network + +After `bash update.sh`: + +```bash +virsh -c qemu:///system net-info coreapp # Active: yes, Autostart: yes +sudo ls -l /etc/libvirt/hooks/network.d/coreapp-dev +sudo nft list table ip coreapp-dev # masquerade for 10.123.0.0/24 +sudo iptables -S | grep coreapp-dev # 3 INPUT and 2 FORWARD accepts +grep virbr-coreapp /etc/ufw/user.rules # DHCP, DNS, route rules +``` + +- Run `bash update.sh` again: no changes, and no ufw rules added twice. (This + also checks that the `grep` patterns match how ufw writes its rules; if one + doesn't, `update` asks for sudo every time.) +- `virsh -c qemu:///system net-destroy coreapp`: the nft table and iptables + rules are gone. `net-start coreapp`: they're back. +- The hook on its own, in a throwaway namespace (no host changes): + ```bash + docker run --rm --cap-add NET_ADMIN -v "$PWD/libvirt/network-hook.sh:/hook:ro" \ + --entrypoint bash corerelay:local -c \ + '/hook coreapp started; /hook coreapp started; iptables -S | grep -c coreapp-dev; /hook coreapp stopped; iptables -S | grep -c coreapp-dev' + ``` + prints 5, then 0 (the `sysctl` error is expected inside a container). + +- Moving from the old network (192.168.123.0/24): with a VM running, + `bash update.sh` redefines `coreapp` but doesn't restart it, and says how. + With no VM running, it restarts it: `net-dumpxml coreapp` shows + `10.123.0.1` and the `dhcp-host=52:54:00:c0:4e:10,...` option, and + `sudo iptables -S | grep -c 192.168.123` prints 0. +- The reservation works: `virsh -c qemu:///system net-dhcp-leases coreapp` + lists `coreserver` at `10.123.0.10`. (dnsmasq still logs "cannot read + .../coreapp.hostsfile": that file is libvirt's, root-only and now empty.) +- From the workstation, the Core's own services answer directly (its + containers are on 192.168.0.0/16, so this fails if the VM network is in + 192.168.x.x): + `curl -sk -o /dev/null -w '%{http_code}\n' --resolve nextcloud.:443:10.123.0.10 https://nextcloud./` + prints 302. + +## CoreServer VM + +1. Put Federated's dashboard key in `~/.config/coreapp-dev/authorized_keys`. +2. `scripts/create-coreserver-vm.sh`. It says how many root keys it's + installing (2: coreapp-dev's and the dashboard's) and prints + `IP: 10.123.0.10` within about two minutes. +3. `virsh -c qemu:///system list`: `coreserver` is running. +4. `ssh -i ~/.local/share/coreapp-dev/ssh/id_ed25519 root@10.123.0.10` + logs in. Inside the VM: + - `cat /root/.ssh/authorized_keys` has both keys + - `df -h /federated` shows about 50 GB (the disk is 60 GB; `/` stays 10 GB) + - `ping -c3 1.1.1.1` and `getent hosts federated.computer` (internet and DNS) +5. To see the screen: `virt-viewer -c qemu:///system coreserver`, or + `virsh -c qemu:///system screenshot coreserver /tmp/s.png`. +6. Running the script again with the same name refuses with "already exists". +7. `systemctl is-enabled systemd-resolved` in the VM says `masked`. +8. Once the Core is installed: `virsh -c qemu:///system reboot coreserver`. + Within a few minutes all its containers are up again, including `pdns` + (`dig +short A @10.123.0.10` answers). +7. To clean up: + `virsh -c qemu:///system destroy coreserver && virsh -c qemu:///system undefine coreserver --remove-all-storage` diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..a8f3d8c --- /dev/null +++ b/install.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# +# Purpose : Install coreapp-dev, the Federated Core app SDK (run by +# CoreDesktop's `sibling add`). Safe to re-run. + +set -e + +SELF="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +COREDESKTOP="$HOME/.local/share/coredesktop" + +if [ ! -f "$COREDESKTOP/utils.sh" ]; then + echo "coreapp-dev needs CoreDesktop, which isn't installed at $COREDESKTOP." + exit 1 +fi + +if [ ! -e /dev/kvm ]; then + echo "/dev/kvm is missing: turn on virtualization (VT-x/AMD-V) in the firmware" + echo "settings, then run this again." + exit 1 +fi + +# Per-machine data (CoreServer images, VM state) lives outside the repo +mkdir -p "$HOME/.local/share/coreapp-dev/images" + +bash "$SELF/update.sh" + +echo +echo "coreapp-dev installed." +# This shell's groups are fixed at login, so a new membership isn't seen yet +if ! id -nG | tr ' ' '\n' | grep -qx libvirt; then + echo "Log out and back in so your new libvirt group membership takes effect." +fi diff --git a/libvirt/network-hook.sh b/libvirt/network-hook.sh new file mode 100755 index 0000000..67fc85f --- /dev/null +++ b/libvirt/network-hook.sh @@ -0,0 +1,74 @@ +#!/bin/bash +# +# Purpose : libvirt network hook for coreapp-dev's `coreapp` network, installed +# as /etc/libvirt/hooks/network.d/coreapp-dev by update.sh. libvirt +# runs it as root with: (XML on stdin). +# +# The coreapp network is in libvirt's "open" mode: libvirt adds no firewall +# rules for it. Its "nat" mode would reject new connections from outside to +# the guests, which corerelay needs to make. This adds what the guests need +# when the network starts, and removes it when it stops: +# - NAT for the guests' traffic out to the internet +# - DHCP and DNS from the guests to libvirt's dnsmasq on the host +# - forwarding of the guests' traffic, and the replies to it +# Connections from outside to a guest are left to whoever forwards them +# (corerelay accepts its own tunnel traffic). + +NETWORK=coreapp +BRIDGE=virbr-coreapp +SUBNET=10.123.0.0/24 +TABLE=coreapp-dev +COMMENT=coreapp-dev + +[ "$1" = "$NETWORK" ] || exit 0 + +ipt_rules() { + # $1: -I to add (if missing), -D to delete + local action=$1 rule + local rules=( + "INPUT -i $BRIDGE -p udp --dport 67" + "INPUT -i $BRIDGE -p udp --dport 53" + "INPUT -i $BRIDGE -p tcp --dport 53" + "FORWARD -i $BRIDGE -s $SUBNET" + "FORWARD -o $BRIDGE -d $SUBNET -m conntrack --ctstate RELATED,ESTABLISHED" + ) + command -v iptables > /dev/null || return 0 + if [ "$action" = -D ]; then + # Every rule with our comment, also ones for an earlier subnet + local chain line + for chain in INPUT FORWARD; do + iptables -w -S "$chain" | grep -F -- "--comment $COMMENT " | while read -r line; do + # shellcheck disable=SC2086 + iptables -w ${line/#-A/-D} + done + done + return 0 + fi + for rule in "${rules[@]}"; do + # shellcheck disable=SC2086 + iptables -w -C $rule -m comment --comment "$COMMENT" -j ACCEPT 2> /dev/null \ + || iptables -w -I $rule -m comment --comment "$COMMENT" -j ACCEPT + done +} + +case "$2" in + started) + sysctl -q -w net.ipv4.ip_forward=1 + nft delete table ip "$TABLE" 2> /dev/null + nft -f - << EOF +table ip $TABLE { + chain postrouting { + type nat hook postrouting priority srcnat; policy accept; + ip saddr $SUBNET ip daddr != $SUBNET masquerade + } +} +EOF + ipt_rules -D + ipt_rules -I + ;; + stopped) + nft delete table ip "$TABLE" 2> /dev/null + ipt_rules -D + ;; +esac +exit 0 diff --git a/scripts/create-coreserver-vm.sh b/scripts/create-coreserver-vm.sh new file mode 100755 index 0000000..69e85f1 --- /dev/null +++ b/scripts/create-coreserver-vm.sh @@ -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 diff --git a/update.sh b/update.sh new file mode 100644 index 0000000..564d670 --- /dev/null +++ b/update.sh @@ -0,0 +1,176 @@ +#!/bin/bash +# +# Purpose : Keep coreapp-dev's host software current (run by CoreDesktop's +# `update`, and by install.sh). Idempotent; never prompts except for +# sudo passwords. + +set -e + +SELF="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +COREDESKTOP="$HOME/.local/share/coredesktop" +# shellcheck source=/dev/null +source "$COREDESKTOP/utils.sh" + +LIBVIRT_URI="qemu:///system" + +# Software for running a CoreServer VM on local KVM +PACKAGES=( + libvirt # VM management daemon and virsh + qemu-base # QEMU/KVM, headless + virt-install + dnsmasq # DHCP/DNS for libvirt's default NAT network + edk2-ovmf # UEFI firmware for guests + dmidecode # host hardware info for libvirt +) + +echo "Checking coreapp-dev's packages..." +install_packages "${PACKAGES[@]}" + +enable_services --now libvirtd.service +if ! systemctl is-active --quiet libvirtd.service; then + echo "Starting libvirtd.service..." + sudo systemctl start libvirtd.service +fi + +# Members of the libvirt group can manage system VMs without sudo +if ! id -nG "$USER" | tr ' ' '\n' | grep -qx libvirt; then + echo "Adding $USER to the libvirt group..." + sudo usermod -aG libvirt "$USER" +fi + +# libvirt's default NAT network (192.168.122.0/24), started now and at boot. +# sudo, because the group above only applies after the next login. +if ! sudo virsh -c "$LIBVIRT_URI" net-info default &> /dev/null; then + echo "Defining libvirt's default network..." + net_xml="$(mktemp)" + cat > "$net_xml" << 'EOF' + + default + + + + + + + + +EOF + sudo virsh -c "$LIBVIRT_URI" net-define "$net_xml" > /dev/null + rm -f "$net_xml" +fi +if ! sudo virsh -c "$LIBVIRT_URI" net-info default | grep -qE '^Autostart:\s+yes'; then + echo "Starting libvirt's default network at boot..." + sudo virsh -c "$LIBVIRT_URI" net-autostart default > /dev/null +fi +if ! sudo virsh -c "$LIBVIRT_URI" net-info default | grep -qE '^Active:\s+yes'; then + echo "Starting libvirt's default network..." + sudo virsh -c "$LIBVIRT_URI" net-start default > /dev/null +fi + +# coreapp-dev's network for CoreServer VMs (10.123.0.0/24). It's in +# libvirt's "open" mode because "nat" mode rejects new connections from +# outside to the guests, and corerelay forwards those to the Core. The hook +# below adds the NAT and firewall rules that "nat" mode would have. +# CoreServer VMs named coreserver get MAC 52:54:00:c0:4e:10 and so always +# 10.123.0.10. +# +# Not 192.168.x.x: Federated Core puts its containers on 192.168.0.0/16, and +# they would send their replies to 192.168.x.x hosts onto their own bridge. +# The reservation is a dnsmasq option, not a libvirt : libvirt writes +# its hosts file readable by root only, and dnsmasq reads it after dropping +# to nobody, so the was ignored. Version 2 of this network is +# 10.123.0.0/24 with that option; version 1 was 192.168.123.0/24. +HOOK=/etc/libvirt/hooks/network.d/coreapp-dev +if ! sudo cmp -s "$SELF/libvirt/network-hook.sh" "$HOOK"; then + echo "Installing coreapp-dev's libvirt network hook..." + new_hook=1 + sudo test -e "$HOOK" && new_hook=0 + sudo install -D -m 755 "$SELF/libvirt/network-hook.sh" "$HOOK" + # libvirtd only finds hooks it saw when it started. Restarting it leaves + # running VMs running. + if [ "$new_hook" = 1 ]; then + echo "Restarting libvirtd so it uses the hook..." + sudo systemctl restart libvirtd.service + fi + # Apply a changed hook to a network that's already up + if sudo virsh -c "$LIBVIRT_URI" net-info coreapp 2> /dev/null | grep -qE '^Active:\s+yes'; then + sudo "$HOOK" coreapp started begin - < /dev/null + fi +fi +NET_MARK="dhcp-host=52:54:00:c0:4e:10,10.123.0.10,coreserver" +# Redefining an existing network needs its UUID, or libvirt sees a new +# network with a name that's taken +net_uuid=$(sudo virsh -c "$LIBVIRT_URI" net-uuid coreapp 2> /dev/null || true) +net_xml="$(mktemp)" +cat > "$net_xml" << EOF + + coreapp${net_uuid:+ + $net_uuid} + + + + + + + + + + + +EOF +if ! sudo virsh -c "$LIBVIRT_URI" net-info coreapp &> /dev/null; then + echo "Defining coreapp-dev's VM network..." + sudo virsh -c "$LIBVIRT_URI" net-define "$net_xml" > /dev/null +elif ! sudo virsh -c "$LIBVIRT_URI" net-dumpxml --inactive coreapp | grep -qF "$NET_MARK"; then + echo "Moving coreapp-dev's VM network to 10.123.0.0/24..." + sudo virsh -c "$LIBVIRT_URI" net-define "$net_xml" > /dev/null +fi +rm -f "$net_xml" +# A running network keeps its old addresses until it restarts. Restarting it +# cuts its VMs off, so only do that when none is connected. +if sudo virsh -c "$LIBVIRT_URI" net-info coreapp | grep -qE '^Active:\s+yes' \ + && ! sudo virsh -c "$LIBVIRT_URI" net-dumpxml coreapp | grep -qF "$NET_MARK"; then + if sudo virsh -c "$LIBVIRT_URI" net-dumpxml coreapp | grep -qE "connections='[1-9]"; then + echo "coreapp-dev's VM network moves to 10.123.0.0/24 when it restarts." + echo "Shut down its VMs and run update again, or restart it yourself:" + echo " virsh -c $LIBVIRT_URI net-destroy coreapp && virsh -c $LIBVIRT_URI net-start coreapp" + echo "(then restart the VMs)." + else + echo "Restarting coreapp-dev's VM network on its new addresses..." + sudo virsh -c "$LIBVIRT_URI" net-destroy coreapp > /dev/null + fi +fi +if ! sudo virsh -c "$LIBVIRT_URI" net-info coreapp | grep -qE '^Autostart:\s+yes'; then + echo "Starting coreapp-dev's VM network at boot..." + sudo virsh -c "$LIBVIRT_URI" net-autostart coreapp > /dev/null +fi +if ! sudo virsh -c "$LIBVIRT_URI" net-info coreapp | grep -qE '^Active:\s+yes'; then + echo "Starting coreapp-dev's VM network..." + sudo virsh -c "$LIBVIRT_URI" net-start coreapp > /dev/null +fi + +# ufw drops incoming and forwarded traffic by default, which blocks guests' +# DHCP/DNS requests to libvirt's dnsmasq and their traffic out. Allow both on +# each VM bridge. (The coreapp hook adds the same rules itself, but ufw keeps +# these across its reloads and reboots.) user.rules is world-readable, so this +# only asks for sudo when a rule is missing. +UFW_RULES=/etc/ufw/user.rules +if command -v ufw > /dev/null && grep -qs '^ENABLED=yes' /etc/ufw/ufw.conf; then + for bridge in virbr0 virbr-coreapp; do + if ! grep -qs "allow udp 67 0.0.0.0/0 any 0.0.0.0/0 in_$bridge" "$UFW_RULES"; then + echo "Allowing DHCP from VMs on $bridge through ufw..." + sudo ufw allow in on "$bridge" to any port 67 proto udp comment 'coreapp-dev VM DHCP' > /dev/null + fi + if ! grep -qs "allow any 53 0.0.0.0/0 any 0.0.0.0/0 in_$bridge" "$UFW_RULES"; then + echo "Allowing DNS from VMs on $bridge through ufw..." + sudo ufw allow in on "$bridge" to any port 53 comment 'coreapp-dev VM DNS' > /dev/null + fi + if ! grep -qs "route:allow any any 0.0.0.0/0 any 0.0.0.0/0 in_$bridge" "$UFW_RULES"; then + echo "Allowing VM internet traffic on $bridge through ufw..." + sudo ufw route allow in on "$bridge" comment 'coreapp-dev VM NAT' > /dev/null + fi + done +fi + +echo "coreapp-dev's host software is up to date."