Preparations before the start of the RPM packaging production process under Linux

Preparation before the start of the RPM packaging production process under Linux

Install the rpmbuild package

  • yum -y install rpm-build

Generate relevant directories

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

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

If there is an error, ignore it, you can see that the rpmbuild directory has been created

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

Write a SPEC file

SPEC keywords

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

Summary: A summary of the contents of the 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., indicating the number of packages, which can be referenced later using %{release}

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

License: Software authorization method, usually GPL

Source: Source code package, can bring multiple sources such as Source1, Source2, etc., and can also be referenced later by %{source1}, %{source2}

BuildRoot: This is the “virtual directory” used when installing or compiling. Considering the multi-user environment, it is generally defined as: %{tmppath}/{name}-%{version}-%{release}-root or %{tmppath }/%{name}-%{version}-%{release}-buildroot-%%__id_u} -n}. This parameter is very important, because in the process of generating rpm, the software will be installed to In the above path, when packaging, it also relies on the “virtual directory” as the “root directory” for operations. It can be referenced later using $RPM_BUILD_ROOT.

URL: Home page of the software

Vendor: Information about the publisher or packaging organization, such as RedFlag Co,Ltd

Distribution: Release ID

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 situation that when installing the rpm package in the future, it is not necessary to install the software to the directory packaged in the rpm. In this way, the identifier must be defined here and referenced when writing the %install script, in order to realize the function of re-specifying the location during rpm installation

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

Build Arch: Refers to the compiled target processor architecture, the noarch flag is not specified, but usually the default value is the content in /usr/lib/rpm/marcros

Requires: The name of the package that the rpm package depends on, you can use >= or <= to indicate that it is greater than or less than a specific version, for example: libpng-devel >= 1.0.20 zlib ※ Both sides of “>=” need to be separated by spaces Open, and different software names are also separated by spaces. For example, PreReq, Requires(pre), Requires(post), Requires(preun), Requires(postun), BuildRequires, etc. are all for different stages of dependency specification

Provides: Indicate some specific functions of this software, so that other rpm can recognize

Packager: information about the packager

?scription A detailed description of the software

SPEC script body

%prep preprocessing script

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

%patch Patches are usually included in the source code tar.gz package, or placed in the SOURCES directory. The general parameters are:

  • %patch -p1 Use the Patch patch defined earlier, -p1 is the first level target of the patch
  • %Patch2 -p1 -b xxx.patch to apply the specified patch, -b means to generate a backup file

Replenish

  • %setup opens the package without any options.
  • %setup -n newdir unzip the software package in the newdir directory.
  • %setup -c creates a directory before unpacking.
  • %setup -b num decompresses the numth source file.
  • %setup -T does not use the default decompression operation.
  • %setup -T -b 0 will decompress the 0th source code file.
  • %setup -c -n newdir specifies the directory name newdir, and generates rpm packages in this directory.
  • %patch The easiest way to patch, automatically specify the patch level.
  • %patch 0 Use the 0th patch file, equivalent to %patch ?p 0.
  • %patch -s does not display information when patching.
  • %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, which is carried out in the /usr/src/asianux/BUILD/%{name}-%{version} directory, using standard writing, and will refer to the parameters defined in /usr/lib/rpm/marcros . Another non-standard way of writing is to refer to the parameter customization in the source code, for example:

quote CFLAGS="$RPM_OPT_FLAGS" CXXFLAGS="$RPM_OPT_FLAGS" ./configure --prefix=%{<!-- -->_prefix}

%build starts to build the package, and performs make work in the /usr/src/asianux/BUILD/%{name}-%{version} directory

%install Start to install the software into the virtual root directory. Perform make install in the /usr/src/asianux/BUILD/%{name}-%{version} directory. This is very important, because if the path here is wrong, it will fail when looking for the file in %file below. Common content is:

  • %makeinstall This is not a keyword, but a standard macro command defined by rpm. You can also use non-standard writing: make DESTDIR=

    R

    P

    m

    B

    u

    I

    L

    D.

    R

    o

    o

    T

    i

    no

    the s

    t

    a

    l

    l

    or quote

    m

    a

    k

    e

    p

    r

    e

    f

    i

    x

    =

    RPM_BUILD_ROOT install or quote make prefix=

    RPMB?UILDR?OOTinstall or quote makeprefix=RPM_BUILD_ROOT install

  • It should be noted that the %install here is mainly for the subsequent %file service. Therefore, you can also use regular system commands: quote install -d $RPM_BUILD_ROOT/ and cp -a * $RPM_BUILD_ROOT/

%clean clean up temporary files

%pre script executed before rpm installation

%post script executed after rpm installation

%preun script executed before rpm uninstallation

Script executed after %postun rpm uninstallation

%files defines which files or directories will be put into rpm

?fattr (-, root, root)** specifies the attributes of the package file, which are (mode, owner, group), – means the default value, 0644 for text files, and 0755 for executable files

%changelog change log

Example:

?fine zabbix_user zabbix #Custom macro, the name is zabbix_user and the value is zabbix, %{zabbix_user} reference
Name: zabbix #The name of the software package, which can be referenced later by %{name}
Version: 3.0.3 #The actual version number of the software, which can be referenced by %{version}
Release: 1%{<!-- -->?dist} #Release the serial number, indicating the number of packages
Summary: zabbix_agentd #Summary of package contents

Group: zabbix #package grouping
License: GPL #Licensing method
URL: www.yang.com #The homepage of the software
Source0: zabbix-3.0.3.tar.gz #Source code package, you can have Source0, Source1 and other sources

BuildRequires: gcc, gcc-c ++ #Basic libraries that depend on when making rpm packages
Requires: gcc, gcc-c ++ , chkconfig #When installing the rpm package, the dependent software package

 ?scription #Define the description information of the rpm package
Zabbix agentd 3.0.3

%pre #rpm package execution script before installation
grep zabbix /etc/passwd > /dev/null
if [ $? != 0 ]
then useradd zabbix -M -s /sbin/nologin
the fi
[ -d /etc/zabbix ]||rm -rf /etc/zabbix*


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


%build #Define the operation when compiling the package
./configure --prefix=/etc/%{<!-- -->name}-%{<!-- -->version} --enable-agent
make -j16 %{<!-- -->?_smp_mflags}

%install #Define the installation package, use the default value
test -L %{<!-- -->buildroot}/etc/%{<!-- -->name} & amp; & amp; 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/%{<!-- -->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 the relevant directories and files created when the rpm package is installed. In this option, you must pay attention to fattr (-, root, root). It is the attribute of the specified installation file, respectively (mode, owner, group), - means the default value, 0644 for text files, and 0755 for executable files.
/etc/%{<!-- -->name}
/etc/%{name}-%{version}/*
/etc/init.d/zabbix_agentd
/etc/profile.d/%{<!-- -->name}.sh

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

RPM package production extension

If you want to add a startup control script or some other configuration files to zabbix, you can put it under SOURCE and copy it in the past

  • Put 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 SPEC file
    • The addition under Source0 is as follows:
 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
  • Add the following lines to the %file area:
 %files
         ?fattr (-,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

 ?scription
Zabbix agentd 3.0.3

%pre
grep zabbix /etc/passwd > /dev/null
if [ $? != 0 ]
then useradd zabbix -M -s /sbin/nologin
the 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} & amp; & amp; 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/%{<!-- -->version} %{<!-- -->buildroot}/etc/%{<!-- --> ->name}

%files
 ?fattr (-,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}