参考:https://rpm-packaging-guide.github.io/
先说一下最简单步骤、例子
centos
安装:
yum install rpm-build
yum install rpmdevtools
安装完之后可以用rpmbuild --version查看版本
假如当前用户是user1
su - user1
能在~目录看到多了一个目录rpmbuild,和一个.rpmmacros文件(以.开始的文件名默认不可见),
进入~/rpmbuild/SPECS目录
touch hello-world.spce,新增hello-world.spec文件,填入内容如下
Name: hello-world
Version: 1
Release: 1
Summary: Most simple RPM package
License: ihavenolicense@me
Group: Applications/System
Url: http://www.example.com
%description
This is my first RPM package, which does nothing.
%prep
# we have no source, so nothing here
%build
echo "build build build **********"
cat > hello-world.sh << EOF
#!/usr/bin/bash
echo Hello world
EOF
%install
echo "install for test rpm"
mkdir -p %{buildroot}/usr/bin/
install -m 755 hello-world.sh %{buildroot}/usr/bin/hello-world.sh
%pre
echo "pre install"
%post
echo "post install"
%files
/usr/bin/hello-world.sh
%changelog
# let's skip this for now
然后执行rpmbuild -ba hello-world.spec
执行成功后,
~/rpmbuild/SRPMS目录里面会看到hello-world-1-1.src.rpm文件,这是源码安装文件
~/rpmbuild/RPMS目录里面会看到x86_64目录,x86_64目录里面会看到hello-world-1-1.x86_64.rpm文件,这是二进制安装文件
然后就可以通过rpm -ivh hello-world-1-1.x86_64.rpm执行安装包过程,不能重复安装,重复安装前要卸载,通过rpm -e --nodeps hello-world-1-1.x86_64卸载包。
安装完成之后会看到/usr/bin目录里面多了一个hello-world.sh,至此,最简单的打包流程就完成了。
接下来分析如上spec文件
执行rpmbuild -ba hello-world.spec的过程中可以看到输出,也就可以知道,%build和%install发生在打包过程中,打包完成之后能在~/rpmbuild/BUILD目录看到build生成的hello-world.sh文件,
如果执行rpmbuild -bi ./hello-world.spec,不会生成包,只执行到打包的install阶段,可以看到~/rpmbuild/BUILDROOT目录中有hello-world-1-1.x86_64存在,%files里面的文件根目录就是~/rpmbuild/BUILDROOT,安装包的时候就会把%files里面的hello-world.sh安装到/usr/bin/目录
上例只是为了最简单,所以只有一个spec文件,实际情况可以在SOURCES目录放入更多文件,在BUILD目录生成更多文件,然后在%install阶段,将其放入~/rpmbuild/BUILDROOT/x86_64目录,打包过程,%files中的文件会从~/rpmbuild/BUILDROOT/x86_64 目录中提取打包,最终生成安装包。
%install
echo "install section **********"
echo %{_sourcedir}
mkdir -p %{buildroot}/usr/bin/
mkdir -p %{buildroot}/etc/
install -m 755 hello-world.sh %{buildroot}/usr/bin/hello-world.sh
install -m 755 %{_sourcedir}/etc/hello-world.conf %{buildroot}/etc/hello-world.conf
%files
/usr/bin/hello-world.sh
/etc/hello-world.conf
如上,须先在%{_sourcedir}/etc目录创建hello-world.conf文件,也可以放其他地方,注意install命令将其复制到%{buildroot}/etc目录即可
执行安装包的过程前,%pre阶段的脚本会被执行,安装之后%post阶段的脚本会执行
rpm -ivh hello-world-1-1.x86_64.rpm
Preparing... ################################# [100%]
pre install
Updating / installing...
1:hello-world-1-1 ################################# [100%]
post install
自动生成初始化的spec文件
rpmdev-newspec hello
会在当前目录生成hello.spec