首页 > 代码库 > 使用spec与fpm 2种方式进行rpm打包

使用spec与fpm 2种方式进行rpm打包

最近分享zabbix与ansible好多,大家看烦了,我也没啥存货了,现在就介绍一下如何进行rpm打包吧。

rpm打包有2个,一个是自己写spec,另外要给是使用ruby的fpm,我个人比较倾向于第二种fpm,这个不需要写spec文件,特简单,不过为了让大家看明白,我分别介绍一下这2款软件是如何进行rpm打包的。

目录

一、使用spec构建rpm

二、使用fpm构建rpm

 

一、使用spec构建rpm

在构架rpm包的时候,需要经过3个过程

1、解压源码包

2、编译环境configure

3、make

然后再使用check install做rpm包

同时如果在安装rpm包的时候,你想查看安装过程可以使用,rpm -qi --scriptsname.rpm查看

如果在构架脚本或者其他非源码包安装的时候,可以使用rpmbild

1、把脚本目录给tar

2、构架spec

3、rpmbuild

注意的完成最终的工作了。请使用根目录用户身份安装rpmbuild命令。在Red Hat中,请用yum install rpm-build完成这项工作。接下来,请确保所有的文件都呆在正确的地方。Tar存档文件必须位于创建软件包的当前用户帐户的~/rpmbuild /SOURCES 目录下,demo.spec文件必须位于~/rpmbuild/SPECS目录下。现在你可以用rpmbuild -ba test.spec命令来创建软件包了。这会在~/rpmbuild/RPMS目录下创建你的RPM文件。

可以参考

http://www.linuxidc.com/Linux/2012-05/60278.htm

http://blog.csdn.net/cybertan/article/details/5972935

我做的测试是创建一个test目录,里有个test.log文件,内容是this is test file

然后给这个文件压缩成tar包,并存放到~/rpmbuild /SOURCES

之后再做个test.spec,内容如下

root@client:~/rpmbuild/SPECS#cat test.spec 
Summary:  Test package for dl528888 blog
Name:     test
Version:  1.0.0
Release:  1
License:  GPL
Group:    test
Source:   test.tar.gz
BuildRoot:%{_tmppath}/%{name}-%{version}-%{release}-root
Url:      http://dl528888.blog.51cto.com
Packager: Linuxing
Prefix:   %{_prefix}
Prefix:   %{_sysconfdir}
%define   userpath /tmp/demo
 
%description
Justa test rpm suite.
 
%prep
%setup-c
%install
install -d $RPM_BUILD_ROOT%{userpath}
cp-a %{name}* $RPM_BUILD_ROOT%{userpath}
 
%clean
rm-rf $RPM_BUILD_ROOT
rm-rf $RPM_BUILD_DIR/%{name}-%{version}
 
%files
%defattr(-,root,root)
%{userpath}

然后再对这个test.spec做安装rpm包,使用rpmbild-ba test.spec,之后就能在~/rpmbuild /RPMS目录下创建你的RPM文件。

二、使用ruby的fpm构建rpm包

1、centos安装

yum install rpm-build
gem install fpm

2、ubuntu安装

apt-get install ruby ruby-dev rubygems
gem install fpm

在centos里安装如下

15:34:53 # yum install rpm-build    
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror, keys, protectbase
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
0 packages excluded due to repository protections
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package rpm-build.x86_64 0:4.8.0-37.el6 will be installed
--> Processing Dependency: elfutils >= 0.128 for package: rpm-build-4.8.0-37.el6.x86_64
--> Processing Dependency: xz for package: rpm-build-4.8.0-37.el6.x86_64
--> Processing Dependency: unzip for package: rpm-build-4.8.0-37.el6.x86_64
--> Processing Dependency: lzma for package: rpm-build-4.8.0-37.el6.x86_64
--> Processing Dependency: file for package: rpm-build-4.8.0-37.el6.x86_64
--> Processing Dependency: /usr/bin/gdb-add-index for package: rpm-build-4.8.0-37.el6.x86_64
--> Running transaction check
---> Package elfutils.x86_64 0:0.152-1.el6 will be installed
--> Processing Dependency: elfutils-libs(x86-64) = 0.152-1.el6 for package: elfutils-0.152-1.el6.x86_64
--> Processing Dependency: libdw.so.1(ELFUTILS_0.149)(64bit) for package: elfutils-0.152-1.el6.x86_64
--> Processing Dependency: libdw.so.1(ELFUTILS_0.148)(64bit) for package: elfutils-0.152-1.el6.x86_64
--> Processing Dependency: libdw.so.1(ELFUTILS_0.138)(64bit) for package: elfutils-0.152-1.el6.x86_64
--> Processing Dependency: libdw.so.1(ELFUTILS_0.127)(64bit) for package: elfutils-0.152-1.el6.x86_64
--> Processing Dependency: libdw.so.1(ELFUTILS_0.126)(64bit) for package: elfutils-0.152-1.el6.x86_64
--> Processing Dependency: libdw.so.1(ELFUTILS_0.122)(64bit) for package: elfutils-0.152-1.el6.x86_64
--> Processing Dependency: libasm.so.1(ELFUTILS_1.0)(64bit) for package: elfutils-0.152-1.el6.x86_64
--> Processing Dependency: libdw.so.1()(64bit) for package: elfutils-0.152-1.el6.x86_64
--> Processing Dependency: libasm.so.1()(64bit) for package: elfutils-0.152-1.el6.x86_64
---> Package file.x86_64 0:5.04-15.el6 will be installed
---> Package gdb.x86_64 0:7.2-60.el6_4.1 will be installed
---> Package unzip.x86_64 0:6.0-1.el6 will be installed
---> Package xz.x86_64 0:4.999.9-0.3.beta.20091007git.el6 will be installed
---> Package xz-lzma-compat.x86_64 0:4.999.9-0.3.beta.20091007git.el6 will be installed
--> Running transaction check
---> Package elfutils-libs.x86_64 0:0.152-1.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================================
 Package                                       Arch                                  Version                                                         Repository                           Size
===============================================================================================================================================================================================
Installing:
 rpm-build                                     x86_64                                4.8.0-37.el6                                                    base                                127 k
Installing for dependencies:
 elfutils                                      x86_64                                0.152-1.el6                                                     base                                201 k
 elfutils-libs                                 x86_64                                0.152-1.el6                                                     base                                181 k
 file                                          x86_64                                5.04-15.el6                                                     base                                 46 k
 gdb                                           x86_64                                7.2-60.el6_4.1                                                  base                                2.3 M
 unzip                                         x86_64                                6.0-1.el6                                                       base                                149 k
 xz                                            x86_64                                4.999.9-0.3.beta.20091007git.el6                                base                                137 k
 xz-lzma-compat                                x86_64                                4.999.9-0.3.beta.20091007git.el6                                base                                 16 k

Transaction Summary
===============================================================================================================================================================================================
Install       8 Package(s)

Total download size: 3.1 M
Installed size: 7.4 M
Is this ok [y/N]: y
Downloading Packages:
(1/8): elfutils-0.152-1.el6.x86_64.rpm                                                                                                                                  | 201 kB     00:00     
(2/8): elfutils-libs-0.152-1.el6.x86_64.rpm                                                                                                                             | 181 kB     00:00     
(3/8): file-5.04-15.el6.x86_64.rpm                                                                                                                                      |  46 kB     00:00     
(4/8): gdb-7.2-60.el6_4.1.x86_64.rpm                                                                                                                                    | 2.3 MB     00:06     
(5/8): rpm-build-4.8.0-37.el6.x86_64.rpm                                                                                                                                | 127 kB     00:00     
(6/8): unzip-6.0-1.el6.x86_64.rpm                                                                                                                                       | 149 kB     00:00     
(7/8): xz-4.999.9-0.3.beta.20091007git.el6.x86_64.rpm                                                                                                                   | 137 kB     00:00     
(8/8): xz-lzma-compat-4.999.9-0.3.beta.20091007git.el6.x86_64.rpm                                                                                                       |  16 kB     00:00     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                          234 kB/s | 3.1 MB     00:13     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : xz-4.999.9-0.3.beta.20091007git.el6.x86_64                                                                                                                                  1/8 
  Installing : xz-lzma-compat-4.999.9-0.3.beta.20091007git.el6.x86_64                                                                                                                      2/8 
  Installing : file-5.04-15.el6.x86_64                                                                                                                                                     3/8 
  Installing : gdb-7.2-60.el6_4.1.x86_64                                                                                                                                                   4/8 
  Installing : elfutils-libs-0.152-1.el6.x86_64                                                                                                                                            5/8 
  Installing : elfutils-0.152-1.el6.x86_64                                                                                                                                                 6/8 
  Installing : unzip-6.0-1.el6.x86_64                                                                                                                                                      7/8 
  Installing : rpm-build-4.8.0-37.el6.x86_64                                                                                                                                               8/8 
  Verifying  : unzip-6.0-1.el6.x86_64                                                                                                                                                      1/8 
  Verifying  : xz-lzma-compat-4.999.9-0.3.beta.20091007git.el6.x86_64                                                                                                                      2/8 
  Verifying  : elfutils-libs-0.152-1.el6.x86_64                                                                                                                                            3/8 
  Verifying  : gdb-7.2-60.el6_4.1.x86_64                                                                                                                                                   4/8 
  Verifying  : elfutils-0.152-1.el6.x86_64                                                                                                                                                 5/8 
  Verifying  : rpm-build-4.8.0-37.el6.x86_64                                                                                                                                               6/8 
  Verifying  : file-5.04-15.el6.x86_64                                                                                                                                                     7/8 
  Verifying  : xz-4.999.9-0.3.beta.20091007git.el6.x86_64                                                                                                                                  8/8 

Installed:
  rpm-build.x86_64 0:4.8.0-37.el6                                                                                                                                                              

Dependency Installed:
  elfutils.x86_64 0:0.152-1.el6                 elfutils-libs.x86_64 0:0.152-1.el6                        file.x86_64 0:5.04-15.el6  gdb.x86_64 0:7.2-60.el6_4.1  unzip.x86_64 0:6.0-1.el6 
  xz.x86_64 0:4.999.9-0.3.beta.20091007git.el6  xz-lzma-compat.x86_64 0:4.999.9-0.3.beta.20091007git.el6 

Complete!

15:35:21 # gem install fpm
/usr/local/ruby-1.9.3/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>‘:
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
Fetching: json-1.8.1.gem (100%)
Building native extensions.  This could take a while...
Fetching: cabin-0.6.1.gem (100%)
Fetching: backports-3.6.0.gem (100%)
Fetching: arr-pm-0.0.9.gem (100%)
Fetching: clamp-0.6.3.gem (100%)
Fetching: ffi-1.9.3.gem (100%)
Building native extensions.  This could take a while...
Fetching: childprocess-0.5.3.gem (100%)
Fetching: fpm-1.1.0.gem (100%)
Successfully installed json-1.8.1
Successfully installed cabin-0.6.1
Successfully installed backports-3.6.0
Successfully installed arr-pm-0.0.9
Successfully installed clamp-0.6.3
Successfully installed ffi-1.9.3
Successfully installed childprocess-0.5.3
Successfully installed fpm-1.1.0
8 gems installed
Installing ri documentation for json-1.8.1...
Installing ri documentation for cabin-0.6.1...
Installing ri documentation for backports-3.6.0...
unable to convert U+00E9 from UTF-8 to US-ASCII for lib/backports/force/string_length.rb, skipping
unable to convert U+00E9 from UTF-8 to US-ASCII for lib/backports/1.9.2/stdlib/matrix.rb, skipping
Installing ri documentation for arr-pm-0.0.9...
Installing ri documentation for clamp-0.6.3...
Installing ri documentation for ffi-1.9.3...
unable to convert "\xE2" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to US-ASCII for lib/ffi/library.rb, skipping
Enclosing class/module ‘moduleFFI‘ for class StructByValue not known
Enclosing class/module ‘moduleFFI‘ for class StructLayout not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class Field not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class Number not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class String not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class Pointer not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class Function not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class Array not known
Enclosing class/module ‘moduleFFI‘ for module LastError not known
Enclosing class/module ‘moduleFFI‘ for class MemoryPointer not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class CharArray not known
Enclosing class/module "rbffi_StructLayoutCharArrayClass" for alias to_str to_s not known
unable to convert "\xC3" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to US-ASCII for ext/ffi_c/libffi/ChangeLog, skipping
unable to convert "\xC3" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to US-ASCII for ext/ffi_c/libffi/README, skipping
Installing ri documentation for childprocess-0.5.3...
Installing ri documentation for fpm-1.1.0...
Installing RDoc documentation for json-1.8.1...
Installing RDoc documentation for cabin-0.6.1...
Installing RDoc documentation for backports-3.6.0...
unable to convert U+00E9 from UTF-8 to US-ASCII for lib/backports/force/string_length.rb, skipping
unable to convert U+00E9 from UTF-8 to US-ASCII for lib/backports/1.9.2/stdlib/matrix.rb, skipping
Installing RDoc documentation for arr-pm-0.0.9...
Installing RDoc documentation for clamp-0.6.3...
Installing RDoc documentation for ffi-1.9.3...
unable to convert "\xE2" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to US-ASCII for lib/ffi/library.rb, skipping
Enclosing class/module ‘moduleFFI‘ for class StructByValue not known
Enclosing class/module ‘moduleFFI‘ for class StructLayout not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class Field not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class Number not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class String not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class Pointer not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class Function not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class Array not known
Enclosing class/module ‘moduleFFI‘ for module LastError not known
Enclosing class/module ‘moduleFFI‘ for class MemoryPointer not known
Enclosing class/module ‘rbffi_StructLayoutClass‘ for class CharArray not known
Enclosing class/module "rbffi_StructLayoutCharArrayClass" for alias to_str to_s not known
unable to convert "\xC3" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to US-ASCII for ext/ffi_c/libffi/ChangeLog, skipping
unable to convert "\xC3" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to US-ASCII for ext/ffi_c/libffi/README, skipping
Installing RDoc documentation for childprocess-0.5.3...
Installing RDoc documentation for fpm-1.1.0...
15:45:31 # find / -name "fpm"
/usr/local/ruby-1.9.3/lib/ruby/gems/1.9.1/doc/fpm-1.1.0/rdoc/lib/fpm
/usr/local/ruby-1.9.3/lib/ruby/gems/1.9.1/gems/fpm-1.1.0/lib/fpm
/usr/local/ruby-1.9.3/lib/ruby/gems/1.9.1/gems/fpm-1.1.0/bin/fpm
/usr/local/ruby-1.9.3/bin/fpm
/usr/local/php-5.3.10/share/php/fpm
root@e3d3e87fbfd7:~
15:45:46 # ln  /usr/local/ruby-1.9.3/bin/fpm /usr/bin/fpm

下面是安装脚本内容

16:10:45# cat install_for_rpm.sh 
#!/bin/bash
mkdir/tmp/fpm
echo"this is test for rpm" >/tmp/fpm/test.txt

下面是在进行rpm安装的时候,安装后运行的脚本

root@e3d3e87fbfd7:~
16:10:48# cat run_rpm.sh 
#!/bin/bash
/bin/bash/tmp/install_for_rpm.sh

也就是说,rpm包安装后,会运行我这个run_rpm.sh脚本,然后这个脚本会运行install_for_fpm.sh脚本

下面是打包过程

root@e3d3e87fbfd7:~
16:13:21 # fpm -s dir --prefix /tmp/ -t rpm -n test_rpm --verbose --version ‘2014‘  --iteration ‘1.0‘   --vendor ‘244979152@qq.com‘ --category ‘Development/test‘   --description ‘Rpm  Install‘ --url ‘qq.com‘ --license ‘BSD‘ -m ‘denglei‘ --after-install run_rpm.sh install_for_rpm.sh              
/usr/local/ruby-1.9.3/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>‘:
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
Setting from flags: category=Development/test {:level=>:info}
Setting from flags: description=Rpm  Install {:level=>:info}
Setting from flags: epoch= {:level=>:info}
Setting from flags: iteration=1.0 {:level=>:info}
Setting from flags: license=BSD {:level=>:info}
Setting from flags: maintainer=denglei {:level=>:info}
Setting from flags: name=test_rpm {:level=>:info}
Setting from flags: url=qq.com {:level=>:info}
Setting from flags: vendor=244979152@qq.com {:level=>:info}
Setting from flags: version=2014 {:level=>:info}
Converting dir to rpm {:level=>:info}
no value for epoch is set, defaulting to nil {:level=>:warn}
Reading template {:path=>"/usr/local/ruby-1.9.3/lib/ruby/gems/1.9.1/gems/fpm-1.1.0/templates/rpm.erb", :level=>:info}
no value for epoch is set, defaulting to nil {:level=>:warn}
Running rpmbuild {:args=>["rpmbuild", "-bb", "--define", "buildroot /tmp/package-rpm-build20140704-5378-1sbkpgd/BUILD", "--define", "_topdir /tmp/package-rpm-build20140704-5378-1sbkpgd", "--define", "_sourcedir /tmp/package-rpm-build20140704-5378-1sbkpgd", "--define", "_rpmdir /tmp/package-rpm-build20140704-5378-1sbkpgd/RPMS", "/tmp/package-rpm-build20140704-5378-1sbkpgd/SPECS/test_rpm.spec"], :level=>:info}
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.w21bbk {:level=>:info}
Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.FNduT3 {:level=>:info}
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.Cdf5BN {:level=>:info}
Processing files: test_rpm-2014-1.0.x86_64 {:level=>:info}
Wrote: /tmp/package-rpm-build20140704-5378-1sbkpgd/RPMS/x86_64/test_rpm-2014-1.0.x86_64.rpm {:level=>:info}
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.mv993g {:level=>:info}
Created package {:path=>"test_rpm-2014-1.0.x86_64.rpm"}
16:05:47# ll /root/
total8
-rw-r--r--1 root root   74 Jul  4 16:02 install_for_rpm.sh
-rw-r--r--1 root root 1709 Jul  4 16:05 test_rpm-2014-1.0.x86_64.rpm

可以看到,有个rpm文件生产,名字是test_rpm-2014-1.0.x86_64.rpm

下面安装rpm的过程

16:14:21# ll /tmp/
total96
lrwxrwxrwx1 root root    30 Jul  3 13:29 mysql.sock ->/data/mysql/datadir/mysql.sock
drwxr-xr-x2 root root  4096 Jul  4 16:12 root
-rw-------1 root root   256 Jun  5 10:17 tmp.GbE4cEGjO7
-rw-------1 root root 32379 Jul  3 17:27 yum_save_tx-2014-07-03-17-27UiVNzL.yumtx
-rw-------1 root root 27578 Jul  3 17:29 yum_save_tx-2014-07-03-17-29nffpfF.yumtx
-rw-------1 root root 27578 Jul  3 17:41 yum_save_tx-2014-07-03-17-41S1tFIE.yumtx

可以看到tmp目录里没有fpm目录,现在开始安装

root@e3d3e87fbfd7:~
16:14:24 # rpm -ivh test_rpm-2014-1.0.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:test_rpm               ########################################### [100%]
root@e3d3e87fbfd7:~
16:14:27 # ll /tmp/
total 104
drwxr-xr-x 2 root root  4096 Jul  4 16:14 fpm
-rw-r--r-- 1 root root    74 Jul  4 16:14 install_for_rpm.sh
lrwxrwxrwx 1 root root    30 Jul  3 13:29 mysql.sock -> /data/mysql/datadir/mysql.sock
drwxr-xr-x 2 root root  4096 Jul  4 16:12 root
-rw------- 1 root root   256 Jun  5 10:17 tmp.GbE4cEGjO7
-rw------- 1 root root 32379 Jul  3 17:27 yum_save_tx-2014-07-03-17-27UiVNzL.yumtx
-rw------- 1 root root 27578 Jul  3 17:29 yum_save_tx-2014-07-03-17-29nffpfF.yumtx
-rw------- 1 root root 27578 Jul  3 17:41 yum_save_tx-2014-07-03-17-41S1tFIE.yumtx
root@e3d3e87fbfd7:~
16:14:29 # ll /tmp/fpm/
total 4
-rw-r--r-- 1 root root 21 Jul  4 16:14 test.txt
root@e3d3e87fbfd7:~
16:15:00 # cat /tmp/fpm/test.txt 
this is test for rpm

可以看到安装后,tmp目录有了fpm目录,并且这个目录里有了test.txt文件。

rpm打包完成后,你可以使用ansible或者salt等软件来进行配置管理,或者你可以自己弄个本地yum源,rpm包放到yum源里让其他人下载也行,本文没有介绍太深入知识,只是个普及,所以有那块不懂的可以自行网上寻找。

本文出自 “吟—技术交流” 博客,请务必保留此出处http://dl528888.blog.51cto.com/2382721/1540312