rpm packaging quick start tutorial

Keywords: Linux CentOS shell

RPM (Redhat Package Manager) is a common package manager for Linux distribution s such as Redhat, CentOS and Fedora. RPM tool can be used to make source installation package and binary installation package. This document provides an example to illustrate how to make an RPM binary package.

1. Preparation

Install the programs required for packaging: Yum install RPM build rpmdevtools

2. Install a simple hello.sh script

  • First, we need to prepare the program hello.sh that needs to be packaged

    #!/bin/bash
    echo "Hello World!"
    
  • Write SPEC document hello.spec

    Note: the name of our spec document should be the same as that of the installer

    Name:     hello
    Version:  2.1
    Release:  1
    Summary:  The "Hello World" script
    Summary(zh_CN):  GNU "Hello World" program
    License:  GPLv3+
    
    %description
    The "Hello World" program, done with all bells and whistles of a proper FOSS 
    project, including configuration, build, internationalization, help files, etc.
    
    %description -l zh_CN
    "Hello World" program, contain FOSS All parts required for the project, Include configuration, structure, internationalization, Help files, etc.
    
    %install
    mkdir -p %{buildroot}/usr/local/bin
    install -m 755 -t %{buildroot}/usr/local/bin /root/dw/rpmTest/hello.sh
    
    
    %files
    /usr/local/bin/hello.sh
    

    spec document description:

    • The header contains the basic information of the installation package. The header item is described as follows (* identification must include this item)

      • Name *: the name of the package
      • Version *: software version number
      • Release *: release version number of the same version of software
      • Summary *: a brief introduction to the software
      • License *: software authorization mode
      • Summary(zh_CN): a brief introduction to the Chinese version
    • The content includes the following items

      • %description
        The complete introduction of the software does not work with the Summary field. You can write multiple lines after completing the content introduction.
      • %prep (not used)
        The process of decompressing the source code.
      • %build (not used)
        The process of compiling the source code.
      • %install
        Install the script. Copy the files to be installed to the% {builderoot} directory.% n The {builderoot} directory is equivalent to the root directory of the installation system.
      • %files
        The list of files to be packaged. Note that the file path here is the root directory after installation specified in ${builderoot}. Wildcards can be used, such as / etc / kept / *. Note: when using wildcards, if there are files in the installation directory that conflict with the installation package, the installation fails.
      • %changelog (not used)
        Software change record.
      • scriptlets execute scripts during installation and uninstallation (not used)
        • Execute before (% pre) or after (% post) package installation
        • Execute before (% preun) or after (% postun) package uninstall
        • Executed at the start (% pretrans) or end (% postrans) of a transaction

3. Use rpm build command to make rpm binary package

Execute rpm build - BB hello.spec to generate binary rpm package. By default, it will be generated in the directory ~ / rpmbuild of the current user

tree rpmbuild/
rpmbuild/
├── BUILD
├── BUILDROOT
├── RPMS
│   └── x86_64
│       └── hello-2.1-1.ky10.x86_64.rpm
├── SOURCES
├── SPECS
└── SRPMS

**Proposed definition_ The topdir macro generates the installation package to the specified directory** The following command generates the rpm package to the current directory:

rpmbuild -bb hello.spec --define  "_topdir $PWD/rpmbuild"

4. Customize rpm package name

The default installation package name is defined in the / usr/lib/rpm/macros file:

%_rpmfilename           %{_build_name_fmt}
%_build_name_fmt        %%{ARCH}/%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm

Modify% in the first line of hello.spec_ rpmfilename macro for custom package name installation

%define _rpmfilename hello.rpm

Execute the package command again and check the rpmbuild/RPMS directory to generate a custom rpm package.

tree rpmbuild/
rpmbuild/
├── BUILD
├── BUILDROOT
├── RPMS
│   └── hello.rpm
├── SOURCES
├── SPECS
└── SRPMS

5. Add the program in the rpm package to the systemd ` self start service

To generate systemd self start service, you need to write hello.service file. This article does not specify the format of hello.service file, but only describes the packaging process. By modifying the hello.spec file, you can add hello.service to the systemd self startup service:

  • %The install phase generates / etc/systemd/system/hello.service
  • %The post phase starts the hello.service service after installation
  • %The preun phase stops and disables the hello.service service before uninstallation

The hello.service file is as follows:

[Unit]

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/usr/local/bin/hello.sh
Restart=always
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=%n

The modified hello.spec file is as follows

%define _rpmfilename hello.rpm

Name:     hello
Version:  2.1
Release:  1
Summary:  The "Hello World" script
Summary(zh_CN):  GNU "Hello World" program
License:  GPLv3+

%description
The "Hello World" program, done with all bells and whistles of a proper FOSS 
project, including configuration, build, internationalization, help files, etc.

%description -l zh_CN
"Hello World" program, contain FOSS All parts required for the project, Include configuration, structure, internationalization, Help files, etc.

%install
mkdir -p %{buildroot}/usr/local/bin
install -m 755 -t %{buildroot}/usr/local/bin /root/dw/rpmTest/hello.sh

# install the systemd unit file to buildroot.
mkdir -p %{buildroot}/etc/systemd/system
install -t %{buildroot}/etc/systemd/system /root/dw/rpmTest/hello.service


%files
/usr/local/bin/hello.sh
/etc/systemd/system/hello.service

%post
systemctl enable hello.service
systemctl start hello.service

%preun
systemctl stop hello.service
systemctl disable hello.service

Install the generated rpm package and view the hello.service service

rpm -ivh rpmbuild/RPMS/hello.rpm

systemctl status hello
● hello.service
   Loaded: loaded (/etc/systemd/system/hello.service; enabled; vendor preset: disabled)
   Active: activating (auto-restart) since Tue 2021-10-12 15:16:37 CST; 80ms ago
  Process: 288322 ExecStart=/usr/local/bin/hello.sh (code=exited, status=0/SUCCESS)
 Main PID: 288322 (code=exited, status=0/SUCCESS)

Uninstall the hello package and execute the stop and remove service commands

rpm -e hello
Removed /etc/systemd/system/multi-user.target.wants/hello.service.

reference resources

Posted by jara06 on Tue, 12 Oct 2021 16:22:39 -0700