前回「 DWM-222 (4G LTE USB ドングル) を Raspberry Pi で ppp する」という記事では Raspberry Pi で DWM-222 をモデムとして認識させ ppp するまでを書いた。
この記事では一歩進めて、ドングルを挿したら勝手に ppp まで実行する便利な環境にする。技術的には、 udev で挿抜検出で systemd をキックし ifup/ifdown 経由で ppp を起動、となる。大まかな手順は以下のようになる。
- ifup/ifdown 用のエントリ作成
- systemd 関連の設定
- udev/systemd 連携の設定
STEP 1. ifup/ifdown 用のエントリ作成
/etc/network/interfaces へエントリを追加すると、 ifup/ifdown の各コマンドで ppp を起動できる (効果は pon/poff コマンドを使った時と同じ) 。今回は iface 名称に 4gppp0 とした (任意で決めて良い) 。
--- /etc/network/interfaces.1 2017-07-12 17:23:50.724414117 +0800
+++ /etc/network/interfaces 2017-07-12 17:18:09.470984080 +0800
@@ -5,3 +5,9 @@
iface wlan8 inet static
iface wlan9 inet static
+
+
+allow-hotplug 4gppp0
+iface 4gppp0 inet ppp
+ provider TaiwanMobile4G
+
手動で ifup/ifdown してみて、 ppp0 が上がってくれば成功。
$ sudo ifup 4gppp0
$ ifconfig ppp0
ppp0 Link encap:Point-to-Point Protocol
inet addr:10.127.179.170 P-t-P:10.64.64.64 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:33 errors:0 dropped:0 overruns:0 frame:0
TX packets:34 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:2190 (2.1 KiB) TX bytes:2229 (2.1 KiB)
$ sudo ifdown 4gppp0
STEP 2. systemd 関連の設定
/etc/network/interface を編集した後は、それぞれの iface エントリに対して /lib/systemd/system/ifup@.service が使えるはずなのだが、実は 2017 年 7 月 14 日現在 (Linux 4.9.35+ #1014 時点) 、 Raspian のデフォルトではうまく動かない。
Ubuntu Xenial の ifup@.service と Raspbian の ifup@.service の内容を比較したら違っていたので、試しに 極力 Xenial のものに合わせてみたら、なんと動いた。 After 指定が 2 箇所にあるのが悪さをしている感じだったので、後に出てくる After 行を削除するだけで良いことがわかったので書き換え。
--- /lib/systemd/system.0/ifup@.service 2017-07-13 21:30:39.335398000 +0800
+++ /lib/systemd/system/ifup@.service 2017-07-13 21:46:12.827728612 +0800
@@ -3,7 +3,6 @@
After=local-fs.target network-pre.target networking.service systemd-sysctl.service
Before=network.target
BindsTo=sys-subsystem-net-devices-%i.device
-After=sys-subsystem-net-devices-%i.device
ConditionPathIsDirectory=/run/network
DefaultDependencies=no
こうしておくと service コマンドで ifup/down が使えるようになる。
$ sudo service ifup@4gppp0 start
$ sudo service ifup@4gppp0 status
● ifup@4gppp0.service - ifup for 4gppp0
Loaded: loaded (/lib/systemd/system/ifup@.service; static)
Active: active (exited) since Thu 2017-07-13 21:52:49 CST; 4s ago
Process: 2031 ExecStop=/sbin/ifdown %I (code=exited, status=0/SUCCESS)
Process: 2183 ExecStart=/sbin/ifup --allow=hotplug %I (code=exited, status=0/SUCCESS)
Main PID: 2183 (code=exited, status=0/SUCCESS)
Jul 13 21:52:51 peanut chat[2217]: send (\d)
Jul 13 21:52:52 peanut pppd[2196]: Serial connection established.
Jul 13 21:52:52 peanut pppd[2196]: Using interface ppp0
Jul 13 21:52:52 peanut pppd[2196]: Connect: ppp0 <--> /dev/ttyUSB1
Jul 13 21:52:53 peanut pppd[2196]: Could not determine remote IP address: defaulting to 10.64.64.64
Jul 13 21:52:53 peanut pppd[2196]: replacing old default route to wlan0 [192.168.68.1]
Jul 13 21:52:53 peanut pppd[2196]: local IP address 10.199.164.204
Jul 13 21:52:53 peanut pppd[2196]: remote IP address 10.64.64.64
Jul 13 21:52:53 peanut pppd[2196]: primary DNS address 61.31.1.1
Jul 13 21:52:53 peanut pppd[2196]: secondary DNS address 168.95.1.1
$ ifconfig ppp0
ppp0 Link encap:Point-to-Point Protocol
inet addr:10.199.164.204 P-t-P:10.64.64.64 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:17 errors:0 dropped:0 overruns:0 frame:0
TX packets:36 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:974 (974.0 B) TX bytes:2381 (2.3 KiB)
$ sudo service ifup@4gppp0 stop
$ sudo service ifup@4gppp0 status
● ifup@4gppp0.service - ifup for 4gppp0
Loaded: loaded (/lib/systemd/system/ifup@.service; static)
Active: inactive (dead) since Thu 2017-07-13 21:53:29 CST; 3s ago
Process: 2326 ExecStop=/sbin/ifdown %I (code=exited, status=0/SUCCESS)
Process: 2183 ExecStart=/sbin/ifup --allow=hotplug %I (code=exited, status=0/SUCCESS)
Main PID: 2183 (code=exited, status=0/SUCCESS)
Jul 13 21:52:53 peanut pppd[2196]: remote IP address 10.64.64.64
Jul 13 21:52:53 peanut pppd[2196]: primary DNS address 61.31.1.1
Jul 13 21:52:53 peanut pppd[2196]: secondary DNS address 168.95.1.1
Jul 13 21:53:02 peanut ntpdate[2236]: adjust time server 103.226.213.30 offset 0.001368 sec
Jul 13 21:53:27 peanut systemd[1]: Stopping ifup for 4gppp0...
Jul 13 21:53:27 peanut pppd[2196]: Terminating on signal 15
Jul 13 21:53:27 peanut pppd[2196]: Connect time 0.6 minutes.
Jul 13 21:53:27 peanut pppd[2196]: Sent 3876 bytes, received 2584 bytes.
Jul 13 21:53:27 peanut pppd[2196]: restoring old default route to wlan0 [192.168.68.1]
Jul 13 21:53:29 peanut systemd[1]: Stopped ifup for 4gppp0.
STEP 3. udev/systemd 連携
さて今迄は前準備でこれからが本番。 udev を利用して DWM-222 の挿抜イベントで systemd の ifup@.service を蹴り ifup/ifdown を駆動する。
/etc/udev/rules.d/99-dwm222.rules を作り、内容を以下のようにする。 udev ルール行に ACTION==”add” や ACTION==”remove” などの ACTION ジャッジを *付けない* のがポイント (理由は /lib/systemd/system/ifup@.service の内容を見よ) 。
# /etc/udev/rules.d/99-dwm222.rules
KERNEL=="ttyUSB1", ATTRS{idVendor}=="2001", ATTRS{idProduct}=="7e35", ENV{SYSTEMD_WANTS}+="ifup@4gppp0.service"
udev ルールをリロード。
$ sudo udevadm control --reload
こうすると DWM-222 挿入により ttyUSB1 を発見した時に systemd が /lib/systemd/system/ifup@.service 経由で ifup 4gppp0 を走らせる。 ttyUSB1 が無くなった時には同様に ifdown 4gppp0 が走る。
参考サイト
- http://qiita.com/CLCL/items/99acf6dd3bd9c251f1aa
- https://manpages.debian.org/testing/manpages-ja/pppd.8.ja.html
- https://docs.oracle.com/cd/E39368_01/e48214/ch07s03.html
- https://www.novell.com/ja-jp/documentation/sles11/book_sle_admin/data/sec_udev_rules.html
- https://wiki.archlinuxjp.org/index.php/Udev
- https://wiki.archlinux.org/index.php/3G_and_GPRS_modems_with_pppd
- http://qref.sourceforge.net/Debian/reference/ch-gateway.ja.html