免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: duanjigang
打印 上一主题 下一主题

Linux平台软件管理系统设计与规划-进阶篇(2)-rpm生成:rpmbuild 和 spec文件剖析 [复制链接]

论坛徽章:
0
31 [报告]
发表于 2012-12-22 22:40 |只看该作者
到此为止,第二篇告一段落,想起来什么再补上吧,阅读的朋友有什么意见和发现的问题欢迎与我交流。以下是参考的所有文章:

论坛徽章:
0
32 [报告]
发表于 2012-12-23 10:24 |只看该作者
好文档啊,果断收藏了

论坛徽章:
0
33 [报告]
发表于 2012-12-24 21:51 |只看该作者
本帖最后由 duanjigang 于 2012-12-24 21:52 编辑



附上一个nginx的spec 文件,感兴趣的朋友可以分析下:
nginx.zip (2.38 KB, 下载次数: 28)

  1. #
  2. %define nginx_home %{_localstatedir}/cache/nginx
  3. %define nginx_user nginx
  4. %define nginx_group nginx

  5. Summary: high performance web server
  6. Name: nginx
  7. Version: 1.2.6
  8. Release: 1%{?dist}.ngx
  9. Vendor: nginx inc.
  10. URL: http://nginx.org/

  11. Source0: http://nginx.org/download/%{name}-%{version}.tar.gz
  12. Source1: logrotate
  13. Source2: nginx.init
  14. Source3: nginx.sysconf
  15. Source4: nginx.conf
  16. Source5: nginx.vh.default.conf
  17. Source6: nginx.vh.example_ssl.conf
  18. Source7: nginx.suse.init

  19. License: 2-clause BSD-like license
  20. %if 0%{?suse_version}
  21. Group: Productivity/Networking/Web/Servers
  22. %else
  23. Group: System Environment/Daemons
  24. %endif

  25. BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
  26. BuildRequires: zlib-devel
  27. BuildRequires: pcre-devel
  28. BuildRequires: perl
  29. %if 0%{?suse_version}
  30. BuildRequires: libopenssl-devel
  31. Requires(pre): pwdutils
  32. %else
  33. BuildRequires: openssl-devel
  34. Requires: initscripts >= 8.36
  35. Requires(pre): shadow-utils
  36. Requires(post): chkconfig
  37. %endif
  38. Provides: webserver

  39. %description
  40. nginx [engine x] is a HTTP and reverse proxy server, as well as
  41. a mail proxy server

  42. %package debug
  43. Summary: debug version of nginx
  44. Group: System Environment/Daemons
  45. Requires: nginx
  46. %description debug
  47. not stripped version of nginx build with the debugging log support

  48. %prep
  49. %setup -q

  50. %build
  51. ./configure \
  52.         --prefix=%{_sysconfdir}/nginx/ \
  53.         --sbin-path=%{_sbindir}/nginx \
  54.         --conf-path=%{_sysconfdir}/nginx/nginx.conf \
  55.         --error-log-path=%{_localstatedir}/log/nginx/error.log \
  56.         --http-log-path=%{_localstatedir}/log/nginx/access.log \
  57.         --pid-path=%{_localstatedir}/run/nginx.pid \
  58.         --lock-path=%{_localstatedir}/run/nginx.lock \
  59.         --http-client-body-temp-path=%{_localstatedir}/cache/nginx/client_temp \
  60.         --http-proxy-temp-path=%{_localstatedir}/cache/nginx/proxy_temp \
  61.         --http-fastcgi-temp-path=%{_localstatedir}/cache/nginx/fastcgi_temp \
  62.         --http-uwsgi-temp-path=%{_localstatedir}/cache/nginx/uwsgi_temp \
  63.         --http-scgi-temp-path=%{_localstatedir}/cache/nginx/scgi_temp \
  64.         --user=%{nginx_user} \
  65.         --group=%{nginx_group} \
  66.         --with-http_ssl_module \
  67.         --with-http_realip_module \
  68.         --with-http_addition_module \
  69.         --with-http_sub_module \
  70.         --with-http_dav_module \
  71.         --with-http_flv_module \
  72.         --with-http_mp4_module \
  73.         --with-http_gzip_static_module \
  74.         --with-http_random_index_module \
  75.         --with-http_secure_link_module \
  76.         --with-http_stub_status_module \
  77.         --with-mail \
  78.         --with-mail_ssl_module \
  79.         --with-file-aio \
  80.         --with-ipv6 \
  81.         --with-debug \
  82. #       --with-cc-opt="%{optflags} $(pcre-config --cflags)" \
  83.         $*
  84. make
  85. %{__mv} %{_builddir}/%{name}-%{version}/objs/nginx \
  86.         %{_builddir}/%{name}-%{version}/objs/nginx.debug
  87. ./configure \
  88.         --prefix=%{_sysconfdir}/nginx/ \
  89.         --sbin-path=%{_sbindir}/nginx \
  90.         --conf-path=%{_sysconfdir}/nginx/nginx.conf \
  91.         --error-log-path=%{_localstatedir}/log/nginx/error.log \
  92.         --http-log-path=%{_localstatedir}/log/nginx/access.log \
  93.         --pid-path=%{_localstatedir}/run/nginx.pid \
  94.         --lock-path=%{_localstatedir}/run/nginx.lock \
  95.         --http-client-body-temp-path=%{_localstatedir}/cache/nginx/client_temp \
  96.         --http-proxy-temp-path=%{_localstatedir}/cache/nginx/proxy_temp \
  97.         --http-fastcgi-temp-path=%{_localstatedir}/cache/nginx/fastcgi_temp \
  98.         --http-uwsgi-temp-path=%{_localstatedir}/cache/nginx/uwsgi_temp \
  99.         --http-scgi-temp-path=%{_localstatedir}/cache/nginx/scgi_temp \
  100.         --user=%{nginx_user} \
  101.         --group=%{nginx_group} \
  102.         --with-http_ssl_module \
  103.         --with-http_realip_module \
  104.         --with-http_addition_module \
  105.         --with-http_sub_module \
  106.         --with-http_dav_module \
  107.         --with-http_flv_module \
  108.         --with-http_mp4_module \
  109.         --with-http_gzip_static_module \
  110.         --with-http_random_index_module \
  111.         --with-http_secure_link_module \
  112.         --with-http_stub_status_module \
  113.         --with-mail \
  114.         --with-mail_ssl_module \
  115.         --with-file-aio \
  116.         --with-ipv6 \
  117. #        --with-cc-opt="%{optflags} $(pcre-config --cflags)" \
  118.         $*
  119. make

  120. %install
  121. %{__rm} -rf $RPM_BUILD_ROOT
  122. %{__make} DESTDIR=$RPM_BUILD_ROOT install

  123. %{__mkdir} -p $RPM_BUILD_ROOT%{_datadir}/nginx
  124. %{__mv} $RPM_BUILD_ROOT%{_sysconfdir}/nginx/html $RPM_BUILD_ROOT%{_datadir}/nginx/

  125. %{__rm} -f $RPM_BUILD_ROOT%{_sysconfdir}/nginx/*.default
  126. %{__rm} -f $RPM_BUILD_ROOT%{_sysconfdir}/nginx/fastcgi.conf

  127. %{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/log/nginx
  128. %{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/run/nginx
  129. %{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/cache/nginx

  130. %{__mkdir} -p $RPM_BUILD_ROOT%{_sysconfdir}/nginx/conf.d
  131. %{__rm} $RPM_BUILD_ROOT%{_sysconfdir}/nginx/nginx.conf
  132. %{__install} -m 644 -p %{SOURCE4} \
  133.    $RPM_BUILD_ROOT%{_sysconfdir}/nginx/nginx.conf
  134. %{__install} -m 644 -p %{SOURCE5} \
  135.    $RPM_BUILD_ROOT%{_sysconfdir}/nginx/conf.d/default.conf
  136. %{__install} -m 644 -p %{SOURCE6} \
  137.    $RPM_BUILD_ROOT%{_sysconfdir}/nginx/conf.d/example_ssl.conf

  138. %{__mkdir} -p $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig
  139. %{__install} -m 644 -p %{SOURCE3} \
  140.    $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig/nginx

  141. # install SYSV init stuff
  142. %{__mkdir} -p $RPM_BUILD_ROOT%{_initrddir}
  143. %if 0%{?suse_version}
  144. %{__install} -m755 %{SOURCE7} \
  145.    $RPM_BUILD_ROOT%{_initrddir}/nginx
  146. %else
  147. %{__install} -m755 %{SOURCE2} \
  148.    $RPM_BUILD_ROOT%{_initrddir}/nginx

  149. %endif
  150. # install log rotation stuff
  151. %{__mkdir} -p $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d
  152. %{__install} -m 644 -p %{SOURCE1} \
  153.    $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d/nginx
  154. %{__install} -m644 %{_builddir}/%{name}-%{version}/objs/nginx.debug \
  155.    $RPM_BUILD_ROOT%{_sbindir}/nginx.debug

  156. %clean
  157. %{__rm} -rf $RPM_BUILD_ROOT

  158. %files
  159. %defattr(-,root,root)

  160. %{_sbindir}/nginx

  161. %dir %{_sysconfdir}/nginx
  162. %dir %{_sysconfdir}/nginx/conf.d

  163. %config(noreplace) %{_sysconfdir}/nginx/nginx.conf
  164. %config(noreplace) %{_sysconfdir}/nginx/conf.d/default.conf
  165. %config(noreplace) %{_sysconfdir}/nginx/conf.d/example_ssl.conf
  166. %config(noreplace) %{_sysconfdir}/nginx/mime.types
  167. %config(noreplace) %{_sysconfdir}/nginx/fastcgi_params
  168. %config(noreplace) %{_sysconfdir}/nginx/scgi_params
  169. %config(noreplace) %{_sysconfdir}/nginx/uwsgi_params
  170. %config(noreplace) %{_sysconfdir}/nginx/koi-utf
  171. %config(noreplace) %{_sysconfdir}/nginx/koi-win
  172. %config(noreplace) %{_sysconfdir}/nginx/win-utf

  173. %config(noreplace) %{_sysconfdir}/logrotate.d/nginx
  174. %config(noreplace) %{_sysconfdir}/sysconfig/nginx
  175. %{_initrddir}/nginx

  176. %dir %{_datadir}/nginx
  177. %dir %{_datadir}/nginx/html
  178. %{_datadir}/nginx/html/*

  179. %attr(0755,root,root) %dir %{_localstatedir}/cache/nginx
  180. %attr(0755,root,root) %dir %{_localstatedir}/log/nginx

  181. %files debug
  182. %attr(0755,root,root) %{_sbindir}/nginx.debug

  183. %pre
  184. # Add the "nginx" user
  185. getent group %{nginx_group} >/dev/null || groupadd -r %{nginx_group}
  186. getent passwd %{nginx_user} >/dev/null || \
  187.     useradd -r -g %{nginx_group} -s /sbin/nologin \
  188.     -d %{nginx_home} -c "nginx user"  %{nginx_user}
  189. exit 0

  190. %post
  191. # Register the nginx service
  192. if [ $1 -eq 1 ]; then
  193.     /sbin/chkconfig --add nginx
  194.     # print site info
  195.     cat <<BANNER
  196. ----------------------------------------------------------------------

  197. Thanks for using NGINX!

  198. Check out our community web site:
  199. * http://nginx.org/en/support.html

  200. If you have questions about commercial support for NGINX please visit:
  201. * http://www.nginx.com/support.html

  202. ----------------------------------------------------------------------
  203. BANNER
  204. fi

  205. %preun
  206. if [ $1 -eq 0 ]; then
  207.     /sbin/service nginx stop > /dev/null 2>&1
  208.     /sbin/chkconfig --del nginx
  209. fi

  210. %postun
  211. if [ $1 -ge 1 ]; then
  212.     /sbin/service nginx upgrade &>/dev/null || :
  213. fi

  214. %changelog
  215. * Tue Dec 11 2012 Sergey Budnevitch <sb@nginx.com>
  216. - 1.2.6

  217. * Tue Nov 13 2012 Sergey Budnevitch <sb@nginx.com>
  218. - 1.2.5

  219. * Tue Sep 25 2012 Sergey Budnevitch <sb@nginx.com>
  220. - 1.2.4

  221. * Tue Aug  7 2012 Sergey Budnevitch <sb@nginx.com>
  222. - 1.2.3
  223. - nginx-debug package now actually contains non stripped binary

  224. * Tue Jul  3 2012 Sergey Budnevitch <sb@nginx.com>
  225. - 1.2.2

  226. * Tue Jun  5 2012 Sergey Budnevitch <sb@nginx.com>
  227. - 1.2.1

  228. * Mon Apr 23 2012 Sergey Budnevitch <sb@nginx.com>
  229. - 1.2.0

  230. * Thu Apr 12 2012 Sergey Budnevitch <sb@nginx.com>
  231. - 1.0.15

  232. * Thu Mar 15 2012 Sergey Budnevitch <sb@nginx.com>
  233. - 1.0.14
  234. - OpenSUSE init script and SuSE specific changes to spec file added

  235. * Mon Mar  5 2012 Sergey Budnevitch <sb@nginx.com>
  236. - 1.0.13

  237. * Mon Feb  6 2012 Sergey Budnevitch <sb@nginx.com>
  238. - 1.0.12
  239. - banner added to install script

  240. * Thu Dec 15 2011 Sergey Budnevitch <sb@nginx.com>
  241. - 1.0.11
  242. - init script enhancements (thanks to Gena Makhomed)
  243. - one second sleep during upgrade replaced with 0.1 sec usleep

  244. * Tue Nov 15 2011 Sergey Budnevitch <sb@nginx.com>
  245. - 1.0.10

  246. * Tue Nov  1 2011 Sergey Budnevitch <sb@nginx.com>
  247. - 1.0.9
  248. - nginx-debug package added

  249. * Tue Oct 11 2011 Sergey Budnevitch <sb@nginx.com>
  250. - spec file cleanup (thanks to Yury V. Zaytsev)
  251. - log dir permitions fixed
  252. - logrotate creates new logfiles with nginx owner
  253. - "upgrade" argument to init-script added (based on fedora one)

  254. * Sat Oct  1 2011 Sergey Budnevitch <sb@nginx.com>
  255. - 1.0.8
  256. - built with mp4 module

  257. * Fri Sep 30 2011 Sergey Budnevitch <sb@nginx.com>
  258. - 1.0.7

  259. * Tue Aug 30 2011 Sergey Budnevitch <sb@nginx.com>
  260. - 1.0.6
  261. - replace "conf.d/*" config include with "conf.d/*.conf" in default nginx.conf

  262. * Tue Aug 10 2011 Sergey Budnevitch
  263. - Initial release

复制代码

论坛徽章:
0
34 [报告]
发表于 2012-12-24 23:32 |只看该作者
我最近也在研究rpmbuild,max-rpm已经快看完了。从哪里能下载到一些(>10个)的source rpm,然后学习下他们的spec脚本?

另外,我在一些spec中看到这样的写法,不知道其作用:

%post -p /sbin/ldconfig

'-p'在这里是什么意思呢?

论坛徽章:
0
35 [报告]
发表于 2012-12-25 05:03 |只看该作者
回复 34# libinbluewolf

spec 中默认对 hook 段的代码采用 /bin/sh 去解释,用户可以自己修改解释器,通过 %hook -p path 的方式来指定。
常见到的好多lib中

  1. %post  -p /sbin/ldconfig
  2. %postun -p /sbin/ldconfig
复制代码
的方式就是在安装/升级/卸载后显示调用/sbin/ldconfig 来更新链接库信息,比如你也可以写成这样子

  1. %post -p /bin/date
  2. %postun -p /usr/bin/uptime
复制代码
这些都容易理解,但是有一个现象比较奇怪,我还没弄清楚。
%post %postun 或者 %preun
在一起出现两个或者以上时 -p path 执行的就没问题,
但是单个出现时,比如:

  1. %post  -p /bin/date

  2. #%preun  -p /usr/bin/uptime

  3. %verifyscript
  4. echo "verify by duanjigang now"
复制代码
这样的方式,安装时,它就会把操作标志位通过第一个参数的方式传递给解释器,从而导致报错,比如 install 时参数是1,update 是2
我上面的写法在安装时会报错:

  1. sudo rpm -ivh RPMS/i386/test-baby-1.1-1.i386.rpm
  2. Preparing...                ########################################### [100%]
  3.    1:test-baby              ########################################### [100%]
  4. /bin/date: 额外的操作数 “1”
  5. 请尝试执行“/bin/date --help”来获取更多信息。
  6. error: %post(test-baby-1.1-1.i386) scriptlet failed, exit status 1
复制代码
这个可以理解,是因为执行了 "/bin/date 1"命令。 但是不清楚为什么%post 单独出现时就会传递operation参数1/2给它,而最少两个hook出现时就不会传递参数给它。。
有知道这个现象的朋友帮解释下,谢谢!



   

论坛徽章:
0
36 [报告]
发表于 2012-12-25 17:10 |只看该作者
所以
  1. %post -p /sbin/ldconfig
复制代码
完全等同于
  1. %post
  2. /sbin/ldconfig
复制代码
喽?

你说的现象难道是把这行
  1. #%preun  -p /usr/bin/uptime
复制代码
uncomment的话,就不会传递operation参数(1/2)给%post/%postun了吗? 我试了一下,并没有你所说的现象……

论坛徽章:
0
37 [报告]
发表于 2012-12-25 17:14 |只看该作者
本帖最后由 libinbluewolf 于 2012-12-26 21:21 编辑

我还有其他的一些疑问,发到这里讨论一下:

  1. - What is `%{with xxx}`

  2.         %if %{with openssl}
  3.         BuildRequires:  openssl-devel
  4.         %endif

  5.         `%{with xxx}` is shorthand for `%{defined with_xxx}`.

  6. - What is `0%{?xxx:1}`

  7.         %if 0%{?_with_stunnel:1}
  8.         # used by the testsuite
  9.         BuildRequires:  stunnel
  10.         %endif

  11.         `{?_with_stunnel:1}`是一个Macro,如果定义了`_with_stunnel` tag,则这个Macro被扩展成`1`,否则就是nothing。`%if 0%{?with_stunnel:1}等同于`%if %{with stuneel}`.
  12.         还有另外一种相反的语法`%{!?SOME:1}`,如果没定义`SOME` tag,则该Macro被扩展为`1`,否则就是nothing。
  13.         因此,下面这句就完成了常用的一种逻辑:使用变量CUSTOM_RELEASE的值,如果没定义,则使用1
  14.         %define rel     %{?CUSTOM_RELEASE} %{!?CUSTOM_RELEASE:1}

  15. - What is `Obsoletes`

  16.         # bug437293
  17.         %ifarch ppc64
  18.         Obsoletes:      curl-64bit
  19.         %endif

  20.         声明这个rpm package安装后,`curl-64bit`这个package就可以废弃了。Refer to http://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch-dependencies.html#RPM_Guide-Dependencies-obsoletes

  21. - What is `%defattr(-,root,root)` in %files list

  22.         What's the difference with `attr` directive?

  23.         Ans. `%defattr` is default attribution. 格式是`%defattr(file_mode, owner, group, dir_mod)`. Refer to http://www.rpm.org/max-rpm-snapshot/s1-rpm-inside-files-list-directives.html

  24. - What is `Requires(pre)`

  25.         Requires(pre): /usr/sbin/useradd
  26.         Requires(post): chkconfig

  27.         我实验的结果是括号中的内容被rpmbuild/rpm忽略,可能只是为了可读性.

  28. - What is `Prereq` and `BuildPrereq`

  29.         # In httpd.spec
  30.         BuildPrereq: apr-devel, apr-util-devel, openldap-devel, db4-devel, expat-devel, findutils, perl, pkgconfig, pcre-devel >= 5.0
  31.         BuildPrereq: /usr/bin/apr-1-config, /usr/bin/apu-1-config
  32.         Requires: apr >= 1.4.2, apr-util >= 1.3.10, pcre >= 5.0, gawk, /usr/bin/find, openldap
  33.         Prereq: /sbin/chkconfig, /bin/mktemp, /bin/rm, /bin/mv
  34.         Prereq: sh-utils, textutils, /usr/sbin/useradd


复制代码

论坛徽章:
0
38 [报告]
发表于 2012-12-25 21:55 |只看该作者
你说的第一点正是我要表达的。
第二个现象我再检查下我的spec文件。

回复 36# libinbluewolf


   

论坛徽章:
0
39 [报告]
发表于 2013-03-22 12:35 |只看该作者
谢谢分享!!!!:wink:

论坛徽章:
9
CU大牛徽章
日期:2013-04-17 11:06:23CU大牛徽章
日期:2013-04-17 11:08:52CU大牛徽章
日期:2013-04-17 11:09:10CU大牛徽章
日期:2013-04-17 11:09:40CU大牛徽章
日期:2013-04-17 11:09:57CU大牛徽章
日期:2013-04-17 11:10:17CU大牛徽章
日期:2013-05-20 10:43:41CU大牛徽章
日期:2013-05-20 10:44:06CU大牛徽章
日期:2013-05-20 10:44:16
40 [报告]
发表于 2013-03-28 16:18 |只看该作者
先mark再看。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP