一、安装工具包
安装rpmbuild包,rpmdev-tools,gcc包
挂载安装镜像,进入Packages目录
rpm -ivh rpm-build-4.8.0-32.el6.x86_64.rpm
rpm -ivh rpmdevtools-7.5-2.el6.noarch.rpm
二、建立相关目录和模板文件
mkdir -p
/root/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
cd /root/rpmbuild/SPECS/
rpmdev-newspec ntp //该命令会生成一个ntp.spec的最小模板,然后我们根据这个模板进行修改
为了方便每次重新编译前清理目录,我做了个小脚本
#cat clean.sh
rm -rf /root/rpmbuild/BUILD/ntp-4.2.8p9
rm -rf
/root/rpmbuild/BUILDROOT/ntp-4.2.8p9-1.el6.x86_64/usr
rm -f /root/rpmbuild/BUILDROOT/RPMS/x86_64/ntp-4.2.8p9-1.el6.x86_64.rpm
rm -f
root/rpmbuild/BUILDROOT/SRPMS/ntp-4.2.8p9-1.el6.src.rpm
三、编辑ntp.spec文件
关于该文件的具体参数解释请查看该参考址:
-------------------------------------------------------------------------------------------------
Name: ntp
Version: 4.2.8p9
Release: 1%{?dist}
Summary: compiled from 4.2.8p9 by yunhw
Group: System Environment/Daemons
License: GPL
Source0: ntp-4.2.8p9.tar.gz
BuildRoot: %_topdir/BUILDROOT
BuildRequires: gcc
Requires: rpm
%description
%prep
%setup -q
%build
%configure
./configure --prefix=/usr
make %{?_smp_mflags}
%install
#rm -rf %{buildroot} 这里之所以注释是为了在/root/rpmbuild/BUILDROOT/ntp-4.2.8p9-1.el6.x86_64目录下能放一些自己的文件,这就需要每次重新编译之前,手动删除这个目录的不用的文件
make install DESTDIR=%{buildroot}
%clean
#rm -rf %{buildroot} 这里之所以注释是为了在/root/rpmbuild/BUILDROOT/ntp-4.2.8p9-1.el6.x86_64目录下能放一些自己的文件,这就需要每次重新编译之前,手动删除这个目录的不用的文件
%files
%defattr(-,root,root,-)
/usr/
/etc/
/var
%exclude /usr/sbin/ntpdate
%changelog
%pre
#/bin/bash
if [ -f /etc/ntp.conf ]
then
cp /etc/ntp.conf /etc/ntp.conf.bak
fi
%post
chkconfig --add ntpd
chkconfig ntpd on
%preun
if [ -f /etc/ntp.conf ]
then
cp /etc/ntp.conf /etc/ntp.conf.bak
fi
---------------------------------------------------------------------------------------------------
四、编译前文件准备
在安装镜像中找到该本的ntp包,拷贝至一个目录,然后使用rpm2cpio
ntp*|cpio -idmv命令把其中的文件解压出来,将其中的/var和/etc目录拷贝至/root/rpmbuild/BUILDROOT/ntp-4.2.8p9-1.el6.x86_64/中,这样/etc/和/var/就会被打包进去了,因为我发现使用源码编译只会在/usr中生成安装文件,至于/etc下的相关配置文件和脚本则没有。
五、编译生成
rpmbuild -bb ntp.spec
生成的rpm文件在/root/rpmbuild/RPMS/x86_64下
六、注意事项
1、这是在redhat6.4
x86_64上编译的,如果你要在更高的系统版本上用,请先做个测试,然后再装
2、如果系统里已经安装了ntp的包,那么安装方法是rpm -Uvh ntp***,没有安装的话,安装方法是rpm -ivh ntp***
3、以前用源码直接编译升级过ntp的请注意先把源码安装的文件删除了,再安装。
4、如果在执行service
ntpd restart时报如下错误:
---------------------------------------------------------------------------------------------------
[root@hxgz01 sbin]# service ntpd start
Starting ntpd: /usr/sbin/ntpd: The 'user' option has been
disabled. -- built without --enable-clockctl or --enable-linuxcaps or
--enable-solarisprivs
ntpd - NTP daemon program - Ver. 4.2.8p8
Usage: ntpd [ - [] |
--[{=| }] ]... \
[ ... ]
Try 'ntpd --help' for more information.
[FAILED]
-------------------------------------------------------------------------------------------
请将安装/etc/sysconfig/ntpd文件OPTIIONS行中的-u ntp:ntp删