Files
davidandClaude Opus 5.5 b405e35ea8 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>
2026-12-03 11:27:23 -07:00

75 lines
2.3 KiB
Bash
Executable File

#!/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: <network> <operation> <sub-op> (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