How to create rpm package from source package in 7 steps

How to create rpm package from source package in 7 steps


This article is a simple translation of the article " 7 Steps to Build a RPM Package from Source on CentOS / RedHat ".

version: 0.1.0 2017-11-19

Sometimes you may need to obtain the source package of an open source application, but there may not be an RPM file that cannot be installed on the system through the application.

In this case, you can either compile the source code to install, or use the source code package to create an rpm package file to implement the function of installing the application.

There is also a situation where you want to create a custom RPM package that you developed yourself.

This guide explains how to improve the source code to create an rpm package.

In order to create an rpm package, you will need the source package (usually a zip file that also contains a SPEC file).

The SPEC file contains the following instructions: how to create an rpm package, the files included in the package, and the path to install.

The RPM will demonstrate the testing process during the creation process.

Execute system commands and macro commands defined in the prep block in the spec file.

Check the contents of the file list

Execute system commands and macro commands defined in the build block in the spec file. The macro commands in the file list are also executed in this step.

Create binary package file

Create source package file

Once RPM has performed the above steps, it will create binary package files and source package files.

Binary package files contain all source files with extra information for installing or uninstalling packages.

Usually all options for installing a package are platform-specific. A binary package file contains a complete application or library of functions compiled for a particular structure. The source package usually contains the original compressed tar file, spec file and the relevant patches when creating the binary package.

Let's use a tar file to see how to create a simple source and binary RPM package.

If you are relatively new to rpm packages, you should first understand how to install, upgrade and uninstall packages using rpm commands on CentOS/Redhat.

  1. Install the rpm-build package

In order to create a spec file based rpm file, we need to use the rpmbuild command.

The rpmbuild command is part of the rpm-build package. The installation process is as follows.

# yum install rpm-build

rpm-build depends on the following packages. If you haven't installed them yet, quickly install these packages automatically.

elfutils-libelf
rpm
rpm-libs
rpm-python
  1. RPM create directory

The rpm-build command automatically creates the following directory structure during the build phase.

# ls -lF /root/rpmbuild/
drwxr-xr-x. 2 root root 4096 Feb  4 12:21 BUILD/
drwxr-xr-x. 2 root root 4096 Feb  4 12:21 BUILDROOT/
drwxr-xr-x. 2 root root 4096 Feb  4 12:21 RPMS/
drwxr-xr-x. 2 root root 4096 Feb  4 12:21 SOURCES/
drwxr-xr-x. 2 root root 4096 Feb  4 12:21 SPECS/
drwxr-xr-x. 2 root root 4096 Feb  4 12:21 SRPMS/

Note: The above directory structure applies to both CentOS and Redhat. You can also use the /usr/local/redhat command, but you will want to change the topdir parameter during the build phase. If you are using SuSE Enterprise Linux, use the /usr/src/packages directory.

If you want to use your own command structure instead of /root/rpmbuild, you can use the following options:

使用-buildroot选择,并在rpmbuild阶段定义自定义的目录。

在rpmrc文件或rpmmacros文件中定义topdir参数
  1. Download the source tar file

Next, download the source tar file of the package you want to create and save it in the SOURCES directory.

In this example, I am using the source package of icecase open source software, which is a streaming multimedia server software. But the steps are the same when creating other rpm packages. You just need to download the source package of the software you want.

# cd /root/rpmbuild/SOURCES/

# wget http://downloads.xiph.org/releases/icecast/icecast-2.3.3.tar.gz

# ls -l
-rw-r--r--. 1 root root 1161774 Jun 11  2012 icecast-2.3.3.tar.gz
  1. Create spec file

In this step, we define the creation process by creating a spec file. This spec file usually contains the following 8 different blocks:

  • Preamble - The preamable block contains information about the created package and defines its dependencies. Usually it's lines that start with label information, followed by a colon, and then some descriptive information.

  • %prep - In this block, we will prepare the software for the creation process. During this process, any previous creations are removed, the source (.tar) file is expanded, and so on. A more critical thing is to understand that there are predefined macros available that can execute various shortcut options to build rpm packages. You are probably using this macro when you are trying to create a complex package. In the example below, I use a macro called %setup which removes the previous creation, unpacks the source file and changes the file's permissions. You can also do this with a sh script in the %prep section, but the %setup macro simplifies the process with a predefined sh script.

  • %description - The description block usually contains a description of the package

  • %build - This is the block that actually executes the build command. Usually a sh script.

  • %install - %install blocks are also executed as sh scripts, like %prep and %build blocks. The steps in the installation phase

  • %files – This block contains a list of files for this package. Files not in this block will not be available. The requirement is the full path, and in this block you can set the properties and permissions of the file.

  • %clean - This block is for cleaning other files that are not in the normal creation area of ​​the application. For example: if the application creates a temporary directory structure under the /tmp directory during creation, it will not be removed; but if it is defined in this block, the temporary directory will be removed after proper installation. remove.

Below is the icecast.spec file

# cat /root/rpmbuild/SPECS/icecast.spec
Name:           icecast
Version:        2.3.3
Release:        0
Summary:        Xiph Streaming media server that supports multiple formats.
Group:          Applications/Multimedia
License:        GPL
URL:            http://www.icecast.org/
Vendor:         Xiph.org Foundation [email protected]
Source:         http://downloads.us.xiph.org/releases/icecast/%{name}-%{version}.tar.gz
Prefix:         %{_prefix}
Packager: 	Karthik
BuildRoot:      %{_tmppath}/%{name}-root

%description
Icecast is a streaming media server which currently supports Ogg Vorbis
and MP3 audio streams. It can be used to create an Internet radio
station or a privately running jukebox and many things in between.
It is very versatile in that new formats can be added relatively
easily and supports open standards for commuincation and interaction.

%prep
%setup -q -n %{name}-%{version}

%build
CFLAGS="$RPM_OPT_FLAGS" ./configure --prefix=%{_prefix} --mandir=%{_mandir} --sysconfdir=/etc

make

%install
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT

make DESTDIR=$RPM_BUILD_ROOT install
rm -rf $RPM_BUILD_ROOT%{_datadir}/doc/%{name}

%clean
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root)
%doc README AUTHORS COPYING NEWS TODO ChangeLog
%doc doc/*.html
%doc doc/*.jpg
%doc doc/*.css
%config(noreplace) /etc/%{name}.xml
%{_bindir}/icecast
%{_prefix}/share/icecast/*

%changelog

In this file, under % prep section you may noticed the macro “%setup -q -n %{name}-%{version}”. This macro executes the following command in the background.

cd /usr/src/redhat/BUILD
rm -rf icecast
gzip -dc /usr/src/redhat/SOURCES/icecast-2.3.3.tar.gz | tar -xvvf -
if [ $? -ne 0 ]; then
  exit $?
fi
cd icecast
cd /usr/src/redhat/BUILD/icecast
chown -R root.root .
chmod -R a+rX,g-w,o-w .

In the %build section you will see CFLAGS with configuration options, which define the options and prefix options that can be used during RPM installation, the mandatory directory is used for install and the sysconfig directory under which system files need to be copied.

On this line you'll see the make utility, which decides the list of files that need to be compiled, and compiles them appropriately.

In the %install section, the lines below %install of "make install" are used to compile the binaries from the previous steps, and install or copy them to the appropriate location so they can be accessed.

  1. Use the rpmbuild command to create rpm files

Once the SPEC file is ready, you can start building the rpm using the rpm -b command. The b option is used to execute all stages of the build process. If you see any errors at this stage, then you need to fix it before trying again. The error is usually a library dependency, which you can download and install when needed.

# cd /root/rpmbuild/SPECS

# rpmbuild -ba icecast.spec
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.Kohe4t
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd /root/rpmbuild/BUILD
+ rm -rf icecast-2.3.3
+ /usr/bin/gzip -dc /root/rpmbuild/SOURCES/icecast-2.3.3.tar.gz
+ /bin/tar -xf -
+ STATUS=0
+ '[' 0 -ne 0 ']'
+ cd icecast-2.3.3
+ /bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.ynm7H7
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd icecast-2.3.3
+ CFLAGS='-O2 -g'
+ ./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking for gcc... gcc
..
..
..
Wrote: /root/rpmbuild/SRPMS/icecast-2.3.3-0.src.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/icecast-2.3.3-0.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.dzahrv
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd icecast-2.3.3
+ '[' /root/rpmbuild/BUILDROOT/icecast-2.3.3-0.x86_64 '!=' / ']'
+ rm -rf /root/rpmbuild/BUILDROOT/icecast-2.3.3-0.x86_64
+ exit 0

Note: If you are using SuSE linux, if the rpmbuild tool is not available, try using "rpm -ba" to create the rpm package.

During the installation process, you may notice the following error message:

Error 1: XSLT configuration file not found

checking for xslt-config... no
configure: error: XSLT configuration could not be found
error: Bad exit status from /var/tmp/rpm-tmp.8J0ynG (%build)
RPM build errors:
    Bad exit status from /var/tmp/rpm-tmp.8J0ynG (%build)

Solution 1: Install libxstl-devel

yum install libxstl-devel

The following dependencies will be installed at the same time:

  • libgcrypt
  • libgcrypt-devel
  • libgpg-error-devel

Error 2: libvorbis error

checking for libvorbis... configure: error: must have Ogg Vorbis v1.0 or above installed
error: Bad exit status from /var/tmp/rpm-tmp.m4Gk3f (%build)

Solution 2: Install libvorbis-devel

yum install libvorbis-devel

The following dependencies will be installed at the same time:

  • libogg
  • libogg-devel
  • libvorbis
  1. Verify source and binary RPM packages

Once the rpmbuild command has completed successfully, you can verify the source rpm package and binary rpm package files under the following command.

# ls -l /root/rpmbuild/SRPMS/
-rw-r--r-- 1 root root 1162483 Aug 25 15:46 icecast-2.3.3-0.src.rpm

# ls -l /root/rpmbuild/RPMS/x86_64/
-rw-r--r--. 1 root root 349181 Feb  4 12:54 icecast-2.3.3-0.x86_64.rpm
7. Install the RPM File to Verify

As a final step, you can install the binary package to verify that the installation was successful and that all dependencies are met.

# rpm -ivvh /root/rpmbuild/RPMS/x86_64/icecast-2.3.3-0.x86_64.rpm
D: ============== /root/rpmbuild/RPMS/x86_64/icecast-2.3.3-0.x86_64.rpm
D: loading keyring from pubkeys in /var/lib/rpm/pubkeys/*.key
D: couldn't find any keys in /var/lib/rpm/pubkeys/*.key
D: loading keyring from rpmdb
D: opening  db environment /var/lib/rpm cdb:mpool:joinenv
D: opening  db index       /var/lib/rpm/Packages rdonly mode=0x0
D:  read h#     210 Header sanity check: OK
D: added key gpg-pubkey-c105b9de-4e0fd3a3 to keyring
D: Using legacy gpg-pubkey(s) from rpmdb
D: Expected size:       349181 = lead(96)+sigs(180)+pad(4)+data(348901)
D:   Actual size:       349181
D: ========== relocations
D:      added binary package [0]
D: found 0 source and 1 binary packages
D: ========== +++ icecast-2.3.3-0 x86_64/linux 0x2
..
..

After the installation is complete, you can verify whether it was successful.

# rpm -qa icecast
icecast-2.3.3-0.x86_64

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324375527&siteId=291194637