RPM 包的制作

1.一般rpm包的应用是
安装:rpm -ivh rpmname.rpm
卸载:rpm -e rpmname
升级:rpm -U rpmname.rpm
查询:rpm -qa | grep some name
校验:rpm -V

2.要制作一个RPM包,首先安装rpm-build软件,我是用yum安装的
安装好后在/usr/src/redhat/目录下面有几个子目录
BUILD:用来存放打包过程中的源文件
RPMS:保存打包好后的rpm格式二进制文件
SOURCES:打包是要用到的源文件和patch
SPECS:保存spce文件,这个文件是关键点
SRPMS:保持打包好后的rpm格式源文件

3.spec文件规范
3.1文件头
 Summary:
  用一句话概括该软件包尽量多的信息。
 Name:
  软件包的名字,最终RPM软件包是用该名字与版本号,释出号及体系号来命名软件包的。name可以作变量放在后面用的%{name}
  Version:
  软件版本号。仅当软件包比以前有较大改变时才增加版本号。
  Release:
  软件包释出号。一般我们对该软件包做了一些小的补丁的时候就应该把释出号加1。
  Vendor:
  软件开发者的名字。
  Copyright:
  软件包所采用的版权规则。具体有:GPL(自由软件),BSD,MIT,Public Domain(公共域),Distributable(贡献),commercial(商业),Share(共享)等,一般的开发都写GPL。
  Group:
  软件包所属类别,具体类别有:
  Amusements/Games (娱乐/游戏)
  Amusements/Graphics(娱乐/图形)
  Applications/Archiving (应用/文档)
  Applications/Communications(应用/通讯)
  Applications/Databases (应用/数据库)
  Applications/Editors (应用/编辑器)
  Applications/Emulators (应用/仿真器)
  Applications/Engineering (应用/工程)
  Applications/File (应用/文件)
  Applications/Internet (应用/因特网)
  Applications/Multimedia(应用/多媒体)
  Applications/Productivity (应用/产品)
  Applications/Publishing(应用/印刷)
  Applications/System(应用/系统)
  Applications/Text (应用/文本)
  Development/Debuggers (开发/调试器)
  Development/Languages (开发/语言)
  Development/Libraries (开发/函数库)
  Development/System (开发/系统)
  Development/Tools (开发/工具)
  Documentation (文档)
  System Environment/Base(系统环境/基础)
  System Environment/Daemons (系统环境/守护)
  System Environment/Kernel (系统环境/内核)
  System Environment/Libraries (系统环境/函数库)
  System Environment/Shells (系统环境/接口)
  User Interface/Desktops(用户界面/桌面)
  User Interface/X (用户界面/X窗口)
  User Interface/X Hardware Support (用户界面/X硬件支持)
  Source:
  源程序软件包的名字。如 stardict-2.0.tar.gz。
 可以定义Source0,Source1...
  %description:
  软件包详细说明,可写在多个行上。
3.2 %prep段
3.3 %build段
3.4 %install段
3.5 %file段
 本段是文件段,用于定义软件包所包含的文件,分为三类--说明文档(doc),配置文件(config)及执行程序。
 这些文件是要打包到rpm包中的,一定要指定的,而且一定要用绝对路径,用/开头的,如果用变量,或用现对路径都不行的。编译不会通过的。
 在%file段中在 %files 中,可以使用一次 %defattr(permissions,user,group) 来定义以后所列文件的缺省许可权、所有者和组。
 可以用 %attr(permissions,user,group) 覆盖个别文件的所有者和许可权。
 可以通过在行中添加 %doc 或 %config 来标记文件。
 %doc 告诉 RPM 这是一个文档文件,因此如果用户安装软件包时使用 --excludedocs ,将不安装该文件。
 也可以在 %doc 下不带路径列出文件名,RPM 会在构建目录下查找这些文件并在 RPM 文件中包括它们,并把它们安装到 /usr/share/doc/%{name}-%{version} 。以 %doc 的形式包括 README 和 ChangeLog 这样的文件是个好主意。

3.6 %changelog
 本段是修改日志段。你可以将软件的每次修改记录到这里,保存到发布的软件包中,以便查询之用。每一个修改日志都有这样一种格式:第一行是:* 星期 月日 年 修改人电子信箱。其中:星期、月份均用英文形式的前3个字母,用中文会报错。接下来的行写的是修改了什么地方,可写多行。一般以减号开始,便于后续的查阅。

4.打包
 1) 只生成二进制格式的rpm包
  rpmbuild -bb xxx.spec
  在执行 %prep, %build 和 %install 之后,生成的文件会在刚才建立的RPM目录下存在。
  2)只生成src格式的rpm包
  rpmbuild -bs xxx.spec
  生成的文件会在刚才建立的SRPM目录下存在。
  3) 只需要生成完整的源文件
  rpmbuild -bp xxx.spec
 执行 spec 文件的 "%prep" 阶段。通常,这会解包源代码并应用补丁
  源文件存在目录BUILD下。这个命令的作用就是把tar包解开然后把所有的补丁文件合并而生成一个完整的具最新功能的源文件。
  4) 完全打包
  rpmbuild -ba xxx.spec
  在执行 %prep, %build 和 %install 之后,产生以上3个过程分别生成的包。存放在相应的目录下。
  软件包制作完成后可用rpm命令查询,看看效果。如果不满意的话可以再次修改软件包描述文件,重新运行以上命令产生新的RPM软件包。
    还有其他 bi:执行 spec 文件的 "%install" 阶段 (在执行了 %prep 和 %build 阶段之后)。这通常等价于执行了一次 "make install"
       bl:执行一次 "列表检查"。spec 文件的 "%files" 段落中的宏被扩展,检测是否每个文件都存在。
    等等

  
5.例子
#
# Spec file for Event2.0
#
#!/bin/sh
Name: event
Summary: A software
Version: 2.0
Release: 1
Vendor:
License: Commercial
Group: Applications/Communications
Source0: %{name}-%{version}-%{release}.tar.gz
URL:
%description

%define event_dir /home/event
%define backup_dir %{event_dir}/backup
%files
%defattr(-,root,root)
/usr/src/redhat/SOURCES/event-2.0-1.tar.gz
%prep
if [ -f %{event_dir}/.bash_profile ] ;
then
  if [ ! -d /var/tmp/event-root ] ; then
    mkdir /var/tmp/event-root
  fi
  cp -f %{event_dir}/.bash_profile /var/tmp/event-root
fi
%post
useradd -m -p '$1$HkH6xLeU$8TpyM1A.PnNacdMTMEhQl0' event
if [ ! -d %{event_dir} ] ; then
  mkdir %{event_dir}
  chown event:event %{event_dir}
fi

if [ -f %{event_dir}/%{Source0} ] ;
then
  if [ ! -d %{backup_dir} ] ; then
    echo "Will create a backup folder for backup event-2.0-1.tar.gz...."
    mkdir %{backup_dir}
  fi
  mv -f %{event_dir}/%{Source0} %{backup_dir}/%{Source0}
fi

if [ -d %{event_dir}/3rd ] ; then
  if [ ! -d %{backup_dir} ] ; then
    echo "Will create a backup folder for backup 3rd...."
    mkdir %{backup_dir}
  fi
  if [ -d %{backup_dir}/3rd ] ; then
    rm -rf %{backup_dir}/3rd
  fi
  mv -f %{event_dir}/3rd %{backup_dir}
fi

if [ -d %{event_dir}/event ] ; then
  if [ ! -d %{backup_dir} ] ; then
    echo "Will create a backup folder for event...."
    mkdir %{backup_dir}
  fi
  if [ -d %{backup_dir}/event ] ; then
    rm -rf %{backup_dir}/event
  fi
  mv -f %{event_dir}/event %{backup_dir}
fi

if [ -d %{event_dir}/deploy ] ; then
  if [ ! -d %{backup_dir} ] ; then
    echo "Will create a backup folder for backup deploy...."
    mkdir %{backup_dir}
  fi
  if [ -d %{backup_dir}/deploy ] ; then
    rm -rf %{backup_dir}/deploy
  fi
  mv -f %{event_dir}/deploy %{backup_dir}
fi

if [ -f /var/tmp/event-root/.bash_profile ]; then
  mv -f /var/tmp/event-root/.bash_profile %{backup_dir}
  rm -rf /var/tmp/event-root
fi

cp /usr/src/redhat/SOURCES/event-2.0-1.tar.gz %{event_dir}
tar -xvf %{event_dir}/event-2.0-1.tar.gz -C %{event_dir}
#if [ -d %{event-dir}/3rd ] ; then
rm -rf %{event_dir}/3rd
rm -rf %{event_dir}/event
mv -f %{event_dir}/deploy/3rd %{event_dir}
mv -f %{event_dir}/deploy/event  %{event_dir}
mv -f %{event_dir}/deploy/.bash_profile  %{event_dir}
chown -R event:event /home/event/*
rm -rf /home/event/deploy
rm -r /home/event/event-2.0-1.tar.gz
%clean


6.参考

http://www-128.ibm.com/developerworks/cn/linux/management/package/rpm/part1/index.html

http://www-128.ibm.com/developerworks/cn/linux/management/package/rpm/part2/index.html

http://www-128.ibm.com/developerworks/cn/linux/management/package/rpm/part3/index.html

发布了5 篇原创文章 · 获赞 0 · 访问量 2518
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章