Adds modules/vms, a generic module for long-lived QEMU VMs run directly as systemd units. VMs sit on a host bridge with IPv4 NAT out of the external interface, optional port forwards from the host's IPv4 address, and, when the host sets a routed IPv6 prefix, a public IPv6 address per VM derived from its index, routed as a /128 with an inbound port allowlist. Traffic from VMs to private ranges is dropped, as are new connections from VMs to the host itself. Ports on a VM's IPv6 address can instead be answered by the host, so a name whose AAAA is the VM can still be served by a reverse proxy on the host. All filtering and NAT for the VMs is in a single inet vms nftables table. nftables evaluates every table at a hook, so the rules apply under any host firewall: on nftables hosts the table joins the host ruleset, and elsewhere a vms-nftables unit loads it and reloads it atomically on change. The host only needs to not drop forwarded bridge traffic in its own rules. On hosts without nftables the rules file is checked with nft at build time, the same way the nftables module checks its ruleset. modules/vms defines the qemu-bridge-helper wrapper with mkDefault. gitea actions-vm defines the same wrapper on iceman, and its capabilities option concatenates across definitions into a string setcap rejects. The disk is seeded from a cloud image on first start, cloud-init config is delivered on a NoCloud ISO, and host directories are shared into the guest over virtiofs with guest root mapped to the vms user. ExecStop shuts the guest down through the guest agent, falls back to ACPI powerdown over QMP, and blocks until qemu exits so systemd never kills a running guest. The unit binds to the bridge device rather than to the unit that creates it, so it works with scripted networking or networkd: the VM shuts down cleanly if the bridge goes away and starts when it returns. It also requires the unit that loads its nftables table. Restart=always brings the VM back if the guest powers itself off. unifi-os runs a Debian 13 VM on iceman with 4 vCPU, 8 GiB RAM and a 64 GiB disk. cloud-init installs the guest agent, podman, slirp4netns and unattended-upgrades with automatic reboots; UniFi OS Server is installed by hand. iceman routes its /64 to VMs, giving this one 2a01:4f9:3100:1048::11, and forwards device inform (8080/tcp) and STUN (3478/udp). Caddy on iceman serves the UI at unifi.hillion.co.uk, on iceman's IPv4 and on the VM's IPv6 web ports. An hourly timer in the guest copies UniFi's auto-backups onto a backup share, which the host backs up with restic. The native UniFi Network Application on boron is removed: the unifi module, its location, its fixed ids and boron's inform and STUN firewall ports. iceman's eth0 gets a static link-local address. It had none, so the kernel could not solicit the IPv6 gateway for forwarded traffic: the gateway neighbour entry went FAILED and all IPv6 egress stopped until host traffic re-resolved it. Test plan: - CI - Built the iceman toplevel; qemu-bridge-helper has a single cap_net_admin+ep and bridge.conf allows both bridges - Booted the generated launcher, cidata ISO and stop script locally under KVM with user-mode networking in place of the bridge: cloud-init applied static IPv4/IPv6, installed packages and mounted the virtiofs share; guest root writes land on the host owned by the runner user - Stop via guest agent and via ACPI fallback (agent masked) both shut the guest down cleanly; guest-initiated poweroff exits qemu - Loaded the generated table in a network namespace with wan, host and VM namespaces: forwarded IPv4 ports reach the VM, other inbound IPv4 and non-allowlisted IPv6 ports are dropped, the VM egresses masqueraded and cannot reach private ranges or open connections to the host, the host can still connect to the VM, and host ports on the VM's IPv6 address reach a listener on the host; all of these differ from a control run without the table - Built an nftables host (cyclone) with a test VM added; the table passes the ruleset check
149 lines
4.4 KiB
Nix
149 lines
4.4 KiB
Nix
{ config, pkgs, lib, ... }:
|
|
|
|
let
|
|
cfg = config.custom.services.unifi-os;
|
|
vmCfg = config.custom.vms.instances.unifi-os;
|
|
autobackupDir = "/home/uosserver/.local/share/containers/storage/volumes/uosserver_var_lib_unifi/_data/backup/autobackup";
|
|
in
|
|
{
|
|
options.custom.services.unifi-os = {
|
|
enable = lib.mkEnableOption "UniFi OS Server VM";
|
|
};
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
# UniFi OS Server itself is installed by hand inside the VM and keeps
|
|
# itself updated. cloud-init only prepares the base system.
|
|
custom.vms.instances.unifi-os = {
|
|
index = 1;
|
|
vcpus = 4;
|
|
memoryMiB = 8 * 1024;
|
|
diskSize = "64G";
|
|
baseImage = pkgs.fetchurl {
|
|
url = "https://cloud.debian.org/images/cloud/trixie/20261103-2651/debian-13-genericcloud-amd64-20261103-2651.qcow2";
|
|
sha512 = "d25999de0770d4cde1be72c299ba3ad94eac7ea8fd4d18f8cc86c69df69337e2e8a91d64465a9bfd8f2a78c566fcbcb0842eacc91c09efdef72d719d9f13b535";
|
|
};
|
|
|
|
shares = [ "backup" ];
|
|
|
|
firewall = {
|
|
allowedTCPPorts = [
|
|
22 # SSH
|
|
8080 # device inform
|
|
];
|
|
allowedUDPPorts = [
|
|
3478 # STUN
|
|
];
|
|
};
|
|
# unifi.hillion.co.uk's AAAA is the VM's address, so the host's Caddy
|
|
# answers the web ports there as it does on the host's own IPv4.
|
|
hostPorts = {
|
|
tcp = [ 80 443 ];
|
|
udp = [ 443 ];
|
|
};
|
|
forwardPorts = [
|
|
{ proto = "tcp"; port = 8080; }
|
|
{ proto = "udp"; port = 3478; }
|
|
];
|
|
|
|
cloudConfig = {
|
|
users = [{
|
|
name = config.custom.user;
|
|
shell = "/bin/bash";
|
|
sudo = "ALL=(ALL) NOPASSWD:ALL";
|
|
lock_passwd = true;
|
|
ssh_authorized_keys = config.users.users.${config.custom.user}.openssh.authorizedKeys.keys;
|
|
}];
|
|
ssh_pwauth = false;
|
|
|
|
package_update = true;
|
|
package_upgrade = true;
|
|
packages = [
|
|
"qemu-guest-agent"
|
|
"unattended-upgrades"
|
|
"podman"
|
|
"slirp4netns"
|
|
"rsync"
|
|
];
|
|
|
|
write_files = [
|
|
{
|
|
path = "/etc/apt/apt.conf.d/20auto-upgrades";
|
|
content = ''
|
|
APT::Periodic::Update-Package-Lists "1";
|
|
APT::Periodic::Unattended-Upgrade "1";
|
|
'';
|
|
}
|
|
{
|
|
path = "/etc/apt/apt.conf.d/52unattended-upgrades-local";
|
|
content = ''
|
|
Unattended-Upgrade::Automatic-Reboot "true";
|
|
Unattended-Upgrade::Automatic-Reboot-Time "04:00";
|
|
'';
|
|
}
|
|
# UniFi only writes its auto-backups inside its container storage, which
|
|
# only the guest's root can copy onto the backup share.
|
|
{
|
|
path = "/etc/systemd/system/unifi-backup-copy.service";
|
|
content = ''
|
|
[Unit]
|
|
Description=Copy UniFi auto-backups to the backup share
|
|
RequiresMountsFor=/mnt/backup
|
|
ConditionPathIsDirectory=${autobackupDir}
|
|
|
|
[Service]
|
|
Type=oneshot
|
|
ExecStart=/usr/bin/rsync -a --no-owner --no-group --delete ${autobackupDir}/ /mnt/backup/autobackup/
|
|
'';
|
|
}
|
|
{
|
|
path = "/etc/systemd/system/unifi-backup-copy.timer";
|
|
content = ''
|
|
[Unit]
|
|
Description=Copy UniFi auto-backups to the backup share hourly
|
|
|
|
[Timer]
|
|
OnCalendar=hourly
|
|
Persistent=true
|
|
|
|
[Install]
|
|
WantedBy=timers.target
|
|
'';
|
|
}
|
|
];
|
|
|
|
runcmd = [
|
|
[ "systemctl" "start" "qemu-guest-agent" ]
|
|
[ "systemctl" "enable" "--now" "unifi-backup-copy.timer" ]
|
|
];
|
|
};
|
|
};
|
|
|
|
services.caddy = {
|
|
enable = true;
|
|
virtualHosts."unifi.hillion.co.uk".extraConfig = ''
|
|
reverse_proxy https://${vmCfg.ipv4Address}:11443 {
|
|
header_up Host {host}
|
|
transport http {
|
|
tls_insecure_skip_verify
|
|
}
|
|
}
|
|
'';
|
|
};
|
|
|
|
age.secrets."backups/unifi-os/restic/mig29" = {
|
|
rekeyFile = ../../secrets/restic/mig29.age;
|
|
};
|
|
|
|
services.restic.backups."unifi-os" = {
|
|
user = "root";
|
|
timerConfig = {
|
|
OnCalendar = "03:00";
|
|
RandomizedDelaySec = "60m";
|
|
};
|
|
repository = "rest:https://restic.${config.ogygia.domain}/mig29";
|
|
passwordFile = config.age.secrets."backups/unifi-os/restic/mig29".path;
|
|
paths = lib.attrValues vmCfg.sharePaths;
|
|
};
|
|
};
|
|
}
|