netns 694 B

1234567891011121314151617181920212223
  1. #!/bin/bash
  2. set -e
  3. if [ -n "${IF_NETNS}" ]
  4. then
  5. # Create netns if it doesn't already exist, and bring up the loopback
  6. if ! (ip netns list | grep -qx ${IF_NETNS})
  7. then
  8. mkdir -p /etc/netns/$IF_NETNS/network/{if-down.d,if-post-down.d,if-pre-up.d,if-up.d}
  9. if [ ! -f /etc/netns/$IF_NETNS/network/interfaces ]; then
  10. touch /etc/netns/$IF_NETNS/network/interfaces
  11. fi
  12. if [ ! -f /etc/netns/$IF_NETNS/resolv.conf ]; then
  13. touch /etc/netns/$IF_NETNS/resolv.conf
  14. fi
  15. mkdir -p /run/network.${IF_NETNS}
  16. rm -rf /run/network.${IF_NETNS}/*
  17. ip netns add ${IF_NETNS}
  18. ip netns exec ${IF_NETNS} ip link set lo up
  19. fi
  20. ip link set ${IFACE} netns ${IF_NETNS}
  21. fi