1
1
#! /bin/bash
2
2
3
+ function nsenter_flags {
4
+ pid=$1
5
+ flags=" --target=${pid} "
6
+ userns=" $( readlink /proc/${pid} /ns/user) "
7
+ mntns=" $( readlink /proc/${pid} /ns/mnt) "
8
+ netns=" $( readlink /proc/${pid} /ns/net) "
9
+
10
+ self_userns=" $( readlink /proc/self/ns/user) "
11
+ self_mntns=" $( readlink /proc/self/ns/mnt) "
12
+ self_netns=" $( readlink /proc/self/ns/net) "
13
+
14
+ if [ " ${userns} " != " ${self_userns} " ]; then
15
+ flags=" $flags --preserve-credentials -U"
16
+ fi
17
+ if [ " ${mntns} " != " ${self_mntns} " ]; then
18
+ flags=" $flags -m"
19
+ fi
20
+ if [ " ${netns} " != " ${self_netns} " ]; then
21
+ flags=" $flags -n"
22
+ fi
23
+ echo " ${flags} "
24
+ }
25
+
3
26
function wait_for_network_namespace {
4
27
# Wait that the namespace is ready.
5
28
COUNTER=0
6
29
while [ $COUNTER -lt 40 ]; do
7
- if nsenter --preserve-credentials -U -n --target=$1 true ; then
30
+ flags=$( nsenter_flags $1 )
31
+ if $( echo $flags | grep -qvw -- -n) ; then
32
+ flags=" $flags -n"
33
+ fi
34
+ if nsenter ${flags} true > /dev/null 2>&1 ; then
8
35
break
9
36
else
10
37
sleep 0.5
@@ -17,7 +44,7 @@ function wait_for_network_device {
17
44
# Wait that the device appears.
18
45
COUNTER=0
19
46
while [ $COUNTER -lt 40 ]; do
20
- if nsenter --preserve-credentials -U -n --target= $1 ip addr show $2 ; then
47
+ if nsenter $( nsenter_flags $1 ) ip addr show $2 ; then
21
48
break
22
49
else
23
50
sleep 0.5
@@ -41,7 +68,7 @@ function wait_process_exits {
41
68
function wait_for_ping_connectivity {
42
69
COUNTER=0
43
70
while [ $COUNTER -lt 40 ]; do
44
- if nsenter --preserve-credentials -U -n --target= $1 ping -c 1 -w 1 $2 ; then
71
+ if nsenter $( nsenter_flags $1 ) ping -c 1 -w 1 $2 ; then
45
72
break
46
73
else
47
74
sleep 0.5
@@ -53,7 +80,7 @@ function wait_for_ping_connectivity {
53
80
function wait_for_connectivity {
54
81
COUNTER=0
55
82
while [ $COUNTER -lt 40 ]; do
56
- if echo " wait_for_connectivity" | nsenter --preserve-credentials -U -n --target= $1 ncat -v $2 $3 ; then
83
+ if echo " wait_for_connectivity" | nsenter $( nsenter_flags $1 ) ncat -v $2 $3 ; then
57
84
break
58
85
else
59
86
sleep 0.5
0 commit comments