大家好,欢迎来到IT知识分享网。
目录
前言
打rpm 包需要的东西有 源码、spec文件(打rpm包的脚本)、rpmbuild工具。
官网教程:RPM Packaging Guide–https://rpm-packaging-guide.github.io/#rpm
【rpm】源码包制作rpm包|修改rpm、重新制作rpm包
安装rpmbuild
$yum install rpmbuild #有些是rpm-build自己用yum list|grep rpm查询确认 $yum install rpmdevtools $rpmdev-setuptree
rpmbuild制作rpm 包
2,编写spec文件
在SPECS文件夹下新建 xxx.spec 打包脚本,其实也就是把我门的源码编译打包成rpm 的一个过程。
(有个rpmdev-newspec工具可以自动生成一个.spec 模板)
vi xxx.spec Name: hellorpm #名字为源码tar.gz 包的名字 Version: 1.0.0 #版本号,一定要与tar.gz包的一致哦 Release: 1%{?dist} #释出号,也就是第几次制作rpm Summary: helloword #描述信息/软件包简介,最好不超过50字符 #生成rpm包名:${Name}-${Version}-${Release}.${BuildArch}.rpm License: GPL #许可,GPL还是BSD等 URL: #自定义该信息,可以写一个网址 Packager: abel Source0: %{name}-%{version}.tar.gz #定义用到的source,也就是你的源码 BuildRoot: %_topdir/BUILDROOT #这个是软件make install 的测试安装目录. BuildRequires: gcc,make #制作过程中用到的软件包 Requires: python-apscheduler >= 2.1.2-1.el7,python-daemon >= 1.6-1.el7 #软件运行依赖的软件包,也可以指定最低版本如 bash >= 1.1.1 %description #描述,随便写 %prep #打包开始 %setup -q #这个作用静默模式解压并cd %build #编译制作阶段,主要目的就是编译,如果不用编译就为空 ./configure \ %{?_smp_mflags} #make后面的意思是:如果就多处理器的话make时并行编译 %install #安装阶段//安装之前需初始化安装目录 rm -rf %{buildroot} #先删除原来的安装的,如果你不是第一次安装的话 cp -rp %_topdir/BUILD/%{name}-%{version}/* $RPM_BUILD_ROOT #将需要需要打包的文件从BUILD 文件夹中拷贝到BUILDROOT文件夹下。 #下面的几步pre、post、preun、postun 没必要可以不写 %pre #rpm安装前制行的脚本 %post #安装后执行的脚本//安装之后需要执行的动作 cp /usr/local/httpd/bin/apachectl /etc/init.d/myhttpd sed -i '1a # chkconfig: 2345 85 15' /etc/init.d/myhttpd %preun #卸载前执行的脚本//卸载该rpm包所执行的一些操作 /etc/init.d/myhttpd stop %postun #卸载后执行的脚本 %clean #清理段,删除buildroot rm -rf %{buildroot} %files #rpm要包含的文件//安装之后生成的文件 %defattr (-,root,root,-) #设定默认权限,如果下面没有指定权限,则继承默认 /etc/hello/word/helloword.c #将你需要打包的文件或目录写下来 /usr/local/httpd/bin/* %dir /usr/local/httpd/logs %doc /usr/local/httpd/man/* %doc /usr/local/httpd/manual/* 7.chagelog section //定义一些日志文件,可以使用:rpm -q --changelog httpd查看到 %changelog * Wed Mar 26 2014 zhangzhg <> 2.2.25 - first rpm from httpd-2.2.25
(这spec里面的%build ,%install 容易写错而报错,所以我们可以不配置 %build ,%install,不让rpmbuild代我们执行build 和install ,我们在外面make install,然后rpmbuild 打包结果)
%build %install xxxxxxxx 错误示例: %build %install xxxxxxxx
spec 文件编写好以后就可以进行打包了。
如果出错了可以通过 不同的命令来看是在打包的那一步出了问题。
4, 安装
rpmbuild -ivh hello.rpm
5, 查询
rpm -qi hello
rpm -qa|grep hello
原文链接:https://blog.csdn.net/u0/article/details/
同时生成devel包
devel是以子包的形式出现的,所以只需在spec文件里面增加%files devel配置段即可
Name: kmymoney Summary: The Personal Finances Manager for KDE. Version: 0.8 Release: 1.%{disttag}%{distver} #生成rpm包名:${Name}-${Version}-${Release}.${BuildArch}.rpm License: GPL Packager: %packer Group: Productivity/Office/Finance Source0: %{name}2-%version.tar.bz2 BuildRoot: %{_tmppath}/%{name}2-%{version}-%{release}-build BuildRequires: kdebase3-devel Prereq: /sbin/ldconfig %description Description goes here... %package devel #Requires: Summary: KMyMoney development files Group: Productivity/Office/Finance Provides: kmymoney-devel %description devel This package contains necessary header files for KMyMoney development. ... more to go here ... %files ... some files ... %files devel ... the devel files ...
示例:
阻止rpmbuid打包时strip程序/库
默认情况下,在使用rpmbuild打包时,会对安装的所有文件进行strip操作,去除文件的一些调试信息,并将这些调试信息放到
中,但在很多时候,我们并不需要rpmbuild帮我们执行strip,也不需要生成debuginfo包,所以我们可以修改一下spec文件,关闭这些选项。
针对文件的strip操作是在__os_install_post这个宏中定义的,我们可以运行一下rpmbuild –showrc看一下原始的__os_install_post做了哪些操作:
- …
–14: __os_install_
/usr/lib/rpm/redhat/brp–compress
%{!?__debug_package:/usr/lib/rpm/redhat/brp–strip:q %{
__strip}} - /usr/lib/rpm/redhat/brp–strip–static–archive %{
__strip} - /usr/lib/rpm/redhat/brp–strip–comment–note %{
__strip} %{
__objdump} - /usr/lib/rpm/brp–python–bytecompile
- /usr/lib/rpm/redhat/brp–python–hardlink
- %{!?__jar_repack:/usr/lib/rpm/redhat/brp–java–repack–jars}
- …
可以看到在打包时会对文件进行一系列操作,比如压缩,strip,编译Python脚本等。
要想不strip,就要把上面带strip的语句去掉,或者让它门失效。
方法1:
在spec文件前面加,直接不做__os_install_post 里的任何事情
%global __os_install_post %{nil}
方法2:
在前面加
%define debug_package %{nil}
%define __strip /bin/true
把__strip 变量定义为/bin/true (原来是/usr/bin/strip),相当于啥也没干。
摘自:https://www.cnblogs.com/LiuYanYGZ/p/9565861.html
$ rpm –showrc | grep -A 4 ‘: __os_install_post’
-14: __os_install_post
/usr/lib/rpm/brp-compress
/usr/lib/rpm/brp-strip
/usr/lib/rpm/brp-strip-static-archive
/usr/lib/rpm/brp-strip-comment-noteRecently, I had an uncommon requirement to disable this option. I do not want to reduce the file size of all files packaged in the rpm. This is achievable using any of 3 mechanisms.
- Spec file
- ~/.rpmmacros
- /etc/rpm/macros
To prevent binary stripping for specific rpm, we could add following line at the top of any rpm spec file.
%global __os_install_post %{nil}
To prevent binary stripping for all rpms created by specific user, we could add following line in ~/.rpmmacros file:
%__os_install_post %{nil}
To prevent binary stripping for all rpms created by all users, we could add following line in /etc/rpm/macros file:
%__os_install_post %{nil}
修改rpm、重新制作rpm包
修改or替换helloworld里面的内容
把helloworld和helloworld.spec 都放入其中<———————-这一步很关键
cp -r helloworld helloworld.spec /tmp/buildrpm
重新编出rpm包
RPM 打包 工具
SPEC文件
spec文件关键字说明大全
主要关键字有:
(spec没有写的依赖也依赖进来了?见后面的章节说明)
%define: 预定义的变量,例如定义日志路径: _logpath /var/log/weblog
预处理脚本,这个段是预处理段,通常用来执行一些解开源程序包的命令,为下一步的编译安装作准备。%prep和下面的%build,%install段一样,除了可以执行RPM所定义的宏命令(以%开头)以外,还可以执行SHELL命令,命令可以有很多行,如我们常写的tar解包命令。
%setup
%setup 不加任何选项,仅将软件包打开。
%setup -q 在安静模式下且最少输出
%setup -D #在解压之前禁止删除目录
%patch #打补丁0
%patch1 #打补丁1
%patch2 #打补丁2
%patch -P 2 #打补丁2
在%build之前,%prep部分将准备好编译工作,解开源码包,并将相应的补丁打进去。
引用
CFLAGS=”$RPM_OPT_FLAGS” CXXFLAGS=”$RPM_OPT_FLAGS” ./configure –prefix=%{_prefix}
%build 开始构建包
开始编译源码构建包,相当于configure以及make部分
所要执行的命令为生成软件包服务,如
CFLAGS=”$RPM_OPT_FLAGS” CXXFLAGS=”$RPM_OPT_FLAGS” ./configure –prefix=%{_prefix}
也可以使用非标准写法:
make DESTDIR=$RPM_BUILD_ROOT install
make prefix=$RPM_BUILD_ROOT install
rm -rf ${RPM_BUILD_ROOT}
mkdir -p $RPM_BUILD_ROOT/{%{_bindir},%{_mandir}/man1,%{_libdir},%{_includedir}}
install -m 755 bzlib.h $RPM_BUILD_ROOT/%{_includedir}
install -m 644 bzip2.1 bzdiff.1 bzgrep.1 bzmore.1 $RPM_BUILD_ROOT/%{_mandir}/man1/
ln -s file/magic ${RPM_BUILD_ROOT}%{_datadir}/magic
%find_lang %{name}
%files -f %{name}.lang
第一句生成一个名为%{name}.lang的文件,内容是所有的%{name}.mo,第二句意思是一个一个列举.mo文件很麻烦,-f参数是将其后边接的文件合并到%files的文件列表。
%preun %postun 的区别是什么呢?
分为三类-说明文档(doc),配置文件(config)及执行程序,还可定义文件存取权限,拥有者及组别
ile1 #文件中也可以包含通配符,如*
file2
directory #所有文件都放在directory目录下
%dir /etc/xtoolwait #仅是一个空目录/etc/xtoolwait打进包里
%doc 表示这是文档文件,因此如安装时使用–excludedocs将不安装该文件,
%doc /usr/X11R6/man/man1/xtoolwait.* #安装该文档
%doc README NEWS #安装这些文档到/usr/share/doc/%{name}-%{version} 或者 /usr/doc或者
%docdir #定义说明文档的目录,例如/root,在这一语句后,所有以/root开头的行都被定义为说明文件。
%config /etc/yp.conf #标志该文件是一个配置文件,升级过程中,RPM会有如下动作。
%config(missisgok) /etc/yp.conf 此配置文件可以丢失,即使丢失了,RPM在卸载软件包时也不认为这是一个错误,并不报错。一般用于那些软件包安装后建立的符号链接文件,如
/etc/rc.d/rc5.d/S55named文件,此类文件在软件包卸载后可能需要删除,所以丢失了也不要紧。
%config(noreplace) /etc/yp.conf
#该配置文件不会覆盖已存在文件(RPM包中文件会以.rpmnew存在于系统,卸载时系统中的该配置文件会以.rpmsave保存下来,如果没有这个选项,安装时RPM包中文件会以.rpmorig存在于系统 )
覆盖已存在文件(没被修改),创建新的文件加上扩展后缀.rpmnew(被修改)
%{_bindir}/*
%config /etc/aa.conf
%ghost /etc/yp.conf #该文件不应该包含在包中,一般是日志文件,其文件属性很重要,但是文件内容不重要,用了这个选项后,仅将其文件属性加入包中。
%attr(mode, user, group) filename #控制文件的权限如%attr(0644,root,root) /etc/yp.conf
如果你不想指定值,可以用-
%config %attr(-,root,root) filename #设定文件类型和权限
%defattr (-,root,root,-) 、%defattr (0644,root,root,0644) #设置文件的默认权限,-表示默认值,对文本文件是0644,可执行文件是0755
%lang(en) %{_datadir}/locale/en/LC_MESSAGES/tcsh* #用特定的语言标志文件
%verify(owner group size) filename #只测试owner,group,size,默认测试所有
%verify(not owner) filename #不测试owner,测试其他的属性
所有的认证如下:
group: 认证文件的组
maj: 认证文件的主设备号
md5: 认证文件的MD5
min: 认证文件的辅设备号
mode: 认证文件的权限
mtime: 认证文件最后修改时间
owner: 认证文件的所有者
size: 认证文件的大小
symlink:认证符号连接
如果描述为目录,表示目录中出%exclude外的所有文件。
由于必须在%file中包括所有套件中的文件,所以,我们需要清楚编译完的套件到底包括那些文件,常见的做法是,人工模拟一次编译的过程:
%changelog
变更日志,本段是修改日志段。你可以将软件的每次修改记录到这里,保存到发布的软件包中,以便查询之用。每一个修改日志都有这样一种格式:
第一行是:* 星期月日 年 修改人电子信箱。
其中:星期、月份均用英文形式的前3个字母,用中文会报错。接下来的行写的是修改了什么地方,可写多行。一般以减号开始,便于后续的查 阅。
* Mon Mar 31 1997 Erik Troan <>
– Fixed problems caused by 64 bit time_t.
更多:【rpm】源码包制作rpm包|修改rpm、重新制作rpm包
详细介绍在:https://www.cnblogs.com/ilanni/p/4312581.html
rpmbuild的目录和Spec宏变量和参数说明
preamble部分
|
SPEC 指令 |
说明 |
|
|
The base name of the package, which should match the SPEC file name. 包的基本名称,应与SPEC文件名匹配。 |
|
|
软件的上游版本号。 |
|
|
The number of times this version of the software was released. Normally, set the initial value to 1%{?dist}, and increment it with each new release of the package. Reset to 1 when a new 此版本软件的发布次数。通常,将初始值设置为1%{?dist},并该版本的每次新发布递增。生成新版本的软件时,重置为1。 |
|
|
一个简短的、单行的包摘要。 |
|
|
The license of the software being packaged. For packages distributed in community distributions such as Fedora this must be an open source license abiding by the specific distribution’s licensing guidelines. |
|
|
The full URL for more information about the program. Most often this is the upstream project website for the software being packaged. 有关该程序的详细信息的完整URL。通常,这是打包软件的上游项目网站。 |
|
|
Path or URL to the compressed archive of the upstream source code (unpatched, patches are handled elsewhere). This should point to an accessible and reliable storage of the archive, for example, the upstream page and not the packager’s local storage. If needed, more SourceX directives can be added, incrementing the number each time, for example: Source1, Source2, Source3, and so on. 上游源代码压缩包的路径或URL(未修补,补丁在其他地方处理)。这应该指向可访问且可靠的包路径,例如,上游页面,而不是打包程序的本地存储。如果需要,可以添加更多的SourceX地址,每次递增数字,例如:Source1、Source2、Source3等。 |
|
|
The name of the first patch to apply to the source code if necessary. If needed, more PatchX directives can be added, incrementing the number each time, for example: Patch1, Patch2, Patch3, and so on. 必要时应用于源代码的第一个补丁的名称。如果需要,可以添加更多的PatchX,每次递增数字,例如:Patch1、Patch2、Patch3等。 |
|
|
If the package is not architecture dependent, for example, if written entirely in an interpreted programming language, set this to 如果包不依赖于平台架构,例如和硬件平台无关的脚本,请将其设置为BuildArch:noarch。如果未设置,软件包将自动设置为 机器的平台结构,例如x86_64。 |
|
|
A comma- or whitespace-separated list of packages required for building the program written in a compiled language. There can be multiple entries of 编译程序用到的文件的列表,用逗号或空格分隔。在SPEC文件中,可以有多个BuildRequires条目,,每个条目单独一行 |
|
|
A comma- or whitespace-separated list of packages required by the software to run once installed. There can be multiple entries of 软件安装后运行所依赖的rpm的文件列表,用逗号或空格分隔。在SPEC文件中,可以有多个Requires条目,每个条目单独一行 (spec没有写的依赖也依赖进来了?见后面的章节说明) |
|
|
If a piece of software can not operate on a specific processor architecture, you can exclude that architecture here. 如果某个软件不能在特定的处理器架构上运行,则可以在此处排除该架构。 |
例如:要生成的包名是 python-2.7.5-34.el7.x86_64,则
Name:python,
Version:2.7.5,Release:34.el7
最后一个标记是x86_64,表示体系结构。体系结构标记不受RPM打包器的直接控制,而是由rpmbuild构建环境定义。唯一的例外是要构建的RPM与硬件平台无关时,自己可以填写
BuildArch:noarch.
Body 部分
|
SPEC 指令 |
说明 |
|
|
A full description of the software packaged in the RPM. This description can span multiple lines and can be broken into paragraphs. RPM中软件包的完整描述。此描述可以跨越多行,并可以拆分为段落。 |
|
|
Command or series of commands to prepare the software to be built, for example, unpacking the archive in 构建之前的处理,例如,创建or删除目录,解压从Source0获取到的压缩包……可以包含shell脚本。 |
|
|
Command or series of commands for actually building the software into machine code (for compiled languages) or byte code (for some interpreted languages). 编译生成程序的的命令或一系列命令。 |
|
|
Command or series of commands for copying the desired build artifacts from the |
|
|
Command or series of commands to test the software. This normally includes things such as unit tests. 测试软件的命令或一系列命令。这通常包括单元测试等内容。 |
|
|
The list of files that will be installed in the end user’s system. 将安装在最终用户系统中的文件列表。(也就是需要打包进rpm包的文件列表–绝对路径) |
|
|
A record of changes that have happened to the package between different 不同版本或发行版本之间对包所做更改的记录。 |
标题宏变量/工作目录
rpmbuild –showrc显示所有的宏,以下划线开头:
符号说明
Note:
如果%clean 不加会默认把BUILDROOT给删掉
原文链接:https://blog.csdn.net/Wang/article/details/
spec文件不写依赖的情况依然存在依赖库
由于很多东西需要保密,所以不放截图rpm -qlp *.rpm查看rpm包中携带的文件ldd file检测编译文件夹下的bin文件是否是bin文件带来的依赖vi /usr/lib/rpm/micro编辑rpm的编译宏
找到455,456行,注释掉
455 #%__find_provides %{_rpmconfigdir}/find-provides 456 #%__find_requires %{_rpmconfigdir}/find-requires
重新编译,使用rpm -qpR *.rpm检测新生成的rpm是否带有依赖
如果问题未被解决,则在spec文件Requires下面加入一行AutoReqprv: no来规避掉二进制文件带来的依赖 AUTOREQ=0 (注意使用AutoReqprv:no 之后需要手工写spc 该rpm包的provider
The AUTOREQPROV, AUTOREQ, and AUTOPROV tags are not available for use with –queryformat.
Available Tags For –queryformat—http://ftp.rpm.org/max-rpm/ch-queryformat-tags.html
rpm包是怎么自动搞进一些依赖项的:
答案是rpmbuild使用ldd命令对%files部分中包含的任何二进制文件生成自动依赖关系:
Automatic Dependencies
CMake制作rpm包
也可以使用cmake 制作rpm包,CMake中包含的三个工具(cmake cpack ctest)中的cpack工具。他可以帮助快速的打包发布你的程序。
HelloWorld
CMakeList.txt
cmake_minimum_required(VERSION 2.8) # 设置项目名称 project(HelloWorld) # 设置安装路径, 默认/usr/local。也可以是idc经常安装的/data/home/user00/xxxserver等 # set(CPACK_PACKAGING_INSTALL_PREFIX /opt) # 如果不是CMake构建的,设置CMAKE_CURRENT_BINARY_DIR为Makefile的构建目录 set(CMAKE_CURRENT_BINARY_DIR ..) # 批量的安装指令,目录、程序、库文件、头文件等 install(PROGRAMS bin/hello DESTINATION bin) install(FILES bin/hello.a DESTINATION lib) install(FILES src/hello.h DESTINATION include) # 以下为RPM信息的设置,包名,概述,供应者,版本, 分组等等信息,通过其变量名称可以知道意思 set(CPACK_PACKAGE_NAME "wetest-helloworld") set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Simple CPack HelloWorld") set(CPACK_PACKAGE_VENDOR "WeTest") set(CPACK_PACKAGE_VERSION "1.0.0") set(CPACK_PACKAGE_VERSION_MAJOR "1") set(CPACK_PACKAGE_VERSION_MINOR "0") set(CPACK_PACKAGE_VERSION_PATCH "0") set(CPACK_RPM_PACKAGE_GROUP "WeTest") set(CPACK_RPM_PACKAGE_URL "http://wetest..com") set(CPACK_RPM_PACKAGE_DESCRIPTION "WeTest Server Dependencies") set(CPACK_PACKAGE_RELEASE 1) set(CPACK_RPM_PACKAGE_LICENSE "WeTest Licence") # 设置默认生成器,RPM生成器会构建RPM安装包,其它还有TGZ/ZIP等 set(CPACK_GENERATOR "RPM") # 安装前和安装后执行的shell脚本, 会打包到RPM中,安装时执行。这里可扩展性很强, 放在源码目录下即可 # set(CPACK_RPM_PRE_INSTALL_SCRIPT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/pre_script.sh) # set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/post_script.sh) # 引入CPack模块,必须的 include(CPack)
构建
其中capck支持其他的打包格式,比如tar.gz和zip,只需要执行cpack -G TGZ 或者 cpack -G ZIP就可打包成相应的格式。
rpm debuginfo包的作用
更多SPEC例子
报错记录
遇到最大的坑就是%install部分的make install
网上的资料是这样说的:
后来发现,使用make install是在已经编写过Makefile的前提下进行的,报错中的install其实是Makefile中的写好的target。
举个例子,在Makefile中编写,如下:
安装 rpm包出现error: unpacking of archive failed on file
【rpm】源码包制作rpm包|修改rpm、重新制作rpm包
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/116611.html

