Browse Source

Initial commit

Benjamin Collet 6 years ago
commit
35f2972e98
5 changed files with 90 additions and 0 deletions
  1. 23 0
      LICENSE
  2. 19 0
      README
  3. 15 0
      if-down.d/netns
  4. 20 0
      if-pre-up.d/netns
  5. 13 0
      if-up.d/netns

+ 23 - 0
LICENSE

@@ -0,0 +1,23 @@
+The MIT License (MIT)
+
+Copyright (c) 2015 m0kct
+Copyright (c) 2018 Benjmin Collet
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+

+ 19 - 0
README

@@ -0,0 +1,19 @@
+# ifupdown-netns
+
+Some simple scripts to simplify configuring network namespaces on Debian-like
+systems.  Copy them into the corresponding directories under `/etc/network`.
+
+To configure an interface in a namespace:
+
+```
+auto eth1
+iface eth1 inet manual
+  netns myns
+```
+
+On invocation of `ifup` it will create the necessary directories under
+`/etc/netns/` and an empty `/etc/netns/<namespace>/network/interfaces` file if
+it doesn't already exist.
+
+If it does and the interface is configured it will invoke `ifup` inside the
+namespace.

+ 15 - 0
if-down.d/netns

@@ -0,0 +1,15 @@
+#!/bin/sh
+
+set -e
+
+if [ -n "${IF_NETNS}" ]
+then
+  unshare -m /bin/sh <<-EOF
+mount --make-rprivate /
+mount --bind /run/network.${IF_NETNS} /run/network
+if (ip netns exec ${IF_NETNS} ifquery --list | grep -qx ${IFACE}); then
+  ip netns exec ${IF_NETNS} ifdown ${IFACE}
+fi
+ip netns exec ${IF_NETNS} ip link set netns 1 dev ${IFACE}
+EOF
+fi

+ 20 - 0
if-pre-up.d/netns

@@ -0,0 +1,20 @@
+#!/bin/bash
+
+set -e
+
+if [ -n "${IF_NETNS}" ]
+then
+  # Create netns if it doesn't already exist, and bring up the loopback
+  if ! (ip netns list | grep -qx ${IF_NETNS})
+  then
+    mkdir -p /etc/netns/$IF_NETNS/network/{if-down.d,if-post-down.d,if-pre-up.d,if-up.d}
+    if [ ! -f /etc/netns/$IF_NETNS/network/interfaces ]; then
+      touch /etc/netns/$IF_NETNS/network/interfaces
+    fi
+    mkdir -p /run/network.${IF_NETNS}
+    rm -rf /run/network.${IF_NETNS}/*
+    ip netns add ${IF_NETNS}
+    ip netns exec ${IF_NETNS} ip link set lo up
+  fi
+  ip link set ${IFACE} netns ${IF_NETNS}
+fi

+ 13 - 0
if-up.d/netns

@@ -0,0 +1,13 @@
+#!/bin/sh
+
+set -e
+
+if [ -n "${IF_NETNS}" ]; then
+  unshare -m /bin/sh <<-EOF
+mount --make-rprivate /
+mount --bind /run/network.${IF_NETNS} /run/network
+if (ip netns exec ${IF_NETNS} ifquery --list | grep -qx ${IFACE}); then
+  ip netns exec ${IF_NETNS} ifup ${IFACE}
+fi
+EOF
+fi