Files
makronix/tools/host-nfs-setup.sh
lotherkandClaude Sonnet 5.5 73c9de51a6 licence: the CNCL line in every file Makronix wrote (tools, docs, www, mobile, top level)
consmirror.c: the BSD notice is replaced (Makronix's own file).  Files with
changes of other sessions pending are left for their commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:50:26 +00:00

144 lines
5.5 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
# host-nfs-setup.sh -- export the Makronix tree over NFS to the build VM.
#
# usage: sudo tools/host-nfs-setup.sh [install|remove]
#
# Runs ON THE HOST (Debian), as root. The VM (docs/BUILD-VM.md) reaches the
# host at 10.0.2.2 through QEMU's user-mode network; those connections arrive
# on the host's loopback, from 127.0.0.1.
#
# What is and is not restricted to loopback (measured 2026-11-07, first run):
#
# - The export is for 127.0.0.1 only (exports(5)). That limits who can
# mount, not who can reach the ports.
# - nfsd and mountd listen on ALL addresses (/proc/fs/nfsd/portlist showed
# `tcp 2049' twice, IPv4 and IPv6, after a restart; `host=' in
# /etc/nfs.conf.d did not change that); mountd takes a random port unless
# `port=' is set.
# - rpcbind is NOT touched by this script: it is socket activated on all
# addresses and the desktop of the host depends on it (CDE: ToolTalk). It
# was running before NFS was installed; the first version of this script
# restarted it, which it must not do. No restart, no rule, no ordering.
# - So the ports are closed to everything but loopback by a firewall rule:
# an nftables table `makronix_nfs' (priority -10, before other filters)
# drops tcp and udp 2049 and 20048 on every interface but lo, and a
# systemd unit loads it before nfs-server starts. mountd is
# pinned to 20048 so that the rule covers it. Without nftables the
# script refuses to install: it does not leave NFS open.
#
# install installs nfs-kernel-server, exports the tree, closes the ports,
# starts the server
# remove takes the export, the rule and the unit away and stops the server
#
# Files it writes: /etc/exports (one line), /etc/nfs.conf.d/makronix.conf,
# /etc/makronix-nfs.nft, /etc/systemd/system/makronix-nfs-firewall.service.
#
# In the guest: mount_nfs -3 -T 10.0.2.2:<tree> /host
# Files that root of the guest writes belong to the user ANONUID here.
set -eu
TREE=$(cd "$(dirname "$0")/.." && pwd)
ANONUID=${SUDO_UID:-1000}
ANONGID=${SUDO_GID:-1000}
OLDLINE="$TREE 127.0.0.1(rw,sync,no_subtree_check,all_squash,anonuid=$ANONUID,anongid=$ANONGID)"
# insecure: QEMU's user-mode network opens the connection from the host side with
# a port above 1024; the default `secure' refuses it ("Permission denied" from
# mount_nfs, measured 2026-11-07). Consequence, stated: any process on the host
# can mount the tree from 127.0.0.1 and then acts as user $ANONUID in it.
LINE="$TREE 127.0.0.1(rw,sync,no_subtree_check,insecure,all_squash,anonuid=$ANONUID,anongid=$ANONGID)"
CONFD=/etc/nfs.conf.d/makronix.conf
NFT=/etc/makronix-nfs.nft
UNIT=/etc/systemd/system/makronix-nfs-firewall.service
PORTS="2049, 20048"
[ "$(id -u)" = 0 ] || { echo "run as root: sudo $0 ${1:-install}" >&2; exit 1; }
case ${1:-install} in
install)
if ! command -v nft > /dev/null 2>&1; then
echo "nft (nftables) is not installed: refusing to open NFS without the" >&2
echo "firewall rule. apt install nftables, then run this again." >&2
exit 1
fi
if ! command -v exportfs > /dev/null 2>&1; then
DEBIAN_FRONTEND=noninteractive apt-get install -y nfs-kernel-server
fi
# the firewall first, so that the ports are never open without it
cat > "$NFT" <<EOF
# Makronix build VM (tools/host-nfs-setup.sh): NFS is for loopback only
table inet makronix_nfs {
chain input {
type filter hook input priority -10; policy accept;
iifname != "lo" tcp dport { $PORTS } drop
iifname != "lo" udp dport { $PORTS } drop
}
}
EOF
cat > "$UNIT" <<EOF
[Unit]
Description=Makronix: NFS ports closed to everything but loopback
Before=nfs-server.service nfs-mountd.service
DefaultDependencies=no
After=network-pre.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStartPre=-/usr/sbin/nft delete table inet makronix_nfs
ExecStart=/usr/sbin/nft -f $NFT
ExecStop=/usr/sbin/nft delete table inet makronix_nfs
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable makronix-nfs-firewall.service
systemctl restart makronix-nfs-firewall.service
mkdir -p /etc/nfs.conf.d
cat > "$CONFD" <<EOF
# Makronix build VM (tools/host-nfs-setup.sh): mountd on a fixed port, so that
# the firewall rule covers it
[mountd]
port=20048
EOF
if grep -qxF "$OLDLINE" /etc/exports 2>/dev/null; then
grep -vxF "$OLDLINE" /etc/exports > /etc/exports.new || true
cat /etc/exports.new > /etc/exports
rm -f /etc/exports.new
fi
grep -qxF "$LINE" /etc/exports 2>/dev/null || echo "$LINE" >> /etc/exports
systemctl enable --now nfs-server
systemctl restart nfs-mountd nfs-server 2>/dev/null || systemctl restart nfs-server
exportfs -ra
echo "--- exports:"
exportfs -v
echo "--- the rule:"
nft list table inet makronix_nfs
echo "--- mountd, rpcbind:"
rpcinfo -p 127.0.0.1 2>/dev/null | grep -E 'mountd|nfs' | head -4 || true
;;
remove)
systemctl disable --now makronix-nfs-firewall.service 2>/dev/null || true
rm -f "$UNIT" "$NFT" "$CONFD"
systemctl daemon-reload
nft delete table inet makronix_nfs 2>/dev/null || true
if [ -f /etc/exports ]; then
grep -vxF "$OLDLINE" /etc/exports | grep -vxF "$LINE" > /etc/exports.new || true
cat /etc/exports.new > /etc/exports
rm -f /etc/exports.new
fi
exportfs -ra 2>/dev/null || true
systemctl disable --now nfs-server 2>/dev/null || true
echo "export, rule and unit removed, nfs-server stopped and disabled."
echo "left in place: the package nfs-kernel-server (apt remove it if unwanted)."
;;
*)
echo "usage: $0 [install|remove]" >&2
exit 1
;;
esac