RPM基本打包操作

  1. spec文件:

linux上使用vim xxx.spec,可以获得空白的spec文件模板:

  1. 基本配置:

Source0:源码包最好以 %{name}-%{version}.tar.gz/bz2 命名(%{name}即包名,%{version}即版本号);

放入SOURCES下的源码tar包中的目录也命名为%{name}-%{version}


BuildArch:写入要编译的架构,noarchmips64elx86_64等;不写默认根据平台编译;


Requires:为安装依赖;


BuildRequires:为编译依赖;


%prep 为编译前的操作,包括解压源码包,打补丁,甚至有些时候之前编译的错误会影响到之后编译,可以将旧有的buildroot清空(该操作其实可以在 %clean 中处理);


%build 为编译操作;

其中%configure 请特别注意,如果只是打包文件,不涉及编译(无configure文件),这一项请删除注释不行,编译过程中也会报错的);


%install 为安装过程,在%{buildroot}下创建系统中对应的虚拟目录,将需要的文件/目录拷贝过去,简单示范可参考下图:


%files 将最后要打包的文件/目录写在此处;


整个参考spec文件:


  1. 进阶:

以上内容只涉及到初步入门,深入学习应该还包括:

  1. 各个过程(%prep%build%installd等)的具体内容;

  2. spec文件中的宏定义;

  3. postprepostunpreun等脚本在安装新包时执行的顺序。


iii可参考链接:https://www.cnblogs.com/michael-xiang/p/10480809.html

iii可参考链接:https://fedoraproject.org/wiki/How_to_create_an_RPM_package/zh-hk#.E8.84.9A.E6.9C.AC.E7.89.87.E6.AE.B5.E5.91.BD.E4.BB.A4



制作一个hello worlddeb

  1. 新建临时工作的目录:mkdir work

  2. 新建程序目录:mkdir hello

  3. 编写程序:hello.c的代码如下:

#include<stdio.h>

int main(int argc,char *argv[])

{

printf(“Hello world! ”);

return 0;

}

Makefile文件如下:

OBJS=hello.o

CC=gcc -g

all:

$(OBJS)

$(CC) -o hello $(OBJS)

clean:

rm -f *.o hello

  1. 我们make一下,测试程序编译是否有问题,然后./hello检查程序是否正确执行。

  2. 如果有问题我们检查程序代码,如果没问题进行下一步。

  3. 清理刚才编译程序的垃圾,make clean一下。

  4. 输入命令,切回上级目录:cd ..

  5. 进行压缩打包:

mv hello hello-1.0(文件名还需包含版本号,所以需要改名)

tar -czvf hello-1.0.tar.gz hello-1.0

  1. 进入hello-1.0目录:cd hello-1.0

  2. 我们需要dh_make工具进行打包前的配置,如果是第一次使用请先安装dh-makeapt-get install dh-make,安装好后,我们就可以使用该命令了。

root@hxj:hello-1.0# dh_make -e hxj@com.cn -f ../hello-1.0.tar.gz

虽然dh_make有许多可以使用的选项标记(请查看它的手册页以获得一个全面的描述),但你只需要使用其中的两个选项就可以开始工作了。在上面的命令中,“-e”告诉dh_make软件包作者的电子邮件地址是hxj@com.cn,而“-f”选项指定了当前源代码树的最初未修改源代码的位置。

在运行dh_make时,你会被问到你想创建哪种类型的软件包:

Type of package: single binary, indep binary, multiple binary, library, kernel module, kernel patch?

[s/i/m/l/k/n] s

因为你是要创建一个单独的二进制软件包,所以选择选项“s”(其他类型更为复杂,详细信息请查看它们的相关文档)。下面是一个典型的dh_make调用的输出:

Maintainer name : root

Email-Address : hxj@com.cn

Date : Thu, 09 Oct 2014 19:30:13 +0800

Package Name : hello

Version : 1.0

License : blank

Type of Package : Singl

Hit <enter> to confirm:

Done. Please edit the files in the debian/ subdirectory now. You should also

check that the hello Makefiles install into $DESTDIR and not in / .

如果一切按计划进行,你将很快在现有的项目源代码目录中获得一个新的debian目录。当你在项目源代码目录中执行ls命令时,你将看到如下的输出:

root@hxj:hello-1.0# ls

debian hello.c Makefile

Debian所做的所有神奇的事情都发生在debian目录中。在该目录中,你将发现dh_make已为你创建了许多标准文件:

root@hxj:debian# ls

changelog hello.cron.d.ex manpage.sgml.ex preinst.ex source

compat hello.default.ex manpage.xml.ex prerm.ex watch.ex

control hello.doc-base.EX menu.ex README.Debian

copyright init.d.ex postinst.ex README.source

docs manpage.1.ex postrm.ex rules

幸运的是,你并不需要在创建一个标准软件包时修改其中的许多文件。但你需要修改control文件以提供你的特定软件包的相关信息。由dh_make所创建的control文件只提供相当基本的内容:

Source: hello

Section: unknown

Priority: optional

Maintainer: root <hxj@com.cn>

Build-Depends: debhelper (>= 8.0.0)

Standards-Version: 3.9.4

Homepage: <insert the upstream URL, if relevant>

#Vcs-Git: git://git.debian.org/collab-maint/hello.git

#Vcs-Browser: http://git.debian.org/?p=collab-maint/hello.git;a=summary


Package: hello

Architecture: any

Depends: ${shlibs:Depends}, ${misc:Depends}

Description: <insert up to 60 chars description>

<insert long description, indented with spaces>

接下来的任务就是修改该信息以提供类似于你在先前的RPM SPEC文件中提供的元数据。请注意,你并不需要指定这个软件包的来源,因为你已位于源代码树中(debian子目录就在你自己的应用程序的源代码目录中——这是Debian的事情)。
下面是针对这个例子修改过的control文件的内容:

Source: hello

Section: devel

Priority: optional

Maintainer: root <hxj@com.cn>

Build-Depends: debhelper (>= 8.0.0)

Standards-Version: 3.9.4

#Homepage: <insert the upstream URL, if relevant>

#Vcs-Git: git://git.debian.org/collab-maint/hello.git

#Vcs-Browser: http://git.debian.org/?p=collab-maint/hello.git;a=summary


Package: hello

Architecture: any

Depends: ${shlibs:Depends}, ${misc:Depends}

Description: A simple Hello world example program.

This program changes the world in many ways .Firstly, it uses the Debian package management system to demonstrate how you can create your own Debian packages.

SourcePackage行分别指定软件包和应用程序自身的名字。Source行指的是新创建的Debian源代码软件包的名字,而不是目前正在使用的程序源代码的位置。Section的含义类似于RPM软件包中的GroupDebian项目定义了各种标准组,你需要查看它们的文档以获得正确的组名称的进一步示例(和RPM的情况一样,不要随便给组命名——这将使得它的运行情况很差,并导致用户最终向你发出抱怨)。control文件的最后是一段对这个软件包的功能的冗长描述。

  1. 软件包的依赖关系

到目前为止,你也许想知道与使用RPM相比,使用Debian软件包的好处是什么(除了为使用相应的工具,需要你为自己的程序源代码添加 Debian文件和目录以外)?Debian软件包真正强大的功能是通过其轻量级的aptAdvanced Package Tool,高级软件包管理工具)软件包管理工具来呈现的。apt使得Debian可以自动解决可能存在的任何依赖关系,它通过安装必备的软件包并确保避免软件包之间的冲突来达到这一点。当然,apt做到所有这一切也离不开软件包管理者的帮助。

Debian软件包的control文件可以包含许多不同类型的依赖关系。下面这些例子来自官方文档:
Depends
:以Depends:开头的行指定必须先安装的软件包,以便顺利安装给定的软件包。这些都是单纯的依赖关系,它们用于避免造成严重的系统损坏。从理智上来讲,你当然不会希望自己犯一点点的错误。
Suggests
: 以Suggests:开头的行描述了那些对顺利操作一个特定的软件包并不是必需的软件包,但它们的存在将在某种程度上增强软件的功能。例如,如果用户有这样的需求,你可能想有(但不是必需)一个图形化工具来帮助将字符串Hello World格式化为霓虹灯效果的标记。
Conflicts
:以Conflicts:开头的行描述了不能与这个特定的软件包同时安装的软件包。例如,如果你要提供一个邮件服务器,你可能会判断Debian默认的exim邮件服务器(及其配置脚本)将与你自己的邮件服务器软件相冲突。
Provides
Debian 有时会定义虚拟软件包或不存在的软件包,它提供了一种友好的方式来指向某一特定类型的软件,可能有许多可用的软件都属于这一类型。这有助于用户寻找一个现 有的软件包或一个著名的软件包在最新的Debian版本中被转移到了哪里,而不需要用户理解这背后的策略。

  1. 构建Debian软件包

Debian软件包的构建过程与RPM不尽相同。但与RPM一样的是,为了成功的构建一个Debian软件包,你需要修改程序的Makefile文件,使得软件被安装到一个临时目录层次中,而不是安装到你的文件系统的根目录下。如果你查看本例中使用的Makefile文件,你将看到所有的文件都被安装到相对于${DESTDIR}的目录下,该变量是在构建软件包时由dpkg定义的。
下面是一个取自示例软件包的dpkg Makefile文件内容:

install:

mkdir -p ${DESTDIR}

cp hello ${DESTDIR}

一旦你已成功地准备好程序源代码,你就可以开始构建自己的Debian软件包了。在顶层目录(即debian子目录的上一层目录)中运行下面的命令:

root@hxj:hello-1.0# dpkg-buildpackage -rfakeroot

类似于rpmbuilddpkg-buildpackage将首先清理程序源代码,然后构建应用程序并从中产生一个Debian软件包。和前面一样,请 注意fakeroot的使用,它是用于欺骗包构建软件,让它认为它是以完全的root权限在运行。之所以能够这样做是因为在构建软件包时并不是真正需要向 标准系统文件位置写入文件,但在自包含软件包的构建过程中,可能需要明确地创建由root用户所拥有的文件。请记住,fakeroot并没有真正授予任何 root特权。
一个成功的dpkg-buildpackage运行将产生以下输出:

dpkg-buildpackage: 警告: 使用超级用户命令

dpkg-buildpackage: 源码包 hello

dpkg-buildpackage: 源码版本 1.0-1

dpkg-buildpackage: source distribution unstable

dpkg-buildpackage: 源码修改者 root <hxj@com.cn>

dpkg-buildpackage: 主机架构 amd64

dpkg-source --before-build hello-1.0

fakeroot debian/rules clean

dh clean

dh_testdir

dh_auto_clean

make[1]: 正在进入目录 `/root/hello-1.0'

rm -fr *.o hello

make[1]:正在离开目录 `/root/hello-1.0'

dh_clean

dpkg-source -b hello-1.0

dpkg-source: info: using source format `3.0 (quilt)'

dpkg-source: info: building hello using existing ./hello_1.0.orig.tar.gz

dpkg-source: info: building hello in hello_1.0-1.debian.tar.gz

dpkg-source: info: building hello in hello_1.0-1.dsc

debian/rules build

dh build

dh_testdir

dh_auto_configure

dh_auto_build

make[1]: 正在进入目录 `/root/hello-1.0'

cc -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -c -o hello.o hello.c

gcc -o hello hello.o

make[1]:正在离开目录 `/root/hello-1.0'

dh_auto_test

fakeroot debian/rules binary

dh binary

dh_testroot

dh_prep

dh_auto_install

dh_installdocs

dh_installchangelogs

dh_perl

dh_link

dh_compress

dh_fixperms

dh_strip

dh_makeshlibs

dh_shlibdeps

dh_installdeb

dh_gencontrol

dpkg-gencontrol: 警告: Depends field of package hello: 未知的替换变量 ${shlibs:Depends}

dh_md5sums

dh_builddeb

dpkg-deb:正在新建软件包 hello,包文件为 ../hello_1.0-1_amd64.deb

signfile hello_1.0-1.dsc

gpg: “root <hxj@com.cn>”已跳过:私钥不可用

gpg: [stdin]: clearsign failed: 私钥不可用


dpkg-genchanges >../hello_1.0-1_amd64.changes

dpkg-genchanges: 上传数据中包含完整的原始代码

dpkg-source --after-build hello-1.0

dpkg-buildpackage: 完整上载(包含原始的代码)

dpkg-buildpackage: 警告: failed to sign .dsc and .changes file

请注意,这个构建过程的输出结果将出现在软件包源代码的上一层目录中。所以,如果你在家目录中解开hello-1.0.tar.gz,你就可以直接 在家目录中找到输出的软件包文件hello_1.0-1_i386.deb。除了这个二进制软件包文件以外,你还将看到创建了其他文件:

hello_1.0-1.diff.gz hello_1.0-1.dsc hello_1.0-1.dsc.asc

这些文件可以被其他开发者用于重新生成二进制软件包。

你会看到在前面的示例的构建过程中产生了一些警告信息。第一个发现的问题是GNU Make抱怨它不能清理源代码。这个问题很好解决,你只需确保在每个Makefile文件中增加一个clean目标,就可以保证在需要的时候你的项目可以 被清理。第二个发现的问题是在构建软件包时,用户没有安装GPG(或有GPG可用)。正确分发的Debian软件包只要有可能,就应包含GPGPGP) 签名,如果你选择允许包含签名,在软件包构建过程中它就将为你执行电子签名。



本文来自博客园,作者:zwbsoft,转载请注明原文链接:https://www.cnblogs.com/zwbsoft/p/14522393.html

电话微信:13514280351
原文地址:https://www.cnblogs.com/zwbsoft/p/14522393.html