-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathkiss-netns
executable file
·716 lines (635 loc) · 18.5 KB
/
kiss-netns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
#!/bin/bash
# author: yin-jianhong@163.com
# version: v0.99
# dependency: iproute tmux
# used to create netns and add ifs into it
LANG=C
P=$0; [[ $0 = /* ]] && P=${0##*/}
switchroot() {
local P=$0 SH=; [[ $0 = /* ]] && P=${0##*/}; [[ -e $P && ! -x $P ]] && SH=$SHELL
[[ $(id -u) != 0 ]] && {
echo -e "\E[1;4m{WARN} $P need root permission, switch to:\n sudo $SH $P $*\E[0m"
exec sudo $SH $P "$@"
}
}
Usage() {
cat <<-EOF
Usage:
$P <\$nsname,\$vethX,\$addr---\$nsname,\$vethX_peer,\$addr | \$nsname,\$vnic_name[,\$addr][?updev=\$if,mode=\$mode,iftype=\$iftype]>
# ^^^ nsname 'host' means default network namespace, br-$suffix,br:\$brname means it's a bridge //[convention over configuration]
# ^^^ vnic_name 'iv-*' means ipvlan nic, 'mv-*' and others means macvlan nic //[convention over configuration]
# ^^^ addr 'dhcp' means get ip address by using dhclient/dhcpcd //[convention over configuration]
# +--------+ +--------+
# | ns0 [veth0.X]------------[veth0.Y] host |
# +--------+ +--------+
# netns ns0,veth0.X,192.168.1.2---host,veth0.Y,192.168.1.1
# +--------+ +--------+
# | ns0 [veth1.X]------------[veth1.Y] ns1 |
# +--------+ +--------+
# netns ns0,veth1.X,192.168.2.2---ns1,veth1.Y,192.168.2.1
# +--------+ +------+ +--------+
# | ns0 [veth3.X]----[veth3.Y] br-0 [veth4.X]----[veth4.Y] ns1 |
# +--------+ +------+ +--------+
# netns ns0,veth3.X,192.168.3.2---br-0,veth3.Y br-0,veth4.X---ns1,veth4.Y,192.168.3.1
# +--------+ +--------+
# | [veth5.X]------------[veth5.Y] |
# | ns0 | | ns1 |
# | [mv-ns0] [mv-ns1] |
# +--------+ \ / +--------+
# \ /
# +------------------+
# | mv-ns0 | mv-ns1 |
# +------------------|
# | eth0 |
# +------------------+
# netns ns0,veth5.X,192.168.4.2---ns1,veth5.Y,192.168.4.1 ns0,mv-ns0,192.168.5.10 ns1,mv-ns1,192.168.5.11
# netns del ns0 ns1
# netns ns0,veth5.X,192.168.4.2---ns1,veth5.Y,192.168.4.1 ns0,mv-ns0,dhcp ns1,mv-ns1,dhcp
$P exec \$nsname -- cmdline
$P del \$nsname
$P ls
$P veth ve0.a-host,ve0.b-ns0 #create veth pair
$P macvlan ifname #create macvlan if; [updev=updev] [mode={bridge|vepa|private|passthru}]
$P ipvlan ifname #create ipvlan if; [updev=updev] [mode={l2|l3}]
$P addrup \$if \$address #set address and up if
$P addif2netns \$ns \$if [\$addr] #add new if to netns, [and setup address and up]
$P detach \$ns \$if #detach if from netns
Options:
-h, --help ; show this help info
-v ; verbose mode
-d ; debug mode
-n <arg> ; netns name
-x[arg] ; expected return code of sub-command exec, if doesn't match output test fail msg
; e.g: -x or -x0 or -x1,2,3 or -x1,10,100-200
-f ; force delete bridge while there still have interface[s] in it.
Examples: host connect ns0 with both veth and macvlan
$P host,ve0.a-host,192.168.0.1---ns0,ve0.b-ns0,192.168.0.2 host,mv-host0,192.168.100.1 ns0,mv-ns0,192.168.100.2
$P exec -v -x ns0 -- ping -c 2 192.168.0.1
$P exec -v -x ns0 -- ping -c 2 192.168.100.1
#curl -s -L https://raw.githubusercontent.com/tcler/kiss-vm-ns/master/utils/make-nfs-server.sh | sudo bash
sudo systemctl start nfs-server
sudo exportfs -o ro,no_root_squash "*:/usr/share"
sudo firewall-cmd --add-service=nfs --add-service=mountd --add-service=rpc-bind
$P exec -v -x ns0 -- showmount -e 192.168.0.1
$P exec -v -x ns0 -- mkdir -p /mnt/ns0/nfs
$P exec -v -x ns0 -- mount 192.168.0.1:/ /mnt/ns0/nfs
$P exec -v -x ns0 -- mount -t nfs4
$P exec -v ns0 -- ls /mnt/ns0/nfs
$P exec -v -x ns0 -- umount /mnt/ns0/nfs
$P exec -v -x32 ns0 -- umount /mnt/ns0/nfs
$P exec -v -x ns0 -- umount /mnt/ns0/nfs #just for show what does -x option work, when test fail
$P exec -v ns0 -- rm -rf /mnt/ns0
$P del ns0
$P delif mv-host0
Examples: host connect ns0 with both veth and ipvlan
$P host,ve0.a-host,192.168.0.1---ns0,ve0.b-ns0,192.168.0.2 host,iv-host0,192.168.99.1 ns0,iv-ns0,192.168.99.2
$P exec -v -x ns0 -- ping -c 2 192.168.99.1
$P del ns0
$P delif iv-host0
Examples: host connect ns0 with veth and bridge br-0
$P host,veth0.X,192.168.66.1---br-0,veth0.Y br-0,veth1.Y---ns0,veth1.X,192.168.66.2
$P exec -v -x ns0 -- ping -c 2 192.168.66.1
$P del ns0 br-0
EOF
}
run() {
[[ $# -eq 0 ]] && return 0
[[ "$DEBUG" = yes ]] && echo "[sys]" "$@"
"$@"
}
is_valid_ip() {
local ip=$1
local stat=1
ip=${ip%/*}
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
ip=(${ip//./ })
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
netns_init_pid() {
local ns=$1
local initpid
initpid=$(tmux -L netns:$ns list-panes -a -F '#{session_name} #{pane_pid}' | awk -v ns="$ns" '$1 == ns {print $2}')
[[ -z "$initpid" ]] && return 1
echo $initpid
}
quote() {
local at=$1
if [[ -z "$at" ]]; then
echo -n "'' "
elif [[ "$at" =~ [^[:print:]]+ || "$at" = *$'\t'* || "$at" = *$'\n'* ]]; then
builtin printf %q "$at"; echo -n " "
elif [[ "$at" =~ "'" && ! "$at" =~ ([\`\"$]+|\\\\) ]]; then
echo -n "\"$at\" "
else
echo -n "$at" | sed -r -e ':a;$!{N;ba};' \
-e "s/'+/'\"&\"'/g" -e "s/^/'/" -e "s/$/' /" \
-e "s/^''//" -e "s/'' $/ /"
fi
}
getReusableCommandLine() {
#if only one parameter, treat it as a piece of script
[[ $# = 1 ]] && { echo "$1"; return; }
local shpattern='^[][0-9a-zA-Z~@%^_+=:,./-]+$'
for at; do
if [[ "$at" =~ $shpattern ]]; then
echo -n "$at "
else
quote "$at"
fi
done
echo
}
_netnsexec() {
local initpid=$1
shift
local _cmdline=$(getReusableCommandLine "$@")
nsenter --target "$initpid" --mount --uts --ipc --net --pid -- bash -c "$_cmdline"
}
netnsexec() {
local ns=$1
shift
local initpid=$(netns_init_pid $ns)
[[ "$VERBOSE" = yes ]] && {
cmdLine=$(getReusableCommandLine "$@")
echo "[NETNS:$ns]> ${cmdLine//\\/\\\\}" | GREP_COLORS='ms=01;36' grep --color . >&2
}
_netnsexec "$initpid" "$@"
rc=$?
faillog() { echo -e "\033[41m{TEST:FAIL} $*\033[0m"; }
[[ -n "$expectedrc" ]] && {
[[ " ${expectedrc[@]} " != *" $rc "* ]] && {
faillog "return code: expect $expectedRC, but got $rc"
}
}
return $rc
}
expandrc() {
local rcrange=$1
local rclist=()
for rc in ${rcrange//,/ }; do
if [[ "$rc" =~ [0-9]+ ]]; then
rclist+=($rc)
elif [[ "$rc" =~ [0-9]+-[0-9]+ ]]; then
eval rclist+=({${rc/-/..}})
fi
done
echo -n ${rclist[@]}
}
netnsdelete() {
for netns; do
echo -e "\n{NETNS:INFO} delete/destroy netns $netns ..."
nshome=/opt/NETNS/$netns
echo -e "- {NETNS:INFO} umount all networking fs ..."
run netnsexec $netns umount -a -t nfs,nfs4,cifs
echo -e "- {NETNS:INFO} remove ifs in netns $netns ..."
for dev in $(netnsexec $netns ip a s | awk -F'[: @]+' '/^[0-9]+:/ {if ($2 != "lo") print $2}'); do
run ip netns exec $netns ip link del "$dev"
done
run ip netns exec $netns ip a s
echo -e "- {NETNS:INFO} exit init bash ..."
tmux -L netns:$netns send-keys -t "$netns" C-z " exit; exit" Enter
echo -e "- {NETNS:INFO} remove netns $netns ..."
run ip netns del $netns 2>/dev/null
echo -e "- {NETNS:INFO} remove netns home $nshome ..."
\rm -rf $nshome 2>/dev/null
done
}
brdelete() {
local iflist=
for br; do
iflist=$(bridge link show | awk -F'[ :@]+' "/master $br/"'{print $2}')
#for dev in $iflist; do run ip link delete dev "$dev"; done
if [[ -n "$iflist" ]]; then
if [[ "$FORCE" = yes ]]; then
for dev in $iflist; do
echo -e "- {NETNS:INFO} remove interface $dev in bridge $br ..."
run ip link delete dev "$dev"
done
echo -e "- {NETNS:INFO} remove bridge $br ..."
run ip link delete dev $br
else
echo -e "- {NETNS:WARN} there still have interface[s] in $br, try again with '-f' option if realy want remove it"
echo "$iflist"|sed 's/^/ /'
continue
fi
else
echo -e "- {NETNS:INFO} remove bridge $br ..."
run ip link delete dev $br
fi
done
}
delbridge() { brdelete "$@"; }
delbr() { brdelete "$@"; }
nsdelete() {
local netnsList=()
local brList=()
for ns; do
case "$ns" in
br[-:]*) brname=${ns#br:}; brList+=("$brname");;
*)
if [[ ! -f /var/run/netns/$ns ]]; then
if is_bridge "$ns"; then
brList+=("$ns")
else
echo -e "- {NETNS:WARN} [$ns] is neither netns nor bridge ..."
fi
else
netnsList+=("$ns")
fi
;;
esac
done
netnsdelete "${netnsList[@]}"
brdelete "${brList[@]}"
}
addif2netns() {
local ns=$1
local if=$2
local addr=$3
local fname=${FUNCNAME[0]}
Usage() { echo -e "Usage:\n $fname <netns> <ifname> [\$ipaddr|dhcp]"; }
[[ $# -lt 2 ]] && {
Usage >&2
return 1
}
xaddr() {
local addr=$1
[[ "$addr" =~ .*/[1-9]+$ ]] || addr+=/24
echo $addr
}
run ip link set $if netns $ns
run ip netns exec $ns ip link set dev $if up
[[ -n "$addr" ]] && {
if [[ "$addr" = dhcp* ]]; then
nshome=/opt/NETNS/$ns
if command -v dhcpcd &>/dev/null; then
run netnsexec $ns dhcpcd -n $if
else
run cp /etc/resolv.conf /etc/resolv.conf.netns.orig
run netnsexec $ns dhclient -pf $nshome/dhclient-$if.pid $if
run cp /etc/resolv.conf.netns.orig /etc/resolv.conf
fi
else
run ip netns exec $ns ip addr add $(xaddr $addr) dev $if
run ip netns exec $ns ip route add default via ${addr%/*} dev $if
fi
}
}
addif2bridge() {
local brname=$1
local if=$2
local fname=${FUNCNAME[0]}
Usage() { echo -e "Usage:\n $fname <brname> <ifname>"; }
[[ $# -lt 2 ]] && {
Usage >&2
return 1
}
run ip link set dev $if master $brname
run ip link set dev $if up
}
detachif() {
local ns=$1
local if=$2
run ip netns exec $ns ip link set $if netns 1
}
addveth() {
local fname=${FUNCNAME[0]}
Usage() { echo -e "Usage:\n $fname vename.x,vename.y [vename2.x,vename2.y ...]"; }
if [[ "${#}" = 0 ]]; then
Usage >&2
return 1
fi
for pair; do
read end0 end1 _ <<<"${pair//,/ }"
if [[ -n "$end0" && -n "$end1" ]]; then
run ip link add $end0 type veth peer name $end1
else
Usage >&2
fi
done
}
addmacvlan() {
local fname=${FUNCNAME[0]}
Usage() { echo -e "Usage:\n $fname [-updev=updev] [-mode={bridge|vepa|private|passthru}] mv1 [mv2 ...]"; }
local updev=$(get-default-if.sh)
local mode=bridge
# parse options
local mvs=()
for arg; do
case "$arg" in
updev=*|-updev=*) val=${arg#*=}; [[ -n "$val" ]] && updev="$val";;
mode=*|-mode=*) val=${arg#*=}; [[ -n "$val" ]] && mode="$val";;
-*) echo "{WARN} unkown option '${arg}'" >&2;;
*) mvs+=($arg);;
esac
done
if [[ "${#mvs[@]}" = 0 ]]; then
Usage >&2
return 1
fi
for mv in $mvs; do
run ip link add link $updev name ${mv} type macvlan mode $mode
done
}
addipvlan() {
local fname=${FUNCNAME[0]}
Usage() { echo -e "Usage:\n $fname [-updev=updev] [-mode={l2|l3}] iv1 [iv2 ...]"; }
local updev=$(get-default-if.sh)
local mode=l2
# parse options
local ivs=()
for arg; do
case "$arg" in
updev=*|-updev=*) val=${arg#*=}; [[ -n "$val" ]] && updev="$val";;
mode=*|-mode=*) val=${arg#*=}; [[ -n "$val" ]] && mode="$val";;
-*) echo "{WARN} unkown option '${arg}'" >&2;;
*) ivs+=($arg);;
esac
done
if [[ "${#ivs[@]}" = 0 ]]; then
Usage >&2
return 1
fi
for iv in $ivs; do
run ip link add link $updev name ${iv} type ipvlan mode $mode
done
}
addbridge() {
local fname=${FUNCNAME[0]}
Usage() { echo -e "Usage:\n $fname br-name1 [br-name2 ...]"; }
if [[ "${#}" = 0 ]]; then
Usage >&2
return 1
fi
for brname; do
run ip link add $brname type bridge
run ip link set dev $brname up
done
}
addbr() { addbridge "$@"; }
addressup() {
local if=$1
local addr=$2
xaddr() {
local addr=$1
[[ "$addr" =~ .*/[1-9]+$ ]] || addr+=/24
echo $addr
}
run ip link set dev $if up
[[ -n "$addr" ]] && {
if [[ "$addr" = dhcp* ]]; then
if command -v dhcpcd &>/dev/null; then
run netnsexec $ns dhcpcd -n $if
else
run cp /etc/resolv.conf /etc/resolv.conf.netns.orig
run dhclient -pf /var/run/dhclient-$if.pid $if
run cp /etc/resolv.conf.netns.orig /etc/resolv.conf
fi
else
run ip addr add $(xaddr $addr) dev $if
fi
}
}
delif() {
for if; do
_pid=$(pgrep -f "dhclient -pf .* $if")
[[ -n "$_pid" ]] && run kill $_pid
run ip link delete dev $if
done
}
# command line parse
oat=("$@")
_at=`getopt -o hvdn:x::f \
--long help \
-a -n "$0" -- "$@"`
eval set -- "$_at"
while true; do
case "$1" in
-h|--help) Usage; shift 1; exit 0;;
-n) NS="$2"; shift 2;;
-v) VERBOSE=yes; shift 1;;
-d) DEBUG=yes; shift 1;;
-x) expectedRC=${2:-0}; expectedrc=$(expandrc ${expectedRC#=}); shift 2;;
-f) FORCE=yes; shift 1;;
--) shift; break;;
esac
done
switchroot "${oat[@]}"
# __prepare__
command -v tmux >/dev/null || dep+=\ tmux
command -v dhcpcd >/dev/null || dep+=\ dhcpcd\ dhclient
[[ -n "$dep" ]] && {
echo -e "{NETNS:INFO} install dependences ..."
sudo yum install --setopts=strict=0 --disablerepo=epel -y $dep >&2
}
# __main__
subcmd=$1
case $subcmd in
exec|exe|ex|e) shift
[[ -z "$NS" ]] && { NS=$1; shift; }
[[ -z "$NS" ]] && { Usage >&2; exit 1; }
netnsexec $NS "$@"
exit $?;;
delif) shift
delif "$@"
exit $?;;
delete|delet|dele|del|de|d) shift
[[ -z "$NS" ]] && { NS=$1; shift; }
[[ -z "$NS" ]] && { Usage >&2; exit 1; }
nsdelete $NS "$@"
exit $?;;
ls) shift
run ip netns
exit $?;;
addveth|veth) shift
addveth "$@"
exit $?;;
addmacvlan|macvlan) shift
addmacvlan "$@"
exit $?;;
addipvlan|ipvlan) shift
addipvlan "$@"
exit $?;;
addressup|addrup|addr) shift
addressup "$@"
exit $?;;
addif2netns|attach) shift
[[ -z "$NS" ]] && { NS=$1; shift; }
[[ -z "$NS" ]] && { Usage >&2; exit 1; }
addif2netns "$NS" "$@"
exit $?;;
detach) shift
[[ -z "$NS" ]] && { NS=$1; shift; }
[[ -z "$NS" ]] && { Usage >&2; exit 1; }
detachif "$@"
exit $?;;
creat*) shift
[[ -z "$NS" ]] && { NS=$1; shift; }
;;
addbr|addbridge) shift
addbridge "$@"
exit $?;;
addif2bridge) shift
addif2bridge "$@"
exit $?;;
delbr|delbridge) shift
delbridge "$@"
exit $?;;
esac
[[ $# = 0 ]] && {
Usage >&2
exit 1
}
ifexist() {
for if; do
ip link show $if &>/dev/null && { echo -e "${if}: if exist"; }
done
}
_netnsexist() {
for ns; do
[[ -f /var/run/netns/$ns ]] && { echo -e "${ns}: netns exist"; }
done
}
netnsexist() {
for ns; do
if [[ "$ns" = br[-:]* ]]; then
brname=${ns#br:}
continue
else
_netnsexist $ns
fi
done
}
existCheck=
VethList=()
VNICList=()
declare -A NS_IFS
echo -e "\n{NETNS:INFO} parse parameters ..."
for topo; do
# ns that connected to/by veth: host,veth0.x,192.168.0.1---ns0,veth0.y,192.168.0.2
if [[ "$topo" =~ ^[^,]+,[^,]+(,[^,]+|,dhcp)?---+[^,]+,[^,]+(,[^,]+|,dhcp)?$ ]]; then
topo=$(echo "$topo"|sed -r 's/---+/ /')
read nodeX nodeY _ <<<"${topo}"
read nsX vethX addrX _ <<<"${nodeX//,/ }"
read nsY vethY addrY _ <<<"${nodeY//,/ }"
for ns in $nsX $nsY; do
[[ "$ns" = br[-:]* ]] && {
brname=${ns#br:}
if [[ -n "$(ifexist $brname)" ]] && ! is_bridge $brname; then
echo -e "{NETNS:ERR} bridge name conflict with existed interface ${brname}"
exit 1
fi
}
done
VethList+=($vethX,$vethY)
NS_IFS[$nsX]+=" $vethX,$addrX"
NS_IFS[$nsY]+=" $vethY,$addrY"
#check if ns or if have been there
existinfo=$(netnsexist $nsX $nsY; ifexist $vethX $vethY)
[[ -n "$existinfo" ]] && existCheck+=${existinfo}$'\n'
# ns that connected to macvlan
elif [[ "$topo" =~ ^[^,]+,[^,]+(,[0-9.]+|,dhcp)?(\?((updev|mode|type)=.*)(,((updev|mode|type)=.*))*)?$ ]]; then
read ifinfo env _ <<<"${topo/\?/ }"
read ns ifname addr _ <<<"${ifinfo//,/ }"
[[ "$ns" = br[-:]* ]] && {
brname=${ns#br:}
echo -e "{NETNS:ERR} bridge(${brname}) only accept veth interface"
exit 1
}
NS_IFS[$ns]+=" $ifname,$addr"
VNICList+=($ifname,${env})
#check if ns or if have been there
existinfo=$(netnsexist $ns; ifexist $ifname)
[[ -n "$existinfo" ]] && existCheck+=${existinfo}$'\n'
else
echo -e "{NETNS:ERR} invalid format '\E[1;31m$topo\E[0m', see Usage:"
Usage >&2
exit 1
fi
done
echo -e "\n{NETNS:INFO} check if ns or vnic has been there ..."
[[ -n "$existCheck" ]] && echo -n "$existCheck" | sort -k 2 | uniq
echo "$existCheck" | grep -q exist$ && {
echo -e "{NETNS:ERR} ^^^ please clean old configure/env, and try again."
exit 1
}
# creating all NetNS
for NS in "${!NS_IFS[@]}"; do
[[ "$NS" = host ]] && continue
if [[ "$NS" = br[-:]* ]]; then
brname=${NS#br:}
echo -e "{NETNS:INFO} creating brige ${brname} ..."
if [[ -n "$(ifexist $brname)" ]]; then
echo -e "{NETNS:INFO} bridge ${brname} has been there."
else
addbridge $brname
fi
else
nshome=/opt/NETNS/$NS
echo -e "{NETNS:INFO} creating NS $NS ..."
run ip netns add "$NS"
run ip netns exec "$NS" ip link set lo up #for ping localhost
run mkdir -p $nshome
[[ "$(getenforce)" != Disabled ]] &&
run chcon --reference=/var/run -R $nshome
tmux -L netns:$NS new -s "$NS" -d "ip netns exec \"$NS\" /bin/bash"
fi
done
# creating all veth ifs
for veth in "${VethList[@]}"; do
read vethX vethY _ <<<"${veth/,/ }"
echo -e "{NETNS:INFO} creating veth pair $vethX $vethY ..."
addveth $vethX,$vethY
done
# creating other VNICs
Upperdev=$(get-default-if.sh)
if is_bridge $Upperdev; then
Upperdev=kss-netns-dummy
run ip link add name $Upperdev type dummy 2>/dev/null
run ip link set $Upperdev up
fi
for vnicinfo in "${VNICList[@]}"; do
iftype=
updev=
mode=
read vif env <<<"${vnicinfo/,/ }"
eval ${env//,/ }
if [[ -z "$iftype" ]]; then
case $vif in
mv-*) iftype=macvlan;;
iv-*) iftype=ipvlan;;
*) iftype=macvlan;;
esac
fi
if [[ -z "$updev" ]]; then
updev=$Upperdev
fi
if [[ "$iftype" = macvlan ]]; then
echo -e "{NETNS:INFO} creating macvlan $vif ${env//,/ } ..."
addmacvlan $vif -updev=$updev -mode=$mode
elif [[ "$iftype" = ipvlan ]]; then
echo -e "{NETNS:INFO} creating iplan $vif ${env//,/ } ..."
addipvlan $vif -updev=$updev -mode=$mode
else
:
fi
done
# add ifs to NetNS or bridge ...
for NS in "${!NS_IFS[@]}"; do
for IFLIST in ${NS_IFS[$NS]}; do
read ifname addr _ <<<"${IFLIST/,/ }"
echo -e "{NETNS:INFO} add $ifname to netns/br($NS) and/or set ip address($addr) ..."
if [[ "$NS" = host ]]; then
addressup $ifname $addr
elif [[ "$NS" = br[-:]* ]]; then
brname=${NS#br:}
addif2bridge "$brname" $ifname
else
addif2netns "$NS" $ifname $addr
fi
done
done