Building RPM packages - Part 3

Sat, 20 Jan. 2024     Thomas Bendler     ~ 5 min to read

As promised in the first two parts, I will close this blog series with a more sophisticated and complex example of how to build an RPM. In this part I will showcase the RPM I build to deploy the SAP router packages on several machines.

%define debug_package %{nil}

Summary:           SAP router
Name:              sap-router
Version:           7.53.1110
Release:           0%{?dist}
License:           SAP
Group:             Applications/Productivity
Source:            %{name}-%{version}.tar.gz
URL:               %{url}
Packager:          %{packager}
Vendor:            %{vendor}
BuildArch:         x86_64
BuildRoot:         %{_tmppath}/%{name}-buildroot
Requires:          sap-common >= 1.1
Requires(pre):     /usr/sbin/useradd, /usr/sbin/groupadd, /usr/bin/getent
Requires(preun):   /sbin/service, /sbin/chkconfig, /usr/sbin/useradd, /usr/sbin/groupadd, /usr/bin/getent
Requires(post):    /sbin/chkconfig
Requires(postun):  /sbin/service, /usr/sbin/userdel

%description
This package install the Netweaver SAP router

The header section doesn’t much differ from the header section described in the previous post. But it introduces a few new keywords. This is for example the source key. Here you define the source tarball used to build the package. There could be two flavours, a tarball containing the code sources as well as a tarball containing the binaries. Ok, there could be a third with a mixture of both, but let’s not overcomplicate the topic. The header now also specifies the requirements for different sections. For example, requires(pre) specifies the operating system commands that are needed to execute the pre-section.

%pre
/usr/bin/getent group r99adm || /usr/sbin/groupadd -r -g 800 r99adm
/usr/bin/getent passwd r99adm || /usr/sbin/useradd -r -u 800 -g r99adm -d /opt/sap/R99/work -s /bin/bash -c "SAP router service user" r99adm

In the pre-section, the user and group to operate the SAP router are created. The user’s home directory is set to the working directory of the service.

%preun
if [ $1 -eq 0 ] ; then
  /sbin/service sap-router stop >/dev/null 2>&1
  /sbin/chkconfig --del sap-router
fi
if [ $1 -ge 1 ] ; then
  /usr/bin/getent group r99adm || /usr/sbin/groupadd -r -g 800 r99adm
  /usr/bin/getent passwd r99adm || /usr/sbin/useradd -r -u 800 -g r99adm -d /opt/sap/R99/work -s /bin/bash -c "SAP router service user" r99adm
fi

The preun-section does the same thing as the pre-section but the other way around. The user and the service definition will be deleted if the package is removed.

%prep

%setup -q

%build

The next three sections are defined but not explicitly configured. Build, set up and the like will use the standard behaviour.

%install
rm -rf $RPM_BUILD_ROOT
install -m 0755 -d $RPM_BUILD_ROOT/opt/sap/R99
install -m 0755 -d $RPM_BUILD_ROOT/opt/sap/R99/exe
install -m 0755 -d $RPM_BUILD_ROOT/opt/sap/R99/profile
install -m 0755 -d $RPM_BUILD_ROOT/opt/sap/R99/lib
install -m 0755 -d $RPM_BUILD_ROOT/opt/sap/R99/lib/fips
install -m 0755 -d $RPM_BUILD_ROOT/opt/sap/R99/log
install -m 0755 -d $RPM_BUILD_ROOT/opt/sap/R99/sec
install -m 0755 -d $RPM_BUILD_ROOT/opt/sap/R99/work
install -m 0755 -d $RPM_BUILD_ROOT/etc/sysconfig
install -m 0755 -d $RPM_BUILD_ROOT/etc/systemd/system
install -m 0755 exe/sapgenpse $RPM_BUILD_ROOT/opt/sap/R99/exe/sapgenpse
install -m 0644 exe/patches.mf $RPM_BUILD_ROOT/opt/sap/R99/exe/patches.mf
install -m 0755 exe/niping $RPM_BUILD_ROOT/opt/sap/R99/exe/niping
install -m 0755 exe/saprouter $RPM_BUILD_ROOT/opt/sap/R99/exe/saprouter
install -m 0644 lib/libsapcrypto.so $RPM_BUILD_ROOT/opt/sap/R99/lib/libsapcrypto.so
install -m 0644 lib/libslcryptokernel.so $RPM_BUILD_ROOT/opt/sap/R99/lib/libslcryptokernel.so
install -m 0644 lib/libslcryptokernel.so.sha256 $RPM_BUILD_ROOT/opt/sap/R99/lib/libslcryptokernel.so.sha256
install -m 0644 lib/sapcrypto.lst $RPM_BUILD_ROOT/opt/sap/R99/lib/sapcrypto.lst
install -m 0644 lib/sapcrypto.mf $RPM_BUILD_ROOT/opt/sap/R99/lib/sapcrypto.mf
install -m 0644 profile/saproutetab $RPM_BUILD_ROOT/opt/sap/R99/profile/saproutetab
install -m 0644 sec/ticket $RPM_BUILD_ROOT/opt/sap/R99/sec/ticket
install -m 0644 sysconfig/sap-router $RPM_BUILD_ROOT/etc/sysconfig/sap-router
install -m 0644 systemd/sap-router.service $RPM_BUILD_ROOT/etc/systemd/system/sap-router.service

%clean
rm -rf $RPM_BUILD_ROOT

In the install-section, the files created in the package build will be installed underneath the build root with the same file and directory structure as in the real system.

%post
echo " "
echo "SAP router directories and files installed!"
echo " "

Once the RPM package is installed in the system, the installation will print a success message onto the console.

%files
%defattr(-,root,root)
%dir /opt/sap/R99
%dir /opt/sap/R99/exe
%dir /opt/sap/R99/profile
%dir /opt/sap/R99/lib
%attr(755, r99adm, r99adm) %dir /opt/sap/R99/log
%attr(755, r99adm, r99adm) %dir /opt/sap/R99/sec
%attr(755, r99adm, r99adm) %dir /opt/sap/R99/work
%config(noreplace) /opt/sap/R99/profile/saproutetab
%config(noreplace) /opt/sap/R99/sec/ticket
%config(noreplace) /etc/sysconfig/sap-router
/opt/sap/R99/exe/sapgenpse
/opt/sap/R99/exe/patches.mf
/opt/sap/R99/exe/niping
/opt/sap/R99/exe/saprouter
/opt/sap/R99/lib/libsapcrypto.so
/opt/sap/R99/lib/libslcryptokernel.so
/opt/sap/R99/lib/libslcryptokernel.so.sha256
/opt/sap/R99/lib/sapcrypto.lst
/opt/sap/R99/lib/sapcrypto.mf
/etc/systemd/system/sap-router.service

The files-section ensures that the installed files have the proper access and usage rights. It also ensures the behaviour of the RPM when an update gets installed. So, which file will be overwritten and which files will stay untouched or, if the new file will be deployed with the ending .rpmnew.

%postun
if [ $1 -ge 1 ] ; then
  /usr/bin/systemctl restart sap-router >/dev/null 2>&1 || :
fi
if [ $1 -eq 0 ] ; then
  /usr/sbin/userdel r99adm
fi

Finally, the service and user will be deleted if the package will be removed.

%changelog
* Wed Mar 29 2023 Thomas Bendler <code@thbe.org> 7.53.1110-0
- Upgrade to saprouter 7.53 PN 1110
- Upgrade to common cryptolib 8.5.48

* Sat Jul 16 2022 Thomas Bendler <code@thbe.org> 7.53.1011-2
- Fix systemd support

* Thu Jul 04 2022 Thomas Bendler <code@thbe.org> 7.53.1011-1
- Add systemd support

* Thu Jun 30 2022 Thomas Bendler <code@thbe.org> 7.53.1011-0
- Upgrade to saprouter 7.53 PN 1011
- Upgrade to common cryptolib 8.5.44

* Thu Apr 16 2021 Thomas Bendler <code@thbe.org> 7.45.116-0
- Upgrade to saprouter 7.45 PN 116
- Upgrade to common cryptolib 8.4.49
- Removed sapcar - separate RPM

* Thu Jun 16 2016 Thomas Bendler <code@thbe.org> 7.45.34-0
- Upgrade to saprouter 7.45 PN 34
- Upgrade to sapcar 7.21 PN 617
- Upgrade to common cryptolib 8.4.49

* Mon Nov 22 2010 Thomas Bendler <code@thbe.org> 7.20-0
- Initial release

The last section shows the changelog of the RPM.

Note: This is only an introduction to RPM packaging. The full RPM packaging guide can be found here

Building RPM packages:



Share on: