RPM

Overview

RPM(Redhat Package Manager) 是由红帽提出的软件包管理器,用于软件的安装、卸载和升级等,广泛的应用于多种 Linux 发行版。本文简要介绍如何把应用软件打成 RPM 包,打包的关键在于编写一个 SPEC 文件,该文件描述了包的基本信息、安装和卸载前后所需执行的操作等,fedora 推荐的模板如下:

Name:           testprogram
Version:        
Release:        1%{?dist}
Summary:        

License:        
URL:            
Source0:        

BuildRequires:  
Requires:       

%description

%prep
%setup -q

%build
%configure
make %{?_smp_mflags}

%install
rm -rf $RPM_BUILD_ROOT
%make_install

%files
%doc

%changelog
* Sun Apr  3 2014 makerpm

SPEC 文件可分为六部分:

  • 基本信息:如 Name, Version, Release, Version 等
  • %prep:预处理阶段执行的命令,比如解压软件包。
  • build:编译软件包时执行的命令,比如 make。
  • %install:安装软件包时执行的命令,比如 make install。
  • %files:定义软件包所包含的三类文件,配置文件、执行程序和说明文档。
  • %changelog:记录了软件的每次修改,以便查询。

Example

根据 Redhat 文档,以常用的命令 tree 为例,我们将其源码编成 RPM,首先下载源码:

$ wget ftp://mama.indstate.edu/linux/tree/tree-1.5.3.tgz
$ tar xvf tree-1.5.3.tgz

安装编包的必要软件:

$ yum install rpm-build rpmdevtools

创建必要目录:

$ mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

把解压后的 tree 移动至 SOURCES 目录:

$ move tree  ~/rpmbuild/SOURCES

在 SPECS 目录创建 tree.spec 文件,内容如下:

Summary: File system tree viewer
Name: tree
Version: 1.5.3
Release: 2%{?dist}
Group: Applications/File
License: GPLv2+
Url: http://mama.indstate.edu/users/ice/tree/
Source: ftp://mama.indstate.edu/linux/tree/tree-%{version}.tgz
Patch1: tree-1.2-carrot.patch
Patch2: tree-1.2-no-strip.patch
Patch3: tree-preserve-timestamps.patch
Patch4: tree-no-color-by-default.patch

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)

%description
The tree utility recursively displays the contents of directories in a treelike
format. Tree is basically a UNIX port of the DOS tree utility.%prep
%setup -q
# Fixed spelling mistake in man page.
%patch1 -p1 -b .carrot
# Don't strip binary in the Makefile -- let rpmbuild do it.
%patch2 -p1 -b .no-strip
# Preserve timestamp on man page.
%patch3 -p1 -b .preserve-timestamps
# Disable color output by default.
%patch4 -p1 -b .no-color-by-default

%build
make CFLAGS="$RPM_OPT_FLAGS" "CPPFLAGS=$(getconf LFS_CFLAGS)" %{?_smp_mlags}

%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT%{_bindir}
make BINDIR=$RPM_BUILD_ROOT%{_bindir} \
 MANDIR=$RPM_BUILD_ROOT%{_mandir}/man1 \
 install
chmod -x $RPM_BUILD_ROOT%{_mandir}/man1/tree.1

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root)
%{_bindir}/tree
%{_mandir}/man1/tree.1*
%doc README LICENSE

%changelog
   ...

最后编包:

$ rpmbuild -ba ~/rpmbuild/SPECS/tree.spec