From 2a105534834bba3ad140a572cec7c546be0a5faa Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 12 Jan 2026 17:17:27 -0800 Subject: wg-reallyquick: specify netns and interface name separately Previously, the interface name is hard coupled to the .conf filename. And the netns name is the same as interface. Now, both can be overriden in the [Interface] section with Name= and NetNS= --- wg-reallyquick | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/wg-reallyquick b/wg-reallyquick index 8762b64..e3f5093 100644 --- a/wg-reallyquick +++ b/wg-reallyquick @@ -26,6 +26,7 @@ parse_options() { CONFIG_FILE="$(readlink -f "$CONFIG_FILE")" ((($(stat -c '0%#a' "$CONFIG_FILE") & $(stat -c '0%#a' "${CONFIG_FILE%/*}") & 0007) == 0)) || echo "Warning: \`$CONFIG_FILE' is world accessible" >&2 INTERFACE="${BASH_REMATCH[2]}" + NETNS="$INTERFACE" shopt -s nocasematch while read -r line || [[ -n $line ]]; do stripped="${line%%\#*}" @@ -35,6 +36,16 @@ parse_options() { [[ $key == "[Interface]" ]] && interface_section=1 if [[ $interface_section -eq 1 ]]; then case "$key" in + # Allow user to additionally specify interface name + # (if unspecified, use filename, see use of BASH_REMATCH above) + Name) + INTERFACE="$value" + continue ;; + # Allow use to specify netns + # (if unspecified, use $INTERFACE) + NetNS) + NETNS="$value" + continue ;; Address) ADDRESSES+=( ${value//,/ } ) continue ;; @@ -56,23 +67,23 @@ parse_options() { add_addr() { local proto=-4 [[ $1 == *:* ]] && proto=-6 - ip -n $NETNS $proto address add "$1" dev "$INTERFACE" + ip -n "$NETNS" $proto address add "$1" dev "$INTERFACE" } up() { - ip netns add $NETNS - ip link add wgvpn0 type wireguard - ip link set wgvpn0 netns $NETNS - ip netns exec $NETNS wg setconf wgvpn0 <(echo "$WG_CONFIG") + ip netns add "$NETNS" + ip link add "$INTERFACE" type wireguard + ip link set "$INTERFACE" netns "$NETNS" + ip netns exec "$NETNS" wg setconf "$INTERFACE" <(echo "$WG_CONFIG") for i in "${ADDRESSES[@]}"; do add_addr "$i" done if [[ -n $MTU ]]; then - ip -n $NETNS link set mtu "$MTU" up dev wgvpn0 + ip -n "$NETNS" link set mtu "$MTU" up dev "$INTERFACE" fi - ip -n $NETNS link set lo up - ip -n $NETNS link set wgvpn0 up - ip -n $NETNS route add default dev wgvpn0 + ip -n "$NETNS" link set lo up + ip -n "$NETNS" link set "$INTERFACE" up + ip -n "$NETNS" route add default dev "$INTERFACE" mkdir -p "/etc/netns/$NETNS" { @@ -82,17 +93,16 @@ up() { } down() { - ip -n $NETNS link del wgvpn0 - ip netns del $NETNS + ip -n "$NETNS" link del "$INTERFACE" + ip netns del "$NETNS" rm -rf "/etc/netns/$NETNS" } COMMAND="$1" parse_options "$2" -NETNS="${3:-$INTERFACE}" +echo "interface: $INTERFACE" echo "netns: $NETNS" -INTERFACE="wgvpn0" #TODO un-hardcode this case "$COMMAND" in up) up "$@" ;; -- cgit v1.3.1