一、前言
打包是指将源码包编译成rpm包,在centos下通过rpmbuild命令和spec文件来实现。
相对于源码编译安装的好处是,不需要每次安装都编译一次,那样很慢,对于打好的rpm包,直接试用rpm命令就可以安装。
相对于一些官方提供的rpm包的优势在于,可以想源码编译一样定制化包的安装,比如修改安装的路径,添加一些自己的配置和文件等。
以下的打包以postgresql为例
二、安装rpmbuild
$ sudo yum install -y rpm-build
三、初始化打包目录
$ sudo mkdir -p /data
$ cd /data/
$ sudo mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SRPMS,SOURCES,SPECS}
$ sudo chown -R $(whoami): rpmbuild
四、准备spec文件
$ sudo mkdir -p rpmbuild/SPECS/postgresql/{9.4,conf}
$ cd rpmbuild/SPECS/postgresql/9.4/
$ vim pg94.el6.spec
%define debug_package %{nil}
Name: postgresql
Summary: PostgreSQL server
Version: 9.4.4
Release: 1%{?dist}
Source: %{name}-%{version}.tar.bz2
URL: http://www.postgresql.org/ftp/source/
Group: Applications/Database
Vendor: PostgreSQL
License: Public Domain
BuildRequires: libxml2-devel libxslt-devel uuid-devel readline-devel
Requires: perl-ExtUtils-Embed libxml2 libxslt readline uuid
%description
Postgres is a truly awesome database. When we started working on Launchpad I wasn't sure if it would be up to the job. I was so wrong. It's been robust, fast, and professional in every regard.
%prep
%setup -q
env CFLAGS=" -march=core2 -O2 " ./configure --prefix=/opt/pg94 --with-openssl --with-libxml --with-libxslt --with-ossp-uuid
%build
make -j4
%install
make install DESTDIR=$RPM_BUILD_ROOT
make install-world DESTDIR=$RPM_BUILD_ROOT
install -d $RPM_BUILD_ROOT/etc/ld.so.conf.d
install -d $RPM_BUILD_ROOT/etc/profile.d
cd $RPM_BUILD_DIR/../SPECS/postgresql
test -L $RPM_BUILD_ROOT/opt/pgsql && rm -f $RPM_BUILD_ROOT/opt/pgsql
ln -sf /opt/pg94 $RPM_BUILD_ROOT/opt/pgsql
test ! -f /etc/ld.so.conf.d/pgsql.conf && cp ./conf/ld.pgsql.conf $RPM_BUILD_ROOT/etc/ld.so.conf.d/pgsql.conf
test ! -f /etc/profile.d/pgsql.sh && cp ./conf/profile.pgsql.sh $RPM_BUILD_ROOT/etc/profile.d/pgsql.sh
%post
%postun
%clean
rm -fr $RPM_BUILD_ROOT
%files
%defattr(-, root, root)
/opt/*
/etc/*
$ cd ../conf/
$ sudo vim ld.pgsql.conf
/opt/pgsql/lib
$ sudo vim profile.pgsql.sh
#!/bin/env bash
export PATH=/opt/pgsql/bin:$PATH
五、打包
先配置变量方便以后的使用:
$ sudo vim /etc/profile.d/rpmbuild.sh
#!/bin/env bash
alias rpmbuild='rpmbuild --define "_topdir /data/rpmbuild" -bb'
$ sudo source /etc/profile.d/rpmbuild.sh
打包就只需要执行:
$ rpmbuild path/xxx.spec
六、~/.rpmmacros
%_topdir %(echo $HOME)/rpmbuild
%_tmppath %{_topdir}/tmp
%buildroot %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
%_rpmfilename %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm
参考:How to create an RPM package和Rpmbuild Tutorial