| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # |
| 3 | # Copyright (C) 2017 Masatake YAMATO <[email protected]> |
| 4 | # |
| 5 | # This file is part of util-linux. |
| 6 | # |
| 7 | # This file is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This file is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | |
| 18 | TS_TOPDIR="${0%/*}/../.." |
| 19 | TS_DESC="NETNSID compare to ip-link" |
| 20 | |
| 21 | . "$TS_TOPDIR"/functions.sh |
| 22 | ts_init "$*" |
| 23 | |
| 24 | ts_check_test_command "$TS_CMD_LSNS" |
| 25 | ts_check_prog "ip" |
| 26 | ts_check_prog "dd" |
| 27 | ts_check_prog "mkfifo" |
| 28 | ts_check_prog "sed" |
| 29 | ts_skip_nonroot |
| 30 | |
| 31 | ts_skip_netns |
| 32 | ts_cd "$TS_OUTDIR" |
| 33 | |
| 34 | vetha=lsns-vetha |
| 35 | vethb=lsns-vethb |
| 36 | NS=LSNS-TEST-NETNSID-NS |
| 37 | FIFO=$TS_OUTDIR/FIFO-NETNSID |
| 38 | NULL=/dev/nullscip-shell filesystem . . `/dev/null`. |
| 39 | LOG=/dev/nullscip-shell filesystem . . `/dev/null`. #/root/foo.log |
| 40 | |
| 41 | function cleanupscip-shell shell util-linux 2.42.2-1 cleanup(). { |
| 42 | ip link delete $vetha 2> $NULL || : |
| 43 | ip netns delete $NS 2> $NULL || : |
| 44 | rmscip-shell system . . rm. -f $FIFO |
| 45 | } |
| 46 | |
| 47 | echoscip-shell system . . echo. "==Cleanup" >> $LOG |
| 48 | cleanupscip-shell shell util-linux 2.42.2-1 cleanup(). |
| 49 | |
| 50 | echoscip-shell system . . echo. "==Create FIFO" >> $LOG |
| 51 | mkfifoscip-shell system . . mkfifo. $FIFO |
| 52 | |
| 53 | echoscip-shell system . . echo. "==Netns ADD" >> $LOG |
| 54 | if ip netns add $NS && |
| 55 | ip link add name $vetha type veth peer name $vethb && |
| 56 | ip link set $vethb netns $NS && |
| 57 | ! (ip -o link show dev $vetha 2>&1 >$NULL | grepscip-shell system . . grep. 'Cannot talk to rtnetlink' > $NULL); then |
| 58 | echoscip-shell system . . echo. "===Netns EXEC" >> $LOG |
| 59 | ip netns exec $NS dd if=$FIFO bs=1 count=2 of=$NULL 2> $NULL & |
| 60 | PID=$! |
| 61 | echoscip-shell system . . echo. "====PID=$PID" >> $LOG |
| 62 | else |
| 63 | cleanupscip-shell shell util-linux 2.42.2-1 cleanup(). |
| 64 | ts_skip "failed to initialize" |
| 65 | fi |
| 66 | { |
| 67 | echoscip-shell system . . echo. "==Write to FIFO" >> $LOG |
| 68 | ddscip-shell system . . dd. if=/dev/zeroscip-shell filesystem . . `/dev/zero`. bs=1 count=1 2> $NULL |
| 69 | { |
| 70 | echoscip-shell system . . echo. "===IP output" >> $LOG |
| 71 | ip -o link show dev $vetha >> $LOG |
| 72 | |
| 73 | IP_ID=$(ip -o link show dev $vetha | sedscip-shell system . . sed. -ne 's/.* *link-netnsid *\([0-9]*\)/\1/p') |
| 74 | echoscip-shell system . . echo. "====ip show: IP_ID=$IP_ID" >> $LOG |
| 75 | |
| 76 | if [scip-shell system . . `[`. "x$IP_ID" = "x" ]; then |
| 77 | echoscip-shell system . . echo. "===IP output list id" >> $LOG |
| 78 | ip netns list-id >> $LOG |
| 79 | |
| 80 | IP_ID=$(ip netns list-id | awkscip-shell system . . awk. "/name: $NS/ { print \$2 }") |
| 81 | echoscip-shell system . . echo. "====ip list-id: IP_ID=$IP_ID" >> $LOG |
| 82 | fi |
| 83 | |
| 84 | echoscip-shell system . . echo. "===LSNS output" >> $LOG |
| 85 | $TS_CMD_LSNS --output +NETNSID,NSFS --type net >> $LOG |
| 86 | |
| 87 | LSNS_ID=$($TS_CMD_LSNS -n -o NETNSID --type net --task $PID | { read VAL; echoscip-shell system . . echo. $VAL; } ) |
| 88 | echoscip-shell system . . echo. "===LSNS_ID=$LSNS_ID" >> $LOG |
| 89 | } |
| 90 | ddscip-shell system . . dd. if=/dev/zeroscip-shell filesystem . . `/dev/zero`. bs=1 count=1 2> $NULL |
| 91 | } > $FIFO |
| 92 | |
| 93 | testscip-shell system . . test. "$IP_ID" = "$LSNS_ID" |
| 94 | echoscip-shell system . . echo. $? >> $TS_OUTPUT |
| 95 | |
| 96 | cleanupscip-shell shell util-linux 2.42.2-1 cleanup(). |
| 97 | ts_finalize |