今天想让板子在开机的时候自动去同步网络上的时间,网上查了一下,需要使用到ntpdate命令。但是我使用的文件系统(busybox制作的文件系统)没有该命令,所以移植了一下。由于移植ntpdate需要用到openssl的头文件和库,所以也移植了openssl。
PC系统:Ubuntu Ubuntu 12.04.4 LTS
1. 创建工作目录
|
1
|
mkdir crossCompileDir
|
2. 获取openssl和ntpdate源码
我是直接使用apt-get来获取源码的,也可以去官网下载: www.ntp.org 和 www.openssl.org
|
1
2 3 |
cd crossCompileDir
apt-get source ntpdate apt-get source openssl |
3. 移植openssl
|
1
2 |
cd openssl-1.0.1
mkdir /opt/crossCompileInstallDir/openssl-1.0.1 -p |
配置
|
1
2 3 |
NM=arm-linux-gnueabihf-nm
AR=arm-linux-gnueabihf-ar \
RANLIB=arm-linux-gnueabihf-ranlib CC=arm-linux-gnueabihf-gcc \ ./config no-asm shared --prefix=/opt/crossCompileInstallDir/openssl-1.0.1 |
然后修改Makefile,将里面的-m64全部去掉(两个地方)
还有:
INSTALL_PREFIX=/opt/crossCompileInstallDir/openssl-1.0.1
不然make install 的时候不对
编译 不能使用多线程编译,否则会报错的!
|
1
2 |
make
make install |
需要处理一下:
|
1
2 |
mv /opt/crossCompileInstallDir/openssl-1.0.1/opt/crossCompileInstallDir/openssl-1.0.1*
/opt/crossCompileInstallDir/openssl-1.0.1
rm -rf /opt/crossCompileInstallDir/openssl-1.0.1/opt |
4. 移植ntpdate命令(ntpdate在ntp软件包中)
|
1
2 |
cd ../ntp-4.2.6.p3+dfsg/
mkdir /opt/crossCompileInstallDir/ntp-4.2.6.p3+dfsg |
配置
|
1
2 3 |
CC=arm-linux-gnueabihf-gcc ./configure
--prefix=/opt/crossCompileInstallDir/ntp-4.2.6.p3+dfsg \
--host=arm-linux-gnueabihf --with-openssl-libdir=/opt/crossCompileInstallDir/openssl-1.0.1/lib \ --with-openssl-incdir=/opt/crossCompileInstallDir/openssl-1.0.1/include |
编译
|
1
2 |
make
make install |
错误:
1、./keyword-gen ./ntp_parser.h > k-g.out
/bin/bash: ./keyword-gen: cannot execute binary file
make[4]: *** [k-g-u-submake] Error 126
http://www.dotmana.com/weblog/2011/01/fix-cross-compile-ntpd-4-2-6-keyword-gen-cannot-execute-binary-file/
5. 板子上使用
将移植出来的文件拷贝到板子上的对应目录中。然后使用,出现了一下错误1:
|
1
2 3 4 |
ntpdate -u ntp.api.bz
Error resolving ntp.api.bz: Servname not supported for ai_socktype (-8) 5 Mar 17:55:22 ntpdate[211]: Can't find host ntp.api.bz: Servname not supported for ai_socktype (-8) 5 Mar 17:55:22 ntpdate[211]: no servers can be used, exiting |
解决办法是将以下两行内容:
|
1
2 |
ntp 123/tcp
ntp 123/udp |
写进/etc/services文件中,就可以了使用命令同步网络时间了。
错误2:
ntpdate: /usr/lib/libcrypto.so.1.0.0: no version information available (required by ntpdate)
解决办法:
用编译的openssl更新libcrypto.so.1.0.0
但是还有一个问题,同步出来的时间和所在的时区不对,如下所示:
|
1
2 3 4 5 |
ntpdate -u ntp.api.bz
5 Mar 09:58:05 ntpdate[217]: adjust time server 61.153.197.226 offset -0.010535 sec #使用date命令可以看到是UTC(协调世界时)的时间的 date Thu Mar 5 09:58:35 UTC 2015 |
设置时区:
从pc机上拷贝/usr/share/zoneinfo/Asia/Hong_Kong (或者使用 Shanghai Chongqing)到板子上的对应位置,然后再 cp /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime,或者做一个软连接即可。
再次同步时间:
|
1
2 3 4 5 |
ntpdate -u ntp.api.bz
5 Mar 18:06:10 ntpdate[221]: adjust time server 61.153.197.226 offset -0.018679 sec date Thu Mar 5 18:06:24 HKT 2015 |
将命令ntpdate -u ntp.api.bz添加到/etc/profile文件中即可。
当然想要开机同步网络时间,前提是板子开机起来就直接可以上网。
本文介绍如何在busybox文件系统中移植ntpdate命令及其依赖openssl,并解决跨编译时遇到的问题。同时,还提供了如何正确设置板子的时区以确保时间和所在地区一致的方法。
3044







This happens when ntp version is cross compiled, In this case keyword-gen is generated for the target architecture. During compilation of ntpd keyword-gen is also executed thus it fails as it is created for some other architecture.
Solution: Compile ntp using gcc(Don’t cross compile). Now replace keyword-gen in cross compiled ntp with keyword-gen in gcc compiled ntp. Now cross compile ntp again.