In Linux, how does one create an RPM package?

Source: Internet
Author: User

To manage software in Linux, software packages are used in various versions of Linux. The advantage of the software package is that the system will track those installation files during the installation process, which makes future management easier. It would be a good idea to bundle a customized software into an RPM package before adding it to your Linux server. In this article, you will learn how to create your own RPM package.

RPM is the default package format of Red Hat Enterprise Linux and SUSE Linux Enterprise Server and Their Derivative versions. It is useful to create an RPM package if you want to install customized software on multiple Linux servers. Put this RPM package in the database. It will be more convenient for all servers that can connect to the database to update the software in the future.

The RPM package contains two files: A shell script and a README file. The README file describes how to use the shell script. If you can create a software package with two files, you can also create a larger software package.

Creating a simple RPM package

Note that there are clear differences between user accounts for specific tasks. If an error occurs, use a common user account. If you use the root user account, you may accidentally erase many files from the disk. Follow these steps to create your own RPM package:

1. Create a directory structure and include the files required by the RPM package. The name of this structure is usually named after the RPM package, including its version number. It does not matter where this directory is stored. In this example, it is in the home directory of the current user account. We name this directory test-1.0. The following command tells you how to create and name this directory, and how to put the required files in it:

$ mkdir ~/test-1.0$ cd ~/test-1.0$ echo echo this is a test > test.sh$ chmod +x test.sh$ echo just launch the script > README

2. Now, all the files required by the RPM package are here.In the next step, 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 rpmbuild/SOURCES-this folder is usually located in the home directory of the current user. You also need a. specs file that contains all the specifications for creating an RPM package. 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.

The first cd command ensures that the rpmbuild directory is located in the home directory of the current user. Run the following command as a normal user:

$ cd$ mkdir -p ~/rpmbuild/SOURCES$ mkdir -p ~/rpmbuild/SPECS$ tar -cvzf ~/rpmbuild/SOURCES/test-1.0.tar.gz test-1.0

3. Now all files are available. Please create a description file as a normal user..The Rpmbuild command will find all the instructions needed to create the file. In the latest Red Hat and derivative versions, it is easy to create a description file: you only need to use the vim editor to create a file containing all the content, and set the extension to. spec. The following figure shows the file in the test-1. 0rpm package (the template changes are marked in bold ):

Name: test

Version: 1.0

Release: 1% {? Dist}

Summary: A test package

Group type: Test Packages Test package

License: GPL

URL: http://test.example.com

Source0: Too Many name‑‑0000version‑.tar.gz

BuildRoot: % (mktemp-ud % {_ tmppath}/% {name}-% {version}-% {release}-XXXXXX)

BuildRequires:/bin/rm,/bin/mkdir,/bin/cp

Requires:/bin/bash,/bin/date

% Description

Demonstration of deploying a single package

%prerp%setup –q%build#configure#make %{? _smp_mflags}%installrm -rf $RPM_BUILD_ROOT#make install DESTDIR=$RPM_BUILD_ROOTmkdir -p $RPM_BUILD_ROOT/usr/local/bincp myscript $RPM_BUILD_ROOT/usr/local/bin%cleanrm -rf $RPM_BUILD_ROOT%files%defattr(-root,root,-)#%doc%attr(0755,root,root)/usr/local/bin/myscript%changelog* Today- Creation of initial RPM

You need to remember a few things. All entries prefixed with a % are internal macros. For simple demonstration, some macros are disabled. The most important of these entries are % configure and % make macros. You must add a # before their names and remove % to disable these macros.

The macro at the beginning is very clear and does not need to be explained. The most important macro is the Source0 definition. The role is defined as the role name‑‑‑‑‑‑‑‑‑version‑.tar.gz, and must be exactly the same as the tar ball name you created earlier.

Another element is BUILD_ROOT. This is an automatically generated environment, and the rpmbuild command will use this environment for its own work. Remember that BUILD_ROOT disappears automatically. This is why root users are not used when creating an RPM package. The % build part can also be used for work such as software compilation.

In the % install section, the script you created earlier is copied. In this example, you only need a simple cp command, but for more complex software packages, these tasks will be very complex.

The last part of the. spec file is the % attr macro, which sets the permission mode to 0755 and makes the root user and root the master of the Installation File.

Now that 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 be in ~ Create your RPM file in the/rpmbuild/RPMS directory.

Original article: http://www.searchsv.com.cn/showcontent_47965.htm

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.