仕事場にまともに使える NTP サーバないので困っていたのですが、窓際に GPS レシーバーを置いて、その信号で NTP サーバを作れば、(GPSは原子時計なので)簡単に Stratum-1な NTP サーバになることに気づいてしまったので、Raspberry Pi 2 と組み合わせて NTP サーバを作ってみました。
† 材料
今回は PC ではなく Raspberry Pi 2 Model B を使いました。OS は導入が楽な Raspbian (Jessie, 2015-09-24) を使います。組み合わせた GPS レシーバーは SiRFstarIV が搭載された、GLOBALSAT BU-353S4 という USB 接続タイプのもの。
以下、構築メモ。
† サーバの下準備
Raspbian をインストールして Raspberry Pi が起動したら、OS を最新にアップデートして必要なソフトウェアを導入します。
最後に一旦マシンをリブートしておきます。
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo rpi-update
$ sudo apt-get install gpsd gpsd-clients python-gps ntp
$ sudo shutdown -r now
† gpsd のセットアップ
USB ポートに GPS レシーバーを接続し、lsusb を使って認識されているかどうか確認します。
以下の例では Prolific Technology, Inc. LP2303 Serial Port という行が GPS になります。
USB に他のシリアル系接続のものを接続していなければ /dev/ttyUSB0 として認識されているはずです。
$ lsusb
Bus 001 Device 006: ID 04ca:005a Lite-On Technology Corp.
Bus 001 Device 005: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 001 Device 004: ID 067b:2303 Prolific Technology, Inc. LP2303 Serial Port
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
$ ls -l /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 0 9月 30 19:12 /dev/ttyUSB0
gpsd を起動する前に設定ファイルを以下のように編集しておきます。
/etc/default/gpsd
# Default settings for the gpsd init script and the hotplug wrapper.
# Start the gpsd daemon automatically at boot time
START_DAEMON="true"
# Use USB hotplugging to add new USB devices automatically to the daemon
USBAUTO="true"
# Devices gpsd should collect to at boot time.
# They need to be read/writeable, either by user gpsd or the group dialout.
DEVICES="/dev/ttyUSB0"
# Other options you want to pass to gpsd
GPSD_OPTIONS="-F /var/run/gpsd.sock -b -n"
編集が終わったら、systemctl を使ってサービスの有効化と起動を行い、起動しているかどうか確認します。
$ sudo systemctl enable gpsd
$ sudo systemctl start gpsd
$ sudo systemctl status gpsd
● gpsd.service - GPS (Global Positioning System) Daemon
Loaded: loaded (/lib/systemd/system/gpsd.service; static)
Active: active (running) since 水 2015-09-30 19:59:24 JST; 2min 14s ago
Main PID: 1089 (gpsd)
CGroup: /system.slice/gpsd.service
mq1089 /usr/sbin/gpsd -N -F /var/run/gpsd.sock -b -n /dev/ttyUSB0
9月 30 20:59:24 raspntp systemd[1]: Started GPS (Global Positioning System) Daemon.
正常に GPS が稼働していて、きちんと gpsd が動作していれば cgps -s や gpsmon を使うと現在時刻や現在地の緯度経度などの情報が表示されるはずです。
$ cgps -s
レトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトソレトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトソ
ウ Time: 2015-09-30T10:39:05.000Z ウウPRN: Elev: Azim: SNR: Used: ウ
ウ Latitude: ##.###### N ウウ 15 62 042 18 Y ウ
ウ Longitude: ###.###### E ウウ 5 14 138 19 Y ウ
ウ Altitude: ##.# m ウウ 24 74 235 16 Y ウ
ウ Speed: #.# kph ウウ 13 30 067 23 Y ウ
ウ Heading: ##.# deg (true) ウウ 18 44 309 24 Y ウ
ウ Climb: #.# m/min ウウ 22 07 313 27 Y ウ
ウ Status: 3D FIX (6 secs) ウウ 20 63 154 16 Y ウ
ウ Longitude Err: +/- 13 m ウウ 28 11 039 12 Y ウ
ウ Latitude Err: +/- 30 m ウウ 21 32 258 16 Y ウ
ウ Altitude Err: +/- 33 m ウウ 12 17 163 13 N ウ
ウ Course Err: n/a ウウ ウ
ウ Speed Err: +/- 223 kph ウウ ウ
ウ Time offset: 0.818 ウウ ウ
ウ Grid Square: PM96ua ウウ ウ
タトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトルタトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトル
† NTPd の設定
後は GPS と時刻同期するように NTP を設定すれば OK。
設定ファイルはこんな感じでしょうか。
$ diff -u /etc/ntp.conf{.org,}
--- /etc/ntp.conf.org 2015-04-13 19:29:51.000000000 +0900
+++ /etc/ntp.conf 2015-09-30 20:04:16.392855889 +0900
@@ -18,11 +18,16 @@
# pool.ntp.org maps to about 1000 low-stratum NTP servers. Your server will
# pick a different set every time it starts up. Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
-server 0.debian.pool.ntp.org iburst
-server 1.debian.pool.ntp.org iburst
-server 2.debian.pool.ntp.org iburst
-server 3.debian.pool.ntp.org iburst
+#server 0.debian.pool.ntp.org iburst
+#server 1.debian.pool.ntp.org iburst
+#server 2.debian.pool.ntp.org iburst
+#server 3.debian.pool.ntp.org iburst
+# gps ntp
+server 127.127.28.0 minpoll 4
+fudge 127.127.28.0 time1 0.183 refid NMEA
+server 127.127.28.1 minpoll 4 prefer
+fudge 127.127.28.1 time1 0.183 refid PPS
# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
編集が終わったら、こちらも systemctl を使ってサービスの有効化と起動を行い、起動しているかどうか確認します。
$ sudo systemctl enable ntp
$ sudo systemctl start ntp
$ sudo systemctl status ntp
● ntp.service - LSB: Start NTP daemon
Loaded: loaded (/etc/init.d/ntp)
Active: active (running) since 水 2015-09-30 19:59:31 JST; 14min ago
CGroup: /system.slice/ntp.service
mq1117 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 107:112
9月 30 19:59:31 raspntp ntpd[1117]: Listen and drop on 1 v6wild2card :: UDP 123
9月 30 19:59:31 raspntp ntpd[1117]: Listen normally on 2 lo 127.0.0.1 UDP 123
9月 30 19:59:31 raspntp ntpd[1117]: Listen normally on 3 eth0 ###.###.###.### UDP 123
9月 30 19:59:31 raspntp ntpd[1117]: peers refreshed
9月 30 19:59:31 raspntp ntpd[1117]: Listening on routing socket on fd #3 for interface updates
9月 30 19:59:31 raspntp systemd[1]: Started LSB: Start NTP daemon.
しばらく待ってから ntpq コマンドで同期の状態を確認しておきます。
$ ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*SHM(0) .NMEA. 0 l 10 16 377 0.000 -3.153 4.957
SHM(1) .PPS. 0 l - 16 0 0.000 0.000 0.000
もうちょっと苦戦するかと思いましたが、所用時間1時間程度で済んでしまいました。
簡単ですね。
† 参考