一、rpmbuild以及其辅助工具的安装
yum -y install rpmbuild ---安装rpmbuild
yum install rpmdevtools ---辅助工具安装
二、生成rpmbuild打包一系列目录和spec打包说明文件
在这里rpm官方推荐为了避免使用root编译安装破坏库环境的问题,使用非root用户进行rpmbuild打包
(1)生成目录
rpmdev-setuptree 命令在当前用户的~目录底下生成rpmbuild目录,里面共5个子目录
BUILD ---源文件解压后默认放置位置
BUILDROOT ---编译安装虚拟根目录
RPMS ---rpmbuild打包后的rpm包存放目录
SOURCES ---源文件存放位置,本次实践从bind官网下载bind-9.11.19.tar.gz源码放到此目录下
SPECS ---spec打包说明文件存放位置,
SRPMS ---rpmbuild打包后的src.rpm包存放目录
(2)生成spec文件
cd ~/rpmbuild/SPECS;
rpmdev-newspec bind.spec
最终在本次实践中最后编辑生成的spec文件如下:
Name: bind
Version: 9.11.19
Release: 1
Summary: The Berkeley Internet Name Domain (BIND) DNS (Domain Name System) server.
Group: System Environment/Daemons
License: BSD-like
URL: http://www.isc.org/products/BIND/
Source0: bind-9.11.19.tar.gz
%description
BIND (Berkeley Internet Name Domain) is an implementation of the DNS
(Domain Name System) protocols. BIND includes a DNS server (named),
which resolves host names to IP addresses; a resolver library
(routines for applications to use when interfacing with DNS); and
tools for verifying that the DNS server is operating properly.
%prep
%setup -q
%build
%configure -prefix=/usr --without-python
make %{?_smp_mflags}
%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT
%pre
if [ "$1" -eq 1 ]; then
echo -e "\033[42mstart install bind.\033[0m"
##add bind group & user
if ! getent group named >/dev/null 2>&1; then
/usr/bin/chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow
/usr/sbin/groupadd -r -g 25 named
fi
if ! getent passwd named >/dev/null 2>&1; then
/usr/sbin/useradd -r -u 25 -g named -d /var/lib/named -c "BIND Service user" -M -s /sbin/nologin named
fi
fi
%post
if [ "$1" -eq 1 ]; then
echo -e "\033[42mbind install ok, enjoy your dns journey\033[0m"
fi
%preun
if [ "$1" -eq 0 ]; then
echo "bind will be removed from your machine"
fi
%postun
if [ "$1" -eq 0 ]; then
##del bind group & user
if getent group named >/dev/null 2>&1; then
/usr/sbin/groupdel named >/dev/null 2>&1
fi
if getent passwd named >/dev/null 2>&1; then
/usr/sbin/userdel named >/dev/null 2>&1
fi
echo -e "\033[42mgoodbye, hope to see you again\033[0m"
fi
%clean
rm -rf $RPM_BUILD_ROOT
%files
/usr/sbin/*
/usr/bin/arpaname
/usr/bin/bind9-config
/usr/bin/isc-config.sh
/usr/lib64/*
%defattr(-,root,root,-)
%config(noreplace) /etc/bind.keys
%doc /usr/share/man/man1/arpaname.1.gz
%doc /usr/share/man/man1/bind9-config.1.gz
%doc /usr/share/man/man1/isc-config.sh.1.gz
%doc /usr/share/man/man3/*
%doc /usr/share/man/man5/named.conf.5.gz
%doc /usr/share/man/man5/rndc.conf.5.gz
%doc /usr/share/man/man8/*
%changelog
* Wed May 27 2020 huangzx3
patch first time
三、进行rpmbuild打包
rpmbuild -ba bind.spec
接下来就是根据spec打包说明文件进行解压包、Make.file文件生成,等待编译、编译安装、rpm打包,输出最终的rpm包了
四、打包时遇见的一些坑
(1) 打包过程当中,因为有些文件或者配置或者可执行文件以及源代码文件不需要打到rpm包中,所以报了Installed (but unpackaged) file(s) found 的报错,并且没办法继续进行进行rpm打包
解决方式:通过编辑全局宏定义文件:/usr/lib/rpm/macros,将其中的__check_file宏定义进行注释
(2)默认生成debug-info的rpm包,这个是我们不需要的,在平常的使用中一般情况下也用不到这种debug-info的包
解决方式:在使用rpmbuild打包的用户目录底下增加一个宏定义以此来覆盖/usr/lib/rpm/macros中的宏定义
echo "%debug_package %{nil}" >> ~/.rpmmacros
(3)centos 6.5上打的包在centos 5.8上无法正常运行,报函数库依赖
解决方式:在centos 5.8上进行打包,可以在5.8及以上系统安装使用,即高版本兼容低版本
(4)使用centos 5.8打包的过程,最后rpmbuild(版本较低)在打包的时候一直没有使用虚拟根目录,而是直接在根目录下的绝对路径进行打包
解决方式:在spec文件中重新定义buildroot(不区分大小写),尝试在外部全局宏定义/usr/lib/rpm/macros以及打包用户的~/.rpmmacros中的宏定义重新定义buildroot,结果发现还是按照根路径进行绝对路径打包,但是在spec中指定之后,就以虚拟根目录buildroot为绝对路径开始来进行打包