Package software with RPM

Source: Internet
Author: User
Tags gz file

RPM (Red Hat Package Manager) is the most common package manager for Linux distributions. Because it allows distribution of compiled software, you can install the software with only one command.
Rpm is the Installation Tool specified in Linux "standard base library" version 1.0.0. Eight of the top 10 Linux distributions are based on RPM (see "Comparison of Linux distributions", which is located on distrowatch.com listed in references later in this article ). Even if some distributions that usually do not use rpm, such as Debian, there are tools available to convert RPM into their own formats. On Linux, rpm is the best choice for packaging software for anyone other than developers.
Whether you develop your own software or provide code to create an RPM package, this article will help you get started. By the way, subsequent articles in this series will cover the following topics: Building an RPM package, not necessarily a root user, patches it before building the software, run the script when installing and uninstalling and when installing or uninstalling other software packages.
Simple instance
I will start from a simple instance and mainly use the default RPM value. Then, I will add several optional features.
The RPM package starts from a program in the form of a source file and is ready for compilation. Instead of creating an example that is meaningless, I chose to use the GNU indent Program (see references ).
It is very easy to create indent in Linux. The indent-2.2.6.tar.gz file is in the current directory, and all you have to do is:
Build indent manually
[Quote7] $ tar xzf indent.2.2.6.tar.gz
$ CD indent-2.2.6
$./Configure
$ Make
$ Make install [/quote7]
If you have already built many open source projects, this may seem familiar. Unpack;./configure; Make; make install sequence is a typical software that uses the GNU Autoconf tool. Because this is too common, most of the indent content I described here can be used for other open source projects without any changes.
Assume that you are using Red Hat 7.1. Later in this article, I will provide some suggestions for using rpm on other Linux distributions.
On Red Hat 7.1,Before continuing, make sure that you have installed the rpm-build package. Run rpm-Q rpm-build to check whether the tool is installed. You should see some information similar to rpm-Build-4.0.2-8 (the version may be different ). If you see package rpm-build is not installed, you need to install it on the Red Hat installation CD.
Create a basic RPM package
To build an RPM package, you need to write an RPM input file named spec file that tells RPM how to build and package your software. To compile a spec file, you must:
[List7] 1. Create a file indent-1.spec, as shown below. You can name it and place it anywhere. RPM has no requirements for this.
2. Log On As the root user.
3. Copy the indent-2.2.6.tar.gz file to/usr/src/RedHat/sources.
4. Run the rpm-Ba indent-1.spec and change the indent-1.spec to the name you are using.

    First spec file: indent-1.spec
    [Quote7] Summary: GNU indent
    Name: Indent
    Version: 2.2.6
    Release: 1
    Source0: Too Many name‑‑0000version‑.tar.gz
    License: GPL
    GROUP: Development/tools
    % Description
    The GNU indent program reformats C code to any of a variety
    Formatting standards, or you can define your own.
    % Prep
    % Setup-Q
    % Build
    ./Configure
    Make
    % Install
    Make install
    % Files
    % Defattr (-, root, root)
    /Usr/local/bin/indent
    % DOC/usr/local/INFO/indent.info
    % Doc % ATTR (0444, root, root)/usr/local/man/Man1/indent.1
    % Doc copying authors readme news [/quote7]
    You should see that RPM unpacks the tar file, compiles and installs it. On Red Hat 7.1, the working directory will be/usr/src/RedHat/build.
    Finally, RPM creates two RPM files. A source RPM file will be created in the/usr/src/RedHat/srpms/indent-2.2.6-1.src.rpm, and a binary RPM file will be created in the/usr/src/RedHat/RPMS/i386/indent-2.2.6-1.i386.rpm.
    The source RPM file is simply bundled with the spec file and all the source files and patch files used by the build package. If you choose to distribute it, others can easily use it to recreate your software. The binary RPM file only contains the compiled software and how to install it.
    What does RPM do?
    What does RPM do when you run rpm-Ba filename. SPEC:
    [List7] * read and parse the filename. spec File
    * Run % prep to unpackage the source code to a temporary directory and apply all patches.
    * Run % build to compile the code.
    * Run % install to install the code to the directory of the build machine.
    * Read the list of files in the % files section, collect files, and create Binary and source RPM files.
    * Run % clean to remove the temporary build directory.

      Spec File Content
      The spec file contains several parts. The first part is unmarked; the other part starts with rows like % prep and % build.
      Header 
      The first part (unmarked) defines a variety of information, the format is similar to the e-mail message header.
      Summary is a line of description about the software package.
      Name is the base name of the software package, and version is the version number of the software. Release is the version number of RPM. If an error in the spec file is fixed and a new RPM of the same version of the software is released, the release number should be added.
      License should provide license terms (such as "GPL", "License cial", and "License ware ").
      Group identifies the software type; programs that attempt to help people manage RPM usually list the RPM by group. You can see a list of groups used by red hat in the/usr/share/doc/rpm-4.0.2/groups file (assuming your installed RPM version is 4.0.2 ). However, you can also use other group names.
      Source0, source1, etc. name these source files (usually tar.gz files ). % {Name} and % {version} Are RPM macros which are extended to the RPM names and versions defined in the header. Therefore, in this instance, source0 is set to a indent-2.2.6.tar.gz.
      Do not include any paths in the source statement. By default, RPM searches for files in/usr/src/RedHat/sources. Copy or link your source file to it. (To make spec files portable as much as possible, avoid embedding them in the hypothetical path on your own development machine. Other developers can instruct RPM to search for source files in other directories without modifying your spec files .)
      Description
      The following part starts with the % description line. You should provide more description of the software here, so that anyone can view it when querying your software package using rpm-Qi. You can explain what the software package is doing, describe any warning or additional configuration instructions, and so on.
      Shell script 
      The following are shell scripts embedded in the spec file.
      % Prep is responsible for unpacking the software package. In the most common cases, you only need to use the % setup macro to decompress the source tar file in the build directory. Add the-Q item to reduce the output.
      % Build should compile the software package. This shell script runs from the subdirectory of the software package. In our example, it is the indent-2.2.6 Directory, which is often as simple as running make.
      % Install the software package on the build system. This seems as simple as make install, but it is usually more complicated. I will explain this below.
      File List 
      % Files lists the files that should be bundled into RPM and can be set with permission and other information optional.
      In % files, you can use % defattr once to define the default permission, owner, and group. In this example, % defattr (-, root, root) all Files Owned by the root user will be installed, using any permissions they have when RPM binds them from the build system.
      You can use % ATTR (permissions, user, group) to overwrite the owner and permission of an individual file.
      You can use one line in % files to include multiple files.
      You can add % Doc or % config to the row to mark the file. % Doc tells RPM that this is a document file, so if you use -- excludedocs when installing the software package, this file will not be installed. You can also list file names without paths in % Doc. RPM searches for these files in the build directory and includes them in the RPM file, install them to/usr/share/doc/% {name}-% {version }. It is a good idea to include files such as readme and changelog in the form of % Doc.
      % Config tells RPM that this is a configuration file. During the upgrade, the RPM will try to avoid overwriting the configuration you have carefully modified with the default configuration file packaged by the RPM.
      Warning:If a directory name is listed under % files, RPM will include all files in the directory. This is usually not what you want, especially for directories such as/bin.
      Avoid problems in simple instances
      There are several issues with this basic spec file. One of the biggest problems is that you finally installed the product on the build system. This may be a software test version, and you may not want to install it in the build system.
      Rpm uses a feature named build root to solve this problem. The idea is to set up your spec file to copy all installed files to a virtual directory tree (starting from the build root), and then the RPM gets the file from there.
      However, this requires support from some software packages. In many GNU software packages including indent, defining destdir during make install will add the destdir value before all installation paths.
      Do not use./configure -- prefix = $ rpm_build_root. This will install the entire software package if the final position of the entire software package file is to build the root. This may not matter to indent, but any program that needs to find its installation file at runtime will fail, because after rpm is finally installed to the user system, these files are no longer under the build root-it's just a temporary directory on your build system.
      See the updated file indent-2.spec, as shown below.
      Second spec file: indent-2.spec
      [Quote7] Summary: GNU indent
      Name: Indent
      Version: 2.2.6
      Release: 2
      Source0: Too Many name‑‑0000version‑.tar.gz
      License: GPL
      GROUP: Development/tools
      Buildroot: % {_ builddir}/% {name}-Root
      % Description
      The GNU indent program reformats C code to any of a variety
      Formatting standards, or you can define your own.
      % Prep
      % Setup-Q
      % Build
      ./Configure
      Make
      % Install
      Rm-RF $ rpm_build_root
      Make destdir = $ rpm_build_root install
      % Clean
      Rm-RF $ rpm_build_root
      % Files
      % Defattr (-, root, root)
      /Usr/local/bin/indent
      % DOC/usr/local/INFO/indent.info
      % Doc % ATTR (0444, root, root)/usr/local/man/Man1/indent.1
      % Doc copying authors readme news [/quote7]
      Change description
      First, we added the release number of the version. Never forget to do so whenever you edit the spec file.
      We added buildroot in the header to tell RPM that this is where the temporary installation file is during build. For temporary files, we use two RPM macros here, instead of assuming a specific location. On Red Hat 7.1, % {_ builddir} ends with something similar to/usr/src/RedHat/build.
      We also need to tell the system to install indent there. Rpm helps us define a shell variable rpm_build_root with the value of the build root. Therefore, when making install, we only need to pass it as the destdir value.
      We also added several lines in % install and % clean to clear the build root before and after installation. % Clean is the script that runs when RPM build ends normally, so that temporary files will not be retained.
      Finally, in % files, note that we do not include buildroot before the path here. We use the "real" path. RPM will search for these files under the build root because you have included the buildroot definition.
      What happened this time
      If you observe carefully, you will find that everything works as usual before rpm is installed. Then, the file is not directly installed in/usr/local/bin, but installed in (for example) /usr/src/RedHat/build/indent-root/usr/local/bin.
      If you check the final binary RPM file (with rpm-qlp indent-2.2.6-2.i386.rpm), you will see that the build root has been removed by rpm. If you install rpm, these files will eventually be installed in the correct directory, such as/usr/local/bin/indent.
      Use rpm on other Linux distributions
      If you are using different Linux distributions, the RPM may have different built-in paths. For example, it almost certainly won't find the source file in/usr/src/RedHat! To determine the desired RPM installation path, run rpm -- showrc and view how the following sections are defined:
      _ Sourcedir 
      Where can I find the source file (TAR file, etc.) with RPM)
      _ Srcrpmdir 
      Where can I add a new source RPM file?
      _ Rpmdir 
      Where will RPM store the new binary RPM file (in a subdirectory specific to the architecture)
      Some of them are defined based on other variables. For example, when you see % {_ topdir}, find the definition of _ topdir, and so on.
      What is next?
      I hope this introduction to package software with RPM will be helpful to you. For more information, see the following references. In subsequent articles in this series, we will discuss these topics:
      [List7] * build an RPM package instead of a root user
      * Patch the software before creating the software.
      * Run the script during installation and uninstallation.
      * Install or uninstallOthersRun scripts in the Software Package

        References 
        [List7] For more information, see the original article on the developerworks global site.
        Source code of the file described in this article
        [List7] indent-1.spec
        Indent-2.spec

          The RPM website has links to many useful resources. The RPM email list is a good place to ask questions.
          To know which Linux distributions are rpm-based, see "Comparison of Linux distributions" on distrowatch.com ". Rpm-based distributions include red hat, Suse, Mandrake, turbolinux, and Caldera.
          Obtain the GNU indent program.
          Maximum rpm is a book about using rpm. It is quite outdated, but is being updated now.
          Rpm howto is becoming obsolete. It tells you the same basic knowledge as this article.
          Eric S. Raymond's software release practice howto document is not specific to RPM or Linux. It has a lot of good skills in how to publish software so that users can use it and programmers can fix and improve it.
          The Free Software Foundation is a source that provides GNU indent and many other useful software packages.
          Obtain IBM developer kit for Linux on itanium, IBM developer kit for Linux, Java 2, version 1.3, or IBM software evaluation kit for Linux.
          Visit more Linux references on developerworks.

          Visit more open source code references on developerworks.

          Original post address: http://www.sudu.cn/info/html/edu/linux/20080428/302896.html

          Related Article

          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.