嵌入式移植NTP(Network Time Protocol)

左心房为你撑大大i 提交于 2019-12-07 12:32:48

ST板子OTT盒子没法通过实时码流获取时间,只能通过网络实时获取时间,这篇文档记录下自己解决的整个过程,网上已经有很多NTP 服务器配置、ARM板子移植博文,大家可以参考,详细原理我也是查阅网上的资料;


1、NTP官网下载源码:

http://www.ntp.org/downloads.html


2、编译

[root@bogon ntp-4.2.6p5]#./configure --prefix=/opt/ntp --exec-prefix=/opt/ntp --host=sh4-linux CC=/opt/STM/STLinux-2.4/devkit/sh4/bin/sh4-linux-gcc

其中:

If you give `configure' the option `--exec-prefix=PATH', the package will use PATH as the prefix for installing programs and libraries.

通常对于嵌入式系统来说,--target、--build不需要更改;

交叉编译参数只需要指定 --host=sh4-linux 即可;


接着编译:

[root@bogon ntp-4.2.6p5]# make
ntp_loopfilter.c:520:27: error: ?.OD_NANO?.undeclared (first use in this function)

参考网上,修改 ntp_loopfilter.c:

[root@bogon ntp-4.2.6p5]# find ./ -name ntp_loopfilter.c
./ntpd/ntp_loopfilter.c

添加下面定义:

ifndef MOD_NANO

#define MOD_NANO 0x2000

#endif


最后编译成功,生成了对应文件:

[root@bogon ntp-4.2.6p5]# make install
[root@bogon ntp]# pwd
/opt/ntp
[root@bogon ntp]# ls bin/
ntpd  ntpdate  ntpdc  ntp-keygen  ntpq  ntptime  ntptrace  ntp-wait  sntp  tickadj


3、配置ntp,同时调用ntpdate 同步时间;

a>首先把上面bin目录下的需要的命令文件拷贝到开发板的/bin目录下,或者全部拷贝过去,

b>同时把服务器/etc/ntp.conf 拷贝到板子上同样目录下,并设置时间同步服务器为:

server 2.cn.pool.ntp.org
server 1.asia.pool.ntp.org

http://www.pool.ntp.org这上面我们可以找到离我们城市最近的NTP Server. NTP建议我们为了保障时间的准确性,最少找两个个NTP Server;我选择了下面两个服务器:


c>接下来在板子后台,使用ntpdate同步授时服务器时间:

# ntpdate 2.cn.pool.ntp.org
ntpdate: error while loading shared libraries: libcap.so.2: cannot open shared object file: No such file or directory

解决方法: 在交叉编译工具目录:/opt/STM/STLinux-2.4/devkit/sh4/target/usr/lib下拷贝libcap.so.2和libattr.so.1 到开发板上;


②上面OK后,提示下面:

# ntpdate 2.cn.pool.ntp.org
Error resolving cn.pool.ntp.org: Servname not supported for ai_socktype (-8)
 1 Jan 00:01:04 ntpdate[1734]: Can't find host cn.pool.ntp.org: Servname not supported for ai_socktype (-8)
 1 Jan 00:01:04 ntpdate[1734]: no servers can be used, exiting

解决方法: 需要设置ntp对应网口(这个我在服务器上看着是有的,板子上没有这个文件), 在/etc/services加入两行

ntp             123/tcp
ntp             123/udp                         # Network Time Protocol


③提示如下:

# ntpdate 2.cn.pool.ntp.org
 1 Jan 00:01:27 ntpdate[1765]: no server suitable for synchronization found
# ntpdate -d cn.pool.ntp.org
 1 Jan 00:01:36 ntpdate[1803]: ntpdate 4.2.6p5@1.2349 Fri Mar  7 11:01:43 UTC 2014 (2)
Looking for host cn.pool.ntp.org and service ntp
host found : dns.sjtu.edu.cn
transmit(202.120.2.101)
transmit(218.75.4.130)
transmit(202.112.29.82)
transmit(202.112.31.197)
transmit(202.120.2.101)
transmit(218.75.4.130)
transmit(202.112.29.82)
transmit(202.112.31.197)
transmit(202.120.2.101)
transmit(218.75.4.130)
transmit(202.112.29.82)
transmit(202.112.31.197)
transmit(202.120.2.101)
transmit(218.75.4.130)
transmit(202.112.29.82)
transmit(202.112.31.197)
transmit(202.120.2.101)
transmit(218.75.4.130)
transmit(202.112.29.82)
transmit(202.112.31.197)
202.120.2.101: Server dropped: no data
218.75.4.130: Server dropped: no data
202.112.29.82: Server dropped: no data
202.112.31.197: Server dropped: no data
server 202.120.2.101, port 123
stratum 0, precision 0, leap 00, trust 000
refid [202.120.2.101], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time:    00000000.00000000  Thu, Feb  7 2036  6:28:16.000
originate timestamp: 00000000.00000000  Thu, Feb  7 2036  6:28:16.000
transmit timestamp:  bc17c266.45aa6c31  Sat, Jan  1 2000  0:01:42.272
filter delay:  0.00000  0.00000  0.00000  0.00000 
         0.00000  0.00000  0.00000  0.00000 
filter offset: 0.000000 0.000000 0.000000 0.000000
         0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
server 218.75.4.130, port 123
stratum 0, precision 0, leap 00, trust 000
refid [218.75.4.130], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time:    00000000.00000000  Thu, Feb  7 2036  6:28:16.000
originate timestamp: 00000000.00000000  Thu, Feb  7 2036  6:28:16.000
transmit timestamp:  bc17c266.78dc33a9  Sat, Jan  1 2000  0:01:42.472
filter delay:  0.00000  0.00000  0.00000  0.00000 
         0.00000  0.00000  0.00000  0.00000 
filter offset: 0.000000 0.000000 0.000000 0.000000
         0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
server 202.112.29.82, port 123
stratum 0, precision 0, leap 00, trust 000
refid [202.112.29.82], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time:    00000000.00000000  Thu, Feb  7 2036  6:28:16.000
originate timestamp: 00000000.00000000  Thu, Feb  7 2036  6:28:16.000
transmit timestamp:  bc17c266.ac0d0ae2  Sat, Jan  1 2000  0:01:42.672
filter delay:  0.00000  0.00000  0.00000  0.00000 
         0.00000  0.00000  0.00000  0.00000 
filter offset: 0.000000 0.000000 0.000000 0.000000
         0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
server 202.112.31.197, port 123
stratum 0, precision 0, leap 00, trust 000
refid [202.112.31.197], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time:    00000000.00000000  Thu, Feb  7 2036  6:28:16.000
originate timestamp: 00000000.00000000  Thu, Feb  7 2036  6:28:16.000
transmit timestamp:  bc17c266.df4488f9  Sat, Jan  1 2000  0:01:42.872
filter delay:  0.00000  0.00000  0.00000  0.00000 
         0.00000  0.00000  0.00000  0.00000 
filter offset: 0.000000 0.000000 0.000000 0.000000
         0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
 1 Jan 00:01:44 ntpdate[1803]: no server suitable for synchronization found

解决方法: 悲剧乎,公司内网要认证才能上网,找了根VPN外网就好了;


接着:

# ntpdate cn.pool.ntp.org
11 Mar 12:13:23 ntpdate[1781]: step time server 218.75.4.130 offset 447855103.036517 sec

OK,这下成功了,不过这个时间是不对的,现在准确应该是20:13左右,这是因为没有设置时区,这个是0时区的时间;

解决方法: 从服务器上把/usr/share/zoneinfo/Asia中的文件Shanghai拷贝到开发板目录/etc中并且改名为localtime;


最后:

# ntpdate cn.pool.ntp.org
11 Mar 20:21:29 ntpdate[1822]: step time server 218.75.4.130 offset 447855575.325113 sec
# date
Tue Mar 11 20:21:44 CST 2014

这下基本OK,没有问题了;


实际情况下,注意 ntpdate 时间更新对依赖时间的模式产生的影响,我这边还一堆连锁问题等着改...


参考:

Linux NTP配置详解 (Network Time Protocol)

http://blog.csdn.net/iloli/article/details/6431757

NTP移植和使用

http://m.blog.csdn.net/blog/orange1988319/8880788


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!