关于RPM的%config和(noreplace)

RPM spec文件的%config宏用于标记配置文件,确保升级时不丢失用户修改。%config(noreplace)则在用户修改过配置文件且升级包中有更新时,保留旧文件并标记为.rpmnew。本文探讨了这两者的区别及应用建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自这里

 

RPM spec文件有个名为 %config 的宏,它可以标识配置文件, 这样在升级时用户对配置文件做过的修改就不会丢失。 没有它,用户千辛万苦修改过的配置文件会在升级过程中被覆盖。

 

%config也可以写成%config(noreplace),不过网上关于它的说明却屈指可数。 下面是关于这两者的一些实验,都是在RedHat 9的RPM(rpm-4.2-0.69)上进行的, 其他版本有可能不同。

 

RPM中的文件的制约条件有三个:

1. 该文件在spec中如何标识(默认,%config或者%config(noreplace));

2. 在rpm升级包中该文件是否被更新;

3. 该文件是否被用户编辑过。

 

下表就是各个条件的组合结果。

文件标识在RPM升级包中是否更新了?旧版本文件未被用户编辑过旧版本文件被用户编辑过
默认No用新文件覆盖用新文件覆盖
Yes用新文件覆盖用新文件覆盖
%configNo用新文件覆盖保持旧文件
Yes用新文件覆盖旧文件改名为.rpmsave并复制新文件
%config(noreplace)No用新文件覆盖保持旧文件
Yes用新文件覆盖保持旧文件,新文件安装为.rpmnew

 

其中(noreplace)有效的两种情况上存在以下的问题:当spec文件中的定义改变时会发生什么?结论如下:

文件标识在RPM升级包中是否更新了?旧版本文件被用户编辑过
由%config(noreplace)改为%configYes旧文件改名为.rpmsave并安装新文件
由%config改为%config(noreplace)Yes保持旧文件,新文件安装为.rpmnew

 

结论:非配置文件或是上次安装后没有被修改的文件会被RPM包内的新文件覆盖。 如果配置文件被修改过,但在新的RPM包中未作更新,则修改过的文件保留。 仅当配置文件被修改并且在新的RPM包中被更新时,具体动作取决于(noreplace)。 %config使得旧文件被改名为.rpmsave并安装新文件,而%config(noreplace)将保持旧文件 并将新文件安装为.rpmnew。

 

建议一般配置文件都标识为(noreplace),除非升级所带来的配置文件变动非常重要, 使用上一版本的配置文件无法正常工作时才使用%config。


 

已知seafile-server版本是11.0.13,如何完善下属配置文件的内容 Name: seafile-server Version: %{_version} Release: 1%{?dist} Summary: Seafile Server - Enterprise file hosting platform Group: Applications/File License: AGPL-3.0 URL: https://www.seafile.com Source0: seafile-server-%{version}.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) # 依赖声明 Requires: python3, python3-setuptools, python3-pip, python3-libs Requires: mariadb-server, memcached, libselinux-python3 Requires: nginx, policycoreutils-python3 # 不自动检测依赖(避免打包系统库) AutoReqProv: no %define _version 9.0.0 # 替换为实际版本 %description Seafile is an open source file hosting system with features on team collaboration, library syncing and data backup. %prep %setup -q -n seafile-server-%{version} %install rm -rf %{buildroot} mkdir -p %{buildroot}/opt/seafile cp -R * %{buildroot}/opt/seafile/seafile-server-%{version} # 创建配置目录 mkdir -p %{buildroot}/etc/seafile # 安装 systemd 服务文件 mkdir -p %{buildroot}%{_unitdir} install -m 644 seafile.service %{buildroot}%{_unitdir} install -m 644 seahub.service %{buildroot}%{_unitdir} # 安装 Nginx 配置 mkdir -p %{buildroot}/etc/nginx/conf.d install -m 644 conf/nginx/seafile.conf %{buildroot}/etc/nginx/conf.d/ %clean rm -rf %{buildroot} %files %defattr(-,root,root,-) /opt/seafile/seafile-server-%{version} %config(noreplace) /etc/nginx/conf.d/seafile.conf %{_unitdir}/seafile.service %{_unitdir}/seahub.service %post #!/bin/bash # 创建 seafile 用户 if ! id seafile >/dev/null 2>&1; then useradd -M -r -s /sbin/nologin -c "Seafile Server" seafile fi # 设置权限 chown -R seafile:seafile /opt/seafile/seafile-server-%{version} # 创建符号链接 ln -sf /opt/seafile/seafile-server-%{version} /opt/seafile/seafile-server-latest # 初始化数据库(首次安装) if [ $1 -eq 1 ]; then # 启动数据库服务 systemctl start mariadb # 创建数据库用户 mysql -e "CREATE DATABASE IF NOT EXISTS seafile_db CHARACTER SET utf8;" mysql -e "CREATE USER 'seafile'@'localhost' IDENTIFIED BY 'password';" mysql -e "GRANT ALL PRIVILEGES ON seafile_db.* TO 'seafile'@'localhost';" mysql -e "FLUSH PRIVILEGES;" fi # 设置 SELinux 上下文 semanage fcontext -a -t httpd_sys_content_t "/opt/seafile/seafile-server-%{version}(/.*)?" restorecon -R /opt/seafile/seafile-server-%{version} # 启用服务 systemctl daemon-reload systemctl enable seafile.service systemctl enable seahub.service %preun #!/bin/bash # 卸载前停止服务 if [ $1 -eq 0 ]; then systemctl stop seafile.service systemctl stop seahub.service systemctl disable seafile.service systemctl disable seahub.service fi %changelog * Tue Oct 26 2023 Your Name <email@example.com> - %{version}-1 - Initial RPM package for Seafile Server
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值