From 5706845d3348a1509c91ecfddbdb239b702d8166 Mon Sep 17 00:00:00 2001 From: "CanbiZ (MickLesk)" <47820557+MickLesk@users.noreply.github.com> Date: Fri, 20 Nov 2026 20:46:37 +0100 Subject: [PATCH] Refactor: Tailscale Script (#18868) * Put add-tailscale-lxc's tun lines into the container, not its last snapshot Snapshots follow a container's settings as [name] sections, so the two lxc lines appended with >> landed in the last snapshot: pct config never showed them, the container got no /dev/net/tun and tailscaled could not start. The grep that skips an existing line also matched the snapshot's copy on a second run. Check and insert them in the container's own section. * Keep add-tailscale-lxc's container output in the log and the trace The install ran as a plain pct exec, so its output only reached the terminal and every failure was reported as "pct exited with status 100" with nothing after the step. It now runs through $STD: the output goes to the run log, and a failure shows it and puts it in the trace. Inside the container the helpers ran in silent mode as well, which kept apt's own error in a log there; VERBOSE=yes prints it instead. A failed download of core.func or tools.func was sourced as an empty file and ended in "load_functions: command not found" (exit 127, 7 of the 22 failed runs in four days). It now stops with exit 7 and says the container could not download them. * Make add-tailscale-lxc's DNS fallback trigger, and always undo it dig +short prints its own errors (";; no servers could be reached") on stdout, and the check counted any line that was not 127.x or 0.0.0.0 as an address, so with a dead resolver the fallback to 1.1.1.1 never ran and the install failed further on. Count only address lines. The original resolv.conf was put back only after a successful install; any failure left the container on 1.1.1.1, and a second run then saved that as the backup. Restore it on exit. * Do not fail add-tailscale-lxc over the tag once Tailscale is installed pct set -tags ran last under set -e, so a container locked by a running backup ("CT is locked (backup)", exit 255) failed the whole run after Tailscale had been installed. The tag is cosmetic: warn with pct's message instead. Read the tags from pct config so a snapshot's copy is not added, and skip the tag when it is already there. --- tools/addon/add-tailscale-lxc.sh | 46 ++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/tools/addon/add-tailscale-lxc.sh b/tools/addon/add-tailscale-lxc.sh index ba7d2fa4c..a50345d25 100644 --- a/tools/addon/add-tailscale-lxc.sh +++ b/tools/addon/add-tailscale-lxc.sh @@ -89,13 +89,22 @@ esac CTID_CONFIG_PATH="/etc/pve/lxc/${CTID}.conf" -# Skip if already configured -grep -q "lxc.cgroup2.devices.allow: c 10:200 rwm" "$CTID_CONFIG_PATH" || echo "lxc.cgroup2.devices.allow: c 10:200 rwm" >>"$CTID_CONFIG_PATH" -grep -q "lxc.mount.entry: /dev/net/tun" "$CTID_CONFIG_PATH" || echo "lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file" >>"$CTID_CONFIG_PATH" +# Snapshots follow the container's own settings as [name] sections, so a line +# appended to the file ends up in the last snapshot instead of the container. +CONF_MAIN=$(awk '/^\[/ {exit} {print}' "$CTID_CONFIG_PATH") +CONF_SNAPSHOTS=$(awk '/^\[/ {s=1} s' "$CTID_CONFIG_PATH") +CONF_ADD="" +grep -q "^lxc.cgroup2.devices.allow: c 10:200 rwm" <<<"$CONF_MAIN" || CONF_ADD+=$'\n'"lxc.cgroup2.devices.allow: c 10:200 rwm" +grep -q "^lxc.mount.entry: /dev/net/tun" <<<"$CONF_MAIN" || CONF_ADD+=$'\n'"lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file" +if [[ -n "$CONF_ADD" && -n "$CONF_SNAPSHOTS" ]]; then + printf '%s%s\n\n%s\n' "$CONF_MAIN" "$CONF_ADD" "$CONF_SNAPSHOTS" >"$CTID_CONFIG_PATH" +elif [[ -n "$CONF_ADD" ]]; then + printf '%s\n' "${CONF_ADD#$'\n'}" >>"$CTID_CONFIG_PATH" +fi msg_info "Installing Tailscale in CT $CTID" -pct exec "$CTID" -- bash -c ' +$STD pct exec "$CTID" -- bash -c ' set -e # Detect OS inside container @@ -119,6 +128,8 @@ if [ -f /etc/alpine-release ]; then else # ── Debian / Ubuntu ── export DEBIAN_FRONTEND=noninteractive + # The helpers then print apt output instead of logging it inside the container. + export VERBOSE=yes # Source os-release properly (handles quoted values) . /etc/os-release @@ -130,7 +141,8 @@ else # Check DNS resolution using multiple methods (dig may not be installed) dns_check_failed=true if command -v dig >/dev/null 2>&1; then - if dig +short pkgs.tailscale.com 2>/dev/null | grep -qvE "^127\.|^0\.0\.0\.0$|^$"; then + # dig prints its own errors (";; no servers could be reached") on stdout + if dig +short pkgs.tailscale.com 2>/dev/null | grep -E "^[0-9.]+$" | grep -qvE "^127\.|^0\.0\.0\.0$"; then dns_check_failed=false fi elif command -v host >/dev/null 2>&1; then @@ -156,6 +168,8 @@ else cp "$ORIG_RESOLV" "$BACKUP_RESOLV" echo "nameserver 1.1.1.1" >"$ORIG_RESOLV" fi + RESTORE_RESOLV="[ ! -f $BACKUP_RESOLV ] || mv -f $BACKUP_RESOLV $ORIG_RESOLV" + trap "$RESTORE_RESOLV" EXIT if ! command -v curl >/dev/null 2>&1; then echo "[INFO] curl not found, installing..." @@ -166,6 +180,13 @@ else # Reuse the shared repository helper instead of hand-rolling the sources entry source <(curl -fsSL "${COMMUNITY_SCRIPTS_CORE_URL:-https://raw.githubusercontent.com/community-scripts/core/main}/core/core.func") source <(curl -fsSL "${COMMUNITY_SCRIPTS_CORE_URL:-https://raw.githubusercontent.com/community-scripts/core/main}/lib/tools.func") + # core.func sets its own EXIT trap + trap "$RESTORE_RESOLV" EXIT + # A failed download is sourced as an empty file + if ! declare -f load_functions setup_deb822_repo >/dev/null; then + echo "[ERROR] Could not download the community-scripts helpers inside the container. Check its network and DNS." >&2 + exit 7 + fi load_functions # Drop the legacy keyring from the pre-deb822 layout @@ -178,18 +199,15 @@ else "${VERSION_CODENAME}" \ "main" - apt-get install -y tailscale >/dev/null - - if [ -f /tmp/resolv.conf.backup ]; then - echo "[INFO] Restoring original /etc/resolv.conf" - mv /tmp/resolv.conf.backup /etc/resolv.conf - fi + apt-get install -y tailscale fi ' -TAGS=$(awk -F': ' '/^tags:/ {print $2}' "$CTID_CONFIG_PATH") -TAGS="${TAGS:+$TAGS; }tailscale" -pct set "$CTID" -tags "$TAGS" +# The tag is cosmetic; Tailscale is installed by now. +TAGS=$(pct config "$CTID" | awk -F': ' '/^tags:/ {print $2}') +if [[ ";${TAGS};" != *";tailscale;"* ]] && ! TAG_OUTPUT=$(pct set "$CTID" -tags "${TAGS:+$TAGS;}tailscale" 2>&1); then + msg_warn "Could not add the tailscale tag to CT $CTID: ${TAG_OUTPUT}" +fi msg_ok "Tailscale installed on CT $CTID" echo -e "${YW}Reboot the container${CL}, then run 'tailscale up' inside the container to activate."