一步步製作RPM包

本文根據馬哥教育視頻http://mageedu.blog.51cto.com/4265610/919986 總結而來,如果想仔細學習請看該視頻。

一.RPM製作步驟

我們在企業中有的軟件基本都是編譯的,我們每次安裝都得編譯,那怎麼辦呢?那就根據我們的需求製作RPM安裝包吧。先來說說基本布驟:

1.Planning what you want             計劃做什麼rpm包。軟件的?庫的?

2.Gathering the software to package  收集原材料,即收集源碼包

3.Patch the software as need         如果需要打補丁,收集補丁文件。此布驟不是必須

4.Outling any dependenies      確定依賴關係包

------------------  上述動作可由我們手動編譯一次軟件來確定  -------------------

5.Building RPMs                      開始動手製作RPM包

5.1 Set up the directory stucture 設定好目錄結構,我們在這些目錄中製作我們的RPM包,我們需要下列目錄

BUILD 源代碼解壓後的存放目錄

RPMS    製作完成後的RPM包存放目錄,裏面有與平臺相關的子目錄

SOURCES 收集的源材料,補丁的存放位置

SPECS   SPEC文件存放目錄

SRMPS   存放SRMPS生成的目錄

5.2 Place the Sources in the right directory   把源材料放到正確的位置

5.3 Create a spec file that tell rpmbuild command what to do 創建spec文件,這是綱領文件,rpmbuild命令根據spec文件來製作合適的rpm包

5.4 Build the source and binary RPMS 製作src或二進制rpm包

6.Test RPMS 測試製作的PRM包

7.Add signature for RPM  爲RPM包簽名

二.RPM包製作實例

我還是用連貫的話爲大家敘述一遍吧,我們首先確實我們要爲什麼做rpm包,通常我們是爲一些軟件,比如httpd,nginx等,然後去收集這些軟件包的源代碼,如果有需要的話也收集這些補丁文件,手動編譯安裝一下這個軟件(當然如果是不需要編譯的就不用演練了),確定依賴的軟件包,並記錄下來,下面開始準備製作tengine的PRM包吧:

1.建立一個普通用戶,有普通用戶來製作rpm,用root的可能會因爲代碼問題導致毀滅的後果

  1. useradd ibuler 
  2. su - ibuler 

2.確定我們在哪個目錄下製作RPM,通常這個目錄我們topdir,這個需要在宏配置文件中指定,這個配置文件稱爲macrofiles,它們通常爲 /usr/lib/rpm/macros:/usr/lib/rpm/macros.*:~/.rpmmacros,這個在rhel 5.8中可以通過rpmbuild --showrc | grep macrofiles  查看,6.3的我使用這個找不到,但使用是一樣的。你可以通過rpmbuild --showrc | grep topdir 查看你係統默認的工作車間 

  1. rpmbuild --showrc | grep topdir 
  2.  
  3. -14: _builddir  %{_topdir}/BUILD 
  4. -14: _buildrootdir  %{_topdir}/BUILDROOT 
  5. -14: _rpmdir    %{_topdir}/RPMS 
  6. -14: _sourcedir %{_topdir}/SOURCES 
  7. -14: _specdir   %{_topdir}/SPECS 
  8. -14: _srcrpmdir %{_topdir}/SRPMS 
  9. -14: _topdir    %{getenv:HOME}/rpmbuild 

我們還是自定義工作目錄(或車間)吧

  1. vi ~/.rpmmacros 
  2. %_topdir        /home/ibuler/rpmbuild    ##目錄可以自定義 
  3.  
  4. mkdir ~/rpmbuild  

3.在topdir下建立需要的目錄

  1. cd ~/rpmbuild  
  2. mkdir -pv {BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} 

4.把收集的源碼放到SOURCES下

  1. cp /tmp/tengine-1.4.2.tar.gz SOURCES  ##事先放好的

5.在SPECS下建立重要的spec文件

  1. cd SPECS 
  2. vi tengine.spec          ##內容見後講解,rhel6.3會自動生成模板 

6.用rpmbuild命令製作rpm包,rpmbuild命令會根據spec文件來生成rpm包 

  1. rpmbuild  
  2. -ba 既生成src.rpm又生成二進制rpm 
  3. -bs 只生成src的rpm 
  4. -bb 只生二進制的rpm 
  5. -bp 執行到pre 
  6. -bc 執行到 build段 
  7. -bi 執行install段 
  8. -bl 檢測有文件沒包含 

我們可以一步步試,先rpmbuild -bp ,再-bc 再-bi 如果沒問題,rpmbuild -ba 生成src包與二進制包吧

7.安裝測試有沒有問題,能否正常安裝運行,能否正常升級,卸載有沒有問題

root用戶測試安裝:

  1. cd /tmp
  2. cp /home/ibuler/rpmbuild/RPMS/x86_64/tengine-1.4.2-1.el6.x86_64.rpm /tmp  
  3. rpm -ivh tengine-1.4.2-1.el6.x86_64.rpm  ##測試安裝 
  4. rpm -e tengine                           ##測試卸載,如果版本號比原來的高,升級測試 

8.如果沒問題爲rpm包簽名吧,防止有人惡意更改    ##這個先不寫了,有點晚了,以後補上

到此整個流程完畢。下面來說說其中最最重要的spec的格式,先說最簡單的,最容易實現的

  1. vi tengine.spec 
  2.  
  3. ### 0.define section               #自定義宏段,這個不是必須的 
  4. ### %define nginx_user nginx       #這是我們自定義了一個宏,名字爲nginx_user值爲nginx,%{nginx_user}引用 
  5.  
  6. ### 1.The introduction section      #介紹區域段 
  7.  
  8. Name:           tengine            #名字爲tar包的名字 
  9. Version:        1.4.2              #版本號,一定要與tar包的一致哦 
  10. Release:        1%{?dist}          #釋出號,也就是第幾次製作rpm 
  11. Summary:        tengine from TaoBao  #軟件包簡介,最好不要超過50字符 
  12.  
  13. Group:          System Environment/Daemons  #組名,可以通過less /usr/share/doc/rpm-4.8.0/GROUPS 選擇合適組 
  14. License:        GPLv2                       #許可,GPL還是BSD等  
  15. URL:            http://laoguang.blog.51cto.com   #可以寫一個網址 
  16. Packager:       Laoguang <ibuler@qq.com> 
  17. Vendor:         TaoBao.com 
  18. Source0:        %{name}-%{version}.tar.gz   
  19. #定義用到的source,也就是你收集的,可以用宏來表示,也可以直接寫名字,上面定義的內容都可以像上面那樣引用 
  20. #patch0:            a.patch                 #如果需要補丁,依次寫 
  21. BuildRoot:      %_topdir/BUILDROOT         
  22. #這個是軟件make install 的測試安裝目錄,也就是測試中的根,我們不用默認的,我們自定義,
  23. #我們可以來觀察生成了哪此文件,方便寫file區域 
  24. BuildRequires:  gcc,make                           #製作過程中用到的軟件包 
  25. Requires:       pcre,pcre-devel,openssl,chkconfig  #軟件運行需要的軟件包,也可以指定最低版本如 bash >= 1.1.1 
  26. %description                                       #軟件包描述,盡情的寫吧 
  27. It is a Nginx from Taobao.                         #描述內容 
  28.  
  29. ###  2.The Prep section 準備階段,主要目的解壓source並cd進去 
  30.  
  31. %prep                                              #這個宏開始 
  32. %setup -q                                          #這個宏的作用靜默模式解壓並cd 
  33. #%patch0 -p1                                       #如果需要在這打補丁,依次寫 
  34.  
  35. ###  3.The Build Section 編譯製作階段,主要目的就是編譯 
  36. %build 
  37. ./configure \                                      #./configure 也可以用%configure來替換 
  38.   --prefix=/usr \                                  #下面的我想大家都很熟悉 
  39.   --sbin-path=/usr/sbin/nginx \ 
  40.   --conf-path=/etc/nginx/nginx.conf \ 
  41.   --error-log-path=/var/log/nginx/error.log \ 
  42.   --http-log-path=/var/log/nginx/access.log \ 
  43.   --pid-path=/var/run/nginx/nginx.pid  \ 
  44.   --lock-path=/var/lock/nginx.lock \ 
  45.   --user=nginx \ 
  46.   --group=nginx \ 
  47.   --with-http_ssl_module \ 
  48.   --with-http_flv_module \ 
  49.   --with-http_stub_status_module \ 
  50.   --with-http_gzip_static_module \ 
  51.   --http-client-body-temp-path=/var/tmp/nginx/client/ \ 
  52.   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ 
  53.   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ 
  54.   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ 
  55.   --http-scgi-temp-path=/var/tmp/nginx/scgi \ 
  56.   --with-pcre 
  57. make %{?_smp_mflags}          #make後面的意思是:如果就多處理器的話make時並行編譯 
  58.  
  59. ###  4.Install section  安裝階段 
  60. %install                                
  61. rm -rf %{buildroot}                #先刪除原來的安裝的,如果你不是第一次安裝的話 
  62. make install DESTDIR=%{buildroot} 
  63. #DESTDIR指定安裝的目錄,而不是真實的安裝目錄,%{buildroot}你應該知道是指的什麼了 
  64.  
  65. ###  4.1 scripts section #沒必要可以不寫 
  66. %pre        #rpm安裝前制行的腳本 
  67. if [ $1 == 1 ];then    #$1==1 代表的是第一次安裝,2代表是升級,0代表是卸載 
  68.         /usr/sbin/useradd -r nginx 2> /dev/null  ##其實這個腳本寫的不完整
  69. fi 
  70. %post       #安裝後執行的腳本 
  71.  
  72. %preun      #卸載前執行的腳本 
  73. if [ $1 == 0 ];then 
  74.         /usr/sbin/userdel -r nginx 2> /dev/null 
  75. fi 
  76. %postun     #卸載後執行的腳本 
  77.  
  78. ###  5.clean section 清理段,刪除buildroot 
  79.  
  80. %clean 
  81. rm -rf %{buildroot} 
  82.      
  83. ###  6.file section 要包含的文件 
  84. %files  
  85. %defattr (-,root,root,0755)   #設定默認權限,如果下面沒有指定權限,則繼承默認 
  86. /etc/           #下面的內容要根據你在%{rootbuild}下生成的來寫     
  87. /usr/ 
  88. /var/ 
  89.      
  90. ###  7.chagelog section  改變日誌段 
  91. %changelog 
  92. *  Fri Dec 29 2012 laoguang <ibuler@qq.com> - 1.0.14-1 
  93. - Initial version 

到此一個簡單的tengine RPM包製作好了。

三.RPM包製作拓展

下面我們來拓展一下,比如:我們想爲tengine增加控制腳本,可以通過start|stop控制,我們還想更換一下默認的首頁index.html,默認的fastcgi_params是不能直接連接php的,所以我們替換爲新的配置文件,我們也可以用設置好的nginx.conf替換原來的nginx.conf。基於上述步驟下面繼續

1.把修改後的首頁文件index.html,控制腳本init.nginx,fastCGI配置文件fastcgi_params,Nginx配置文件nginx.conf 放到SOURCES中 。 

  1. [ibuler@ng1 rpmbuild]$ ls SOURCES/ 
  2. fastcgi_params  index.html  init.nginx  nginx.conf  tengine-1.4.2.tar.gz 

2 編輯tengine.spec,修改

2.1 介紹區域的SOURCE0下增加如下

  1. Source0:        %{name}-%{version}.tar.gz 
  2. Source1:        index.html 
  3. Source2:        init.nginx 
  4. Source3:        fastcgi_params 
  5. Source4:        nginx.conf 

2.2 安裝區域增加如下

  1. make install DESTDIR=%{buildroot} 
  2. %{__install} -p -D %{SOURCE1} %{buildroot}/usr/html/index.html  #%{__install}這個宏代表install命令
  3. %{__install} -p -D -m 0755 %{SOURCE2} %{buildroot}/etc/rc.d/init.d/nginx 
  4. %{__install} -p -D %{SOURCE3} %{buildroot}/etc/nginx/fastcgi_params 
  5. %{__install} -p -D %{SOURCE4} %{buildroot}/etc/nginx/nginx.conf 

2.3 腳本區域增加如下

  1. %post 
  2. if [ $1 == 1 ];then 
  3.         /sbin/chkconfig --add nginx 
  4. fi 

2.4 %file區域增加如下

  1. %files 
  2. %defattr (-,root,root,0755) 
  3. /etc/ 
  4. /usr/ 
  5. /var/ 
  6. %config(noreplace) /etc/nginx/nginx.conf  #%config表明這是個配置文件noplace表明不能替換
  7. %config(noreplace) /etc/nginx/fastcgi_params 
  8. %doc /usr/html/index.html  #%doc表明這個是文檔
  9. %attr(0755,root,root) /etc/rc.d/init.d/nginx #%attr後面的是權限,屬主,屬組

3. 生成rpm文件測試

  1. rpmbuild -ba tengine.spec 

4. 安裝測試 

到此RPM包製作完畢,你可以根據你的需求製作RPM包吧。

四.RPM包簽名

1.生成GPG簽名密鑰,我用的是root用戶

  1. gpg --gen-key 
  2.  
  3. Your selection?1<Enter>  ##默認即可
  4. What keysize do you want? (2048) 1024<Enter>  ##密鑰長度
  5. Key is valid for? (0) 1y<Enter>  ##有效期
  6. Is this correct? (y/N) y<Enter>  ##確認
  7. Real name: LaoGuang<Enter>  ##密鑰名稱
  8. Email address: [email protected]<Enter>  ##郵件
  9. Comment: GPG-RPM-KEY<Enter>  ##備註
  10. Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O<ENTER> 
  11. Enter passphrase  OK <Enter>  ##使用空密碼,也可以輸入                              
  12. <Take this one anyway> <Enter> 
  13. <Take this one anyway> <Enter> 

有時可能因爲隨機數不夠導致卡在那裏,這時候你就yum 安裝幾個包組,馬上就夠了。

2.查看成生的密鑰

  1. [root@ng1 dev]# gpg --list-keys 
  2. /root/.gnupg/pubring.gpg 
  3. ------------------------ 
  4. pub   1024R/49C99488 2012-11-28 [expires: 2013-11-28] 
  5. uid                  LaoGuang (GPG-RPM-KEY) <ibuler@qq.com> 
  6. sub   1024R/69BA199D 2012-11-28 [expires: 2013-11-28] 

3.導出公鑰以供大家使用驗證

  1. gpg --export -a "LaoGuang" > RPM-GPG-KEY-LaoGuang 

4.編緝 .rpmmacros說明我們用哪一個密鑰加密,我們用root加密的那就在/root下編輯

  1. vi ~/.rpmmacros 
  2. %_gpg_name LaoGuang 

5.爲rpm包加簽名

  1.  rpm --addsign tengine-1.4.2-1.el6.x86_64.rpm  
  2. Enter pass phrase:   ##輸入密鑰
  3. Pass phrase is good. 
  4. tengine-1.4.2-1.el6.x86_64.rpm: 

到此簽名添加成功,下面來驗證

6.講剛纔導出的公鑰導入rpm中

  1. rpm --import RPM-GPG-KEY-LaoGuang 

7.驗證

  1. rpm --checksig tengine-1.4.2-1.el6.x86_64.rpm  
  2.  
  3. tengine-1.4.2-1.el6.x86_64.rpm: rsa sha1 (md5) pgp md5 OK 

到此整個過程完畢,你也試試吧

PS:覺的寫的不錯就贊一下吧,寫篇文檔不容易啊。

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