使用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
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 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 : hello Relocations: (not relocatable) Version : 1.0 Vendor: (none)
Release : 1 Build Date: Mon 14 Mar 2011 12:42:50 AM HKT Install Date: (not installed) Build Host: fedora
Group : Applications/System Source RPM: hello-1.0-1.src.rpm
Size : 4592 License: 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 root@fedora i386]# rpm -ivh hello-1.0-1.i386.rpm
Preparing… ########################################### [100%]
package hello-1.0-1.i386 is already installed
在当前目录下运行hello,如下:
[root@fedora 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

你可能感兴趣的:(Linux)