Ubuntuで起動時に veth pair, bridge, netns を作成し接続する方法


Ubuntu 14.04.04 にて /etc/network/interfaces に設定を追加する事により、起動時に veth pair, bridge, network namespace (netns) を作成し繋がる事を確認したのでメモ。


なお、 "/etc/network/interfaces での veth の作り方" を参考にさせて頂きました。
http://qiita.com/kwi/items/c82ea2839e2df5be780f

## bridge-utilsのインストール
```shell-session
  $ sudo apt-get update
  $ sudo apt-get install bridge-utils
```

## /etc/network/interfaces の編集
```shell-session
$ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

auto veth1
iface veth1 inet manual
    pre-up ip link add veth1 type veth peer name gveth1
    pre-up ip netns add host1
    pre-up ip link set gveth1 netns host1
    pre-up ip netns exec host1 ip link set gveth1 up
    pre-up ip netns exec host1 ip addr add 10.0.0.1/24 brd + dev gveth1
    post-down ip link del veth1

auto veth2
iface veth2 inet manual
    pre-up ip link add veth2 type veth peer name gveth2
    pre-up ip netns add host2
    pre-up ip link set gveth2 netns host2
    pre-up ip netns exec host2 ip link set gveth2 up
    pre-up ip netns exec host2 ip addr add 10.0.0.2/24 brd + dev gveth2
    post-down ip link del veth2

auto br1
iface br1 inet manual
    bridge_ports veth1 veth2
```

## 再起動後、設定が反映されている事を確認
* bridge br1, veth1/veth2 が作成されている事を確認
* veth1/veth2 が作成され、br1 に接続されている事（master br1）を確認。

```shell-session
$ ip a
6: veth1@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br1 state UP group default qlen 1000
    link/ether aa:6d:fb:ec:14:9e brd ff:ff:ff:ff:ff:ff
    inet6 fe80::a86d:fbff:feec:149e/64 scope link
       valid_lft forever preferred_lft forever
7: br1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 0e:10:4f:3b:06:1c brd ff:ff:ff:ff:ff:ff
    inet6 fe80::a86d:fbff:feec:149e/64 scope link
       valid_lft forever preferred_lft forever
9: veth2@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br1 state UP group default qlen 1000
    link/ether 0e:10:4f:3b:06:1c brd ff:ff:ff:ff:ff:ff
    inet6 fe80::c10:4fff:fe3b:61c/64 scope link
       valid_lft forever preferred_lft forever
```

* host1/host2 それぞれのnetnsにgveth1/gveth2が作成されアドレスが設定されている事を確認。

```shell-session
$ sudo ip netns exec host1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
5: gveth1@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 1a:f4:12:ed:24:5c brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.1/24 brd 10.0.0.255 scope global gveth1
       valid_lft forever preferred_lft forever
    inet6 fe80::18f4:12ff:feed:245c/64 scope link
       valid_lft forever preferred_lft forever

$ sudo ip netns exec host2 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
8: gveth2@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 1a:8f:fe:ed:55:06 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.2/24 brd 10.0.0.255 scope global gveth2
       valid_lft forever preferred_lft forever
    inet6 fe80::188f:feff:feed:5506/64 scope link
       valid_lft forever preferred_lft forever
```

* netns 間のインターフェースで ping できる事を確認

```shell-session
$ sudo ip netns exec host1 ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.126 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.047 ms

$ sudo ip netns exec host2 ping 10.0.0.1
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.116 ms
64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.041 ms
```