I'm writing a bash script to pull packages from remote repos, using reposync, so I can point my nodes to pull locally. As such I am trying to keep the local repo configs as similar as possible to the usptream repo configs, like this:
# upstream
baseurl=http://mirror.freedomvoice.com/centos/$releasever/os/$basearch/
# local
baseurl=http://user:[email protected]/centos/stable/$releasever/os/$basearch/
Within the bash script, is there a cleaner way to get $releasever and $basearch values? I was thinking of doing the following:
yum_metadata=$(yum version nogroups)
Which returns:
Loaded plugins: versionlock Installed: 6/x86_64 360:6167019baac7e76f94c26320424dc41a7f046a70 version
Then regexing for the 6/x86_64 values. Kind of messy, and looking for a more elegant approach.
Yum obtains the value of $releasever from the distroverpkg=value line in the /etc/yum. conf configuration file.
basearch Refers to the base architecture of the system. For example, i686 and i586 machines both have a base architecture of i386, and AMD64 and Intel64 machines have a base architecture of x86_64. $
You can use and reference the following built-in variables in yum commands and in all Yum configuration files (that is, /etc/yum. conf and all . repo files in the /etc/yum. repos.
Most distro uses the distroverpkg version to get the releasever and basearch.
If you look at /etc/yum.conf, you will see that distrover is set to redhat-release (for RHEL), enterpriselinux-release (for OEL), and others.
To get the package name:
distro=$(sed -n 's/^distroverpkg=//p' /etc/yum.conf)
To get the releasever:
releasever=$(rpm -q --qf "%{version}" -f /etc/$distro)
To get the basearch:
basearch=$(rpm -q --qf "%{arch}" -f /etc/$distro)
The new code above will try to get the package associated with a file /etc/$distro
. Some Linux adds /etc/redhat-release
to their package release.
If you get file not owned by any package
then use the /etc/*-release
file that came with your distro. It is probably /etc/centos-release
.
You can check the appropriate /etc/*-release
appropriate for this code by checking which file is packaged with centos.
rpm -qf /etc/*-release
Then use this file instead of the first line above.
distro=/etc/centos-release
Here's an example from OEL where /etc/redhat-release
is packaged as enterprise-release
.
rpm -q --qf "%{name}" -f /etc/redhat-release
Output:
enterprise-release
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With