Linux RPM packaging process

  • 2020-06-23 02:40:01
  • OfStack

Preparation before starting

Install the rpmbuild package

yum -y install rpm-build

Generate related directories

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


[root@yang data]# rpmbuild zabbix_agentd_ops.spec 
error: File /root/rpmbuild/SOURCES/zabbix-3.0.3.tar.gz: No such file or directory
 There was an error. Don't worry about it. You can see it rpmbuild The directory has been created 
[root@yang ~]# tree rpmbuild/
rpmbuild/
 ├ ─ ─  BUILD
 ├ ─ ─  BUILDROOT
 ├ ─ ─  RPMS
 ├ ─ ─  SOURCES
 ├ ─ ─  SPECS
 └ ─ ─  SRPMS

Write the SPEC file

SPEC keyword

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

Summary: A summary of the contents of the package

Version: the actual version number of the software, for example: 1.0.1, etc., followed by %{version} reference

Release: Publish serial Numbers, such as: 1linuxing, indicating the number of packages, followed by %{release} references

Group: Software grouping. Standard grouping is recommended

License: Software licensing, usually GPL

Source: Source code package, you can use multiple sources such as Source1, Source2, followed by %{source1}, %{source2} reference

BuildRoot: This is the "virtual directory" used at installation or compilation time. For multi-user environments, 1 is generally defined as: %{tmppath}/{name}-%{release}-root or %{tmppath}/%{name}-%{version}- buildroot}-% n}. This parameter is important because during the generation of rpm, the software is installed in the path described above when make install is executed. Also rely on virtual directory for root. You can use the $RPM_BUILD_ROOT reference later.

URL: Home page of the software

Vendor: Information from a publisher or packaging organization, such as RedFlag Co,Ltd

Disstribution: Distribution logo

Patch: Patch source code, can use Patch1, Patch2 and other identified multiple patches, using %patch0 or %{patch0} reference

Prefix: %{_prefix} this is mainly to solve the future installation of rpm packages, do not have to install the software into rpm packaged directory. Thus, the identity must be defined here and referenced when writing the %install script in order to be able to reposition the rpm installation

The reason for Prefix: %{sysconfdir} is the same as above, but since %{prefix} refers to /usr, other files, such as configuration files under /etc, need to be identified with %{_sysconfdir}

Build Arch: refers to the compilation of the target processor architecture, noarch logo is not specified, but usually are based on the contents of the/usr lib/rpm/marcros to default values

Requires: The name of the package on which the rpm package depends > = or < = means greater than or less than a 1 specific version, for example: ES124en-ES125en > = 1.0.20 zlib does" > = "both sides of the sign should be separated by Spaces, and different software names should be separated by Spaces, such as PreReq, Requires(pre), Requires(post), Requires(preun), Requires(postun), BuildRequires, etc., are specified for different stages of dependency

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

Packager: The packager's message

Details of the description software

SPEC script body

%prep pre-processing script

% % setup n % {name} - {version} * * put the source code package decompression and is usually/usr/src/into/usr asianux/SOURCES bag/src/asianux BUILD / % % {name} - {version}. 1 general use % setup-ES172en is ok, but there are two cases: 1 is to compile multiple source packages at the same time, 2 is the source tar package name and unzipped directory not 1, then need to use the -ES174en parameter to specify 1.

Patches are usually patched from 1 in the source tar.gz package, or in the SOURCES directory. 1 General parameters are:

% patch-p1 USES the Patch patch defined earlier. -p1 is layer 1 of patch that is ignored % Patch2-ES188en1-ES189en xxx.patch is patched as specified. -ES192en is used to generate backup files

supplement

%setup opens the package without any options. Unzip the package in the newdir directory. %setup -c Generates directories before decompressing. % setup-ES203en num unzip the num source file. % ES207en-ES208en does not use default's decompression operation. % ES210en-ES211en-ES212en 0 unzip the 0th source file. % ES213en-ES214en-ES215en newdir specifies the directory name newdir where the rpm suite is generated. %patch The simplest way to patch, automatically specify patch level. %patch 0 USES the 0th patch file, equivalent to %patch ? p 0. % ES225en-ES226en does not display information when patched. % patch-ES228en removes all output files that were patched.

The %configure is not a keyword, but a standard macro command defined by rpm. Mean to perform source code configure configuration in/usr/src/asianux BUILD / % % {name} - {version} directory, use the standard wording, cite/usr lib rpm/marcros defined parameters. Another non-standard way is to refer to the source code for custom parameters, such as:


 reference CFLAGS="$RPM_OPT_FLAGS" CXXFLAGS="$RPM_OPT_FLAGS" ./configure --prefix=%{_prefix}

% build begin to build the package, in/usr/src/asianux BUILD / % % {name} - {version} make working directory

% install began to install the software in the virtual root directory. In the/usr src/asianux BUILD / % % {name} - {version} directory for make install operation. This is important because if the path here is wrong, you will fail to find the file in %file below. Common contents include:

This is not a keyword, but a standard macro command defined by rpm. You can also use non-standard writing: make DESTDIR=$RPM_BUILD_ROOT install or quote make prefix=$RPM_BUILD_ROOT install It should be noted that %install here is mainly for the service of the following %file. So you can also use the regular system command: install-ES284en $RPM_BUILD_ROOT/ and ES288en-ES289en * $RPM_BUILD_ROOT/

Clean up temporary files

%pre rpm Scripts executed before installation

%post rpm Scripts executed after installation

%preun rpm Script executed before uninstalling

%postun rpm Script executed after uninstalling

%files defines which files or directories go into rpm

%defattr (-,root,root)** Specifies the properties of the wrapper file, which are (mode,owner,group), - for the default, 0644 for the text file and 0755 for the executable

changelog change log

Example:


%define zabbix_user zabbix          # Custom macro named zabbix_user A value of zabbix,%{zabbix_user} reference 
Name:  zabbix                # The name of the package, available later %{name} reference 
Version:  3.0.3              # The actual version number of the software can be used %{version} reference 
Release:  1%{?dist}            # Issue a serial number indicating the number of packages   
Summary:  zabbix_agentd          # Package content Overview 

Group:    zabbix              # Package grouping 
License:  GPL                # Mode of Authorization 
URL:    www.yang.com          # Home page of the software 
Source0:  zabbix-3.0.3.tar.gz        # Source code package, can have Source0,Source1 Such as the source 

BuildRequires:    gcc, gcc-c++      # making rpm The base library on which the package depends 
Requires:  gcc, gcc-c++, chkconfig      # The installation rpm When a package is dependent on a package 

%description                # define rpm The description information of the package 
Zabbix agentd 3.0.3

%pre                    #rpm Scripts executed before package installation 
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 The script executed after the package is installed 
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 Scripts executed before uninstalling 
systemctl stop zabbix_agentd
%postun                    #rpm The script executed after uninstalling 
userdel zabbix
rm -rf /etc/zabbix*
%prep                    # This macro starts 
%setup -q                  # Unzip and cd Go to the relevant directory 


%build                    # Defines the actions to take when compiling a software package 
./configure --prefix=/etc/%{name}-%{version}  --enable-agent
make -j16 %{?_smp_mflags}

%install                  # Define the installation package, using the default values 
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                    # define rpm The associated directories and files created when the package was installed. In that option %defattr (-,root,root)1 Be careful. It is the property that specifies the installation file, respectively (mode,owner,group) . - Represents the default value for a text file 0644 , the executable is 0755 . 
/etc/%{name}
/etc/%{name}-%{version}/*
/etc/init.d/zabbix_agentd
/etc/profile.d/%{name}.sh

%changelog                  # Mainly used for software change logs. This option is optional 
%clean 
rm -rf %{buildroot}             # Clean up temporary files 

RPM bag making expansion

If you want to add a boot control script or some other configuration file to zabbix, you can put it under SOURCE and copy it over

Place the startup script in the SOURCE directory


   [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

Edit the SPEC file

The following is added under 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

Add the following lines to the installation area:


    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

The %file area is increased by the following lines:


%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

The following is the complete SPEC file:


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}


Related articles: