以libfuse为例介绍rpm打包工具rpmbuild的使用和SPEC文件的编写

一、rpmbuild命令的安装

 yum install rpm-build

二、用法

rpmbuild -bb XXXX.spec或者rpmbuild -ba XXX.tar.gz

三、目录概述

rpmbuild在运行后会在用户目录下生成一个rpmbuild的文件夹:

[root@localhost rpmbuild]# ls ~/rpmbuild/
BUILD BUILDROOT RPMS SOURCES SPECS SRPMS


有四个目录:
BUILD:你要打包的文件将会在这里编译;
BUILDROOT:在虚拟安装(make install)的目录;
RPMS:存放生成的二进制的rpm包,也就是我们普通用的那种RPM包;
SOURCES:你要编译的源码包会被copy到这里;
SPECS:你执行的spec文件会被copy到这里;
SRPMS:存放的是rpm源码包,源码包里只有一个文件,就是你的XXXX.tar.gz

四、SPEC文件的编写

vim一个空文件,可以看到rpmbuild已经帮我们生成了一个格式:

[root@localhost packages]# vi aaa.spec 
Name:
Version:
Release:        1%{?dist}
Summary:

Group:
License:
URL:
Source0:

BuildRequires:
Requires:

%description


%prep
%setup -q


%build
%configure
make %{?_smp_mflags}


%install
make install DESTDIR=%{buildroot}


%files
%doc



%changelog
View Code

相对应的关键字介绍如下:

Name:软件包的名称 %{name}
Version:软件的实际版本号
Release:发布序列号 %{release}
比如:moosefs-3.0.81-1.tar.gz
Name:moosefs; 版本号:3.0.81 Release:1

Summary:软件包的内容概要
Group:软件分组,建议使用标准分组
License:软件授权方式
URL:软件的主页
Source0:源代码包地址,多个源可以用"%{source1}""%{source1}"等引用
Requires:该rpm包所依赖的软件包名称,可以用>=或<=表示大于或小于某一特定版本
PreReq、Requires(pre)、Requires(post)、Requires(preun)、Requires(postun)、BuildRequires等都是针对不同打包阶段的依赖指定的
%description:软件的详细说明
%prep:表示要开始编译软件了,通常包括%setup和%patch两个命令
%setup -q: 解压源文件程序
%patch:把补丁放到源码中
%build表示开始构建
%configure 相当于"./configure",也可以指定参数
make %{?_smp_mflags} 就是"make"
%install:表示开始安装(其实是安装到虚拟目录,一般我们指定是BUILDROOT)
make install DESTDIR=%{buildroot}就是"make install"
%files:用来指定要把那些文件打包到rpm包中
%defattr(644,root,root,755)用来设定文件的默认权限
%doc:这个非常重要,只有写在这下面的文件,才会被打包到rpm中去

如果目录不对都会提醒你File not found by glob: /root/rpmbuild/BUILDROOT/XXXXX
其实在打包的时候%doc可以先不用写,执行过后到 /root/rpmbuild/BUILDROOT/目录下看都安装了什么,再写;
%changelog:变更日志,有些时候回提醒你前面加"*"

下面是我给fuse-2.9.7写的spec文件:

%define _relname .devel

Summary:    File System in Userspace (FUSE) utilities
Name:        fuse
Version:    2.9.7
Release:    1%{?_reasename}
License:    commercial
Group:      System Environment/Base
Source0:    %{name}-%{version}.tar.gz 
BuildRoot:  %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
Requires:   kernel >= 2.6.14

%description 
libfuse

%prep
%setup -q

%clean
rm -rf $RPM_BUILD_ROOT

%build
%configure
make

%install
make install DESTDIR=$RPM_BUILD_ROOT 

%files
%defattr(644,root,root,755) 
%doc
%{_includedir}/*
%{_includedir}/fuse/*
%{_libdir}/*
%{_libdir}/pkgconfig/*
%{_bindir}/*
%{_mandir}/man1/*
%{_mandir}/man8/*
/sbin/*
%{_sysconfdir}/init.d/*
%{_sysconfdir}/udev/*
%{_sysconfdir}/udev/rules.d/*

%changelog
*Mon Feb 13 2006 Peter Lemenkov <lemenkov newmail ru> - 2.9.1-1
*Proper udev rule
*Mon Feb 13 2006 Peter Lemenkov <lemenkov newmail ru> - 2.9.1-1
*Added missing requires
 
View Code

五、打包之后的疑问和解决方法

1.那么打包之后的文件是不是我们想要的呢?

答:可以用"rpm --prefix"去安装,结果:error: package fuse is not relocatable,用rpm命令查看包信息,原来被禁掉了。

[root@localhost x86_64]# rpm -ivh fuse-2.9.7-1.x86_64.rpm --prefix=/usr/local/fuse3
error: package fuse is not relocatable
[root@localhost x86_64]# rpm -qpi fuse-2.9.7-1.x86_64.rpm 
Name : fuse
Version : 2.9.7
Release : 1
Architecture: x86_64
Install Date: (not installed)
Group : System Environment/Base
Size : 833112
License : commercial
Signature : (none)
Source RPM : fuse-2.9.7-1.src.rpm
Build Date : Wed Aug 10 20:06:19 2016
Build Host : localhost
Relocations : (not relocatable)
Summary : File System in Userspace (FUSE) utilities
Description :
libfuse

2.怎么样不安装rpm就知道里面有什么文件呢?

#先看我们之前说的SRPM中打的源码包是什么样子的:
[root@localhost SRPMS]# pwd /root/rpmbuild/SRPMS [root@localhost SRPMS]# rpm -qlp fuse-2.9.7-1.src.rpm fuse-2.9.7.tar.gz libfuse.spec #再看我们最终想要的二进制的rpm包是神样子的: [root@localhost x86_64]# pwd /root/rpmbuild/RPMS/x86_64 [root@localhost x86_64]# rpm -qlp fuse-2.9.7-1.x86_64.rpm /etc/init.d/fuse /etc/udev/rules.d /etc/udev/rules.d/99-fuse.rules /sbin/mount.fuse /usr/bin/fusermount /usr/bin/ulockmgr_server /usr/include/fuse

3.怎么样不安装rpm包就可以提取rpm包里的文件:
http://blog.chinaunix.net/uid-33787-id-3331183.html

 4.怎样确定系统多出来的命令就是我们刚装上的呢?

[root@localhost packages]# rpm -qi fuse-libs
Name        : fuse-libs
Version     : 2.9.2
Release     : 6.el7
Architecture: x86_64
Install Date: Wed Aug 10 10:14:39 2016
Group       : System Environment/Libraries
Size        : 293074
License     : LGPLv2+
Signature   : RSA/SHA256, Wed Nov 25 22:30:48 2015, Key ID 24c6a8a7f4a80eb5
Source RPM  : fuse-2.9.2-6.el7.src.rpm
Build Date  : Fri Nov 20 13:49:56 2015
原文地址:https://www.cnblogs.com/bugutian/p/5758323.html