LoginSignup
51
64

More than 5 years have passed since last update.

CentOS 7 の nmcli で bonding

Last updated at Posted at 2014-08-05

CentOS 7 (RHEL7) からネットワーク周りの設定は nmcli (NetworkManager) を使えということらしいので、bonding 設定を nmcli で試してみた。

前提

  • ネットワークのインターフェースが2つある
  • 両方とも switch に接続されている
  • 片方だけIPアドレスが設定してあり有効になっている
  • オンラインで切り替える

↓こんな環境

$ nmcli c
NAME      UUID                                  TYPE            DEVICE
enp8s0f0  86b340dd-08f8-4067-95da-64e5d065c9e2  802-3-ethernet  enp8s0f0
enp8s0f1  5ddd53ce-04fc-4def-8c28-142c5c1e3f4a  802-3-ethernet  --
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp8s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether a8:0c:0d:xx:xx:xxe brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.100/24 brd 192.168.0.255 scope global enp8s0f0
       valid_lft forever preferred_lft forever
    inet6 fe80::aa0c:dff:xxxx:xxxx/64 scope link
       valid_lft forever preferred_lft forever
3: enp8s0f1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether a8:0c:0d:xx:xx:xx brd ff:ff:ff:ff:ff:ff

bonding 設定

bonding デバイスを追加する

※デバイス名はふざけてます、良い子は真似しないように (bond0 とかで良いのではないかと)

$ sudo nmcli connection add type bond autoconnect no con-name jamesbond ifname jamesbond mode active-backup
Connection 'jamesbond' (3f972a86-feaf-4fd3-89c0-0b781d76aa18) successfully added.
$ nmcli c
NAME       UUID                                  TYPE            DEVICE
enp8s0f0   86b340dd-08f8-4067-95da-64e5d065c9e2  802-3-ethernet  enp8s0f0
jamesbond  3f972a86-feaf-4fd3-89c0-0b781d76aa18  bond            --
enp8s0f1   5ddd53ce-04fc-4def-8c28-142c5c1e3f4a  802-3-ethernet  --

slave を追加する

ひとまず使ってないインターフェースを slave として追加

$ sudo nmcli connection add type bond-slave autoconnect no ifname enp8s0f1 master jamesbond
Connection 'bond-slave-enp8s0f1' (30170a37-4157-44be-99dd-9e99442839ff) successfully added.
$ nmcli c
NAME                 UUID                                  TYPE            DEVICE
jamesbond            3f972a86-feaf-4fd3-89c0-0b781d76aa18  bond            --   
enp8s0f0             86b340dd-08f8-4067-95da-64e5d065c9e2  802-3-ethernet  enp8s0f0
bond-slave-enp8s0f1  30170a37-4157-44be-99dd-9e99442839ff  802-3-ethernet  --   
enp8s0f1             5ddd53ce-04fc-4def-8c28-142c5c1e3f4a  802-3-ethernet  --

connection.* を確認すると jamesbond の slave だということがわかります

$ nmcli -f connection c s bond-slave-enp8s0f1
connection.id:                          bond-slave-enp8s0f1
connection.uuid:                        30170a37-4157-44be-99dd-9e99442839ff
connection.interface-name:              enp8s0f1
connection.type:                        802-3-ethernet
connection.autoconnect:                 no
connection.timestamp:                   0
connection.read-only:                   no
connection.permissions:
connection.zone:                        --
connection.master:                      jamesbond
connection.slave-type:                  bond
connection.secondaries:
connection.gateway-ping-timeout:        0

@autoconnect` が有効で DHCP サーバーがいたりするとこの時点でIPアドレスが振られちゃってます

bonding インターフェースにIPアドレスなどを設定する

設定前

$ nmcli -f ipv4 c s jamesbond
ipv4.method:                            auto
ipv4.dns:
ipv4.dns-search:
ipv4.addresses:
ipv4.routes:
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.never-default:                     no
ipv4.may-fail:                          yes

設定

$ sudo nmcli c e jamesbond
nmcli> set ipv4.method manual
nmcli> set ipv4.dns 192.168.0.5,192.168.0.6
nmcli> set ipv4.dns-search example.com
nmcli> set ipv4.addresses 192.168.0.100/24 192.168.0.1
nmcli> p ipv4
['ipv4' setting values]
ipv4.method:                            manual
ipv4.dns:                               192.168.0.5, 192.168.0.6
ipv4.dns-search:                        example.com
ipv4.addresses:                         { ip = 192.168.0.100/24, gw = 192.168.0.1 }
ipv4.routes:
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.never-default:                     no
ipv4.may-fail:                          yes
nmcli> verify
Verify connection: OK
nmcli> save
Connection 'jamesbond' (3f972a86-feaf-4fd3-89c0-0b781d76aa18) successfully updated.
nmcli> quit

restart で入れ替わるように autoconnect を調整する

sudo sed -i -e 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfg-bond-slave-enp8s0f1
sudo sed -i -e 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfg-jamesbond
sudo nmcli c m enp8s0f0 connection.autoconnect no

sed でファイルを直接書き換えているのには意味があって、no にする場合はすぐに down したりしない(autoconnect だから down とは関係ない)が、nmcli コマンドで yes に変更するとすぐにインターフェースが有効になってしまうため。

restart

$ sudo service network restart; sudo nmcli c up bond-slave-enp8s0f1

1行で入力しないと slave が up してくれなくてネットワークアクセスできなくなっちゃいます。もちろん、コンソールからアクセスしてれば分けてももーまんたい。OS 再起動すれば up するんですけど、なにが足りないんだろうか。誰か教えてください。

もう一方のインターフェースも bonding に追加

$ sudo nmcli connection add type bond-slave ifname enp8s0f0 master jamesbond

今回は autoconnect の指定はなし (default yes) で ok

$ nmcli c
NAME                 UUID                                  TYPE            DEVICE
jamesbond            0f731c67-075e-447a-8ddb-c7a4b9dd0d81  bond            jamesbond
bond-slave-enp8s0f1  9bb37dd8-24ed-4335-9e5e-b3a1da8fa51c  802-3-ethernet  enp8s0f1
enp8s0f1             5ddd53ce-04fc-4def-8c28-142c5c1e3f4a  802-3-ethernet  --   
bond-slave-enp8s0f0  bdb5239b-a868-40e2-8ff7-81be4313efac  802-3-ethernet  enp8s0f0
enp8s0f0             86b340dd-08f8-4067-95da-64e5d065c9e2  802-3-ethernet  --
$ cat /proc/net/bonding/jamesbond
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: enp8s0f1
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: enp8s0f1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: a8:0c:0d:xx:xx:xx
Slave queue ID: 0

Slave Interface: enp8s0f0
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: a8:0c:0d:xx:xx:xx
Slave queue ID: 0

ついでなので slave を切り替えてみる

$ sudo ifenslave -c jamesbond enp8s0f0
$ grep 'Active Slave:' /proc/net/bonding/jamesbond
Currently Active Slave: enp8s0f0
$ sudo ifenslave -c jamesbond enp8s0f1
$ grep 'Active Slave:' /proc/net/bonding/jamesbond
Currently Active Slave: enp8s0f1

bonding を解除する

片方のインターフェースを開放

enp8s0f1 が Active Slave なので enp8s0f0 を外して独立させる

$ nmcli c
NAME                 UUID                                  TYPE            DEVICE
jamesbond            0f731c67-075e-447a-8ddb-c7a4b9dd0d81  bond            jamesbond
bond-slave-enp8s0f1  9bb37dd8-24ed-4335-9e5e-b3a1da8fa51c  802-3-ethernet  enp8s0f1
enp8s0f1             5ddd53ce-04fc-4def-8c28-142c5c1e3f4a  802-3-ethernet  --   
bond-slave-enp8s0f0  bdb5239b-a868-40e2-8ff7-81be4313efac  802-3-ethernet  enp8s0f0
enp8s0f0             86b340dd-08f8-4067-95da-64e5d065c9e2  802-3-ethernet  --
$ sudo nmcli c del bond-slave-enp8s0f0
$ nmcli c
NAME                 UUID                                  TYPE            DEVICE
jamesbond            0f731c67-075e-447a-8ddb-c7a4b9dd0d81  bond            jamesbond
bond-slave-enp8s0f1  9bb37dd8-24ed-4335-9e5e-b3a1da8fa51c  802-3-ethernet  enp8s0f1
enp8s0f1             5ddd53ce-04fc-4def-8c28-142c5c1e3f4a  802-3-ethernet  --   
enp8s0f0             86b340dd-08f8-4067-95da-64e5d065c9e2  802-3-ethernet  --

独立インターフェースを再設定

bond-slave-enp8s0f0 が消えたので enp8s0f0 を再設定
だけど、前のがの残ってるから今回は autoconnect をいじるだけ

$ nmcli -f connection,ipv4 c s enp8s0f0
connection.id:                          enp8s0f0
connection.uuid:                        86b340dd-08f8-4067-95da-64e5d065c9e2
connection.interface-name:              --
connection.type:                        802-3-ethernet
connection.autoconnect:                 no
connection.timestamp:                   1407189160
connection.read-only:                   no
connection.permissions:
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.secondaries:
connection.gateway-ping-timeout:        0
ipv4.method:                            manual
ipv4.dns:                               192.168.0.5, 192.168.0.6
ipv4.dns-search:                        example.com
ipv4.addresses:                         { ip = 192.168.0.100/24, gw = 192.168.0.1 }
ipv4.routes:
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.never-default:                     no
ipv4.may-fail:                          yes

nmcli connection edit enp8s0f0set connection.autoconnect yes して save すると、すぐ有効になっちゃうから modify コマンドを使うこと。

bonding インターフェースが自動起動しないようにして、enp8s0f0 が起動するようにする

$ sudo nmcli c m jamesbond connection.autoconnect no
$ sudo nmcli c m bond-slave-enp8s0f1 connection.autoconnect no
$ sudo nmcli c m enp8s0f0 connection.autoconnect yes

restart network service

$ sudo service network restart

今回は enp8s0f0 がちゃんと Up したぞ。

bonding の設定を掃除

$ sudo nmcli c del bond-slave-enp8s0f1
$ sudo nmcli c del jamesbond
$ nmcli c
NAME      UUID                                  TYPE            DEVICE
enp8s0f1  5ddd53ce-04fc-4def-8c28-142c5c1e3f4a  802-3-ethernet  --
enp8s0f0  86b340dd-08f8-4067-95da-64e5d065c9e2  802-3-ethernet  enp8s0f0

元に戻った。めでたしめでたし。

2015/12/15 追記

実は sed とか不要だった?

環境は

CentOS Linux release 7.1.1503 (Core)

まだケーブルは1本しかささってないけど、今後のために bonding にしておこうと試した。

p2p1 だけが接続されている状態から p2p2 との bonding に切り替える。

# bonding interface 作成
nmcli connection add type bond autoconnect no con-name bond0 ifname bond0 mode active-backup

# bonding の slave を作成
nmcli connection add type bond-slave autoconnect no ifname p2p2 master bond0
nmcli connection add type bond-slave autoconnect no ifname p2p1 master bond0

# bonding interface に ip address などを設定する
nmcli c e bond0
> set ipv4.method manual
> set ipv4.dns xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx
> set ipv4.dns-search example.com
> set ipv4.addresses xxx.xxx.xxx.xxx/xx
> set ipv4.gateway xxx.xxx.xxx.xxx
> set ipv4.routes xxx.xxx.xxx.xxx/xx xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx/xx xxx.xxx.xxx.xxx
> p ipv4
> save
> quit

# 物理 interface の自動起動を無効にする
nmcli c m p2p1 connection.autoconnect no
nmcli c m p2p2 connection.autoconnect no

# bonding interface の自動起動を有効にする
nmcli c m bond-slave-p2p1 connection.autoconnect yes
nmcli c m bond-slave-p2p2 connection.autoconnect yes
nmcli c m bond0 connection.autoconnect yes

これで再起動すれば ok
OS の再起動を避けたい場合は以前と同じように

sudo service network restart; sudo nmcli c up bond-slave-p2p1
51
64
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
51
64