From: Ian Kelling Date: Sun, 15 Jan 2017 17:29:48 +0000 (-0800) Subject: fix error handle path, add doc and cteate options X-Git-Url: https://iankelling.org/git/?a=commitdiff_plain;h=d0657d01d7f29b36a03c72571762ad665552429b;p=newns fix error handle path, add doc and cteate options --- diff --git a/newns b/newns index 4fc887c..d60c0bc 100755 --- a/newns +++ b/newns @@ -14,17 +14,79 @@ # limitations under the License. -# Create a network namespace. Designed for use from systemd. - [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@" -cd "${BASH_SOURCE%/*}" -source ../errhandle/errcatch-function -source ../errhandle/bash-trace-function -errcatch +if [[ ! $ERRHANDLE_PATH ]]; then + ERRHANDLE_PATH=$(readlink -f "${BASH_SOURCE}") + ERRHANDLE_PATH=$(readlink -f ${ERRHANDLE_PATH%/*}/../errhandle) +fi +err_sourced=true +for p in $ERRHANDLE_PATH/{errcatch-function,bash-trace-function}; do + if [[ -e $p ]]; then + source $p + else + err_sourced=false + fi +done +if $err_sourced; then + errcatch +else + set -eE -o pipefail + trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR +fi + +usage() { + cat </dev/null; then existing=true else found=true @@ -94,6 +158,12 @@ find_network() { start() { + find_network + if ! $found; then + echo "$0: error: no open network found" + exit 1 + fi + mkdir -p /root/mount_namespaces if ! mountpoint /root/mount_namespaces >/dev/null; then mount --bind /root/mount_namespaces /root/mount_namespaces @@ -107,24 +177,24 @@ start() { fi - - find_network - if ! $found; then - echo "$0: error: no open network found" - exit 1 + if $create; then + ip netns add $nn + ip -n $nn link set dev lo up fi + + echo 1 | dexec dd of=/proc/sys/net/ipv4/ip_forward 2>/dev/null _errcatch_cleanup=stop - ip link add $v0 type veth peer name $v1 - ip link set $v0 netns default + ipnn link add $v0 type veth peer name $v1 + ipnn link set $v0 netns default ipd addr add $network.1/24 dev $v0 ipd link set $v0 up nat -C &>/dev/null || nat -A - ip addr add $network.2/24 dev $v1 - ip link set $v1 up - ip route add default via $network.1 + ipnn addr add $network.2/24 dev $v1 + ipnn link set $v1 up + ipnn route add default via $network.1 } @@ -137,6 +207,9 @@ stop() { if ! $existing; then if nat -C &>/dev/null; then nat -D; fi fi + if $create; then + ip netns del $nn + fi } case $action in