RPM构建spec文件升级时删除uniin-cluster-agent程序配置目录问题咨询
问题根因
你遇到的升级后文件丢失问题,核心是SPEC文件中的卸载脚本没有区分「完全卸载」和「升级过程删除旧版本」两种场景,结合RPM升级的固定执行顺序触发了误删:
- RPM升级时的脚本执行顺序为:安装新版本文件 → 执行新版本%post脚本 → 执行旧版本%preun脚本 → 执行旧版本%postun脚本
- 你当前写的
%preun和%postun没有加场景判断,不管是升级还是完全卸载都会执行所有逻辑:- 升级过程中旧版本的
%preun会直接停止、禁用刚被新版本重启的服务 - 后续旧版本的
%postun会直接执行rm -rf /etc/unilin_cluster_agent,把新版本刚写入的配置目录整体删除
- 升级过程中旧版本的
- 至于可执行文件被删除,额外检查两点:一是有没有在其他脚本中误加了删
/usr/bin/uniin-cluster-agent的逻辑,二是你当前%files段只声明了/etc/unilin_cluster_agent/*,没有明确声明目录归属,RPM判断目录无归属时可能会自动清理。
修复方案
修改你的SPEC文件如下:
1. 给卸载脚本增加场景判断
RPM脚本执行时会传入数字参数:
- 参数为
0:代表当前是完全卸载包 - 参数为
1:代表当前是升级过程中删除旧版本包
%preun if [ $1 -eq 0 ]; then # 仅完全卸载时停服务、禁用服务 systemctl stop uniin-cluster-agent.service systemctl disable uniin-cluster-agent.service fi %postun systemctl daemon-reload if [ $1 -eq 0 ]; then # 仅完全卸载时删除配置目录 rm -rf /etc/unilin_cluster_agent fi
2. 优化%files段配置
标记配置文件属性,明确目录归属,避免升级覆盖用户修改的配置、避免目录被误删:
%files %{_bindir}/uniin-cluster-agent # 明确声明目录归属 %dir /etc/unilin_cluster_agent # 标记为配置文件,升级时不覆盖用户修改的内容,新配置自动存为.rpmnew后缀 %config(noreplace) /etc/unilin_cluster_agent/* /etc/systemd/system/uniin-cluster-agent.service
可选优化:%post脚本区分场景
避免升级时重复执行enable操作:
%post systemctl daemon-reload if [ $1 -eq 1 ]; then # 仅首次安装时执行enable systemctl enable uniin-cluster-agent.service fi systemctl restart uniin-cluster-agent.service
内容的提问来源于stack exchange,提问作者wyx
相关产品推荐
相关产品推荐

