🌐 🇵🇱 Polski · 🇬🇧 EN
A key skill for any administrator is the installation and configuration of software. First, you need to know how to create your own RPM package containing specific software. Initially, this may seem complicated, but it is only a matter of time before this process becomes very straightforward. RPM packages contain a compressed cpio archive with data, along with additional information about the contents. This additional information includes a list of software contained in the package and its dependencies. Dependencies are a list of packages and programs required for installation on the system to ensure the software functions correctly. An RPM package is built based on the manifest contained in a spec file. RPM loosely stands for Red Hat Package Manager, and despite the name containing "Red Hat," it is an open-source tool available to everyone. Its task is to install, update, and remove software. Information about installed software is also easily accessible from the database that RPM maintains. Thanks to this database, you can easily check if a given package is present in the system and what it contains.
CREATING A CUSTOM RPM PACKAGE
To create your own package, you need to follow a few simple steps:
- Create the directory structure
- Copy the source code into the structure
- Create the spec file
- Build the RPM package
The structure that must be created according to package building standards is as follows:
- BUILD Space intended for software compilation
- RPMS Contains software binaries
- SOURCES Holds the RPM source code
- SPEC Contains the spec file (one per RPM)
- SRPMS Contains RPM sources during package creation
Creating an RPM
Step 1 - Install required packages
# yum inastall -y rpm-build make
Step 2 - Verify package installation
# rpm -qa | grep rpm-build
rpm-build-4.8.0-19.el6.i686
# rpm -qa | grep make
make-3.81-19.el6.i686
Creating the directory structure is usually done in /usr/src/redhat, but it can be done elsewhere if preferred.
Step 3 - Create the required directory structure:
# mkdir -p /usr/src/redhat/{BUILD,RPMS,SOURCES,SPEC,SRPMS,pliczki}
The /pliczki directory serves as a temporary place where we create sample files that will make up the package content, and it is also a place for any additional components if needed. The SOURCES location will hold the archived files (tar.gz) containing the software source code from which the package will be built. Red Hat exams require building only single RPM packages, so we do not need full software sources here.
A faster way to create the directory structure for building your own RPMs is to use the command:
# rpmbuild nazwa_pliku
After this operation, the following directory structure will be created in the user's home directory:
BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
Step 4 - Create a location for the sample files to be included in the package
# cd /root/rpmbuild
# cd /root/rpmbuild
# mkdir pliczki
# touch plik{1,2,3,4,5,6,7,8,9}
Step 5 - Create an archive from the sample files
# cd /root/rpmbuild
# tar cf pliczki.tar.gz pliczki/
# mv pliczki.tar.gz SOURCES/
Step 6 - The final step before creating the package is to create a spec file (sample.spec). This file contains the instructions used to build the package. This file must be located in the SPEC directory. Below is an example of the file content used to create the sample package.
Name:pliczki
Version:1.0
Release:0
Summary:Paczka testowa pliczki
Group: Development/tools
License:GPL
Source0:%{name}.tar.gz
BuildRoot: /root/rpmbuild/BUILDROOT/
%description
paczka testowa pliczki
%prep
%setup -n pliczki
%install
mkdir -p "$RPM_BUILD_ROOT/opt/mojapaczka"
cp -R * "$RPM_BUILD_ROOT/opt/mojapaczka"
%clean
rm -rf $RPM_BUILD_ROOT
%files
/opt/mojapaczka/
%post
chown mariusz:mariusz -R /opt/mojapaczka
chmod 775 -R /opt/mojapaczka
%changelog
The code above certainly requires some explanation. The first main points are all the information required for the package, which was already discussed when explaining the package structure. The "Source" and "BuildRoot" parameters play an important role because they contain information about how the package is built. The source is the archive the package will use, and in this example, it will be the previously created archive named pliczki.tar.gz located in the SOURCE directory. BuildRoot is a parameter indicating the location where the package will actually be built.
The rest of the spec file consists of options starting with the % character:
%description - a section allowing you to set the package description.
%prep and %setup - Both options are responsible for moving the SOURCE to BUILD and unpacking the archive files. The -n switch specifies the name of the directory that will be created after decompression.
%install - an option usually responsible for the locations where software is compiled and installed on the current system. After installation, files should be collected in such a way that they can be installed via the package found in the %files section. Here, we include any files that should be in the package. Every directory and subdirectory with files found here will also be included in the package. Since we are creating a package with only a few files, we can specify the path (opt/mojapaczka) in the %install section.
The last two sections are necessary, although not used in this case, and are not preceded by the % sign.
%clean - an option responsible for cleaning up files after the package is created. In this example, $RPM_BUILD_ROOT will be removed, which is the only location requiring cleanup.
Using %post allows for executing additional scripts or commands after the installation is complete.
Permissions for user01 were excluded because such a user does not necessarily exist on every system. Now we can proceed to build the package.
Step 7 - Create the package using the rpmbuild command
Installation progress:
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.QHAjaB
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd /root/rpmbuild/BUILD
+ rm -rf pliczki
+ /bin/tar -xvvf /root/rpmbuild/SOURCES/pliczki.tar.gz
drwxr-xr-x root/root 0 2013-01-02 09:21 pliczki/
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik3
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik8
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik9
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik1
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik4
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik7
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik5
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik6
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik2
+ cd pliczki
+ /bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.nxUxnW
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd pliczki
+ mkdir -p /root/rpmbuild/BUILDROOT/pliczki-1.0-0.x86_64/opt/mojapaczka
+ cp -R plik1 plik2 plik3 plik4 plik5 plik6 plik7 plik8 plik9 /root/rpmbuild/BUILDROOT/pliczki-1.0-0.x86_64/opt/mojapaczka
+ /usr/lib/rpm/brp-compress
+ /usr/lib/rpm/brp-strip
+ /usr/lib/rpm/brp-strip-static-archive
+ /usr/lib/rpm/brp-strip-comment-note
Processing files: pliczki-1.0-0.x86_64
Requires(interp): /bin/sh
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Requires(post): /bin/sh
Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/pliczki-1.0-0.x86_64
warning: Could not canonicalize hostname: station1.example.com
Wrote: /root/rpmbuild/SRPMS/pliczki-1.0-0.src.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/pliczki-1.0-0.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.hBs6nb
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd pliczki
+ rm -rf /root/rpmbuild/BUILDROOT/pliczki-1.0-0.x86_64
+ exit 0
VERIFICATION
During package building, you can monitor what is happening in the directory set as BUILDROOT in a separate session.
# rpmbuild -ba /root/rpmbuild/SPEC/pliczki.spec
Installation progress:
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.QHAjaB
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd /root/rpmbuild/BUILD
+ rm -rf pliczki
+ /bin/tar -xvvf /root/rpmbuild/SOURCES/pliczki.tar.gz
drwxr-xr-x root/root 0 2013-01-02 09:21 pliczki/
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik3
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik8
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik9
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik1
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik4
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik7
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik5
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik6
-rw-r--r-- root/root 0 2013-01-02 09:21 pliczki/plik2
+ cd pliczki
+ /bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.nxUxnW
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd pliczki
+ mkdir -p /root/rpmbuild/BUILDROOT/pliczki-1.0-0.x86_64/opt/mojapaczka
+ cp -R plik1 plik2 plik3 plik4 plik5 plik6 plik7 plik8 plik9 /root/rpmbuild/BUILDROOT/pliczki-1.0-0.x86_64/opt/mojapaczka
+ /usr/lib/rpm/brp-compress
+ /usr/lib/rpm/brp-strip
+ /usr/lib/rpm/brp-strip-static-archive
+ /usr/lib/rpm/brp-strip-comment-note
Processing files: pliczki-1.0-0.x86_64
Requires(interp): /bin/sh
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Requires(post): /bin/sh
Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/pliczki-1.0-0.x86_64
warning: Could not canonicalize hostname: station1.example.com
Wrote: /root/rpmbuild/SRPMS/pliczki-1.0-0.src.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/pliczki-1.0-0.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.hBs6nb
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd pliczki
+ rm -rf /root/rpmbuild/BUILDROOT/pliczki-1.0-0.x86_64
+ exit 0
VERIFICATION
During package building, you can monitor what is happening in the directory set as BUILDROOT in a separate session.
# watch -d -n 1 ls /root/rpmbuild/BUILDROOT/
You will then see the moment the package is created.
After the package is created, we can install it on the system:
# rpm -ivh /root/rpmbuild/RPMS/x86_64/pliczki-1.0-0.x86_64.rpm
After installation, the files contained in the package will appear in the directory we specified in the %install section.
# cd /opt/mojapaczka
# ll
-rwxrwxr-x. 1 mariusz mariusz 0 Jan 2 10:11 plik1
-rwxrwxr-x. 1 mariusz mariusz 0 Jan 2 10:11 plik2
-rwxrwxr-x. 1 mariusz mariusz 0 Jan 2 10:11 plik3
-rwxrwxr-x. 1 mariusz mariusz 0 Jan 2 10:11 plik4
-rwxrwxr-x. 1 mariusz mariusz 0 Jan 2 10:11 plik5
-rwxrwxr-x. 1 mariusz mariusz 0 Jan 2 10:11 plik6
-rwxrwxr-x. 1 mariusz mariusz 0 Jan 2 10:11 plik7
-rwxrwxr-x. 1 mariusz mariusz 0 Jan 2 10:11 plik8
-rwxrwxr-x. 1 mariusz mariusz 0 Jan 2 10:11 plik9
To uninstall the package, we issue the command:
# rpm -qa | grep pliczki - search for the installed package
# rpm -e pliczki-1.0-0.x86_64 - uninstall the package (the /opt/mojapaczka directory will be removed)
USEFUL LINKS

Comments