18

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.

2
  • What would be the values for $basearch and $releasever from the above example?
    – devnull
    Jan 8, 2014 at 6:41
  • Installed: 6/x86_64, 6 is releasever and x86_64 is basearch. Jan 8, 2014 at 6:47

2 Answers 2

26

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
8
  • This doesn't seem to be working on centos 6.x. I keep getting package redhat-release is not installed Jan 8, 2014 at 18:10
  • You can change the query as long as the file /etc/redhat-release is part of a package. Or if there is /etc/centos-release. I'll modify the code above shortly.
    – alvits
    Jan 8, 2014 at 19:36
  • 1
    Your updates worked as described. Giving a +1, going to accept in a few days if nothing else turns up. Thanks again. Jan 9, 2014 at 0:25
  • 2
    @CraigRinger - This answer has been a while. basearch=$(rpm —eval %{_arch}) should work even on fedora.
    – alvits
    Dec 12, 2019 at 8:13
  • 3
    @alvits Confirmed. Thankyou. It's quite ridiculous that RH/Fedora don't offer a convenient, documented and stable interface for this. Dec 13, 2019 at 4:13
0

Alternatively, you can print all conf using python.

Cf. post from @Mark_McKinstry :

https://unix.stackexchange.com/questions/19701/yum-how-can-i-view-variables-like-releasever-basearch-yum0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.