2018-04-07 19:40:38 +00:00
|
|
|
|
# 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
|
|
|
|
|
```
|
|
|
|
|
|
2018-04-07 20:05:36 +00:00
|
|
|
|
On invocation of `ifup`:
|
|
|
|
|
* if the namespace doesn't exist it will be created
|
|
|
|
|
* if the folders `if-down.d`, `if-post-down.d`, `if-pre-up.d` and `if-up.d`
|
|
|
|
|
under `/etc/netns/<namespace>/network` don't exist, they will be created
|
|
|
|
|
* if the `/etc/netns/<namespace>/network/interfaces` file doesn't exist a
|
|
|
|
|
blank one will be created
|
2018-04-08 06:41:19 +00:00
|
|
|
|
* if it does and the interface is configured, the script will invoke `ifup`
|
2018-04-07 20:05:36 +00:00
|
|
|
|
for this interface inside the namespace.
|
2018-04-07 20:08:02 +00:00
|
|
|
|
|
2018-12-24 12:31:25 +00:00
|
|
|
|
## `zsh` functions to run commands and start a shell in a namespace with a custom prompt
|
2018-04-07 20:08:02 +00:00
|
|
|
|
```
|
2018-04-08 06:51:40 +00:00
|
|
|
|
nse () {
|
|
|
|
|
if [ $# -lt 2 ]; then
|
|
|
|
|
echo "Please specify a network namespace and a command"
|
2018-04-08 06:56:49 +00:00
|
|
|
|
echo "Usage: $0 <namespace> <command>"
|
2018-04-07 20:08:02 +00:00
|
|
|
|
return
|
|
|
|
|
fi
|
2018-04-08 06:51:40 +00:00
|
|
|
|
NS=${1}
|
|
|
|
|
shift
|
2018-12-24 12:31:25 +00:00
|
|
|
|
ARGS=${@}
|
|
|
|
|
ip netns exec ${NS} unshare -m /bin/sh -c "mount --make-rprivate /;mount --bind /run/network.${NS} /run/network; ${ARGS}"
|
2018-04-07 20:08:02 +00:00
|
|
|
|
}
|
2018-04-08 06:51:40 +00:00
|
|
|
|
|
|
|
|
|
nss () {
|
|
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
|
echo "Please specify a network namespace"
|
2018-04-08 06:56:49 +00:00
|
|
|
|
echo "Usage: $0 <namespace>"
|
2018-04-08 06:51:40 +00:00
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
nse $1 zsh -i
|
|
|
|
|
}
|
2018-12-24 12:31:25 +00:00
|
|
|
|
|
|
|
|
|
alias nsr="nsenter -t 1 -n"
|
|
|
|
|
|
|
|
|
|
NS=`ip netns identify $$`
|
|
|
|
|
|
|
|
|
|
if [ "$NS" ]; then;
|
|
|
|
|
NS="/$NS"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "`id -u`" -eq 0 ]; then
|
|
|
|
|
export PS1="%{[33;36;1m%}%T%{[0m%} %{[33;34;1m%}%n%{[0m[33;33;1m%}@%{[33;37;1m%}%m${NS} %{[33;32;1m%}%~%{[0m[33;33;1m%}%#%{[0m%} "
|
|
|
|
|
else
|
|
|
|
|
export PS1="%{[33;36;1m%}%T%{[0m%} %{[33;31;1m%}%n%{[0m[33;33;1m%}@%{[33;37;1m%}%m${NS} %{[33;32;1m%}%~%{[0m[33;33;1m%}%#%{[0m%} "
|
|
|
|
|
fi
|
|
|
|
|
|
2018-04-07 20:08:02 +00:00
|
|
|
|
```
|