IBM Cloud: （小ネタ）VPC上のVSIのNICを一時的に停止する方法

#遭遇した問題

IBM CloudのVPC上のVSIで複数インターフェースを持っている際に、テスト目的で一時的にeth1を停止したかったので`ifconfig eth1 down`を実行した。

```
[root@keepalived-1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:00:16:02:70:58 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global dynamic eth0
       valid_lft 295sec preferred_lft 295sec
    inet6 fe80::16ff:fe02:7058/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:00:0a:0d:50:6a brd ff:ff:ff:ff:ff:ff
    inet 10.4.0.100/24 brd 10.4.0.255 scope global dynamic eth1
       valid_lft 299sec preferred_lft 299sec
    inet6 fe80::aff:fe0d:506a/64 scope link
       valid_lft forever preferred_lft forever

[root@keepalived-1 ~]# ifconfig eth1 down

[root@keepalived-1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:00:16:02:70:58 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global dynamic eth0
       valid_lft 251sec preferred_lft 251sec
    inet6 fe80::16ff:fe02:7058/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether 02:00:0a:0d:50:6a brd ff:ff:ff:ff:ff:ff
    inet 10.4.0.100/24 brd 10.4.0.255 scope global dynamic eth1
       valid_lft 249sec preferred_lft 249sec
```

たしかに、eth1のステータスはDOWNになっているなと思い、他の作業をしていたら5分もしないうちに、またいつのまにかeth1が起動していた！？なぜなぜ？

```
[root@keepalived-1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:00:16:02:70:58 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global dynamic eth0
       valid_lft 264sec preferred_lft 264sec
    inet6 fe80::16ff:fe02:7058/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:00:0a:0d:50:6a brd ff:ff:ff:ff:ff:ff
    inet 10.4.0.100/24 brd 10.4.0.255 scope global dynamic eth1
       valid_lft 298sec preferred_lft 298sec
    inet6 fe80::aff:fe0d:506a/64 scope link
       valid_lft forever preferred_lft forever
```



#解決方法

よく見たら、`ifconfig eth1 down`を実行した後の`ip a`コマンドの結果を見ると、`valid_lft 249sec preferred_lft 249sec`という記載がある。つまり、DHCP Clientが定期的に情報を取得しに行ってインターフェースを起動させているようだ。そもそも、ifcfg-eth1なども存在しないしね。

```
[root@keepalived-1 ~]# ls -l /etc/sysconfig/network-scripts/ifcfg-*
-rw-r--r--. 1 root root 159 Nov 24 20:10 /etc/sysconfig/network-scripts/ifcfg-eth0
-rw-r--r--. 1 root root 254 Mar 29  2019 /etc/sysconfig/network-scripts/ifcfg-lo
```

よって、eth1のDHCPからのリースを停止させる必要がある。

```txt:DHCPサーバーからリースされているeth1のIPアドレスを解放する。これでvalid_lftもforeverになった。
[root@keepalived-1 ~]# dhclient -r eth1
[root@keepalived-1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:00:16:02:70:58 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global dynamic eth0
       valid_lft 222sec preferred_lft 222sec
    inet6 fe80::16ff:fe02:7058/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:00:0a:0d:50:6a brd ff:ff:ff:ff:ff:ff
    inet6 fe80::aff:fe0d:506a/64 scope link
       valid_lft forever preferred_lft forever
```
```txt:必要あればさらにeth1も停止する。
[root@keepalived-1 ~]# ifconfig eth1 down
[root@keepalived-1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:00:16:02:70:58 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global dynamic eth0
       valid_lft 202sec preferred_lft 202sec
    inet6 fe80::16ff:fe02:7058/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether 02:00:0a:0d:50:6a brd ff:ff:ff:ff:ff:ff
```

これで勝手にeth1が起動されることがないし、IPアドレスが割り振られることもない。

#eth1の構成を復活させる方法

サーバーを再起動させてもいいが、コマンドで実行するなら以下のとおり。

```
[root@keepalived-1 ~]# dhclient eth1
[root@keepalived-1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:00:16:02:70:58 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global dynamic eth0
       valid_lft 237sec preferred_lft 237sec
    inet6 fe80::16ff:fe02:7058/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:00:0a:0d:50:6a brd ff:ff:ff:ff:ff:ff
    inet 10.4.0.100/24 brd 10.4.0.255 scope global dynamic eth1
       valid_lft 299sec preferred_lft 299sec
    inet6 fe80::aff:fe0d:506a/64 scope link
       valid_lft forever preferred_lft forever
```