LoginSignup
4
2

GPSモジュールを接続したRaspberry piでchronyを動作

Last updated at Posted at 2019-10-09

はじめに

以前書いた記事では、Raspberry pi3 Model B+にGPSモジュールを接続し、ntpdを利用してNTPサーバを構築していました。
今回は上記の記事で構築した環境・機器を利用し、chronyでNTPサーバを動作させます。

2024/03/09時点
リンク切れや細かな表現を修正

コマンド入力

下記のコマンドを入力する。
/etc/chrony/chrony.confは次の項目に記載の内容に基づいて設定する。
途中に出てくる「systemd-timesyncd」はネットワークを介して時刻同期するNTPクライアントのサービスです。今回の環境では不要のため無効化しています。
また、筆者の環境ではchronydを有効化した段階でntpdは停止されていました。systemd-timesyncdが停止にならなかった理由は分かりません。

sudo apt-get install chrony
sudo cp -a /lib/systemd/system/gpsd.service /etc/systemd/system/
sudo vim /etc/chrony/chrony.conf 
sudo systemctl enable gpsd.service
sudo systemctl restart gpsd.service
sudo systemctl disable systemd-timesyncd.service
sudo systemctl enable chrony
sudo systemctl start chrony

設定ファイル

/etc/chrony/chrony.confを設定すれば動作する。/etc/systemd/system/chronyd.serviceは元から必要なパラメータが最初から設定されていたため、特に変更はいらなかった。

/etc/chrony/chrony.conf

gpsdを利用しているため、refclock SHM 0 refid GPSを追記する。precisionやdelayなどのオプションで細かな設定を追記が可能。
refclockで記述しているSHMについてはNTP document -refclock-Shared Memory Driverchrony.conf(5) Manual Pageに詳細な説明がある。
gpsdからどのようにchronyが時刻を取得しているか参考になる。

log文では出力させたいログの種類を選択可能だが、今回の環境ではtrackingとmeasurements、statisticsのみ出力される。

# NTP authentication.
keyfile /etc/chrony/chrony.keys

# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift

# Uncomment the following line to turn logging on.
log tracking measurements statistics rtc refclocks tempcomp

# Log files location.
logdir /var/log/chrony

# Stop bad estimates upsetting machine clock.
maxupdateskew 100.0

# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
rtcsync

# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
makestep 1 3

# setting for GPS
refclock SHM 0 refid GPS precision 1e-1 offset 0.9999 delay 0.2


allow 192.168.1.0/24

/etc/systemd/system/chronyd.service

[Unit]
Description=chrony, an NTP client/server
Documentation=man:chronyd(8) man:chronyc(1) man:chrony.conf(5)
Conflicts=systemd-timesyncd.service openntpd.service ntp.service ntpsec.service
After=network.target
ConditionCapability=CAP_SYS_TIME

[Service]
Type=forking
PIDFile=/run/chronyd.pid
EnvironmentFile=-/etc/default/chrony
ExecStart=/usr/sbin/chronyd $DAEMON_OPTS
ExecStartPost=-/usr/lib/chrony/chrony-helper update-daemon
PrivateTmp=yes
ProtectHome=yes
ProtectSystem=full

[Install]
Alias=chronyd.service
WantedBy=multi-user.target

動作確認

chronyc sources -vtimedatectl statusで動作しているか確認可能。

pi@raspi:~ $ chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
#* GPS                           0   4   377    17    -14ms[  -17ms] +/-  200ms

参考サイト

4
2
0

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
4
2