rpm打包-helloword

示例

这里用官方文档中的例子来操作一遍。 下面演示 GNU“Hello World” 项目的打包过程。

下载源码

cd ~/rpmbuild/SOURCES
wget http://ftp.gnu.org/gnu/hello/hello-2.10.tar.gz

编辑SPEC文件

编辑SPEC文件,Emacs 和 vi 的最新版本有 .spec 文件编辑模式,它会在创建新文件时打开一个类似的模板。所以可使用以下命令来自动使用模板文件:

cd ~/rpmbuild/SPECS
vim hello.spec

Name:     hello
Version:  2.10
Release:  1%{?dist}
Summary:  The "Hello World" program from GNU
Summary(zh_CN):  GNU "Hello World" 程序
License:  GPLv3+
URL:      http://ftp.gnu.org/gnu/hello    
Source0:  http://ftp.gnu.org/gnu/hello/%{name}-%{version}.tar.gz
%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" 程序, 包含 FOSS 项目所需的所有部分, 包括配置, 构建, 国际化, 帮助文件等.
%prep
%setup -q
%build
%configure
make %{?_smp_mflags}
%install
make install DESTDIR=%{buildroot}
%files
%doc
%changelog
* Sun Dec 4 2016 Your Name <youremail@xxx.xxx> - 2.10-1
- Update to 2.10
* Sat Dec 3 2016 Your Name <youremail@xxx.xxx> - 2.9-1
- Update to 2.9
  1. Name 标签就是软件名
  2. Version 标签为版本号
  3. Release 是发布编号。
  4. Summary 标签是简要说明,英文的话第一个字母应大写,以避免 rpmlint 工具(打包检查工具)警告。
  5. License 标签说明软件包的协议版本,审查软件的 License 状态是打包者的职责,这可以通过检查源码或 LICENSE 文件,或与作者沟通来完成。
  6. Group 标签过去用于按照 /usr/share/doc/rpm-/GROUPS 分类软件包。目前该标记已丢弃,vim的模板还有这一条,删掉即可,不过添加该标记也不会有任何影响。
  7. %changelog 标签应包含每个 Release 所做的更改日志,尤其应包含上游的安全/漏洞补丁的说明。Changelog 日志可使用 rpm --changelog -q 查询,通过查询可得知已安装的软件是否包含指定漏洞和安全补丁。%changelog 条应包含版本字符串,以避免 rpmlint 工具警告。
  8. 多行的部分,如 %changelog 或 %description 由指令下一行开始,空行结束
  9. 一些不需要的行 (如 BuildRequires 和 Requires) 可使用 ‘#’ 注释。
  10. %prep、%build、%install、%file暂时用默认的,未做任何修改。
构建RPM包

命令:

rpmbuild -ba hello.spec

包含要安装的文件

不过上边的命令执行失败了。
命令执行后,提示并列出未打包的文件

RPM build errors:
    Installed (but unpackaged) file(s) found:
   /usr/bin/hello
   /usr/share/info/dir
   /usr/share/info/hello.info.gz
   /usr/share/locale/bg/LC_MESSAGES/hello.mo
   /usr/share/locale/ca/LC_MESSAGES/hello.mo
   ...

原因%files 需要声明
那些需要安装在系统中的文件,我们需要在 %files 中声明它们,这样rpmbuild命令才知道哪些文件是要安装的。
注意
不要使用形如 /usr/bin/ 的硬编码, 应使用类似 %{_bindir}/hello 这样的宏来替代。手册页应在 %doc 中声明 : %doc %{_mandir}/man1/hello.1.*。

由于示例的程序使用了翻译和国际化,因此会看到很多未声明的 i18 文件。 使用 推荐方法 来声明它们:

  • 包含程序安装的相关文件
  • 查找 %install 中的语言文件: %find_lang %{name}
  • 添加编译依赖: BuildRequires: gettext
  • 声明找到的文件: %files -f %{name}.lang

%files部分的内容为:

%files -f %{name}.lang
%doc AUTHORS ChangeLog NEWS README THANKS TODO
%license COPYING
%{_mandir}/man1/hello.1.*
%{_infodir}/hello.info.*
%{_bindir}/hello
info文件的处理

如果程序使用 GNU info 文件,你需要确保安装和卸载软件包,不影响系统中的其他软件,按以下步骤操作:

  • 在 %install 中添加删除 ‘dir’ 文件的命令: rm -f %{buildroot}/%{_infodir}/dir
  • 在安装后和卸载前添加依赖 Requires(post): info 和 Requires(preun): info
  • 添加以下安装脚本(在%install和%files中间即可,分别对应安装后和卸载前的阶段,详见后边内容):
%post
/sbin/install-info %{_infodir}/%{name}.info %{_infodir}/dir || :
%preun
if [ $1 = 0 ] ; then
/sbin/install-info --delete %{_infodir}/%{name}.info %{_infodir}/dir || :
fi

看看各个目录里边的东西

  • %_sourcedir下边仍然是源码的压缩包;

  • %_builddir下边是源码解压出来的文件夹hello-2.10及其下边的所有文件;

  • %_buildrootdir下边是一个名为“hello-2.10-1.nd7.mips64el”的文件夹(那么生成的RPM包的完整名称也是**{Name}-{Version}-{Release}.{Arch}.rpm **),这个文件夹下边有“usr”文件夹,其下还有“bin”、“lib”、“share”、“src”这几个文件夹,这里的目录结构和之后安装的各个文件和文件夹的位置是一致的。这里要注意的是,“usr”所在的“/", 这里“hello-2.10-1.nd7.mips64el”文件夹就是指定的根目录(“/”),
    注意

  • hello-2.10-1.nd7.mips64el用宏表示就是%{buildroot},有的地方也用$RPM_BUILD_ROOT 代替 %{buildroot},不过与%{_buildrootdir}不是一个概念,%{_buildrootdir}是BUILDROOT,注意区分。

  • 在失败是进行查看,因为成功打包之后有些文件夹(比如%_builddir和%_buildrootdir)内的内容就会被清理掉了,不过也可以在%build和%install阶段的时候把这俩文件夹内的东西tree一下或者干脆复制到其他地方再看也行。

本示例最终的完整SPEC
Name:     hello
Version:  2.10
Release:  1%{?dist}
Summary:  The "Hello World" program from GNU
Summary(zh_CN):  GNU "Hello World" 程序
License:  GPLv3+
URL:      http://ftp.gnu.org/gnu/hello
Source0:  http://ftp.gnu.org/gnu/hello/%{name}-%{version}.tar.gz

BuildRequires:  gettext
Requires(post): info
Requires(preun): info

%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" 程序, 包含 FOSS 项目所需的所有部分, 包括配置, 构建, 国际化, 帮助文件等.

%prep
%setup -q

%build
%configure
make %{?_smp_mflags}

%install
make install DESTDIR=%{buildroot}
%find_lang %{name}
rm -f %{buildroot}/%{_infodir}/dir

%post
/sbin/install-info %{_infodir}/%{name}.info %{_infodir}/dir || :

%preun
if [ $1 = 0 ] ; then
/sbin/install-info --delete %{_infodir}/%{name}.info %{_infodir}/dir || :
fi

%files -f %{name}.lang
%doc AUTHORS ChangeLog NEWS README THANKS TODO
%license COPYING
%{_mandir}/man1/hello.1.*
%{_infodir}/hello.info.*
%{_bindir}/hello

%changelog
* Sun Dec 4 2016 Your Name <youremail@xxx.xxx> - 2.10-1
- Update to 2.10
* Sat Dec 3 2016 Your Name <youremail@xxx.xxx> - 2.9-1
- Update to 2.9

执行

rpmbuild -ba hello.spec

结果

[loongson@localhost rpmbuild]$ tree ~/rpmbuild/*RPMS
/home/loongson/rpmbuild/RPMS
└── mips64el
    ├── hello-2.10-1.nd7.mips64el.rpm
    └── hello-debuginfo-2.10-1.nd7.mips64el.rpm
/home/loongson/rpmbuild/SRPMS
└── hello-2.10-1.nd7.src.rpm

1 directory, 3 files
[loongson@localhost rpmbuild]$ 

在RPMS文件夹下生成了RPM包,在mips64el下,表示所应用的架构,由于没有指定arch为noarch,所以默认用本机架构。在SRPMS文件夹下生产了源码包;有些人喜欢在装软件的时候从源码开始安装,因为更能贴合本机的物理情况.

运行安装RPM

rpm -ivh hello-2.10-1.nd7.mips64el.rpm

验证

[loongson@localhost rpmbuild]$ hello 
世界你好!
[loongson@localhost rpmbuild]$ which hello
/usr/bin/hello
[loongson@localhost rpmbuild]$ rpm -qa | grep hello
hello-2.10-1.nd7.mips64el
[loongson@localhost rpmbuild]$
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值