解决 RHEL 换 CentOS 源后无法取 $releasever 变量

Table of Contents

RHEL 的 yum 源换成 CentOS 源之后,/etc/yum.repos.d 下的配置文件中“$releasever”变量统统失效。解决方法:

  1. 用“centos”替换“$releasever”。
  2. 把 redhat-release 包替换为 centos-release:
a). 用 rpm -e --nodeps 命令手动删除 redhat-release 包;
b). 找个源手动下载安装 centos-release;
c). 修改 /etc/yum.conf,把 distroverpkg=redhat-release 改成 distroverpkg=centos-release

1 $releasever 取值原理

$releasever 的取值默认是 redhat-release,见 yum/config.py:

class StartupConf(BaseConfig):
    debuglevel = IntOption(2, 0, 10)
    errorlevel = IntOption(2, 0, 10)

    distroverpkg = Option('redhat-release')
    ...

版本是从 redhat-release/centos-release 的 RPM 包信息里获得的,见 _getsysver 函数实现:

def _getsysver(installroot, distroverpkg):
    ts = rpmUtils.transaction.initReadOnlyTransaction(root=installroot)
    ts.pushVSFlags(~(rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS))
    try:
        idx = ts.dbMatch('provides', distroverpkg)
    except TypeError, e:
        if sys.hexversion < 0x02050000:
            if hasattr(e,'message'):
                raise Errors.YumBaseError("Error: " + str(e.message))
            else:
                raise Errors.YumBaseError("Error: " + str(e))
        raise Errors.YumBaseError("Error: " + str(e))
    except rpm.error, e:
        raise Errors.YumBaseError("Error: " + str(e))
    if idx.count() == 0:
        releasever = '$releasever'
    else:
        hdr = idx.next()
        releasever = hdr['version']  # 这里,获得版本信息
        del hdr
    del idx
    del ts
    return releasever

可以下载 centos-release 的RPM包,用 rpm -qpi 命令验证:

$ rpm -qpi centos-release-7-5.1804.el7.centos.x86_64.rpm