【Linux】【Services】【Package】rpm包制作

1. 概念

1.1. BUILD:源代码解压之后存放的位置

1.2. RPMS:制作完成之后的RPM包的存放位置,包括架构的子目录,比如x86,x86_64

1.3. SOURCES:所有的原材料都应该放在这,比如配置文件,源码包,补丁包

1.4. SPECS:纲领性文件,说明怎样安装,怎样卸载等等

1.5. SRPMS:src相关

1.6. BUILDROOT

2. spec文件详解:

变量以%开头,使用{}引用,使用一个下划线开头的表示本身环境的变量,两个下划线表示跟编译平台相关的命令

2.1. 简介段(The introduction section)

Summary: Text file format converter
Name: dos2unix
Version: 3.1
Release: 27.2%{?dist}
Group: Applications/Text
License: Freely distributable
Source: %{name}-%{version}.tar.bz2
Patch0: %{name}-%{version}.patch
Patch1: dos2unix-3.1-segfault.patch
Patch2: dos2unix-3.1-safeconv.patch
Patch3: dos2unix-3.1-manpage-update-57507.patch
Patch4: dos2unix-3.1-preserve-file-modes.patch
Patch5: dos2unix-3.1-tmppath.patch
Patch6: dos2unix-c-missing-arg.patch
Patch7: dos2unix-preserve-file-modes.patch

Buildroot: %{_tmppath}/%{name}-%{version}-root

%description
Dos2unix converts DOS or MAC text files to UNIX format.

  

2.2. 准备段(The prep section)

%prep
%setup -q
%patch0 -p1 -b .orig
%patch1 -p1 -b .segfault
%patch2 -p1 -b .safeconv
%patch3 -p1 -b .manpage-update-57507
%patch4 -p1 -b .preserve-file-modes
%patch5 -p1 -b .tmppath
%patch6 -p1 -b .c-missing-arg
%patch7 -p1 -b .preserve-file-modes

for I in *.[ch]; do
	sed -e 's,#endif.*,#endif,g' -e 's,#else.*,#else,g' $I > $I.new
	mv -f $I.new $I
done

  

2.3. 创建段(The build section)

%build
make clean
make CFLAGS="$RPM_OPT_FLAGS -D_LARGEFILE_SOURCE $(getconf LFS_CFLAGS)"
make link

  

2.4. 安装段(The install section)

%install
rm -rf $RPM_BUILD_ROOT

mkdir -p $RPM_BUILD_ROOT{%{_bindir},%{_mandir}/man1}
install -m755 dos2unix $RPM_BUILD_ROOT%{_bindir}
install -m755 mac2unix $RPM_BUILD_ROOT%{_bindir}
install -m444 dos2unix.1 $RPM_BUILD_ROOT%{_mandir}/man1
install -m444 mac2unix.1 $RPM_BUILD_ROOT%{_mandir}/man1


 

2.5. 文件段(The files section)

%files
%defattr(-,root,root,0755)
%doc COPYRIGHT
%{_bindir}/dos2unix
%{_bindir}/mac2unix
%{_mandir}/*/*

  

2.6. 清理段(The clean section)

%clean
rm -rf $RPM_BUILD_ROOT

  

2.7. 变更段(The change section)

%changelog
* Mon Feb  2 2009 Tim Waugh <twaugh@redhat.com> 3.1-27.2
- Preserve file modes (bug #437465).
- Applied patch to fix segfault with missing -c argument (bug #429231).

* Wed Jul 12 2006 Jesse Keating <jkeating@redhat.com> - 3.1-27.1
- rebuild
.
.
.
.
.

  

2.8. 脚本段

%pre #在安装之前
%post #在安装完成之后
%preun #在卸载之前
%postun #在卸载完成之后

  

3. rpmbuild命令详解:

rpmbuild --showrc:显示宏命令帮助文件

  macrofiles:表示宏命令的读取顺序

  _topdir:表示编译的根目录

  -bb:制作二进制格式的包

  -bs:制作源码包

  -ba:制作二进制和源码格式的包

  -bc:执行完%build段就不执行了

  -bp:执行玩%prep段就不执行了

  -bi:执行完%install段就不执行了

  -bl:检测有哪些文件在BUILDROOT过程中出错了(%file中的每个文件必须对应到%BUILDROOT的某一个路径下)

4. 修改用户默认的制作目录:在当前用户的家目录中添加.rpmmacros文件。

%_topdir  /home/ericz/rpmbuid

5. RPM制作过程:

5.1. 环境

OS:Red Hat Enterprise Linux Server release 7.2 (Maipo)

rpm-build:rpm-build-4.11.3-17.el7.x86_64

kernel:3.10.0-327.el7.x86_64

5.2. 安装前准备:

5.2.1. 安装rpm-build:yum install gcc gcc-c++ rpm-build -y

  

5.2.2. 创建新的用户用于制作包

[root@hctjtstn01 yum.repos.d]# useradd rpmbuild
[root@hctjtstn01 yum.repos.d]# echo "redhat"|passwd --stdin rpmbuild
Changing password for user rpmbuild.
passwd: all authentication tokens updated successfully.

  

5.2.3. 创建一个空的结构

[root@hctjtstn01 yum.repos.d]# su - rpmbuild
Last login: Mon Apr 10 20:05:08 CST 2017 on pts/0
[rpmbuild@hctjtstn01 ~]$ rpmbuild /dev/null
error: File /dev/null is not a regular file.
[rpmbuild@hctjtstn01 ~]$ tree rpmbuild/
rpmbuild/
├── BUILD
├── BUILDROOT
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS

6 directories, 0 files

  

5.2.4. 准备原材料

#本例中使用RH403实验手册中的例子,tar包和src包可以随便使用
[rpmbuild@hctjtstn01 ~]$ tar xvf hello-1.0.tar.gz 
hello-1.0/
hello-1.0/libhello.c
hello-1.0/hello.c
hello-1.0/Makefile
hello-1.0/README
hello-1.0/hello.conf
hello-1.0/libhello.h
hello-1.0/hello.1
[rpmbuild@hctjtstn01 ~]$ cp hello-1.0.tar.gz rpmbuild/SOURCES/
[rpmbuild@hctjtstn01 ~]$ ll dos2unix-3.1-27.2.el5.src.rpm 
-rw-r--r-- 1 rpmbuild rpmbuild 26924 Apr 10 20:16 dos2unix-3.1-27.2.el5.src.rpm
[rpmbuild@hctjtstn01 ~]$ rpm2cpio dos2unix-3.1-27.2.el5.src.rpm |cpio -id
72 blocks
#提取模板文件
[rpmbuild@hctjtstn01 ~]$ mv dos2unix.spec rpmbuild/SPECS/hello.spec

  

原文地址:https://www.cnblogs.com/demonzk/p/6691315.html