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
+ if nsenter $( nsenter_flags $1 ) true ; then
8
31
break
9
32
else
10
33
sleep 0.5
@@ -17,7 +40,7 @@ function wait_for_network_device {
17
40
# Wait that the device appears.
18
41
COUNTER=0
19
42
while [ $COUNTER -lt 40 ]; do
20
- if nsenter --preserve-credentials -U -n --target= $1 ip addr show $2 ; then
43
+ if nsenter $( nsenter_flags $1 ) ip addr show $2 ; then
21
44
break
22
45
else
23
46
sleep 0.5
@@ -41,7 +64,7 @@ function wait_process_exits {
41
64
function wait_for_ping_connectivity {
42
65
COUNTER=0
43
66
while [ $COUNTER -lt 40 ]; do
44
- if nsenter --preserve-credentials -U -n --target= $1 ping -c 1 -w 1 $2 ; then
67
+ if nsenter $( nsenter_flags $1 ) ping -c 1 -w 1 $2 ; then
45
68
break
46
69
else
47
70
sleep 0.5
@@ -53,7 +76,7 @@ function wait_for_ping_connectivity {
53
76
function wait_for_connectivity {
54
77
COUNTER=0
55
78
while [ $COUNTER -lt 40 ]; do
56
- if echo " wait_for_connectivity" | nsenter --preserve-credentials -U -n --target= $1 ncat -v $2 $3 ; then
79
+ if echo " wait_for_connectivity" | nsenter $( nsenter_flags $1 ) ncat -v $2 $3 ; then
57
80
break
58
81
else
59
82
sleep 0.5
0 commit comments