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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章