一、rpmbuild方式定制rpm包
rpmbuid是用于制作rpm格式包的工具。rpm 4.4.x版本之前,rpmbuid工具默认的工作车间为/usr/src/redhat,所以造成普通用户不能制作rpm包。rpm 4.5.x版本开始,将rpmbuid工具默认的工作车间为$HOME/rpmbuild(用户家目录),并且推荐用户尽量不用root账号制作rpm包。
#rpm版本查看方法:
[root@localhost ~]# rpm -qa | grep rpm
perl-srpm-macros-1-8.el7.noarch
rpm-python-4.11.3-35.el7.x86_64
rpm-4.11.3-35.el7.x86_64
rpm-build-libs-4.11.3-35.el7.x86_64
redhat-rpm-config-9.1.0-87.el7.centos.noarch
rpm-build-4.11.3-35.el7.x86_64
rpm-libs-4.11.3-35.el7.x86_64
rpmbuild默认工作车间为/usr/lib/rpm/macros这个文件,具体由%_topdir宏变量进行定义。官方不建议在/usr/lib/rpm/macros目录中更改这个工作路径。如果需要则可以在用户家目录下建立一个.rpmmacros隐藏文件,在里面重新定义%_topdir,指向一个新的目录名。内容一般为:
[gxm@localhost ~]$ cat .rpmmacros
%_topdir $HOME/新目录
在%_topdir目录下一般需要有6个目录(实际操作的时候执行命令自动创建):
备注:执行rpmdev-setuptree命令会在当前用户的家目录下的rpmbuild目录(如果该目录不存在也会被自动创建)里自动建立上述目录。
目录名macros宏名说明
BUILD%_builddir编译rpm包的临时目录
BUILDROOT%_buildrootdir编译后生成的软件临时安装目录
RPMS%_rpmdir最终生成的可安装rpm包的所在目录
SOURCES%_sourcedir所有源代码和补丁文件的存放目录
SPECS%_specdir存放SPEC文件的目录(重要)
SRPMS%_srcrpmdir软件最终的rpm源码格式存放路径(暂时忽略掉,别挂在心上)
当上述目录建立好之后,将所有用于生成rpm包的源代码、shell脚本、配置文件都拷贝到SOURCES目录里,注意通常情况下源码的压缩格式都为*.tar.gz格式。然后将编辑好的SPEC文件,命名为“软件名-版本.spec”,将其拷贝到SPECS目录下。
最后切换SPEC目录下执行命令制作rpm包:
[gxm@localhost ~]$ cd SPEC
[gxm@localhost SPEC]$ rpmbuild -bb 软件名-版本.spec
#打包好的rpm包在RPMS目录下
SPEC文件是最核心的,在它里面定义了头部信息和一些“阶段”(%prep、%build、%install、%clean、%pre、%post、%preun和%postun),当rpmbuild执行时它首先会去解析SPEC文件,然后依次执行每个“阶段”里的指令。
参考:http://blog.chinaunix.net/uid-23069658-id-3944462.html
1、安装相关包和查看默认工作车间
[root@localhost ~]# yum -y install rpm-build rpmdevtools
[root@localhost ~]# rpmbuild --showrc | grep topdir
-14: _builddir %{_topdir}/BUILD
-14: _buildrootdir %{_topdir}/BUILDROOT
-14: _rpmdir %{_topdir}/RPMS
-14: _sourcedir %{_topdir}/SOURCES
-14: _specdir %{_topdir}/SPECS
-14: _srcrpmdir %{_topdir}/SRPMS
-14: _topdir %{getenv:HOME}/rpmbuild
2、新增普通用户
[root@localhost ~]# useradd gxm
[root@localhost ~]# passwd gxm
更改用户 gxm 的密码 。
新的 密码:
无效的密码: 密码未通过字典检查 - 它基于字典单词
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost ~]# su - gxm
[gxm@localhost ~]$
3、自动生成rpm打包所需目录
[gxm@localhost ~]$ rpmdev-setuptree
[gxm@localhost ~]$ ll
总用量 0
drwxrwxr-x 7 gxm gxm 67 4月 16 11:38 rpmbuild
[gxm@localhost ~]$ cd rpmbuild/
[gxm@localhost rpmbuild]$ ll
总用量 0
drwxrwxr-x 2 gxm gxm 6 4月 16 11:38 BUILD
drwxrwxr-x 2 gxm gxm 6 4月 16 11:38 RPMS
drwxrwxr-x 2 gxm gxm 6 4月 16 11:38 SOURCES
drwxrwxr-x 2 gxm gxm 6 4月 16 11:38 SPECS
drwxrwxr-x 2 gxm gxm 6 4月 16 11:38 SRPMS
4、将源码、配置文件等放入SOURCES目录下
[gxm@localhost rpmbuild]$ cd SOURCES/
[gxm@localhost SOURCES]$ wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX Latest Stable/4.0.4/zabbix-4.0.4.tar.gz
[gxm@localhost SOURCES]$ 上传其它文件
5、创建SPEC模板文件熟悉
[gxm@localhost rpmbuild]$ cd SPECS/
[gxm@localhost SPECS]$ rpmdev-newspec -o zabbix_agent-4.0.4.spec
zabbix_agent-4.0.4.spec created; type minimal, rpm version >= 4.11.
[gxm@localhost SPECS]$ cat zabbix_agent-4.0.4.spec
Name: zabbix_agent-4.0.4
Version:
Release: 1%{?dist}
Summary:
License:
URL:
Source0:
BuildRequires:
Requires:
%description
%prep
%setup -q
#这里要特别注意,如果压缩包和解压出来的文件名不相同,则用%setup -n 解压后目录名
%build
%configure
make %{?_smp_mflags}
%install
rm -rf $RPM_BUILD_ROOT
%make_install
%files
%doc
%changelog
SPEC完整版如下:
%define zabbix_user zabbix
%define zabbix_dir /home/zabbix
Name: zabbix_agent
Version: 4.0.4
Release: 1%{?dist}
Summary: zabbix agent
Group: zabbix
License: GPL
URL: www.zabbix.com
Source0: zabbix-4.0.4.tar.gz
Source1: zabbix_agentd
Source2: discovertcpport.sh
Source3: portlist.txt
Source4: discoveryproc.sh
Source5: proclist.txt
Source6: cmdline-jmxclient-0.10.3.jar
Source7: jvm.py
Source8: tomcat.py
Source9: ca.crt
Source10: client.crt
Source11: client.key
Source12: java
Source13: monitor_port
Source14: monitor_proc
BuildRequires: gcc, gcc-c++, pcre, pcre-devel, openssl, openssl-devel
Requires: gcc, gcc-c++, pcre, pcre-devel, openssl, openssl-devel
%description
Zabbix agentd 4.0.4
%pre
grep %zabbix_user /etc/passwd > /dev/null
if [ $? != 0 ]
then useradd %zabbix_user -M -s /sbin/nologin
fi
[ -d %{zabbix_dir} ]||rm -rf %{zabbix_dir}
%post
mkdir -p %{zabbix_dir}/logs
chown -R %zabbix_user.%zabbix_user %{zabbix_dir}
chmod -R 755 %{zabbix_dir}
sed -i “s/LogFile=/tmp/zabbix_agentd.log/LogFile=%{zabbix_dir}/logs/zabbix_agentd.log/g” %{zabbix_dir}/etc/zabbix_agentd.conf
sed -i “s/ServerActive=127.0.0.1/ServerActive=192.168.7.10/g” %{zabbix_dir}/etc/zabbix_agentd.conf
ipaddr=ip a | grep -w "inet" | egrep "eno16777736|eth0" |sed 's/^.*inet //g'|sed 's/\/[0-9][0-9].*$//g'
sed -i “s/Hostname=Zabbix server/Hostname=$ipaddr/g” %{zabbix_dir}/etc/zabbix_agentd.conf
sed -i “s/Timeout=3/Timeout=30/g” %{zabbix_dir}/etc/zabbix_agentd.conf
echo “UnsafeUserParameters=1” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “EnableRemoteCommands=1” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “Include=%{zabbix_dir}/etc/zabbix_agentd.conf.d/” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “PidFile=%{zabbix_dir}/zabbix_agentd.pid” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “LogFileSize=0” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “StartAgents=0” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “HostMetadataItem=system.uname” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “User=zabbix” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “TLSConnect=cert” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “TLSAccept=cert” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “TLSCAFile=%{zabbix_dir}/zabbix_ssl/ca.crt” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “TLSCertFile=%{zabbix_dir}/zabbix_ssl/client.crt” >>%{zabbix_dir}/etc/zabbix_agentd.conf
echo “TLSKeyFile=%{zabbix_dir}/zabbix_ssl/client.key” >>%{zabbix_dir}/etc/zabbix_agentd.conf
sed -i “s/BASEDIR=/usr/local/BASEDIR=/home/zabbix/g” /etc/rc.d/init.d/zabbix_agentd
systemctl daemon-reload
/etc/init.d/zabbix_agentd start
systemctl enable zabbix_agentd
%preun
systemctl stop zabbix_agentd
%postun
userdel -r zabbix
rm -rf /etc/rc.d/init.d/zabbix_agentd
rm -rf %{zabbix_dir}
%prep
%setup -n zabbix-4.0.4
%build
./configure --prefix=%{zabbix_dir} --enable-agent --with-openssl
make %{?_smp_mflags}
%install
rm -rf %{buildroot}
install -d %{buildroot}/etc/profile.d
make install DESTDIR=%{buildroot}
install -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/rc.d/init.d/zabbix_agentd
install -p -D %{SOURCE2} %{buildroot}%{zabbix_dir}/script/monitor_port/discovertcpport.sh
install -p -D %{SOURCE3} %{buildroot}%{zabbix_dir}/script/monitor_port/portlist.txt
install -p -D %{SOURCE4} %{buildroot}%{zabbix_dir}/script/monitor_proc/discoveryproc.sh
install -p -D %{SOURCE5} %{buildroot}%{zabbix_dir}/script/monitor_proc/proclist.txt
install -p -D %{SOURCE6} %{buildroot}%{zabbix_dir}/script/jvm/cmdline-jmxclient-0.10.3.jar
install -p -D %{SOURCE7} %{buildroot}%{zabbix_dir}/script/jvm/jvm.py
install -p -D %{SOURCE8} %{buildroot}%{zabbix_dir}/script/jvm/tomcat.py
install -p -D %{SOURCE9} %{buildroot}%{zabbix_dir}/zabbix_ssl/ca.crt
install -p -D %{SOURCE10} %{buildroot}%{zabbix_dir}/zabbix_ssl/client.crt
install -p -D %{SOURCE11} %{buildroot}%{zabbix_dir}/zabbix_ssl/client.key
install -p -D %{SOURCE12} %{buildroot}%{zabbix_dir}/etc/zabbix_agentd.conf.d/java
install -p -D %{SOURCE13} %{buildroot}%{zabbix_dir}/etc/zabbix_agentd.conf.d/monitor_port
install -p -D %{SOURCE14} %{buildroot}%{zabbix_dir}/etc/zabbix_agentd.conf.d/monitor_proc
echo ‘export PATH=%{zabbix_dir}/bin:%{zabbix_dir}/sbin:$PATH’ > %{buildroot}/etc/profile.d/%{name}.sh
%files
%defattr (-,root,root,0755)
%{zabbix_dir}
%{zabbix_dir}/*
/etc/profile.d/%{name}.sh
%attr(0755,root,root) /etc/rc.d/init.d/zabbix_agentd
%changelog
%clean
rm -rf %{buildroot}
6、打包编译
[root@localhost ~]# yum install gcc, gcc-c++, pcre, pcre-devel, openssl, openssl-devel -y
[gxm@localhost SPECS]$ rpmbuild -ba zabbix_agent-4.0.4.spec
[gxm@localhost SPECS]$ rpmbuild -bb zabbix_agent-4.0.4.spec
#-ba和-bb二选一
7、下载安装
[gxm@localhost SPECS]$ sz …/RPMS/x86_64/zabbix_agent-4.0.4-1.el7.centos.x86_64.rpm
[root@localhost ~]# yum localinstall zabbix_agent-4.0.4-1.el7.centos.x86_64.rpm -y
[root@localhost ~]# rpm -qpi zabbix_agent-4.0.4-1.el7.centos.x86_64.rpm
Name : zabbix_agent
Version : 4.0.4
Release : 1.el7.centos
Architecture: x86_64
Install Date: (not installed)
Group : zabbix
Size : 813091
License : GPL
Signature : (none)
Source RPM : zabbix_agent-4.0.4-1.el7.centos.src.rpm
Build Date : 2019年04月20日 星期六 22时22分05秒
Build Host : localhost
Relocations : (not relocatable)
URL : www.zabbix.com
Summary : zabbix agent
Description :
Zabbix agentd 4.0.4