Skip to content

Commit 7289f4a

Browse files
committed
tailscale: move tailscaled startup logic to a separate script
I was hoping to use this with the devcontainer lifecycle hooks, but they do not appear to let the process persist. The cleanup itself still has value, so landing just that piece. Updates #cleanup
1 parent 3b2d788 commit 7289f4a

File tree

3 files changed

+64
-66
lines changed

3 files changed

+64
-66
lines changed

src/tailscale/install.sh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
set -xeuo pipefail
77

8+
export DEBIAN_FRONTEND=noninteractive
9+
810
platform=$(uname -m)
911
if [ "$platform" = "x86_64" ]; then
1012
tailscale_url="https://pkgs.tailscale.com/stable/tailscale_${VERSION}_amd64.tgz"
@@ -56,23 +58,22 @@ download() {
5658
script_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
5759
scratch_dir="/tmp/tailscale"
5860
mkdir -p "$scratch_dir"
59-
trap 'rm -rf "$scratch_dir"' EXIT
61+
trap 'rm -rf "$scratch_dir" /var/lib/apt/lists/*' EXIT
6062

61-
download "$tailscale_url" |
62-
tar -xzf - --strip-components=1 -C "$scratch_dir"
63-
install -D "$scratch_dir/tailscale" /usr/local/bin/tailscale
64-
install -D "$scratch_dir/tailscaled" /usr/local/sbin/tailscaled
65-
install -D "$script_dir/tailscaled-entrypoint.sh" /usr/local/sbin/tailscaled-entrypoint
63+
download "$tailscale_url" | tar -xzf - --strip-components=1 -C "$scratch_dir"
64+
install -D -m 755 "$scratch_dir/tailscale" /usr/local/bin/tailscale
65+
install -D -m 755 "$scratch_dir/tailscaled" /usr/local/sbin/tailscaled
66+
install -D -m 755 "$script_dir/tailscaled-entrypoint.sh" /usr/local/sbin/tailscaled-entrypoint
67+
install -D -m 755 "$script_dir/tailscaled-devcontainer-start.sh" /usr/local/sbin/tailscaled-devcontainer-start
6668

6769
mkdir -p /var/lib/tailscale /var/run/tailscale /var/log
6870
touch /var/log/tailscaled.log
6971

7072
if ! command -v iptables >& /dev/null; then
7173
if command -v apt-get >& /dev/null; then
7274
apt-get update
73-
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends iptables
74-
rm -rf /var/lib/apt/lists/*
75+
apt-get install -y --no-install-recommends iptables
7576
else
76-
echo "WARNING: iptables not installed. tailscaled might fail."
77+
>&2 echo "WARNING: iptables not installed. tailscaled might fail."
7778
fi
7879
fi
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) 2025 Tailscale Inc & AUTHORS All rights reserved.
3+
# Use of this source code is governed by a BSD-style
4+
# license that can be found in the LICENSE file.
5+
6+
if [[ $(id -u) -ne 0 ]]; then
7+
if ! command -v sudo > /dev/null; then
8+
>&2 echo "tailscaled could not start as root."
9+
exit 1
10+
fi
11+
exec sudo --non-interactive -E "$0" "$@"
12+
fi
13+
14+
if [[ ! -c /dev/net/tun ]]; then
15+
mkdir -p /dev/net
16+
mknod /dev/net/tun c 10 200
17+
if [[ ! -c /dev/net/tun ]]; then
18+
>&2 cat - <<-EOF
19+
Error: /dev/net/tun is missing and could not be created!
20+
21+
taiilscaled will fail to start.
22+
23+
You can start tailscaled manually in userspace mode, see:
24+
https://tailscale.com/kb/1112/userspace-networking
25+
EOF
26+
fi
27+
fi
28+
29+
30+
TAILSCALED_PID=""
31+
TAILSCALED_SOCK=/var/run/tailscale/tailscaled.sock
32+
TAILSCALED_LOG=/var/log/tailscaled.log
33+
34+
# Note: TS_DEBUG_FIREWALL_MODE: it is not recommended that users copy this
35+
# setting into other environments, the feature is in test and will be formally
36+
# released in the future, debug flags may later be recycled for other purposes
37+
# leading to unexpected behavior.
38+
>$TAILSCALED_LOG 2>&1 TS_DEBUG_FIREWALL_MODE=auto /usr/local/sbin/tailscaled &
39+
TAILSCALED_PID=$!
40+
41+
if [[ -n "$TAILSCALED_PID" ]]; then
42+
count=100
43+
while ((count--)); do
44+
[[ -f $TAILSCALED_SOCK ]] && break
45+
sleep 0.01
46+
47+
if ! kill -0 "$TAILSCALED_PID"; then
48+
>&2 echo "ERROR: tailscaled exited during startup, logs follow:"
49+
>&2 cat $TAILSCALED_LOG
50+
break
51+
fi
52+
done
53+
fi

src/tailscale/tailscaled-entrypoint.sh

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,6 @@
33
# Use of this source code is governed by a BSD-style
44
# license that can be found in the LICENSE file.
55

6-
check_userspace() {
7-
if [[ ! -c /dev/net/tun ]]; then
8-
>&2 cat - <<-EOF
9-
Error: /dev/net/tun is missing and could not be created!
10-
11-
taiilscaled will fail to start.
12-
13-
You can start tailscaled manually in userspace mode, see:
14-
https://tailscale.com/kb/1112/userspace-networking
15-
EOF
16-
fi
17-
}
18-
19-
# Note: It is not recommended that users copy this setting into other
20-
# environments, the feature is in test and will be formally released in the
21-
# future, debug flags may later be recycled for other purposes leading to
22-
# unexpected behavior.
23-
export TS_DEBUG_FIREWALL_MODE=auto
24-
TAILSCALED_PID=""
25-
TAILSCALED_SOCK=/var/run/tailscale/tailscaled.sock
26-
TAILSCALED_LOG=/var/log/tailscaled.log
27-
if [[ "$(id -u)" -eq 0 ]]; then
28-
if [[ ! -c /dev/net/tun ]]; then
29-
mkdir -p /dev/net
30-
mknod /dev/net/tun c 10 200
31-
fi
32-
check_userspace
33-
>$TAILSCALED_LOG 2>&1 /usr/local/sbin/tailscaled &
34-
TAILSCALED_PID=$!
35-
elif command -v sudo > /dev/null; then
36-
if [[ ! -c /dev/net/tun ]]; then
37-
sudo --non-interactive mkdir -p /dev/net
38-
sudo --non-interactive mknod /dev/net/tun c 10 200
39-
fi
40-
check_userspace
41-
>$TAILSCALED_LOG 2>&1 \
42-
sudo --non-interactive "TS_DEBUG_FIREWALL_MODE=$TS_DEBUG_FIREWALL_MODE" \
43-
/usr/local/sbin/tailscaled &
44-
TAILSCALED_PID=$!
45-
else
46-
>&2 echo "tailscaled could not start as root."
47-
fi
48-
unset TS_DEBUG_FIREWALL_MODE
49-
50-
if [[ -n "$TAILSCALED_PID" ]]; then
51-
count=100
52-
while ((count--)); do
53-
[[ -f $TAILSCALED_SOCK ]] && break
54-
sleep 0.01
55-
56-
if ! kill -0 "$TAILSCALED_PID"; then
57-
>&2 echo "ERROR: tailscaled exited during startup, logs follow:"
58-
>&2 cat $TAILSCALED_LOG
59-
break
60-
fi
61-
done
62-
fi
6+
/usr/local/sbin/tailscaled-devcontainer-start
637

648
exec "$@"

0 commit comments

Comments
 (0)