#!/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
# 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."