1. 程式人生 > >使用rpmbuild 進行rpm 打包

使用rpmbuild 進行rpm 打包

1.安裝打包工具

Redhat 架構的 Linux 系統是通過 rpmbuild 命令來製作 RPM 安裝包,而 Fedora14 預設情況下 是沒有安裝 rpmbuild 命令,只好手動安裝。 進入 Fedora 14 中,開啟終端,輸入以下命令: $ su -# 切換 root 使用者 $ yum -y install rpm-build# 安裝 rpmbuild 安裝完畢後,切換回一般使用者。

2.準備工作

使用一般使用者在當前使用者的 home 目錄中新建 RPM build 目錄,如下所示: mkdir rpmbuild mkdir rpmbuild/BUILD mkdir rpmbuild/RPMS mkdir rpmbuild/RPMS/athlon mkdir rpmbuild/RPMS/i386 mkdir rpmbuild/RPMS/i486 mkdir rpmbuild/RPMS/i586 mkdir rpmbuild/RPMS/i686 mkdir rpmbuild/RPMS/noarch mkdir rpmbuild/SOURCES mkdir rpmbuild/SPECS mkdir rpmbuild/SRPMS 然後新建檔案.rpmmacros,內容如下: %_topdir /home/mutse/rpmbuild 提示:mutse為筆者的使用者名稱,操作時,請修改為讀者自己的使用者名稱。

3.編寫原始碼 請使用你喜歡的編輯器編寫hello.c檔案

如下: $ vi hello.c #include <stdio.h> int main() { printf(“Hello, Welcome to Fedora 14!\n”); return 0; } 然後編寫Makefile檔案,如下: # this is a makefile of demo hello.c SRC = hello.c hello: $(SRC) gcc $(SRC) -o hello clean: rm -f hello install: install -m 755 hello $(RPM_INSTALL_ROOT)/usr/local/bin/hello 提示:請不要忘記TAB鍵!

4.RPM安裝包製作

(1)生成tar.gz包 使用以下命令製作tar.gz包: $ cd .. $ tar -czvf hello-1.0.tar.gz hello-1.0 (2)編寫spcs檔案 Summary: Hello Fedora 14. Name: hello Version: 1.0 Release: 1 License: GPL Group:Applications/System Source:hello-1.0.tar.gz Url:http://mutse.blogbus.com Packager: mutse BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root %description This package will print Hello, welcome to Fedora 14. %prep %setup -q %build make %install rm -rf %{buildroot} mkdir -p %{buildroot}/usr/local/bin make install RPM_INSTALL_ROOT=%{buildroot} %files /usr/local/bin/hello %changelog * Mon Mar 14 2011 Mutse Young <
[email protected]
> 1.0 - build the program (3)將spec檔案和tar.gz包分別拷貝到相應的目錄中 操作命令如下: $ cp hello.spec ~/rpmbuild/SPECS $ cp hello-1.0.tar.gz ~/rpmbuild/SOURCES (4)打包 進入spec所在目錄,進行打包,操作命令如下: $ cd ~/rpmbuild/SPECS $ rpmbuild -ba hello.spec Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.u40tsy + umask 022 + cd /home/mutse/rpmbuild/BUILD + cd /home/mutse/rpmbuild/BUILD + rm -rf hello-1.0 + /bin/tar -xf - + /usr/bin/gzip -dc /home/mutse/rpmbuild/SOURCES/hello-1.0.tar.gz + STATUS=0 + ’[' 0 -ne 0 ']‘ + cd hello-1.0 + /bin/chmod -Rf a+rX,u+w,g-w,o-w . + exit 0 Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.mBXp1g + umask 022 + cd /home/mutse/rpmbuild/BUILD + cd hello-1.0 + make gcc hello.c -o hello + exit 0 Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.JPt8NZ + umask 022 + cd /home/mutse/rpmbuild/BUILD + cd hello-1.0 + rm -rf /home/mutse/rpmbuild/BUILDROOT/hello-1.0-1.i386 + mkdir -p /home/mutse/rpmbuild/BUILDROOT/hello-1.0-1.i386/usr/local/bin + make install RPM_INSTALL_ROOT=/home/mutse/rpmbuild/BUILDROOT/hello-1.0-1.i386 install -m 755 hello /home/mutse/rpmbuild/BUILDROOT/hello-1.0-1.i386/usr/local/bin/hello + /usr/lib/rpm/brp-compress + /usr/lib/rpm/brp-strip + /usr/lib/rpm/brp-strip-static-archive + /usr/lib/rpm/brp-strip-comment-note Processing files: hello-1.0-1.i386 Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 Requires: libc.so.6 libc.so.6(GLIBC_2.0) rtld(GNU_HASH) Checking for unpackaged file(s): /usr/lib/rpm/check-files /home/mutse/rpmbuild/BUILDROOT/hello-1.0-1.i386 Wrote: /home/mutse/rpmbuild/SRPMS/hello-1.0-1.src.rpm Wrote: /home/mutse/rpmbuild/RPMS/i386/hello-1.0-1.i386.rpm Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.bYvpzc + umask 022 + cd /home/mutse/rpmbuild/BUILD + cd hello-1.0 + /bin/rm -rf /home/mutse/rpmbuild/BUILDROOT/hello-1.0-1.i386 + exit 0 (5)驗證 若上述操作成功,原始碼包和安裝包分別儲存到~/rpmbuild/SRPMS和~/rpmbuild/RPMS/i386目錄中, 使用rpm命令進行驗證: $ cd ~/rpmbuild/RPM/i386 $ rpm -qpi hello-1.0-1.i386.rpm Name: helloRelocations: (not relocatable) Version : 1.0Vendor: (none) Release: 1Build Date: Mon 14 Mar 2011 12:42:50 AM HKT Install Date: (not installed) Build Host: fedora Group: Applications/SystemSource RPM: hello-1.0-1.src.rpm Size: 4592License: GPL Signature : (none) Packager: mutse URL: http://mutse.blogbus.com Summary: Hello Fedora 14. Description : This package will print Hello, welcome to Fedora 14. 切換到root使用者,進行安裝,操作如下: $ su - $ cd /home/mutse/rmpbuild/RPM/i386 [email protected] i386]# rpm -ivh hello-1.0-1.i386.rpm Preparing…########################################### [100%] package hello-1.0-1.i386 is already installed 在當前目錄下執行hello,如下: [[email protected] i386]# hello Hello, welcome to Fedora 14! 上述列印資訊證明rpm安裝製作成功。

參考資料:

【1】:鳥哥的linux私房菜 【2】:http://leedd.com/2010/03/linux-c-i18n-l10n-xgettext-msgfmt-rpmbuild/ 摘自: http://www.linuxeden.com/html/news/20110313/107593.html