Skip to content

Commit 470a4d5

Browse files
authored
Merge pull request #1341 from kernelkit/allow-to-run-nsenter
2 parents 02ca310 + dd5c491 commit 470a4d5

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

test/nsenter

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,33 @@ lower=$1
99
shift
1010
[ $# -gt 0 ] || set sh
1111

12+
# Detect if we're running inside the infamy container or on the host
13+
# Inside the container, we can directly access the interfaces
14+
# On the host, we need to use docker/podman exec
15+
if ip link show "$lower" >/dev/null 2>&1; then
16+
# We can see the lower interface directly, so we're in the right namespace
17+
lookup_ifname() {
18+
ip -j link show | jq -r ".[] | select(.ifindex == $1) | .ifname"
19+
}
20+
else
21+
# We're on the host, need to query the infamy container
22+
infamy=$(infamy 2>/dev/null) || {
23+
echo "ERROR: Cannot see interface '$lower' and no infamy container found" >&2
24+
echo " Run this script from inside the infamy container or from the host with docker/podman available" >&2
25+
exit 1
26+
}
27+
lookup_ifname() {
28+
$(runner) exec $infamy ip -j link show | jq -r ".[] | select(.ifindex == $1) | .ifname"
29+
}
30+
fi
31+
1232
for pid in $(pgrep -fx "sleep infinity"); do
1333
ifidxs=$(nsenter -t $pid -U -n ip -j link show | \
1434
jq '.[] | select(has("link_netnsid")) | .link_index')
1535
for ifidx in $ifidxs; do
16-
ifname=$(ip -j link show | jq -r ".[] | select(.ifindex == $ifidx) | .ifname")
36+
ifname=$(lookup_ifname $ifidx)
1737
if [ "$ifname" = "$lower" ]; then
18-
exec nsenter -t $pid -U -n env PS1="$(build_ps1 "(netns:$ifname)")" "$@"
38+
exec nsenter -t $pid -U -n env PS1="(netns:$ifname) # " "$@"
1939
fi
2040
done
2141
done

0 commit comments

Comments
 (0)