From 0654d4c7126d662e065b60e53695d5f299887d7d Mon Sep 17 00:00:00 2001 From: David Young Date: Thu, 3 Dec 2026 11:40:04 -0700 Subject: [PATCH] coreapp-vm command, uninstall.sh, stale-system check - bin/coreapp-vm replaces scripts/create-coreserver-vm.sh: create (downloads and checks the CoreServer image when it's missing, resumes an interrupted download), list, ip, ssh (own known_hosts), start, stop, delete (asks for the name). update.sh symlinks it into ~/.local/bin. - uninstall.sh removes coreapp-vm, the coreapp network, its hook and ufw rules; asks before deleting VMs, the image and keys. - install.sh runs CoreDesktop's update first when the system wasn't upgraded in the last day, as `sibling add` does. Co-Authored-By: Claude Opus 5.5 --- .gitignore | 1 + PLAN.md | 13 -- README.md | 51 +++++-- TEST.md | 53 +++++-- bin/coreapp-vm | 254 ++++++++++++++++++++++++++++++++ install.sh | 16 ++ scripts/create-coreserver-vm.sh | 103 ------------- uninstall.sh | 91 ++++++++++++ update.sh | 4 + 9 files changed, 443 insertions(+), 143 deletions(-) create mode 100755 bin/coreapp-vm delete mode 100755 scripts/create-coreserver-vm.sh create mode 100644 uninstall.sh diff --git a/.gitignore b/.gitignore index b3c81bb..e6534cb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ images/ *.qcow2 +.claude/ diff --git a/PLAN.md b/PLAN.md index bd435fe..88c806d 100644 --- a/PLAN.md +++ b/PLAN.md @@ -57,17 +57,4 @@ What we learned provisioning (2026-12-02/03): ## 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 index 085f3e4..0426b3e 100644 --- a/README.md +++ b/README.md @@ -17,22 +17,22 @@ 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. +and back in afterwards), sets up the `coreapp` VM network (10.123.0.0/24) and +installs the `coreapp-vm` command. 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 +1. 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: +2. Create the VM: ``` - scripts/create-coreserver-vm.sh + coreapp-vm create ``` - It gets 10.123.0.10. Log in with - `ssh -i ~/.local/share/coreapp-dev/ssh/id_ed25519 root@10.123.0.10`. + The first time, it downloads the CoreServer image (1.3 GB) to + `~/.local/share/coreapp-dev/images/`. The VM is called `coreserver` and + gets 10.123.0.10. +3. Log in with `coreapp-vm ssh`, or run a command on it: + `coreapp-vm ssh docker ps`. 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) @@ -41,11 +41,40 @@ and back in afterwards), and sets up the `coreapp` VM network ([Provisioning a Core](https://documentation.federated.computer/self-hosted/cores/provisioning-a-core/)), with the public IP as `ip`. +## Commands + +| Command | Does | +|---|---| +| `coreapp-vm create [name]` | Create a VM (downloads the image if it's missing) | +| `coreapp-vm list` | VMs, their state and address | +| `coreapp-vm ip [name]` | A VM's address | +| `coreapp-vm ssh [name] [cmd]` | Log in as root, or run a command | +| `coreapp-vm start` / `stop [name]` | Start / shut down | +| `coreapp-vm delete [name]` | Delete a VM and its disk (asks first) | + +`name` defaults to `coreserver`. Before deleting a VM that has a Core on it, +delete the Core in Federated's dashboard, or at least before putting another +VM behind the same public IP: deleting a Core makes Federated log in to its +IP and tear down whatever it finds there. + +## Uninstall + +`sibling remove` only deletes the clone, so first run + +``` +bash ~/.local/share/siblings/coreapp-dev/uninstall.sh +``` + +It removes `coreapp-vm`, the `coreapp` network and its firewall rules, and +asks before deleting VMs, the downloaded image and your keys. libvirt and QEMU +stay installed. + ## 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 | +| `bin/coreapp-vm` | Creates and manages CoreServer VMs (`coreapp-vm help`) | +| `uninstall.sh` | Removes what coreapp-dev set up | | [PLAN.md](PLAN.md), [TEST.md](TEST.md) | What's next, and how to test it | diff --git a/TEST.md b/TEST.md index efc804b..2f6caa6 100644 --- a/TEST.md +++ b/TEST.md @@ -8,14 +8,16 @@ a temporary folder. Run these on a machine or VM you don't mind changing. ```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 +for f in install.sh update.sh uninstall.sh bin/coreapp-vm libvirt/network-hook.sh; do bash -n "$f"; done +shellcheck -x install.sh update.sh uninstall.sh bin/coreapp-vm libvirt/network-hook.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. +1. If the system was last upgraded more than a day ago, `bash install.sh` + says so and runs `update` first (pacman's lists would otherwise name + versions the mirrors have deleted: 404s). Under `update` itself + (`COREDESKTOP_ALLOW_PACMAN=1`) it doesn't. 2. `bash install.sh`. It should: - install whichever of `libvirt qemu-base virt-install dnsmasq edk2-ovmf dmidecode` are missing @@ -100,21 +102,40 @@ grep virbr-coreapp /etc/ufw/user.rules # DHCP, DNS, route rules ## 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: +2. `coreapp-vm create`. Without the image, it downloads it first (1.3 GB; + interrupt it and run again: it resumes) and checks its SHA-256. 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. `coreapp-vm list`: `coreserver` is running at 10.123.0.10. +4. `coreapp-vm ssh` logs in (after cloud-init, a minute later). 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`. + - `systemctl is-enabled systemd-resolved` says `masked` +5. `coreapp-vm ssh hostname` prints `coreserver`; `coreapp-vm ip nope` says + there's no such VM. +6. `coreapp-vm create` again refuses with "already exists". +7. To see the screen: `virt-viewer -c qemu:///system coreserver`. 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` +9. `coreapp-vm stop`, then `coreapp-vm start`. +10. `coreapp-vm delete`: asks for the name; anything else keeps the VM. With + the name, the VM and `/var/lib/libvirt/images/coreserver.qcow2` are gone, + the image in `~/.local/share/coreapp-dev/images/` stays. + +## Uninstall + +On a machine you can rebuild (it removes the network every VM uses): + +1. `bash uninstall.sh` with a VM on the `coreapp` network, answering no: it + stops, and nothing is removed. +2. Again, answering yes: the VMs and their disks are gone, + `virsh -c qemu:///system net-list --all` has no `coreapp`, + `/etc/libvirt/hooks/network.d/coreapp-dev` is gone, + `sudo iptables -S | grep -c coreapp-dev` prints 0, + `grep -c virbr-coreapp /etc/ufw/user.rules` prints 0 (virbr0's rules stay), + and `coreapp-vm` isn't on `PATH`. +3. Answering no to the data questions keeps `~/.local/share/coreapp-dev` and + `~/.config/coreapp-dev`. +4. `bash install.sh` puts everything back. diff --git a/bin/coreapp-vm b/bin/coreapp-vm new file mode 100755 index 0000000..5663b32 --- /dev/null +++ b/bin/coreapp-vm @@ -0,0 +1,254 @@ +#!/usr/bin/env bash +# +# Purpose : Create and manage CoreServer VMs (Federated Core's server image) +# on local KVM, on coreapp-dev's `coreapp` network. +# +# coreapp-vm create [name] create a VM (downloads the image if missing) +# coreapp-vm list VMs and their addresses +# coreapp-vm ip [name] a VM's address +# coreapp-vm ssh [name] [cmd] log in as root (or run cmd on it) +# coreapp-vm start|stop [name] start / shut down +# coreapp-vm delete [name] delete a VM and its disk (asks first) +# +# name defaults to coreserver. Env overrides for create: VCPUS, RAM_MB, +# DISK_GB, NETWORK, MAC. + +set -euo pipefail + +URI="qemu:///system" +DATA="$HOME/.local/share/coreapp-dev" +IMAGE_NAME="coreserver-enterprise-x86-26-05-12.qcow2" +IMAGE_URL="https://nginx.core-x86builder.federatedcomputer.cloud/$IMAGE_NAME" +# Not published by Federated: the SHA-256 of our first download (2026-12-02) +IMAGE_SHA256="8c3a64e9be12121b71e1a3ad3dd7c6de686734b83ee77f3852d80cee1b6dd4a4" +BASE="$DATA/images/$IMAGE_NAME" +# 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" +KNOWN_HOSTS="$DATA/ssh/known_hosts" +EXTRA_KEYS="$HOME/.config/coreapp-dev/authorized_keys" +POOL="default" +POOL_DIR="/var/lib/libvirt/images" + +v() { virsh -c "$URI" "$@"; } + +usage() { + sed -n '/^# coreapp-vm /s/^# //p' "${BASH_SOURCE[0]}" + echo + echo "To see a VM's screen: virt-viewer -c $URI " +} + +exists() { v dominfo "$1" &> /dev/null; } + +need_vm() { + exists "$1" || { echo "No VM named '$1'. coreapp-vm list shows them." >&2; exit 1; } +} + +# The VM's IPv4 address, from libvirt's DHCP leases; nothing if it has none +vm_ip() { + v domifaddr "$1" 2> /dev/null | awk '/ipv4/ {split($4, a, "/"); print a[1]; exit}' +} + +download_image() { + [[ -f "$BASE" ]] && return 0 + echo "Downloading the CoreServer image (1.3 GB) to $BASE..." + mkdir -p "$(dirname "$BASE")" + # Resumes a download that was interrupted + curl -fL --retry 3 --progress-bar -C - -o "$BASE.part" "$IMAGE_URL" + echo "Checking the download..." + if [[ "$(sha256sum "$BASE.part" | cut -d' ' -f1)" != "$IMAGE_SHA256" ]]; then + rm -f "$BASE.part" + echo "The download isn't the image coreapp-dev was tested with (SHA-256 differs)." >&2 + echo "Deleted it; run create again to retry." >&2 + exit 1 + fi + mv "$BASE.part" "$BASE" +} + +cmd_create() { + local name="$1" + local vcpus="${VCPUS:-4}" ram_mb="${RAM_MB:-8192}" disk_gb="${DISK_GB:-60}" + local network="${NETWORK:-coreapp}" mac="${MAC:-}" + local vol="$name.qcow2" user_data ip + # coreapp's DHCP gives this MAC 10.123.0.10, corerelay's default target + if [[ "$name" == coreserver && -z "$mac" ]]; then mac=52:54:00:c0:4e:10; fi + + if exists "$name"; then + echo "VM '$name' already exists." >&2; exit 1 + fi + if v vol-info "$vol" --pool "$POOL" &> /dev/null; then + echo "Disk '$POOL_DIR/$vol' already exists; coreapp-vm delete $name, or pick another name." >&2 + exit 1 + fi + if ! v net-info "$network" &> /dev/null; then + echo "Network '$network' doesn't exist. Run coreapp-dev's update.sh (or CoreDesktop's update)." >&2 + exit 1 + fi + if [[ ! -f "$EXTRA_KEYS" ]]; then + echo "Note: $EXTRA_KEYS is missing. Put the SSH key from Federated's dashboard" + echo "(Cores) there before creating the VM, or the Provisioner can't log in." + fi + + download_image + + 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 ! v pool-info "$POOL" &> /dev/null; then + v pool-define-as "$POOL" dir --target "$POOL_DIR" > /dev/null + v pool-build "$POOL" > /dev/null 2>&1 || true + fi + v pool-start "$POOL" > /dev/null 2>&1 || true + v pool-autostart "$POOL" > /dev/null + v pool-refresh "$POOL" > /dev/null + + # Copy the pristine image (keeps the download reusable), then grow it. + # The image's grow-federated.service gives the extra space to /federated. + echo "Copying the image to a ${disk_gb} GB disk..." + v vol-create-as "$POOL" "$vol" "${disk_gb}G" --format qcow2 > /dev/null + v vol-upload "$vol" "$BASE" --pool "$POOL" --sparse + v vol-resize "$vol" "${disk_gb}G" --pool "$POOL" > /dev/null + + v net-start "$network" &> /dev/null || true + + # The image needs a display device: it stalls at "Loading initial ramdisk" + # with --graphics none + virt-install --connect "$URI" \ + --name "$name" \ + --vcpus "$vcpus" --cpu host-passthrough \ + --memory "$ram_mb" \ + --disk "path=$POOL_DIR/$vol,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 > /dev/null + + echo "Created '$name'. Waiting for an IP..." + for _ in $(seq 1 60); do + ip="$(vm_ip "$name")" + if [[ -n "$ip" ]]; then + echo "IP: $ip" + echo "Log in: coreapp-vm ssh$([[ "$name" == coreserver ]] || echo " $name") (cloud-init needs a minute more)" + exit 0 + fi + sleep 2 + done + echo "No IP yet. Watch the screen: virt-viewer -c $URI $name" >&2 + exit 1 +} + +cmd_list() { + local name state ip + printf '%-20s %-10s %s\n' NAME STATE IP + while read -r name; do + [[ -n "$name" ]] || continue + state="$(v domstate "$name" 2> /dev/null)" + ip="" + [[ "$state" == running ]] && ip="$(vm_ip "$name")" + printf '%-20s %-10s %s\n' "$name" "$state" "$ip" + done < <(v list --all --name) +} + +cmd_ip() { + local ip + need_vm "$1" + ip="$(vm_ip "$1")" + [[ -n "$ip" ]] || { echo "'$1' has no address (is it running?)" >&2; exit 1; } + echo "$ip" +} + +cmd_ssh() { + local name="$1" ip + shift + ip="$(cmd_ip "$name")" + # Its own known_hosts: a re-created VM gets a new host key at the same IP + exec ssh -i "$SSH_KEY" -o UserKnownHostsFile="$KNOWN_HOSTS" \ + -o StrictHostKeyChecking=accept-new "root@$ip" "$@" +} + +cmd_start() { + need_vm "$1" + if [[ "$(v domstate "$1")" == running ]]; then echo "'$1' is already running."; return; fi + v start "$1" > /dev/null + echo "Started '$1'." +} + +cmd_stop() { + need_vm "$1" + if [[ "$(v domstate "$1")" != running ]]; then echo "'$1' isn't running."; return; fi + v shutdown "$1" > /dev/null + echo -n "Shutting down '$1'..." + for _ in $(seq 1 60); do + if [[ "$(v domstate "$1")" == "shut off" ]]; then echo " done."; return; fi + sleep 2 + done + echo + echo "Still running after 2 minutes. To force it off: virsh -c $URI destroy $1" >&2 + exit 1 +} + +cmd_delete() { + local name="$1" ip answer + need_vm "$name" + ip="$(vm_ip "$name")" + echo "This deletes VM '$name' and its disk $POOL_DIR/$name.qcow2, with everything" + echo "on it. The downloaded image is kept." + echo + echo "If a Federated Core was provisioned on it, delete that Core in the dashboard" + echo "before you put another VM behind the same public IP: deleting a Core makes" + echo "Federated log in to its IP and tear down whatever it finds there." + echo + read -r -p "Type the VM's name to delete it: " answer + [[ "$answer" == "$name" ]] || { echo "Not deleted."; exit 1; } + if [[ "$(v domstate "$name")" == running ]]; then v destroy "$name" > /dev/null; fi + v undefine "$name" --nvram > /dev/null 2>&1 || v undefine "$name" > /dev/null + v vol-delete "$name.qcow2" --pool "$POOL" > /dev/null 2>&1 || true + if [[ -n "$ip" && -f "$KNOWN_HOSTS" ]]; then + ssh-keygen -q -R "$ip" -f "$KNOWN_HOSTS" > /dev/null 2>&1 || true + rm -f "$KNOWN_HOSTS.old" + fi + echo "Deleted '$name'." +} + +cmd="${1:-help}" +[[ $# -gt 0 ]] && shift +case "$cmd" in + create) cmd_create "${1:-coreserver}" ;; + list | ls) cmd_list ;; + ip) cmd_ip "${1:-coreserver}" ;; + ssh) + # The first argument is a VM's name if there's a VM by that name + name=coreserver + if [[ $# -gt 0 ]] && exists "$1"; then name="$1"; shift; fi + cmd_ssh "$name" "$@" + ;; + start) cmd_start "${1:-coreserver}" ;; + stop) cmd_stop "${1:-coreserver}" ;; + delete | rm) cmd_delete "${1:-coreserver}" ;; + help | -h | --help) usage ;; + *) usage >&2; exit 1 ;; +esac diff --git a/install.sh b/install.sh index a8f3d8c..c7e769f 100644 --- a/install.sh +++ b/install.sh @@ -13,6 +13,22 @@ if [ ! -f "$COREDESKTOP/utils.sh" ]; then exit 1 fi +# Packages are installed from pacman's package lists. If the system hasn't +# been upgraded for a day, they may name versions the mirrors have deleted +# (404s). `sibling add` checks this itself; this is for running install.sh by +# hand. Only CoreDesktop's update may upgrade the system. +if [ -z "${COREDESKTOP_ALLOW_PACMAN:-}" ]; then + last="$(grep -F '[PACMAN] starting full system upgrade' /var/log/pacman.log 2> /dev/null | tail -n 1)" + last="${last#\[}" + last="${last%%]*}" + if [ -z "$last" ] || ! last="$(date -d "$last" +%s 2> /dev/null)" \ + || (( ($(date +%s) - last) / 3600 >= 24 )); then + echo "This system hasn't been upgraded today. Running update first, so" + echo "coreapp-dev installs current packages..." + "$HOME/.local/bin/update" || echo "update reported errors (above); installing anyway." + fi +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." diff --git a/scripts/create-coreserver-vm.sh b/scripts/create-coreserver-vm.sh deleted file mode 100755 index 69e85f1..0000000 --- a/scripts/create-coreserver-vm.sh +++ /dev/null @@ -1,103 +0,0 @@ -#!/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/uninstall.sh b/uninstall.sh new file mode 100644 index 0000000..057654a --- /dev/null +++ b/uninstall.sh @@ -0,0 +1,91 @@ +#!/bin/bash +# +# Purpose : Remove what coreapp-dev set up. CoreDesktop's `sibling remove` +# only deletes the clone, so run this first: +# bash ~/.local/share/siblings/coreapp-dev/uninstall.sh +# Asks before deleting VMs, the downloaded image or keys. Keeps +# libvirt, QEMU and libvirt's default network, which other VMs may +# use. Safe to re-run. + +set -e + +URI="qemu:///system" +NETWORK=coreapp +BRIDGE=virbr-coreapp +HOOK=/etc/libvirt/hooks/network.d/coreapp-dev +DATA="$HOME/.local/share/coreapp-dev" +CONFIG="$HOME/.config/coreapp-dev" + +ask() { + # $1: question. Yes only if answered y; no when not run in a terminal. + local answer=n + [ -t 0 ] && read -r -p "$1 [y/N] " answer + [[ "$answer" =~ ^[Yy] ]] +} + +v() { sudo virsh -c "$URI" "$@"; } + +# VMs attached to the coreapp network +vms=() +if command -v virsh > /dev/null && v net-info "$NETWORK" &> /dev/null; then + while read -r name; do + [ -n "$name" ] || continue + if v domiflist "$name" 2> /dev/null | awk '{print $3}' | grep -qx "$NETWORK"; then + vms+=("$name") + fi + done < <(v list --all --name) +fi +if [ ${#vms[@]} -gt 0 ]; then + echo "These VMs use coreapp-dev's network: ${vms[*]}" + echo "If a Federated Core was provisioned on one, delete that Core in the" + echo "dashboard before you put another machine behind the same public IP." + if ask "Delete these VMs and their disks?"; then + for name in "${vms[@]}"; do + if [ "$(v domstate "$name")" = running ]; then v destroy "$name" > /dev/null; fi + disks=$(v domblklist "$name" --details | awk '$2 == "disk" {print $4}') + v undefine "$name" --nvram > /dev/null 2>&1 || v undefine "$name" > /dev/null + for disk in $disks; do v vol-delete "$disk" > /dev/null 2>&1 || true; done + echo "Deleted VM $name." + done + else + echo "Kept them, so the network stays too. Delete them with coreapp-vm delete" + echo " and run this again." + exit 1 + fi +fi + +rm -f "$HOME/.local/bin/coreapp-vm" + +if command -v virsh > /dev/null && v net-info "$NETWORK" &> /dev/null; then + echo "Removing coreapp-dev's VM network..." + # Stopping it runs the hook, which removes its NAT and firewall rules + v net-destroy "$NETWORK" > /dev/null 2>&1 || true + v net-undefine "$NETWORK" > /dev/null +fi +if sudo test -e "$HOOK"; then + echo "Removing coreapp-dev's libvirt network hook..." + sudo rm -f "$HOOK" +fi + +# The ufw rules update.sh added for the coreapp bridge (virbr0's stay, for +# libvirt's default network) +if command -v ufw > /dev/null && grep -qs "in_$BRIDGE" /etc/ufw/user.rules; then + echo "Removing coreapp-dev's ufw rules for $BRIDGE..." + sudo ufw delete allow in on "$BRIDGE" to any port 67 proto udp > /dev/null || true + sudo ufw delete allow in on "$BRIDGE" to any port 53 > /dev/null || true + sudo ufw route delete allow in on "$BRIDGE" > /dev/null || true +fi + +if [ -d "$DATA" ] && ask "Delete the CoreServer image and coreapp-dev's SSH key in $DATA?"; then + rm -rf "$DATA" +elif [ -d "$DATA" ]; then + echo "Kept $DATA" +fi +if [ -d "$CONFIG" ] && ask "Delete coreapp-dev's settings (the dashboard's SSH key) in $CONFIG?"; then + rm -rf "$CONFIG" +elif [ -d "$CONFIG" ]; then + echo "Kept $CONFIG" +fi + +echo "coreapp-dev removed. libvirt and QEMU are still installed; remove them" +echo "with pacman if nothing else uses them. Then: sibling remove coreapp-dev" diff --git a/update.sh b/update.sh index 564d670..ffa3324 100644 --- a/update.sh +++ b/update.sh @@ -173,4 +173,8 @@ if command -v ufw > /dev/null && grep -qs '^ENABLED=yes' /etc/ufw/ufw.conf; then done fi +# The coreapp-vm command: a symlink into this clone, so pulling updates it +mkdir -p "$HOME/.local/bin" +ln -sfn "$SELF/bin/coreapp-vm" "$HOME/.local/bin/coreapp-vm" + echo "coreapp-dev's host software is up to date."