如何用rpm打包

参考 https://www.cnblogs.com/schangech/p/5641108.htmlhttps://www.linuxidc.com/Linux/2013-12/93786.htm

  1. 建立一个普通用户,有普通用户来制作rpm,用root的可能会因为代码问题导致毁灭的后果

    useradd albert 
    su - albert 
    
  2. 确定我们在哪个目录下制作RPM,通常这个目录我们topdir,这个需要在宏配置文件中指定,这个配置文件称为macrofiles,它们通常为 usr/lib/rpm/macros:/usr/lib/rpm/macros.*:~.rpmmacros,这个在rhel 5.8中可以通过rpmbuild –showrc | grep macrofiles 查看,6.3的我使用这个找不到,但使用是一样的。你可以通过rpmbuild –showrc | grep topdir 查看你系统默认的工作目录。

    rpmbuild --showrc | grep topdir 
    
    -14: _builddir  %{_topdir}/BUILD 
    -14: _buildrootdir  %{_topdir}/BUILDROOT 
    -14: _rpmdir    %{_topdir}/RPMS 
    -14: _sourcedir %{_topdir}/SOURCES 
    -14: _specdir   %{_topdir}/SPECS 
    -14: _srcrpmdir %{_topdir}/SRPMS 
    -14: _topdir    %{getenv:HOME}/rpmbuild 
    

    我们还是自定义工作目录

    vi ~/.rpmmacros 
    %_topdir    /home/albert/rpmbuild    ##目录可以自定义 
    
    mkdir ~/rpmbuild  
    
  3. 在topdir下建立需要的目录

    cd ~/rpmbuild  
    mkdir -pv {BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} 
    
  4. 把收集的源码放到SOURCES下

    cp /tmp/tengine-1.4.2.tar.gz ~/rpmbuild/SOURCES  #事先放好的
    
  5. 在SPECS下建立重要的spec文件

    cd SPECS 
    vim tengine.spec          ##内容见后讲解,rhel6.3会自动生成模板 
    
  6. 用rpmbuild命令制作rpm包,rpmbuild命令会根据spec文件来生成rpm包

    rpmbuild  
      -ba 既生成src.rpm又生成二进制rpm 
      -bs 只生成src的rpm 
      -bb 只生二进制的rpm 
      -bp 执行到pre 
      -bc 执行到 build段 
      -bi 执行install段 
      -bl 检测有文件没包含 
    

    我们可以一步步试,先rpmbuild -bp ,再-bc 再-bi 如果没问题,rpmbuild -ba 生成src包与二进制包吧

  7. spec

    [albert@master SPECS]$ cat tengine.spec
    %define _unpackaged_files_terminate_build 0
    %define _missing_doc_files_terminate_build 0
    
    %define _prefix    /home/albert/nginx
    %define _user      albert
    #%define _user_uid  99
    %define _group     albert
    #%define _group_uid 99
    #%define _sbin_path      /usr/sbin
    
    %define name      Tengine
    %define summary   Tengine modified for pingmesh
    %define version   2.2.3
    %define release   1
    %define license   GPL
    %define group     Application/WebServer
    %define source    tengine-%{version}.tar.gz
    %define url       http://tengine.taobao.org/
    %define vendor    Taobao
    %define packager  albert
    
    
    Name:       tengine
    Version:    2.2.3
    Release:    1
    Vendor:     Taobao
    Summary:    GUN Tengine 2.2.3 X86_64
    
    Group:      System Enviroment/Daemons
    License:    GPL
    URL:        http://tengine.taobao.org/
    
    Source0:    tengine-%{version}.tar.gz
    
    Packager:   albert
    
    BuildRoot:  %{_tmppath}/%{name}-%{version}-%{release}-root
    
    
    %description
    Taobao tengine for pingmesh with modified ngx_tcp_proxy_module
    
    
    %prep
    %setup -q -n tengine-%{version}
    
    
    %build
    ./configure \
      --without-pcre \
      --with-http_v2_module \
      --without-http_limit_conn_module \
      --without-http_limit_req_module \
      --without-http_rewrite_module \
      --without-select_module \
      --without-poll_module \
      --without-http_userid_module \
      --without-http_auth_basic_module \
      --without-http_autoindex_module \
      --without-http_memcached_module \
      --without-http_empty_gif_module \
      --with-http_ssl_module \
      --with-http_upstream_check_module \
      --without-http_fastcgi_module \
      --without-http_uwsgi_module \
      --without-http_scgi_module \
      --prefix=/home/albert/nginx \
      --conf-path=/home/albert/nginx/conf/nginx.conf \
      --add-module=/home/albert/nginx_tcp_proxy_module
    make
    
    
    %install
    rm -rf %{buildroot}
    make install DESTDIR=%{buildroot}
    #make DESTDIR=$RPM_BUILD_ROOT install
    
    
    %clean
    #[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf "$RPM_BUILD_ROOT"
    #make clean
    rm -rf %{buildroot}
    
    
    %pre
    grep -q ^%{_group}: /etc/group || %{_sbin_path}/groupadd -g %{_group_gid} %{_group}
    grep -q ^%{_user}: /etc/passwd || %{_sbin_path}/useradd -g %{_group} -u %{_user_uid} -d %{_prefix} -s /sbin/nologin -M %{_user}
    
    
    %files
    %defattr (-,%{_user},%{_group})
    %dir %{_prefix}/
    #%dir /home/albert/nginx/
    #%dir %{_prefix}
    #%defattr(-,root,root,-)
    #%dir %{_prefix}/
    %attr(0755,%{_user},%{_group}) %dir %{_prefix}/logs
    #%dir %{_prefix}/modules
    %dir %{_prefix}/sbin
    %dir %{_prefix}/conf
    %dir %{_prefix}/html
    %{_prefix}/sbin/nginx
    %{_prefix}/sbin/dso_tool
    %{_prefix}/conf/module_stubs
    %{_prefix}/conf/win-utf
    %{_prefix}/conf/koi-utf
    %{_prefix}/conf/nginx.conf.default
    %{_prefix}/conf/koi-win
    %{_prefix}/conf/mime.types
    %config(noreplace) %{_prefix}/conf/nginx.conf
    %{_prefix}/conf/mime.types.default
    %{_prefix}/html/50x.html
    %{_prefix}/html/index.html
    %{_prefix}/conf/browsers
    #%{_initrddir}/tengine
    %doc %{_prefix}/html/index.html
    %attr(0775,%{_user},%{_group}) %{_prefix}/sbin/nginx
    
    
    %changelog
     * Mon Jan 21 2019 Jinan <georgealbert@qq.com>
     - ver 2.2.3 
    

rpmbuild报错Installed (but unpackaged) file(s) found https://blog.csdn.net/zhoujiaxq/article/details/38108787

_unpackaged_files_terminate_build 0  
_missing_doc_files_terminate_build 0 

在tengine.spec里面直接
%define _unpackaged_files_terminate_build 0
%define _missing_doc_files_terminate_build 0

非root用户安装rpm包,只能 sudo 了

[albert@master x86_64]$ rpm -qpl tengine-2.2.3-1.x86_64.rpm
/home/albert/nginx
/home/albert/nginx/conf
/home/albert/nginx/conf/browsers
/home/albert/nginx/conf/koi-utf
/home/albert/nginx/conf/koi-win
/home/albert/nginx/conf/mime.types
/home/albert/nginx/conf/mime.types.default
/home/albert/nginx/conf/module_stubs
/home/albert/nginx/conf/nginx.conf
/home/albert/nginx/conf/nginx.conf.default
/home/albert/nginx/conf/win-utf
/home/albert/nginx/html
/home/albert/nginx/html/50x.html
/home/albert/nginx/html/index.html
/home/albert/nginx/logs
/home/albert/nginx/modules
/home/albert/nginx/sbin
/home/albert/nginx/sbin/dso_tool
/home/albert/nginx/sbin/nginx
[albert@master x86_64]$ rpm -qi tengine-2.2.3-1.x86_64.rpm
package tengine-2.2.3-1.x86_64.rpm is not installed

[albert@master x86_64]$ rpm -qpi tengine-2.2.3-1.x86_64.rpm
Name        : tengine
Version     : 2.2.3
Release     : 1
Architecture: x86_64
Install Date: (not installed)
Group       : System Enviroment/Daemons
Size        : 1002368
License     : GPL
Signature   : (none)
Source RPM  : tengine-2.2.3-1.src.rpm
Build Date  : Mon 21 Jan 2019 05:25:55 PM CST
Build Host  : master.k8s
Relocations : (not relocatable)
Packager    : albert
Vendor      : Taobao
URL         : http://tengine.taobao.org/
Summary     : GUN Tengine 2.2.3 X86_64
Description :
Taobao tengine for pingmesh with modified ngx_tcp_proxy_module