在安装/升级 rpm 时如何在 %pre、%post、%preun 或者 %postun 阶段(规范文件)执行脚本

RPM 规范文件有几个部分,允许包在安装和删除时运行代码。
这些代码被称为scriptlet,主要用于使用包中的信息更新正在运行的系统。

当调用 scriptlet 时,它们将被提供一个参数。
这个参数,通过 $1 访问(对于 shell 脚本)是这个名称的包的数量,当操作完成时它将留在系统上

所有小脚本必须以0退出状态退出。

执行顺序

%pre 和 %post 中的 scriptlet 分别在安装包之前和之后运行。
脚本 %preun 和 %postun 在卸载包之前和之后运行。
脚本 %pretrans 和 %posttrans 在事务的开始和结束时运行。
升级时,脚本按以下顺序运行:

  • 新软件包的%pre
    (安装软件包)
  • 新软件包的%post
  • 旧软件包的%preun
    (移除旧安装包)
  • 旧软件包的%postun

我创建了两个具有两个不同版本的 rpm,在规范文件中具有以下内容

%pre
echo "-------------"
echo "This is pre"
echo "Install Value: "
echo "Upgrade Value: "
echo "Uninstall Value: "
echo "-------------"
%post
echo "-------------"
echo "This is post"
echo "Install Value: "
echo "Upgrade Value: "
echo "Uninstall Value: "
echo "-------------"
%preun
echo "-------------"
echo "This is preun"
echo "Install Value: "
echo "Upgrade Value: "
echo "Uninstall Value: "
echo "-------------"
%postun
echo "-------------"
echo "This is postun"
echo "Install Value: "
echo "Upgrade Value: "
echo "Uninstall Value: "
echo "-------------"

安装rpm软件包

以下是安装 rpm 时的脚本值

# rpm -ivh /tmp/rpmbuild/RPMS/x86_64/jack-1.0.0-1.x86_64.rpm
Preparing...                          ################################# [100%]
------------
This is pre
Install Value: 1
Upgrade Value: 1
Uninstall Value: 1
------------
Updating/installing...
   1:jack-1.0.0-1                   ################################# [100%]
------------
This is post
Install Value: 1
Upgrade Value: 1
Uninstall Value: 1
------------

更新rpm软件包

# rpm -Uvh /tmp/rpmbuild/RPMS/x86_64/jack-2.0.0-1.x86_64.rpm
Preparing...                          ################################# [100%]
------------
This is pre
Install Value: 2
Upgrade Value: 2
Uninstall Value: 2
------------
Updating/installing...
   1:jack-2.0.0-1                   ################################# [ 50%]
------------
This is post
Install Value: 2
Upgrade Value: 2
Uninstall Value: 2
------------
------------
This is preun
Install Value: 1
Upgrade Value: 1
Uninstall Value: 1
------------
Cleaning up/removing...
   2:jack-1.0.0-1                   ################################# [100%]
------------
This is postun
Install Value: 1
Upgrade Value: 1
Uninstall Value: 1
------------

删除 rpm软件包

# rpm -e jack
------------
This is preun
Install Value: 0
Upgrade Value: 0
Uninstall Value: 0
------------
------------
This is postun
Install Value: 0
Upgrade Value: 0
Uninstall Value: 0
------------

正如我们现在看到的,我们有必须使用的脚本值。

所以让我们用它们来编写一些脚本

最终的spec文件

下面是最终的spec文件参考:

%pre
if [  == 1 ];then
   echo "-----------------------"
   echo "RPM is getting installed"
   echo "Put your script here"
   echo "-----------------------"
elif [  == 2 ];then
   echo "-----------------------"
   echo "RPM is getting upgraded"
   echo "Put your script here"
   echo "-----------------------"
fi
%post
if [  == 1 ];then
   echo "-----------------------"
   echo "RPM is getting installed"
   echo "Put your script here"
   echo "-----------------------"
elif [  == 2 ];then
   echo "-----------------------"
   echo "RPM is getting upgraded"
   echo "Put your script here"
   echo "-----------------------"
fi
%preun
if [  == 1 ];then
   echo "-----------------------"
   echo "RPM is getting upgraded"
   echo "Put your script here which will be called when this rpm is removed"
   echo "-----------------------"
elif [  == 0 ];then
   echo "--------------------"
   echo "RPM is getting removed/uninstalled"
   echo "Put your script here which will be called before uninstallation of this rpm"
   echo "--------------------"
fi
%postun
if [  == 1 ];then
   echo "-----------------------"
   echo "RPM is getting upgraded"
   echo "Put your script here which will be called when this rpm is removed"
   echo "-----------------------"
elif [  == 0 ];then
   echo "--------------------"
   echo "RPM is getting removed/uninstalled"
   echo "Put your script here which will be called after uninstallation of this rpm"
   echo "--------------------"
fi

安装 rpm

# rpm -ivh ../RPMS/x86_64/jack-1.0.0-1.x86_64.rpm
Preparing...                          ################################# [100%]
----------------------
RPM is getting installed
Put your script here
----------------------
Updating/installing...
   1:jack-1.0.0-1                   ################################# [100%]
----------------------
RPM is getting installed
Put your script here
----------------------

升级 rpm

# rpm -Uvh ../RPMS/x86_64/jack-2.0.0-1.x86_64.rpm
Preparing...                          ################################# [100%]
----------------------
RPM is getting upgraded
Put your script here
----------------------
Updating/installing...
   1:jack-2.0.0-1                   ################################# [ 50%]
----------------------
RPM is getting upgraded
Put your script here
----------------------
----------------------
RPM is getting upgraded
Put your script here which will be called when this rpm is removed
----------------------
Cleaning up/removing...
   2:jack-1.0.0-1                   ################################# [100%]
----------------------
RPM is getting upgraded
Put your script here which will be called when this rpm is removed
----------------------

删除 rpm

# rpm -e jack
-------------------
RPM is getting removed/uninstalled
Put your script here which will be called before uninstallation of this rpm
-------------------
-------------------
RPM is getting removed/uninstalled
Put your script here which will be called after uninstallation of this rpm
-------------------
日期:2020-06-02 22:18:30 来源:oir作者:oir