本文在 CentOS 7 上验证通过。 # 准备 安装`rpmbuild`和`rpmdevtools`: ``` yum install rpmbuild yum install rpmdevtools ``` # 打包 用户的`home`目录下创建`rpmbuild`目录: ``` [root@localhost ~]# mkdir rpmbuild ``` 在`rpmbuild`目录下创建`BUILD`、`BUILDROOT`、`SPECS`、`RPMS`和`SRPMS`等目录,也可以`rpmdev-setuptree`工具创建: ``` [root@localhost ~]# rpmdev-setuptree ``` `rpmbuild/SPECS`目录中创建并编写一个`libcgroup-tools-cgred-0.41-21.el7.x86_64.spec`文件,名字无所谓,格式如下: ``` Name: libcgroup-tools-cgred Version: 0.41 Release: 21.el7 Summary: libcgroup-tools-cgred package. Group: Applications/System License: GPL URL: www.200yi.com %description bcgroup-tools-cgred rpm package. %prep %build %install mkdir -p $RPM_BUILD_ROOT/usr/sbin mkdir -p $RPM_BUILD_ROOT/usr/lib/systemd/system mkdir -p $RPM_BUILD_ROOT/etc/sysconfig mkdir -p $RPM_BUILD_ROOT/etc/cgconfig.d mkdir -p $RPM_BUILD_ROOT/etc/cgrules.d cp ../BUILD/etc/cgrules.conf $RPM_BUILD_ROOT/etc/ cp ../BUILD/etc/sysconfig/cgred $RPM_BUILD_ROOT/etc/sysconfig/ cp ../BUILD/usr/sbin/cgrulesengd $RPM_BUILD_ROOT/usr/sbin/ cp ../BUILD/usr/lib/systemd/system/cgred.service $RPM_BUILD_ROOT/usr/lib/systemd/system/ %files /etc/cgrules.conf /etc/sysconfig/cgred /etc/cgconfig.d/ /etc/cgrules.d/ /usr/sbin/cgrulesengd /usr/lib/systemd/system/cgred.service ``` 把需要打包的文件按照目录结构放到`rpmbuild/BUILD`目录下。 现在可以执行打包命令了: ``` [root@localhost SPECS]# rpmbuild -bb libcgroup-tools-cgred-0.41-21.el7.x86_64.spec ``` 打包成功后,`rpm`包将生成在`rpmbuild/RPMS`目录下。 # 打包遇到的问题 ## contains an invalid rpath 错误内容如下: ``` ERROR 0002: file 'xxx.so' contains an invalid rpath 'xxx' in [xxx] ``` 解决方法: 修改`~/.rpmmacros`文件,注释掉以下行: ``` %__arch_install_post \ ... /usr/lib/rpm/check-rpaths /usr/lib/rpm/check-buildroot ``` ## /etc/ld.so.conf: No such file or directory 警告内容如下: ``` /sbin/ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory ``` # 安装rpm ``` [root@localhost x86_64]# rpm -ivh ./libcgroup-tools-cgred-0.41-21.el7.x86_64.rpm ``` # 参考 * [二进制包制作rpm (service)](https://blog.csdn.net/banana1006034246/article/details/97414622) * [构建RPM之SPEC详解](https://www.cnblogs.com/yuhaohao/p/12894875.html)