RPM package Creation

Source: Internet
Author: User
Tags rpmbuild
Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://mumaren.blog.51cto.com/4180778/971507

In practical applications, the use of Binary packages seems a little troublesome. The best way is to make the source package into an RPM package, put it into the yum repository, and then use puppet for management. This will be much more convenient for O & M, you know.

Actually, creating an RPM package is not a complicated task. The key is to compile the spec package description file. The following describes how to create an RPM software package.

One rpmThe main steps for creating a package are as follows:

1. plan what you want to do. Some RPM packages are software, but some such as cacti are a lot of web files. Is it pure files, library files, or software source code.

2. obtain raw materials of the software package. For example, the binary package must be the original

3. Patch Required Software

4. Create an RPM package that can specify the installation path (not to mention)

5. Consider upgrading relationships

6. Plan the dependency to write the spec file, and write the entire process into it.

7. Start production. Some also have Certificate Signing: Use the GPG key to sign the RPM software package. You can use rpmbuild-ba-sign name. spec to build and sign the software package in one step. Use rpm -- resign name-version.rpm to add or change a GPG Signature

8 Test

The focus is on writing spec files.

######################################

Step 1: Set the directory (uppercase is required), Working directory (Red Hat 6And buildroot)

1. Set the directory to be created and the required directory structure (note:Cannot Use RootDo you know why ?)

Creating an RPM package is similar to a workspace. The default address of this workspace is/usr/src/RedHat, because the default permission is root, therefore, a directory is generally customized.

This is the directory structure provided for rpm-build.

  1. /Usr/src/RedHat/
  2.  
  3. | -- Build
  4.  
  5. | -- RPMs
  6.  
  7. | -- I386
  8.  
  9. | '--...
  10.  
  11. | -- Sources
  12.  
  13. | -- Foo-1.2.tar.gz
  14.  
  15. | -- Foo-1.2-add_feature.patch
  16.  
  17. | '-- Foo-1.2-change_default.patch
  18.  
  19. | -- Specs
  20.  
  21. | '-- Foo. spec
  22.  
  23. '-- Srpms

Build: Decompress the source code and compile the workshop. You don't need to put anything in it.

RPMS: There are subdirectories of different platforms. The generated RPM package is here. You can use RPM to compile

Sources: raw material location

Specs: storage location of spec files

Srpms: the location where the RPM package in SRC format is stored. There is no platform relevance and no sub-directories.

2. Place the source program in the specified position.

Create a .tar.gz file containing the rpmfile. This directory also makes it easier to create a file. The .tar.gz file must be located in the directory named rpmbuild/sources. You also need a. Specs file, which contains the specifications for all created RPM packages. The. Specs file is the core component of the RPM package, which contains instructions for all files. The files in the RPM package are installed according to these instructions. The. Specs file should be placed in a specs directory.

3 rpmbuild command:

After the directory structure is known, you need to understand the rpmbuild command.

After the spec file is created, it is time to complete the final work. Use the root directory user identity to install the rpmbuild command. In red hat, use Yum install rpm-build to complete the job. Next, make sure that all files are in the correct place. The tar archive file must be in ~ /Rpmbuild/sources directory, the demo. spec file must be in ~ /Rpmbuild/specs directory. Now you can use the rpmbuild-Ba test. spec command to create a software package. This will create your RPM file in the/usr/src/RedHat/RPMS directory.

Bytes --------------------------------------------------------------------------------------------------------------

# Note:

How to change the rpmbuild directory:

Rpmbuild -- showrc | grep macrofiles

Macrofiles, which defines the files in which these macros are set, such as profile.

So many last matches take effect: rpmmacros files hidden in the Home Directory

If the home directory is redefined once, the built-in

Actually _ topdir Definition

_ Usrsrc is defined under _ SRC.

Create a. rpmmacros file in your home directory.

Directly write:

% _ Topdir/home/XXX/rpmbuild

Exit and try again

Mkdir-PV rpmbuild/{build, rpms, sources, specssrpms}

Bytes -------------------------------------------------------------------------------------------

######################################## ##

Step 2: Create a specFile: This step is the most important!

One specThe document consists of several paragraphs:

INTRODUCTION sectionDescription section:

Prep SectionPreparation phase:

Build SectionCompilation phase:

Build SectionCompilation phase:

Install SectionInstallation phase:

Clean Section: Clearing segment

Files Section: File segment

Changelog Section: Change segment

Each stage starts with %Start with stage name

 

Note:

1. Note that a lot of information in the spec file is taken from macro commands. If it is not explicitly specified, it will inherit the files of/usr/lib/RPM/macros and other related macro commands.

 

 

INTRODUCTION sectionDescription section:

The segment that rpm-qi can query.

First look at the information of a software package:-Qi Query

 

  1. % Define name Foo # Name
  2.  
  3. % Define Version 16 # version
  4.  
  5. Name: % {name} # name is the above macro command
  6.  
  7. Version: % {version} # The above macro command is also used.
  8.  
  9. Release: 1
  10.  
  11. License: GPL
  12.  
  13. GROUP: Applications/productigroups # group of software. For more information, see/usr/share/doc/rpm-version/groups.
  14.  
  15. URL: http://www.mysqlops.com
  16.  
  17. Source: ftp://www.mysqlops.com/pub/xxx.tar.gz
  18.  
  19. Patch0: foo-1.2-change_default.patch
  20.  
  21. Pattern: foo-1.2-add_feature.patch
  22.  
  23. Prereq: unzip # required before installation
  24.  
  25. Requires: Pam # What is required after installation
  26.  
  27. Buildprereq: GCC & gt; = 2.96 # What is required before packaging
  28.  
  29. Buildroot: % {_ tmppath}/% {name}-root # Prepare the chroot directory of the source code file
  30.  
  31. Summary: a fictional open source package for the offering. # Summary
  32.  
  33. % Description # description

Relocatable: Can I change the installation location?

Summary: Simple Description

Version: Do not use a horizontal line in the middle

There is a platform macro after release: % {? Dist },? With judgment, only the number of times the RPM package producer made by himself, and the release number of the RPM package

GROUP:SupportedLess/usr/share/doc/rpm-xxx/groupViewNeed to be clear

Vendor: Producer provider

Source: macros are also supported.

Buildroot: The file generated by rpmbuild is under this Directory, which is similar to chroot.

Buildrequires: dependency

Prep SectionPreparation phase:

% Prep this part is composed of some parameters to form a shell script to extract the source code from sources and apply patches to the build directory to prepare the following compilation.
Macro commands related to % prep
% Setup
% Patch

Example:

 

  1. % Prep
  2. % Setup-Q # extract the source code to the build directory.-Q indicates that the output is not displayed (Quietly)
  3. % Patch0-P1 # apply patch0
  4. % Pattern-P1-B. orig # apply pattern but save the source file as the suffix of. oring.
  5. Unzip foo_data.zip # decompress the ZIP file

Build SectionCompilation phase:

% Build is to compile and prepare the software. It runs like a shell script, which runs in the source code directory specified in the context directory. call the script during compilation. config in the local directory (configured in % configure ).
Macros related to % build
% Configure # You can view its settings by running rpm-eval % configure.

Example:

 

  1. % Build
  2. % Configure-enable-shared
  3. Cflags =-O2 make

Or directly use % configure, similar to % setup

? _ Smp_mflags: determines whether it is an SMP symmetric multi-processor.

Install SectionInstallation phase:

Start with % install:

Delete the file first. For the impact of the previous installation

Make destdir specifies the installation directory

Find_lang

% Install is used for RPM installation of all required files. These packaged files will be copied to the directory tree in buildroot described earlier. This directory is usually used in/var/tmp.
$ Rpm_build_root parameter to set buildroot. The main reason why you cannot use a real directory is that during compilation, files may be replaced with files in your system, causing the system
And software problems. For other file paths such as % {_ Mandir}, % {_ bindir}, % {_ sysconfdir}, etc., they are all specified using a pre-defined macro.
By default, it runs under the rpm_build_root directory.
Example:

  1. % Install
  2.  
  3. Rm-RF $ rpm_build_root # The default $ rpm_build_root is/var/tmp/% {name}-root.
  4.  
  5. Make destdir = $ rpm_build_root install # specify the path of the Installation File
  6.  
  7. Install-m644 foo.8 $ {rpm_build_root}/% {_ Mandir}/man8/foo.8

 

 

Note:

Script section: Sometimes there is this section after installationScriptlets

These options allow you to dynamically use shell scripts to control installation and deletion,
% Pre, % post is a script running in the package after installation, note that there is no interaction
% Preun, % postun run with script when the package is deleted
Rpm-Q-scripts packagename # You can see the script Information

For example:

  1. % Pre
  2.  
  3. Groupadd-G 201 foo
  4.  
  5. Useradd-G foo-S/bin/false-D/var/foo-M foo
  6.  
  7. % Post
  8.  
  9. /Sbin/ldconfig
  10.  
  11. Chkconfig -- add food
  12.  
  13. % Preun
  14.  
  15. If [= 0]
  16.  
  17. Then
  18.  
  19. Service food stop & gt;/dev/null 2 & gt; & 1
  20.  
  21. Chkconfig -- del food
  22.  
  23. Fi
  24.  
  25. % Postun
  26.  
  27. If [= 0]
  28.  
  29. Then
  30.  
  31. Userdel foo
  32.  
  33. Groupdel foo
  34.  
  35. Else
  36.  
  37. /Sbin/ldconfig
  38.  
  39. Service food condrestart & gt;/dev/null 2 & gt; & 1
  40.  
  41. Fi

 

 

Note that the number in this field, if it is 1, indicates the first installation, if it is 2, it may be an upgrade. If it is 0 at % postun, it means it is completely deleted.

For example, after httpd is installed, add users.

% Pre before installation

% Post after installation

% Preun before uninstalling

% After postun is uninstalled, leave it empty.

Note: Install can be used as CP

Bytes -----------------------------------------------------------------------------------------

Clean Section: Clearing segment

% Clean is used to clean the temporary files after build. It is mainly used to delete $ rpm_build_root and run make clean.
Example:


  1. % Clean
  2. Rm-RF $ rpm_build_root
  3. Make clean

Files Section: File segment

Defattr defines Default Permissions

% Files any packaged file must be in the detailed file list of this package. If it is a directory, all the directories of the package owner are in the middle, and % DIR is used to specify an empty directory, available
% Files-F/tmp/dyanmic_filelist to specify a file list. The default % config will replace the configuration and change the original configuration name to. rpmorig,
If you do not want to modify it, use % config (noreplace) to name the new configuration file. rpmnew.
% Defattr (mode, user, group)
% ATTR (mode, user, group) filename
% Config configuration file
% Doc document
Example:

  1. % Files
  2. % Defattr (-, root, root)
  3. % Config/etc/Foo. conf # This parameter can be used for update and deletion.
  4. /Usr/sbin/food
  5. /Usr/bin/foo
  6. % Doc readme # directory is in/usr/share/doc/% {name}-% {version}
  7. % DOC/usr/share/man/man8/food.8
  8. /Usr/share/Foo/
  9. % DIR/var/lock/Foo/# Empty directory

 

 

Changelog Section: Change segment

% Changelog is the modification of the record package. For example, add a new patch and modify the configuration. The log uses data + "% A % B % d % Y ",
You can use rpm-Q-changelog to display the changed log information of the package. For example:

% Changelog
? Mon Aug 5 2002 Elvis Presley

######################################## #

Step 3: Create an RPMPackage

Rpmbuild command

The process can be divided into stages

-Create BB in binary format

-BS source code format

-Ba source code and binary

-After BL is created, files generated by buildroot but not included in the RPM package are detected. Note that an error will occur if the files are not included in the build.

-BC only executes to %

-Bi is only executed in the % install segment.

-BP is only executed in the % prep segment.

This article is from the "mumaren wild boar" blog, please be sure to keep this source http://mumaren.blog.51cto.com/4180778/971507

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.