Deep description of RPM package rpmbuild SPEC File

Source: Internet
Author: User
Tags rpmbuild

I. Write spec scripts
The principle of rpm package creation is not complex. It can be understood as sorting out some information in a standard format, including basic software information and scripts executed before and after installation and uninstallation, decompress, patch, compile, and install the source code package.
In the actual process, the most important thing is to know the location of the virtual path and macro definition.

Ii. Keywords
Spec scripts include many keywords, mainly including:
Reference Name: the name of the software package, which can be referenced later in the form of % {Name}

Summary: Summary of the Package content

Version: the actual version of the software, such as 1.0.1, which can be referenced later by % {Version }.

Release: release serial number, such as 1linuxing, indicating the number of packages. It can be referenced later using % {Release }.

Group: Software Group. Standard Group is recommended.

License: Software authorization method, usually GPL

Source: Source code package, which can contain multiple sources such as Source1 and Source2, and can be referenced later by % {source1} and % {source2}

BuildRoot: this is the "virtual directory" used for installation or compilation. Considering the multi-user environment, it is generally defined:
% {_ Tmppath}/% {name}-% {version}-% {release}-root
Or
% {_ Tmppath}/% {name}-% {version}-% {release}-buildroot-% (% {__ id_u}-n}
This parameter is very important, because during the rpm generation process, the software will be installed to the above path when you execute make install. During packaging, it also relies on "virtual directory" as "root directory" for operations.
You can reference it later using the $ RPM_BUILD_ROOT method.

URL: home page of the software

Vendor: information of a publisher or package organization, such as RedFlag Co, Ltd

Disstribution: Release ID

Patch: Patch source code. Multiple patches can be identified using patch0 or % {patch0 }.

Prefix: % {_ prefix} is mainly used to solve the problem of installing the rpm package in the rpm package directory. In this way, you must define this identifier here and reference it when writing the % install script to re-specify the location during rpm installation.

Prefix: % {_ sysconfdir} is the same reason as above, but because % {_ prefix} refers to/usr, for other files, such as the configuration file in/etc, % {_ sysconfdir} is required.

Build Arch: indicates the compiled target processor architecture. The noarch identifier is not specified, but the default value is usually the content in/usr/lib/rpm/marcros.

Requires: name of the software package on which the rpm package depends. You can use >=or <=to indicate that the package is greater than or less than a specific version. For example:
Libpng-devel> = 1.0.20 zlib
※"> = "Must be separated by spaces, and different software names must be separated by spaces.
For example, PreReq, Requires (pre), Requires (post), Requires (preun), Requires (postun), and BuildRequires are all specified for dependencies of different stages.

Provides: specifies some specific features of the software for other RPMs to identify

Packager: package information

% Description

Iii. spec script subject
The subject of the spec script also contains many keywords and descriptions. I will mark some areas that require special attention.
% Prep preprocessing script

% Setup-n % {name}-% {version} decompress the source code package and set it
Usually decompress the package from/usr/src/asianux/SOURCES to/usr/src/asianux/BUILD/% {name}-% {version.
Generally, you can use % setup-c, but there are two cases: one is to compile multiple source code packages at the same time, second, the tar package name of the source code is inconsistent with the extracted directory. In this case, you need to use the-n parameter to specify it.

% Patch Patching
The common patch will be in the source code tar.gz package together or in the SOURCES directory. The general parameter is:
% Patch-p1 is performed with the Patch defined earlier.-p1 is the first directory to ignore the patch.
% Patch2-p1-B xxx. patch with the specified patch,-B is used to generate a backup file

◎ Add
Reference % setup without any options, only open the software package.
% Setup-n newdir: Decompress the package in the newdir directory.
Before % setup-c is decompressed, a directory is generated.
% Setup-B num decompress the num source file.
% Setup-T does not use the default decompression operation.
% Setup-T-B 0 decompress the 0th source code files.
% Setup-c-n newdir: Specify the directory name newdir and generate the rpm suite in this directory.
% Patch: Specifies the patch level automatically.
% Patch 0 uses 0th patch files, which is equivalent to % patch? P 0.
% Patch-s does not display information during patching.
% Patch-T delete all output files generated during patching.

% Configure is not a keyword, but a standard macro command defined by rpm. Execute the configure configuration of source code.
Run the following command in the/usr/src/asianux/BUILD/% {name}-% {version} directory, parameters defined in/usr/lib/rpm/marcros are referenced.
Another non-standard writing method is to refer to the parameter customization in the source code, for example:
Reference CFLAGS = "$ RPM_OPT_FLAGS" CXXFLAGS = "$ RPM_OPT_FLAGS"./configure -- prefix =%{_ prefix}
% Build start build package
Make in the/usr/src/asianux/BUILD/% {name}-% {version} directory. Common Syntax:
Reference make % {? _ Smp_mflags} OPTIMIZE = "% {optflags }"
Are some optimization parameters, defined in/usr/lib/rpm/marcros

% Install start to install the software to the virtual root directory
Perform the make install operation in the/usr/src/asianux/BUILD/% {name}-% {version} directory. This is very important, because if the path here is incorrect, it will fail to find the file in % file below. Common Content:
% Makeinstall is not a keyword, but a standard macro command defined by rpm. You can also use a non-standard statement:
Reference make DESTDIR = $ RPM_BUILD_ROOT install
Or
Reference make prefix = $ RPM_BUILD_ROOT install
It should be noted that the % install here is mainly for the following % file service. Therefore, you can also use common system commands:
Reference install-d $ RPM_BUILD_ROOT/
Cp-a * $ RPM_BUILD_ROOT/
% Clean clear temporary files
The general content is:
Reference ["$ RPM_BUILD_ROOT "! = "/"] & Rm-rf "$ RPM_BUILD_ROOT"
Rm-rf $ RPM_BUILD_DIR/% {name}-% {version}
※Differentiate between $ RPM_BUILD_ROOT and $ RPM_BUILD_DIR:
$ RPM_BUILD_ROOT refers to the BuildRoot defined at the beginning, while $ RPM_BUILD_DIR usually refers to/usr/src/asianux/BUILD. In this example, the preceding content is required by % file.

% Pre rpm script executed before installation

% Post rpm script executed after installation

% Preun rpm script executed before uninstallation

% Postun rpm script executed after uninstalling

% Files defines which files or directories will be placed in rpm
This will be performed in the virtual root directory. Do not write absolute paths, and apply macros or variables to indicate relative paths. If it is described as a directory, it indicates all the files except % exclude in the directory.
% Defattr (-, root, root) specifies the properties of the packaging file, namely (mode, owner, group).-indicates the default value. For text files, the value is 0644, And the executable file is 0755.

% Exclude list files that do not want to be packaged into rpm
※Be careful. If the file specified by % exclude does not exist, an error will also occur.
% Changelog change log 4. Example
The following. spec script is a simple example. It is used to package all the files in a directory into an rpm package.
1. Preliminary work
We assume that the directory to be packaged is our source code file. In this way, you can temporarily ignore troublesome patching, compilation, and other issues, and it is also a common method. Before writing the. spec script, you need to prepare the "source code", that is, the directory. The content is relatively simple: Reference [root @ mail html] # ll
Total 4
Drwxr-xr-x 3 root 4096 Jun 4 14:45 demo
[Root @ mail html] # ll demo/
Total 4
Drwxr-xr-x 3 root 4096 Jun 4 14:45 images
-Rw-r -- 1 root 0 Jun 4 14:45 index.html
Because rpmonly recognizes the tar.gz format, it must be packaged and moved to the SOURCES Directory: Reference [root @ mail html] # tar czvf demo.tar.gz demo/
Demo/
Demo/images/
Demo/images/logo.gif/
Demo/index.html
[Root @ mail html] # mv demo.tar.gz/usr/src/asianux/SOURCES/
2. demo. spec content
After the preparation is complete, the script Content Used in the example is as follows: [root @ mail html] # cd/usr/src/asianux/SPECS/
[Root @ mail SPECS] # cat demo. spec reference Summary: Test package for bkjia webblog
Name: suite
Version: 1.0.0
Release: 1
License: GPL
Group: System
Source: demo.tar.gz
BuildRoot: % {_ tmppath}/% {name}-% {version}-% {release}-root
Url:

4. How to Write % file segments
Since all files in the suite must be included in % file, we need to know which files are included in the compiled suite?
A common practice is to manually simulate a compilation process:


./Configrue -- prefix =/usr/local/xxx
Make
Make DESTDIR =/usr/local/xxx install
Or
Make prefix =/usr/local/xxx install
In this way, the contents of the entire suite will be put into/usr/local/xxx, And the % file and % exclude segments can be compiled as needed.
※Of course, this method can only be used for compiling the source code in GNU mode and creating packages using GNU autotool. Custom Makefile cannot be generalized.
5. Execution script in rpm
If the rpm package is prepared to be placed on the system installation disc, check whether the script defined in the rpm has problems. Because the system is only dependent on a small environment during installation, this environment is very different from the actual environment after installation. Therefore, most scripts cannot take effect in this installation environment, and may even cause troubles.
Therefore, it is better to put the package in the installation disc without executing the script.
In addition, to provide information that can be referenced in operations, rpm also provides a signal mechanism: different operations return different information and put it in the default variable $1.


0 indicates uninstallation, 1 indicates installation, and 2 indicates upgrade.
You can use this method as follows:


Reference % postun
If ["$1" = "0"]; then
/Sbin/ldconfig
Fi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.