Home > Operation and Maintenance > Linux Operation and Maintenance > Sharing of RPM packaging process under Linux

Sharing of RPM packaging process under Linux

小云云
Release: 2018-02-08 14:54:10
Original
2453 people have browsed it

This article mainly shares with you the RPM packaging process under Linux, hoping to help everyone.

Preparation before starting

Install rpmbuild software package

  • yum -y install rpm-build

    Generate related directories

To generate rpm-related directories, you can create them manually or through the rpmbuild command. For example:

[root@yang data]# rpmbuild zabbix_agentd_ops.spec 
error: File /root/rpmbuild/SOURCES/zabbix-3.0.3.tar.gz: No such file or directory

有报错,无需理会,可以看到rpmbuild目录已经创建完成

[root@yang ~]# tree rpmbuild/
rpmbuild/
├── BUILD
├── BUILDROOT
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS
Copy after login

Write SPEC file

SPEC keyword

Name: The name of the software package, which can be referenced later using %{name}

Summary: Summary of the contents of the software package

Version: The actual version number of the software, for example: 1.0.1, etc., which can be referenced later using %{version}

Release: Release serial number, for example: 1linuxing, etc., indicate the number of times to package, you can use %{release} to reference later

Group: Software grouping, it is recommended to use standard grouping

License: Software authorization method, usually GPL

Source: Source code package, you can bring multiple sources such as Source1, Source2, etc., and you can also use %{source1}, %{source2} later to reference

BuildRoot: This is used during installation or compilation "Virtual directory", considering the multi-user environment, is generally defined as: %{tmppath}/{name}-%{version}-%{release}-root or %{tmppath}/%{name}-%{version }-%{release}-buildroot-%%__id_u} -n}. This parameter is very important because during the process of generating rpm, the software will be installed into the above path when executing make install. When packaging, It also relies on the "virtual directory" to be the "root directory" for operations. It can be referenced later using the $RPM_BUILD_ROOT method.

URL: Home page of the software

Vendor: Publisher or packaging organization information, such as RedFlag Co,Ltd

Disstribution: Distribution identification

Patch : Patch source code, you can use Patch1, Patch2, etc. to identify multiple patches, use %patch0 or %{patch0} to reference

Prefix: %{_prefix} This is mainly to solve the problem that when installing rpm packages in the future, it will not necessarily Install the software into the directory packaged in rpm. In this way, the identifier must be defined here and referenced when writing the %install script to realize the function of re-specifying the location during rpm installation

Prefix: %{sysconfdir} This reason is the same as above, but because %{prefix} refers to /usr, and for other files, such as configuration files under /etc, you need to use %{_sysconfdir} to identify

Build Arch: refers to the target processor architecture for compilation, and the noarch indicator does not Specified, but usually the content in /usr/lib/rpm/marcros is used as the default value

Requires: The name of the software package that the rpm package depends on. You can use >= or <= to indicate greater than Or less than a specific version, for example: libpng-devel >= 1.0.20 zlib ※ The ">=" sign needs to be separated by spaces, and different software names are also separated by spaces, as well as PreReq, Requires (pre ), Requires(post), Requires(preun), Requires(postun), BuildRequires, etc. are all specified for dependencies at different stages

Provides: Indicate some specific functions of this software so that other rpm can identify them

Packager: Packager’s information

%description Detailed description of the software

SPEC script body

%prep Preprocessing script

%setup - n %{name}-%{version}** Unzip and put the source code package, usually from the package in /usr/src/asianux/SOURCES to /usr/src/asianux/BUILD/%{name}-% {version}. Generally, %setup -c is enough, but there are two situations: one is to compile multiple source code packages at the same time, and the other is that the name of the source code tar package is inconsistent with the decompressed directory. In this case, you need to use the -n parameter to specify For a moment.

%patch When patching, usually the patches will be included in the source code tar.gz package, or placed in the SOURCES directory. The general parameters are:

  • %patch -p1 Use the Patch defined previously, -p1 is the first layer item that ignores the patch

  • %Patch2 -p1 -b xxx.patch applies the specified patch, -b refers to generating a backup file

Supplement

  • %setup does not add Any option, only the package will be opened.

  • %setup -n newdir Unzip the software package in the newdir directory.

  • %setup -c generates the directory before decompression.

  • %setup -b num Decompress the numth source file.

  • %setup -T does not use the default decompression operation.

  • %setup -T -b 0 Decompress the 0th source code file.

  • %setup -c -n newdir Specifies the directory name newdir and generates the rpm package in this directory.

  • %patch is the simplest patching method, automatically specifying the patch level.

  • %patch 0 uses the 0th patch file, equivalent to %patch ?p 0.

  • %patch -s does not display patching information.

  • %patch -T deletes all output files generated during patching.

%configure This is not a keyword, but a standard macro command defined by rpm. It means to execute the configure configuration of the source code in the /usr/src/asianux/BUILD/%{name}-%{version} directory. Using standard writing, the parameters defined in /usr/lib/rpm/marcros will be referenced. . Another non-standard way of writing is to refer to the parameter customization in the source code, for example:

引用CFLAGS="$RPM_OPT_FLAGS" CXXFLAGS="$RPM_OPT_FLAGS" ./configure --prefix=%{_prefix}
Copy after login

%build Start building the package in /usr/src/asianux/BUILD/%{name}-%{ version} directory to perform make work

%install 开始把软件安装到虚拟的根目录中.在/usr/src/asianux/BUILD/%{name}-%{version}目录中进行make install的操作。这个很重要,因为如果这里的路径不对的话,则下面%file中寻找文件的时候就会失败。 常见内容有:

  • %makeinstall 这不是关键字,而是rpm定义的标准宏命令。也可以使用非标准写法:make DESTDIR=$RPM_BUILD_ROOT install或引用make prefix=$RPM_BUILD_ROOT install

  • 需要说明的是,这里的%install主要就是为了后面的%file服务的。所以,还可以使用常规的系统命令:引用install -d $RPM_BUILD_ROOT/和cp -a * $RPM_BUILD_ROOT/

%clean        清理临时文件

%pre           rpm安装前执行的脚本

%post        rpm安装后执行的脚本

%preun         rpm卸载前执行的脚本

%postun    rpm卸载后执行的脚本

%files 定义那些文件或目录会放入rpm中

%defattr (-,root,root)** 指定包装文件的属性,分别是(mode,owner,group),-表示默认值,对文本文件是0644,可执行文件是0755

%changelog     变更日志

实例:

%define zabbix_user zabbix                    #自定义宏,名字为zabbix_user值为zabbix,%{zabbix_user}引用
Name:    zabbix                                #软件包的名字,后面可用%{name}引用
Version:    3.0.3                            #软件的实际版本号,可使用%{version}引用
Release:    1%{?dist}                        #发布序列号,标明第几次打包    
Summary:    zabbix_agentd                    #软件包内容概要

Group:        zabbix                            #软件包分组
License:    GPL                                #授权许可方式
URL:        www.yang.com                    #软件的主页
Source0:    zabbix-3.0.3.tar.gz                #源代码包,可以有Source0,Source1等源

BuildRequires:        gcc, gcc-c++            #制作rpm包时,所依赖的基本库
Requires:    gcc, gcc-c++, chkconfig            #安装rpm包时,所依赖的软件包

%description                                #定义rpm包的描述信息
Zabbix agentd 3.0.3

%pre                                        #rpm包安装前执行的脚本
grep zabbix /etc/passwd > /dev/null
if [ $? != 0 ] 
then useradd zabbix -M -s /sbin/nologin
fi
[ -d /etc/zabbix   ]||rm -rf /etc/zabbix*


%post                                        #rpm包安装后执行的脚本
sed -i "/^ServerActive=/c\ServerActive=172.30.17.35" /etc/zabbix/etc/zabbix_agentd.conf
sed -i "/^Server=/c\Server=172.30.17.35" /etc/zabbix/etc/zabbix_agentd.conf
sed -i "/Timeout=3/c\Timeout=30" /etc/zabbix/etc/zabbix_agentd.conf
sed -i "/HostMetadata=/c\HostMetadata=PostgreSQL" /etc/zabbix/etc/zabbix_agentd.conf
sed -i "/^Hostname=/c\Hostname=PostgreSQL" /etc/zabbix/etc/zabbix_agentd.conf
echo "UnsafeUserParameters=1" >>/etc/zabbix/etc/zabbix_agentd.conf
echo "EnableRemoteCommands=1" >>/etc/zabbix/etc/zabbix_agentd.conf
echo "Include=/etc/zabbix/etc/zabbix_agentd.conf.d/*.conf" >>/etc/zabbix/etc/zabbix_agentd.conf
chkconfig zabbix_agentd on

%preun                                        #rpm卸载前执行的脚本
systemctl stop zabbix_agentd
%postun                                        #rpm卸载后执行的脚本
userdel  zabbix
rm -rf /etc/zabbix*
%prep                                        #这个宏开始
%setup -q                                    #解压并cd到相关目录


%build                                        #定义编译软件包时的操作
./configure --prefix=/etc/%{name}-%{version}   --enable-agent
make -j16 %{?_smp_mflags}

%install                                    #定义安装软件包,使用默认值即可
test -L %{buildroot}/etc/%{name} && rm -f %{buildroot}/etc/%{name}
install -d %{buildroot}/etc/profile.d
install -d %{buildroot}/etc/init.d
make install DESTDIR=%{buildroot}
echo 'export PATH=/etc/zabbix/bin:/etc/zabbix/sbin:$PATH' > %{buildroot}/etc/profile.d/%{name}.sh
ln -sf /etc/%{name}-%{version}             %{buildroot}/etc/%{name}
cp %{_buildrootdir}/postgresql.conf         %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/postgresql.conf
cp %{_buildrootdir}/tcp_connections.sh      %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/tcp_connections.sh
cp %{_buildrootdir}/iostat-collect.sh          %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-collect.sh 
cp %{_buildrootdir}/iostat-parse.sh          %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-parse.sh
cp %{_buildrootdir}/iostat-zabbix.conf      %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-zabbix.conf 
cp %{_buildrootdir}/zabbix_agentd         %{buildroot}/etc/init.d/zabbix_agentd

%files                                        #定义rpm包安装时创建的相关目录及文件。在该选项中%defattr (-,root,root)一定要注意。它是指定安装文件的属性,分别是(mode,owner,group),-表示默认值,对文本文件是0644,可执行文件是0755。
/etc/%{name}
/etc/%{name}-%{version}/*
/etc/init.d/zabbix_agentd
/etc/profile.d/%{name}.sh

%changelog                                    #主要用于软件的变更日志。该选项可有可无
%clean 
rm -rf %{buildroot}                         #清理临时文件
Copy after login

RPM包制作拓展

如果想为zabbix增加启动控制脚本或一些其他的配置文件,可以将其放在SOURCE下,然后复制过去

  • 将启动脚本放在SOURCE目录

      [root@yang ~/rpmbuild/SOURCES]# ll
      total 15116
      -rwxr-xr-x 1 root root      362 Aug  1 12:03 hostmonitor.conf
      -rwxr-xr-x 1 root root      505 Aug  1 12:03 iostat-collect.sh
      -rwxr-xr-x 1 root root      953 Aug  1 12:03 iostat-parse.sh
      -rw-r--r-- 1 root root      772 Aug  1 12:03 iostat-zabbix.conf
      -rwxr-xr-x 1 root root      813 Aug  1 12:03 nginx_monitor.sh
      -rw-r--r-- 1 root root    14868 Aug  1 12:03 postgresql.conf
      -rw-r--r-- 1 root root       77 Aug  1 12:03 process.discovery
      -rw-r--r-- 1 root root      552 Aug  1 12:03 redis_check.conf
      -rw-r--r-- 1 root root      356 Aug  1 12:03 redis_cluster_check.py
      -rw-r--r-- 1 root root      363 Aug  1 12:03 redis_multiport_check.py
      -rwxr-xr-x 1 root root      783 Aug  1 12:03 tcp_connections.sh
      -rw-r--r-- 1 root root      852 Aug  1 12:03 userparameter_nginx.conf
      -rw-r--r-- 1 root root      172 Aug  1 12:03 userparameter_process.conf
      -rw-r--r-- 1 root root 15407273 Jul 20 10:53 zabbix-3.0.3.tar.gz
      -rwxr-xr-x 1 root root     2182 Aug  1 12:03 zabbix_agentd
Copy after login
  • 编辑 SPEC文件

    • Source0下增加如下:

        Source0:        zabbix-3.0.3.tar.gz
        Source1:        zabbix_agentd
        Source2:        nginx_monitor.sh
        Source3:        userparameter_nginx.conf
        Source4:        hostmonitor.conf
        Source5:        process.discovery
        Source6:        userparameter_process.conf
        Source7:        redis_check.conf
        Source8:        redis_cluster_check.py
        Source9:        redis_multiport_check.py
        Source10:       tcp_connections.sh
        Source11:       iostat-collect.sh
        Source12:       iostat-parse.sh
        Source13:       iostat-zabbix.conf
Copy after login
  • 安装区域增加如下行:

        make install DESTDIR=%{buildroot}
        install -p -D -m 0755 %{SOURCE1}        %{buildroot}/etc/init.d/zabbix_agentd
        install -p -D         %{SOURCE2}        %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/nginx_monitor.sh
        install -p -D         %{SOURCE3}        %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/userparameter_nginx.conf
        install -p -D         %{SOURCE4}        %{buildroot}/etc/nginx/conf.d/hostmonitor.conf
        install -p -D         %{SOURCE5}        %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/process.discovery
        install -p -D         %{SOURCE6}        %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/userparameter_process.conf
        install -p -D         %{SOURCE7}        %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/redis_check.conf
        install -p -D         %{SOURCE8}        %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/redis_cluster_check.py
        install -p -D         %{SOURCE9}        %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/redis_multiport_check.py
        install -p -D         %{SOURCE10}       %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/tcp_connections.sh
        install -p -D         %{SOURCE11}       %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-collect.sh
        install -p -D         %{SOURCE12}       %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-parse.sh
        install -p -D         %{SOURCE13}       %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-zabbix.conf
Copy after login
  • %file区域增加如下行:

        %files
        %defattr (-,root,root,0755)
        /etc/%{name}
        /etc/%{name}-%{version}/*
        /etc/profile.d/%{name}.sh
        /etc/nginx/conf.d/hostmonitor.conf
        %attr(0755,root,root) /etc/rc.d/init.d/zabbix_agentd
Copy after login

以下为完整的SPEC文件:

Name:    zabbix    
Version:    3.0.3        
Release:    1%{?dist}
Summary:    zabbix_agentd

Group:        zabbix
License:    GPL
URL:        www.yang.com
Source0:    zabbix-3.0.3.tar.gz
Source1:    zabbix_agentd
Source2:    nginx_monitor.sh
Source3:    userparameter_nginx.conf
Source4:    hostmonitor.conf
Source5:    process.discovery
Source6:    userparameter_process.conf
Source7:    redis_check.conf
Source8:    redis_cluster_check.py
Source9:    redis_multiport_check.py
Source10:    tcp_connections.sh
Source11:    iostat-collect.sh
Source12:    iostat-parse.sh
Source13:    iostat-zabbix.conf

BuildRequires:        gcc, gcc-c++
Requires:    gcc, gcc-c++, chkconfig

%description
Zabbix agentd 3.0.3

%pre
grep zabbix /etc/passwd > /dev/null
if [ $? != 0 ] 
then useradd zabbix -M -s /sbin/nologin
fi
[ -d /etc/zabbix   ]||rm -rf /etc/zabbix
[ -d /etc/zabbix   ]||rm -rf /etc/zabbix-3.0.3


%post
sed -i "/^ServerActive=/c\ServerActive=172.30.17." /etc/zabbix/etc/zabbix_agentd.conf
sed -i "/^Server=/c\Server=172.30.17." /etc/zabbix/etc/zabbix_agentd.conf
sed -i "/Timeout=3/c\Timeout=30" /etc/zabbix/etc/zabbix_agentd.conf
sed -i "/HostMetadata=/c\HostMetadata=OPS-TMP" /etc/zabbix/etc/zabbix_agentd.conf
sed -i "/^Hostname=/c\Hostname=OPS-TMP" /etc/zabbix/etc/zabbix_agentd.conf
echo "UnsafeUserParameters=1" >>/etc/zabbix/etc/zabbix_agentd.conf
echo "EnableRemoteCommands=1" >>/etc/zabbix/etc/zabbix_agentd.conf
echo "Include=/etc/zabbix/etc/zabbix_agentd.conf.d/*.conf" >>/etc/zabbix/etc/zabbix_agentd.conf
chkconfig zabbix_agentd on

%preun
systemctl stop zabbix_agentd
%postun
userdel  zabbix
rm -rf /etc/zabbix*
%prep
%setup -q


%build
./configure --prefix=/etc/%{name}-%{version}   --enable-agent
make -j16 %{?_smp_mflags}

%install
test -L %{buildroot}/etc/%{name} && rm -f %{buildroot}/etc/%{name}
install -d %{buildroot}/etc/profile.d
make install DESTDIR=%{buildroot}
install -p -D -m 0755 %{SOURCE1}     %{buildroot}/etc/init.d/zabbix_agentd
install -p -D         %{SOURCE2}     %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/nginx_monitor.sh
install -p -D         %{SOURCE3}     %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/userparameter_nginx.conf
install -p -D         %{SOURCE4}     %{buildroot}/etc/nginx/conf.d/hostmonitor.conf
install -p -D         %{SOURCE5}     %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/process.discovery
install -p -D         %{SOURCE6}     %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/userparameter_process.conf
install -p -D         %{SOURCE7}     %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/redis_check.conf
install -p -D         %{SOURCE8}     %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/redis_cluster_check.py
install -p -D         %{SOURCE9}     %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/redis_multiport_check.py
install -p -D         %{SOURCE10}     %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/tcp_connections.sh
install -p -D         %{SOURCE11}    %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-collect.sh
install -p -D         %{SOURCE12}    %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-parse.sh
install -p -D            %{SOURCE13}       %{buildroot}/etc/%{name}-%{version}/etc/zabbix_agentd.conf.d/iostat-zabbix.conf

echo 'export PATH=/etc/zabbix/bin:/etc/zabbix/sbin:$PATH' > %{buildroot}/etc/profile.d/%{name}.sh
ln -sf /etc/%{name}-%{version}             %{buildroot}/etc/%{name}

%files
%defattr (-,root,root,0755)
/etc/%{name}
/etc/%{name}-%{version}/*
/etc/profile.d/%{name}.sh
/etc/nginx/conf.d/hostmonitor.conf
%attr(0755,root,root) /etc/rc.d/init.d/zabbix_agentd
%changelog
%clean 
rm -rf %{buildroot}
Copy after login

相关推荐:

RPM 进行安装、卸载及管理的操作实例

Linux命令之rpm安装命令的实例代码详解

关于Linux中安装rpm包时报错的解决办法 详解

The above is the detailed content of Sharing of RPM packaging process under Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template