首頁 > 軟體

yum中$releasever、 $basearch等變數含義

2020-06-16 16:51:37

Red Hat Linux 或 CentOS 發行版本號

第一種方法,也是最容易想到的方法是檢視 /etc/issue 和 /etc/RedHat-release這兩個檔案,這兩個檔案都差不多,都含有發行號, 但這是在檔案中,很容易修改,我那個同事覺得這個不太靠譜。

第二種方法是使用命令,在RHEL 5 或 CentOS 5 中有個命令: lsb-release,通過這個命令可以查到,不過在RHEL 6就沒這麼幸運了,在我的最小化系統中找不到這個命令,而我那個同事偏偏是在CentOS6上獲取發行版本號, 看來此路又不通了。

不過有一個線索, 那就是在CentOS自帶的Yum原始檔中使用了 $releasever , $basearch等這些變數, 奇怪的是這些變數的值是從哪獲取的呢?

Google 一下之後,說明Yum變數的說明可以在這裡(5.3.3. Using YumVariables)找到,說明如下:

5.3.3. Using Yum Variables

You can use and reference the following built-in variables inyum commands and in all Yum configuration files (that is,/etc/yum.conf and all .repo files in the /etc/yum.repos.d/directory):

$releasever
You can use this variable toreference the release version of Red Hat Enterprise Linux. Yumobtains the value of $releasever from the distroverpkg=value linein the /etc/yum.conf configuration file. If there is no such linein /etc/yum.conf, then yum infers the correct value by deriving theversion number from the redhat-release package.
$arch

You can use this variable to refer to the system’s CPUarchitecture as returned when calling Python’s os.uname() function.Valid values for $arch include: i586, i686 and x86_64.
$basearch

You can use $basearch to reference the base architecture ofthe system. For example, i686 and i586 machines both have a basearchitecture of i386, and AMD64 and Intel64 machines have a basearchitecture of x86_64.
$YUM0-9

These ten variables are each replaced with the value of anyshell environment variables with the same name. If one of thesevariables is referenced (in /etc/yum.conf for example) and a shellenvironment variable with the same name does not exist, then theconfiguration file variable is not replaced.

To define a custom variable or to override the value of anexisting one, create a file with the same name as the variable(without the “$” sign) in the /etc/yum/vars/ directory, and add thedesired value on its first line.

For example, repository descriptions often include theoperating system name. To define a new variable called $osname,create a new file with “Red Hat Enterprise Linux” on the first lineand save it as /etc/yum/vars/osname:

~]# echo “Red Hat Enterprise Linux” >/etc/yum/vars/osname

Instead of “Red Hat Enterprise Linux 6”, you can now use thefollowing in the .repo files:

name=$osname $releasever

文中說到$releasever的定義可以檢視 /etc/yum.conf 檔案的事distroverpkg=value行,開啟 /etc/yum.conf 看一下,預設檔案(我的是CentOS 6)內容如下:
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=16&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
distroverpkg=centos-release 代表什麼? 去哪找 centos-release。找到這裡我再一次表示困惑。

不過另一篇文章進行了我的視野: yum的$releasever真是太反動了, 關鍵內空如下:

在 /etc/yum.repos.d/ 目錄下的軟體庫定義檔案中,常常會在 baseurl 的路徑中提到$releasever 這個變數,表示當前發行版的大版本號,但大家知道這個變數是在哪設定的嗎?我 grep 了整個 etc目錄都沒找到,還是看了 yum.conf 才知道的,是在 yum.conf 檔案裡 distroverpkg選項定義的。但這個選項就很有問題:

distroverpkg 和 releasever 名字不同,且看不出什麼聯絡
distroverpkg的值,並不是明文,而是“redhat-release”。不知道大家看到這個會有什麼想法,反正我是首先想到了/etc/redhat-release 檔案,但我錯了。實際上指的是 redhat-release這個RPM包。所謂“distroverpkg=redhat-release”的意思,其實是將 $releasever 設定為redhat-release 這個RPM包的版本號

原來如此。這發行版本號可是藏的夠深的。
小結:
yum中的$releasever變數是取redhat-release-serverrpm包的屬性值( %{version})。
[root@ldap01 ~]# rpm -q --qf %{version}redhat-release-server;echo
6Server
檢視關於yum的設定

ls /etc/yum* -ld:

檢視/etc/yum.repos.d下的檔案

檢視CentOS-Base.repo內容:

部分內容:

[base]
baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=file:///etc/pki/rpm-pgg/RPM-GPG-KEY-CentOS-6
[update]
baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates

我們看出來裡面包含大量的變數:

$releasever和$basearch

$releasever的值,這個表示當前系統的發行版本,可以通過如下命令檢視:

rpm -qi centos-release

結果如下:

其中的Version:6就是我們系統的版本號

$basearch是我們的系統硬體架構(CPU指令集),就是我們常說的i386i486i586i686...

使用如下命令即可:

arch

輸出如下:

我這個處理器是i686,現在所有的intel 32位元體系(包括AMD等相容CPU)都叫i386體系,包括P4。i686仍然屬於i386體系,不過對CPU(相對於386)的特性作了指令優化。 GNU/Linux分為i386、alpha、PowerPC、Sun等各個不同版本,所有從Intel386-P4都用i386版本,但i386版本中 有幾個核心(i386,i486,i586,i686),安裝時安裝程式檢測到你得CPU級別後,自動為你安裝相應核心。

在Linux中使用命令arch可以初步檢視出當前系統所識別出來的機器CPU型別,如"i386", "i486","i586", "alpha", "sparc", "arm", "m68k","mips","ppc","ia64","x86_64"等;ia64和x86_64就說明這台機器是64位元的;

所以我這兒要使用i386作為$basearch的值

baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/

完整路徑便是

baseurl=http://mirror.centos.org/centos/6/os/i386/

本文永久更新連結地址https://www.linuxidc.com/Linux/2018-05/152526.htm


IT145.com E-mail:sddin#qq.com