【需求】
接触那么久的Linux,还没有试验过怎么打rpm包
【过程】
1、rpm打包准备工作
系统默认的rpmbuild的相关路径
如果想修改这个目录,怎么修改呢?
在/data1/guosong/rpmbuild目录下创建如下目录BUILD BUILDROOT code RPMS SOURCES SPECS SRPMS
还必须修改~/.rpmmacros
%_topdir /data1/guosong/rpmbuild
2、rpm打包过程
打包helloworld需要程序sources文件,spec文件
*编写hellword源文件(hello.c)
#include <stdio.h> int main(){ printf("Hello, World!\n"); return 0; }
*打包成tar文件
创建hello-0.1目录,将hello.c挪到hello-0.1目录中
将tar包转移到/data1/guosong/rpmbuild/SOURCES目录下
*在/data1/guosong/rpmbuild/SPECS目录下编写spec文件
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root Summary: hello world rpm package Name: hello Version: 0.1 Release: 1
#文件名必须保持一致 Source: hello-0.1.tar.gz License: GPL Packager: guosong Group: Application %description This is a simple example for rpm package! %prep %setup -q %build gcc -o hello hello.c %install mkdir -p %{buildroot}/usr/local/bin install -m 755 hello %{buildroot}/usr/local/bin/hello %files # 这里的路径是在buildroot虚拟根基础上的,即%{buildroot}/%{name}-%{version}-%{release}-root/usr/local/bin/hello /usr/local/bin/hello
spec文件解释说明:
http://www.rpm.org/max-rpm/s1-rpm-build-creating-spec-file.html
http://cpbest.blog.163.com/blog/static/412415192009917477295/
*制作rpm包
rpmbuild -bb hello.spec
输出的日志如下:
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.72022 + umask 022 + cd /data1/guosong/rpmbuild/BUILD + LANG=C + export LANG + unset DISPLAY + cd /data1/guosong/rpmbuild/BUILD + rm -rf hello-0.1 + /bin/gzip -dc /data1/guosong/rpmbuild/SOURCES/hello-0.1.tar.gz + tar -xf - + STATUS=0 + '[' 0 -ne 0 ']' + cd hello-0.1 ++ /usr/bin/id -u + '[' 0 = 0 ']' + /bin/chown -Rhf root . ++ /usr/bin/id -u + '[' 0 = 0 ']' + /bin/chgrp -Rhf root . + /bin/chmod -Rf a+rX,u+w,g-w,o-w . + exit 0 Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.95084 + umask 022 + cd /data1/guosong/rpmbuild/BUILD + cd hello-0.1 + LANG=C + export LANG + unset DISPLAY + gcc -o hello hello.c + exit 0 Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.95084 + umask 022 + cd /data1/guosong/rpmbuild/BUILD + cd hello-0.1 + LANG=C + export LANG + unset DISPLAY + mkdir -p /var/tmp/hello-0.1-1-root/usr/local/bin + install -m 755 hello /var/tmp/hello-0.1-1-root/usr/local/bin/hello + /usr/lib/rpm/find-debuginfo.sh /data1/guosong/rpmbuild/BUILD/hello-0.1 + /usr/lib/rpm/redhat/brp-compress + /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip + /usr/lib/rpm/redhat/brp-strip-comment-note /usr/bin/strip /usr/bin/objdump + /usr/lib/rpm/brp-python-bytecompile + /usr/lib/rpm/redhat/brp-java-repack-jars Processing files: hello-0.1-1 Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 Requires: libc.so.6()(64bit) libc.so.6(GLIBC_2.2.5)(64bit) rtld(GNU_HASH) Processing files: hello-debuginfo-0.1-1 Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 Checking for unpackaged file(s): /usr/lib/rpm/check-files /var/tmp/hello-0.1-1-root Wrote: /data1/guosong/rpmbuild/RPMS/x86_64/hello-0.1-1.x86_64.rpm Wrote: /data1/guosong/rpmbuild/RPMS/x86_64/hello-debuginfo-0.1-1.x86_64.rpm
*测试安装
[root@typhoeus79 x86_64]# rpm -qps hello-0.1-1.x86_64.rpm normal /usr/local/bin/hello
[root@typhoeus79 x86_64]# rpm -ivh hello-0.1-1.x86_64.rpm Preparing... ########################################### [100%] package hello-0.1-1.x86_64 is already installed
说明已经安装过了,重新删除再次测试安装
[root@typhoeus79 x86_64]# rpm -e hello [root@typhoeus79 x86_64]# echo $? 0 [root@typhoeus79 x86_64]# hello bash: /usr/local/bin/hello: No such file or directory [root@typhoeus79 x86_64]# rpm -ivh hello-0.1-1.x86_64.rpm Preparing... ########################################### [100%] 1:hello ########################################### [100%] [root@typhoeus79 x86_64]# hello Hello, World!
【参考文献】
http://blog.csdn.net/king_on/article/details/7169384
http://www.thegeekstuff.com/2011/08/yum-command-examples/
http://wiki.centos.org/HowTos/SetupRpmBuildEnvironment
http://www.rpm.org/max-rpm/s1-rpm-build-creating-spec-file.html
http://cpbest.blog.163.com/blog/static/412415192009917477295/