SUSE 包构建教程

Jump to: navigation, search

SUSE 包构建教程

这是一份说明如何为 SUSE 制作 RPM 包的教程。


导言

这份文档提供对 SUSE 制作包结构的全面论述,能够帮助你建立自己的临时打包环境。文档前四部提供概括性的描述,后面部分有例子帮你试验。

先决条件

你应对 RPM包的创建有良好的理解。这份文档的目的并不是替代 RPM 包的文档。对于那些问题,现在已有许多相关教程、指南和书籍 (下面资源部分有相关 RPM 包的链接)。

你也应对 PGP 相关工具有些经验,尤其是 GNU Privacy Guard (gpg) ,因为他是 OpenPGP 最广泛的应用。当我们后面讲到包签名的时候,将会解释为什么 PGP 如此重要。在使用 SUSE 包构建环境,你将需要一个你自己的 PGP 密钥用于签署包。

当然,你也应该对你要打包的项目所用源代码环境很熟悉。SUSE 包构建结构可以解决一些常见的项目维护者的错误,但是了解一些关于 make, autoconf, imake 或其他源代码配置原理或知识对构建包的工作相当有帮助。

必要条件

简单,你需要 3 GB 硬盘空间和包构建工具 (y2pmbuild)。

你需要 3 GB 硬盘空间是因为SUSE包构建结构需要另一个独立的Linux安装在你硬盘上作为你构建包的环境。这个孤立的环境有一大好处:构建包不会搞乱你的现有Linux。唯一的缺点就是你必须(临时)划出3G硬盘空间作为一个独立的包构建环境。但现在普遍来讲这点空间已经不成问题了。

包构建工具 (y2pmbuild) 在给定位置设置了包构建环境并在那里建包。包构建环境包括一个基础系统和一些你在 RPM包 spec 文件中指定的附加包。


工作流程

下列步骤概括了创建一个 RPM 包的流程。当然实际操作起来你可能会在不能通过一些步骤,这就需要你重复尝试直到成功为止。这个提纲只是给你一种大概的印象。

安装要求

确认你安装了以下软件,如果没有,通过 YaST 从你的 SUSE 安装源安装:

  • y2pmsh
  • bzip2
  • gpg

配置 y2pmsh

配置构建包的工具 (y2pmbuild)。有一些设置你需要调整,使之符合本地环境。例如:

  • 从哪里得到 RPM 包建立构建环境
  • 构建根目录的基名(和路径名相对)
  • 哪里存放构建好的 RPM 包
  • 设置你的 RPM 宏
  • 使用你自己的 RPM brp 脚本
  • 复制你的 gpg 设置

诸如此类。

创建源目录

创建一个你自己和超级用户 (root) 都可有权限操作的目录。它将是你放置一些构建 RPM 包所用文件的目录。你可以随意选择路径,但它必须对你和 root 都是可操作的。因为只有 root才能 启动构建环境设置。但在构建过程中,你未必希望一直用 root 的权限,所以你需要一个两者能共享的目录。

取得项目源码

从你想要构建包的那个软件的项目那里得到源代码。一般来说源码被压缩为tar文件发布。

创建 spec 文件

现在你有了源码,只要按照普通的 RPM 包构建过程就行。为包创建一个 specfile ,你可以在 spec 文件59494章节找到 SUSE 专用的一些设置参数。

构建包

创建了 RPM 的 spec 文件后,可以尝试构建了。到root帐户下进入你源码、spec文件和相关补丁(patch)所在的位置,启动 y2pmbuild。

测试包

构建成功之后, y2pmbuild 将把包存放到它的仓库。这个仓库是一个无格式的 YaST 仓库,你可以用它进行安装。 y2pmbuild 仓库可以作为你下次构建包的主仓库。这就保证了解决新旧包的依赖性问题。


Specfile

RPM specfile 应该按 openSUSE 的软件包规范书写。因为这里很复杂,所以有一个针对这个话题的单独的文档。请严格按照文档行事。


y2pmbuild 特性

配置

y2pmbuild 环境变量

  • BUILD_DIST

你构建包的目标发行版


   export BUILD_DIST=10.0; y2pmbuild

y2pmbuild 配置文件

y2pmbuild 配置文件存在于

   /etc/y2pmbuild

在这个目录下有一个所有发行版通用的配置文件:

   /etc/y2pmbuild/dists_common

还有一个目录存放因发行版而异的配置文件:

   /etc/y2pmbuild/dists/发行版名称/

y2pmbuild 首先读取通用配置文件,然后才是发行版专用文件。

配置文件实质上是 shell 脚本,被 y2pmbuild 解析,它们遵循 bash 语法。这意味着你可以引用任何环境变量或者配置文件中的变量,但请不要使用特殊的 shell 特性,以便于文件被其他程序解析。文件中变量是事件敏感的,备注可以在文件中任何地方出现(但不能在引号内)。备注要在 # 符号后面开始一直到这一行末结束。空行,只有空格或备注的行将被忽略。这里是一个变量/值及其意义的列表。

建立构建用的根目录:

  • url_*

你可以指定 RPM 包来源的 URL,可以是 y2pmsh 能够识别的任何来源。FTP, HTTP, Samba/CIFS, NFS, CD, DVD 和本地目录都可以。

   url_opensuse="file:/space/dist/opensuse/10.0/"
   url_kde4="http://opensuse.org/projects/kde4/packages/"
   url_gcc5="ftp://ftp.gcc.gnu.org/pub/releases/gcc5/RPMS/"
  • add_sources

构建根目录设置中所用的安装源

   add_sources="opensuse kde4 gcc5"


  • buildrootbase

buildrootbase 标签制定实际构建所用的目录。最终名称将是 $buildrootbase.$USER.$BUILD_DIST

   buildrootbase="/space/build"

包签名:

  • rpm_sign_name

签署包所用身份。

   rpm_sign_name="Tux Builder <tux@example.com>"

或者

   rpm_sign_name="tux@example.com"
  • rpm_sign_key

签名的 key 文件。

   rpm_sign_key="/etc/y2pmbuild/signkey.asc"

创建文件:

   gpg -a --export > signkey.asc
   gpg -a --export-secret-key >> signkey.asc

处理构建好的 RPM 包所用的变量:

  • repository

储存创建好的 RPM 包的目录。

   repository="/space/done/$BUILD_DIST"
  • regen_repository

为了把构建的包作为安装源(比如你下一次构建包的时候就会用到),有 RPM 包的目录必须被转换成能被 YaST识别的安装源。你可以用 genIS_PLAINcache (独立程序)、createrepo (yum) 或者 reate_package_descr (ul) 实现。对最后那个软件的维护工作目前中断了。

   regen_repository="plain"
   regen_repository="ul"

关于安装源的详细信息参考条目安装源

y2pmbuild 设置好以后,你可以给它一些附加文件让它复制进构建环境。这里牵扯到因发行版而异的目录,而且用了 rsync,所以你要注意下目录名后面是否要加斜杠。

   /etc/y2pmbuild/files/发行版名称/
  • extra_copy

包安装好以后,其他需要被复制到构建根目录的目录。

   extra_copy="/etc/profile"

实例

这有一些真实的例子,供参考借鉴。

KDE 包

BuildRequires: kdelibs3-devel update-desktop-files
Name: kvpnc License: GPL Group: Productivity/Networking/System Summary: GUI frontend for openswan and vpnc Version: 0.7.2 Release: 1 URL: http://home.gna.org/kvpnc/ BuildRoot:  %{_tmppath}/%{name}-%{version}-build Source0:  %name-%{version}.tar.bz2
%description The application can create configurations for various kinds of VPN connections. You should install also the "vpnc" or "openswan" package.
%prep # extract the source and go into the kvpnc-0.7.2 directory %setup -q # source the standard build enviroment as defined in kdelibs3 package . /etc/opt/kde3/common_options # replace the admin/ folder with the version from kdelibs3 (will work for sure with # current autoconf and automake) and create Makefile.in and configure script. update_admin --no-unsermake
%build # source the standard build enviroment as defined in kdelibs3 package . /etc/opt/kde3/common_options ./configure $configkde --disable-final # do compile with the number of jobs given to the --jobs parameter of the build script. make %{?jobs:-j%jobs}
%install iconv -f iso8859-1 -t utf-8 src/kvpnc.desktop > w && mv w src/kvpnc.desktop # install all files into the BuildRoot make DESTDIR=$RPM_BUILD_ROOT install rm -rf $RPM_BUILD_ROOT/opt/kde3/share/doc/HTML/kvpnc # updates the .desktop file. It applys additional translations and Categories for a # proper position in the KDE menu %suse_update_desktop_file %name System Network # does find all localisation files and adds proper database attributes to them %find_lang %name
%clean # clean up the hard disc after build rm -rf $RPM_BUILD_ROOT
%files -f %name.lang %defattr(-,root,root) %dir /opt/kde3/share/icons/hicolor/16x16/apps %dir /opt/kde3/share/icons/hicolor/32x32/apps /opt/kde3/bin/kvpnc /opt/kde3/share/appl*/*/kvpnc.desktop /opt/kde3/share/apps/kvpnc /opt/kde3/share/icons/*/*/*/kvpnc*.png

Gnome 包

   # the following line is not a remark, but contains a macro for all 
   # needed packages for a standard Gnome application:
   # neededforbuild  gnome2-devel-packages
Name: gwget  %define prefix /opt/gnome  %define sysconfdir /etc%{prefix} License: GPL Group: Productivity/Networking/Web/Utilities Requires: wget Version: 0.95 Release: 1 Summary: Front-end for wget written in GTK+ URL: http://gnome.org/projects/gwget/ # the packages filesystem and gconf2 needs to be installed, before this # package gets installed. Otherwise the script in %post would fail. PreReq: filesystem gconf2 Source:  %{name}-%{version}.tar.bz2 Source:  %name.desktop BuildRoot:  %{_tmppath}/%{name}-%{version}-build
 %description Front-end for wget written in GTK+
# extract source tar ball  %prep  %setup -n %{_name}-%{version}
 %build # use global compile flags and run configure script CFLAGS="$RPM_OPT_FLAGS" \ ./configure \ --prefix=%prefix \ --libexecdir=%prefix/lib/gwget \ --libdir=%prefix/%_lib \ --sysconfdir=%sysconfdir \ --datadir=%prefix/share \ --localstatedir=/var/lib \ --mandir=%{_mandir} \ --disable-schemas-install # do compile make
 %install # install all files make DESTDIR=$RPM_BUILD_ROOT install # create file list of localized files into file %name.lang  %find_lang %{name} # install the gwget.desktop file from %Source1 and apply Catgories (Network and FileTransfer)  %suse_update_desktop_file -i gwget Network FileTransfer
# the calls which does get called after installing this package  %post # update the gconf database export GCONF_CONFIG_SOURCE=`opt/gnome/bin/gconftool-2 --get-default-source` opt/gnome/bin/gconftool-2 --makefile-install-rule etc/opt/gnome/gconf/schemas/gwget.schemas >/dev/null
 %clean # clean up the hard disc after build rm -rf $RPM_BUILD_ROOT
 %files -f %{name}.lang  %defattr(-,root,root) # the files to package


Perl 包

Perl 程序很容易打包,因为他们已经为 CPAN 架构准备好了。

Name:         perl-Curses
Version:      1.12
Release:      1
# define the name from CPAN
%define cpan_name Curses
# do this package was known as "perl_cur" in old times. So we do need to Provide and Obsolete
# this package. YaST will install this package during update instead of the old one in this way.
Provides:     %cpan_name
# we better require the exact perl version, which was used to create this package
Requires:     perl = %{perl_version}
Group:        Development/Libraries/Perl
License:      Artistic License, Other License(s), see package
URL:          http://cpan.org/modules/by-module/Curses/
Summary:      A Dynamic Loadable Curses Module for Perl5
Source:       %cpan_name-%{version}.tar.bz2
BuildRoot:    %{_tmppath}/%{name}-%{version}-build
%description This is a dynamic loadable curses module for Perl5. This package can be found at any CPAN archive.
%prep %setup -q -n %cpan_name-%{version}
%build perl Makefile.PL OPTIMIZE="$RPM_OPT_FLAGS -Wall" make make test
%install make DESTDIR=$RPM_BUILD_ROOT install_vendor %perl_process_packlist
%clean # clean up the hard disc after build rm -rf $RPM_BUILD_ROOT
%files %defattr(-,root,root) %doc Artistic Copying README %doc %{_mandir}/man3/* %{perl_vendorarch}/%cpan_name.pm %{perl_vendorarch}/auto/%cpan_name /var/adm/perl-modules/%{name}

资源