| 1 | |
| 2 | # config-functions.sh |
| 3 | # |
| 4 | # configuration functions for Relax-and-Recover |
| 5 | # |
| 6 | |
| 7 | # This file is part of Relax-and-Recover, licensed under the GNU General |
| 8 | # Public License. Refer to the included COPYING for full text of license. |
| 9 | |
| 10 | # Find out which OS vendor and version we run on (openSUSE, SLES, RHEL, Fedora, Debian, ...) |
| 11 | function SetOSVendorAndVersion () { |
| 12 | |
| 13 | # If these variables are already set, skip doing it again. |
| 14 | # This is needed, so that they can be overridden in $WORKFLOW.conf |
| 15 | # if this happens, then ALL the variables OS_* have to be set there. |
| 16 | # The test must match OS_VENDOR=generic or OS_VERSION=none in default.conf: |
| 17 | if test "$OS_VENDOR" = generic -o "$OS_VERSION" = none ; then |
| 18 | |
| 19 | # Recent Linux distro's with systemd has the /etc/os-release file |
| 20 | # Try to find all the required information from that file |
| 21 | if [[ -f /etc/os-release ]] ; then |
| 22 | grep -q -i 'fedora' /etc/os-release && OS_VENDOR=Fedora |
| 23 | grep -q -i -E '(centos|rhel|scientific|oracle)' /etc/os-release && OS_VENDOR=RedHatEnterpriseServer |
| 24 | grep -q -i 'suse' /etc/os-release && OS_VENDOR=SUSE_LINUX |
| 25 | grep -q -i 'debian' /etc/os-release && OS_VENDOR=Debian |
| 26 | grep -q -i -E '(ubuntu|linuxmint)' /etc/os-release && OS_VENDOR=Ubuntu |
| 27 | grep -q -i 'arch' /etc/os-release && OS_VENDOR=Arch_Linux |
| 28 | |
| 29 | local VERSION_ID |
| 30 | eval "$(grep "^VERSION_ID=" /etc/os-release)" |
| 31 | contains_visible_char "$VERSION_ID" && OS_VERSION="$VERSION_ID" |
| 32 | fi |
| 33 | |
| 34 | # For non-systemd distro's try the /etc/system-release file |
| 35 | if test "$OS_VENDOR" = generic ; then |
| 36 | if [[ -f /etc/system-release ]] ; then |
| 37 | grep -q -i 'fedora' /etc/system-release && OS_VENDOR=Fedora |
| 38 | grep -q -i -E '(centos|redhat|scientific|oracle)' /etc/system-release && OS_VENDOR=RedHatEnterpriseServer |
| 39 | grep -q -i 'suse' /etc/system-release && OS_VENDOR=SUSE_LINUX |
| 40 | grep -q -i 'mandriva' /etc/system-release && OS_VENDOR=Mandriva |
| 41 | majornr=$( grep -o -E '[0-9]+' /etc/system-release | head -1 ) |
| 42 | minornr=$( grep -o -E '[0-9]+' /etc/system-release | head -2 | tail -1 ) |
| 43 | OS_VERSION="$majornr.$minornr" |
| 44 | fi |
| 45 | fi |
| 46 | |
| 47 | # For older distro's we try to interpret the /etc/SuSE-release or /etc/redhat-release file |
| 48 | # The /etc/issue file cannot be trusted as it can contain customer related info instead of release info |
| 49 | if test "$OS_VENDOR" = generic ; then |
| 50 | if [[ -f /etc/SuSE-release ]] ; then |
| 51 | OS_VENDOR=SUSE_LINUX |
| 52 | majornr=$( grep VERSION /etc/SuSE-release | awk '{print $3}' ) |
| 53 | minornr=$( grep PATCHLEVEL /etc/SuSE-release | awk '{print $3}' ) |
| 54 | OS_VERSION="$majornr.$minornr" |
| 55 | fi |
| 56 | |
| 57 | if [[ -f /etc/redhat-release ]] ; then |
| 58 | OS_VENDOR=RedHatEnterpriseServer |
| 59 | majornr=$( grep -o -E '[0-9]+' /etc/system-release | head -1 ) |
| 60 | minornr=$( grep -o -E '[0-9]+' /etc/system-release | head -2 | tail -1 ) |
| 61 | OS_VERSION="$majornr.$minornr" |
| 62 | fi |
| 63 | |
| 64 | if [[ -f /etc/slackware-version ]] ; then |
| 65 | OS_VENDOR=Slackware |
| 66 | OS_VERSION=$(cat /etc/slackware-version | sed -r -e 's/slackware\s*//i') |
| 67 | fi |
| 68 | fi |
| 69 | |
| 70 | # If OS_VENDOR is still generic then use as last resource 'lsb_release' to find out |
| 71 | if test "$OS_VENDOR" = generic ; then |
| 72 | # When OS_VENDOR is still "generic" we are using a pre-systemd system and need to fallback |
| 73 | # to lsb_release, therefore, as it is not a required binary we will check if we have this |
| 74 | # executable and when absent bail out with an error |
| 75 | if ! has_binary lsb_release ; then |
| 76 | Error "The 'lsb_release' command cannot be run. |
| 77 | Detecting the operating system and its version requires LSB support. |
| 78 | Install a software package that provides the 'lsb_release' command. |
| 79 | Alternatively you can manually specify OS_VENDOR and OS_VERSION in |
| 80 | '$CONFIG_DIR/os.conf' and verify that your setup actually works. |
| 81 | See '$SHARE_DIR/lib/config-functions.sh' for more details." |
| 82 | fi |
| 83 | |
| 84 | OS_VENDOR="$( lsb_release -i -s | tr -s '[:blank:]' '_' )" |
| 85 | test "$OS_VENDOR" || Error "Failed to detect OS_VENDOR. You may manually specify it in $CONFIG_DIR/os.conf" |
| 86 | # For all SUSE distributions (SLES and openSUSE) ReaR uses |
| 87 | # only .../SUSE_LINUX/... sub-directories plus conf/SUSE_LINUX.conf |
| 88 | # so that 'lsb_release -i -s' output must be unified to 'SUSE_LINUX'. |
| 89 | # For example 'lsb_release -i -s' outputs |
| 90 | # on SLES11 SP3 : 'SUSE LINUX' |
| 91 | # on SLES12 12 SP2 : 'SUSE' |
| 92 | # on openSUSE Leap 42.1 : 'SUSE LINUX' |
| 93 | # on openSUSE Tumbleweed 20170304 : 'openSUSE' |
| 94 | # so that the common substring is 'SUSE'. |
| 95 | # When OS_VENDOR contains the substring 'SUSE', set OS_VENDOR to 'SUSE_LINUX': |
| 96 | test "${OS_VENDOR#*SUSE}" = "$OS_VENDOR" || OS_VENDOR="SUSE_LINUX" |
| 97 | |
| 98 | OS_VERSION="$( lsb_release -r -s | tr -s '[:blank:]' '_' )" |
| 99 | test "$OS_VERSION" || Error "Failed to detect OS_VERSION. You may manually specify it in $CONFIG_DIR/os.conf" |
| 100 | fi |
| 101 | |
| 102 | fi |
| 103 | |
| 104 | # combined stuff |
| 105 | OS_VENDOR_VERSION="$OS_VENDOR/$OS_VERSION" |
| 106 | OS_VENDOR_ARCH="$OS_VENDOR/$MACHINE" |
| 107 | OS_VENDOR_VERSION_ARCH="$OS_VENDOR/$OS_VERSION/$MACHINE" |
| 108 | |
| 109 | # add OS_MASTER_* vars in case this is a derived OS |
| 110 | case "$( echo $OS_VENDOR_VERSION | tr '[A-Z]' '[a-z]' )" in |
| 111 | (*oracle*|*centos*|*fedora*|*redhat*|*scientific*) |
| 112 | OS_MASTER_VENDOR="Fedora" |
| 113 | ;; |
| 114 | (*ubuntu*|*linuxmint*) |
| 115 | OS_MASTER_VENDOR="Debian" |
| 116 | ;; |
| 117 | (*archlinux*) |
| 118 | OS_MASTER_VENDOR="Arch_Linux" |
| 119 | ;; |
| 120 | (*suse*) |
| 121 | # When OS_VENDOR_VERSION contains 'SUSE', set OS_MASTER_VENDOR to 'SUSE' |
| 122 | # but do not set OS_MASTER_VENDOR same as OS_VENDOR (i.e. 'SUSE_LINUX') |
| 123 | # (cf. above: all SUSE distributions ... must be unified to 'SUSE_LINUX') |
| 124 | # because then scripts in a .../SUSE_LINUX/... sub-directoriy and conf/SUSE_LINUX.conf |
| 125 | # get sourced twice by the (buggy) SourceStage function in lib/framework-functions.sh |
| 126 | OS_MASTER_VENDOR="SUSE" |
| 127 | ;; |
| 128 | (*) |
| 129 | # set fallback values to avoid error exit for 'set -eu' because of unbound variables: |
| 130 | OS_MASTER_VENDOR="" |
| 131 | ;; |
| 132 | esac |
| 133 | |
| 134 | # Set master version to the MAJOR release version. ReaR assumes that OS_MASTER_VERSION |
| 135 | # is just the major release number extracted from OS_VERSION and not the version of the derived OS. |
| 136 | # If OS_VERSION is of the form 12.34.56, OS_MASTER_VERSION is only the first part '12'. |
| 137 | # |
| 138 | # Because openSUSE Tumbleweed has rolling releases, OS_VERSION is a date of the form YYYYMMDD. |
| 139 | # Therefore, there is no real OS_MASTER_VERSION and this variable will be set to the same value |
| 140 | # as OS_VERSION. |
| 141 | # |
| 142 | # TODO: Rename the variable to be less confusing, e.g. to OS_VERSION_MAJOR. |
| 143 | OS_MASTER_VERSION="${OS_VERSION%%.*}" |
| 144 | |
| 145 | # combined stuff for OS_MASTER_* |
| 146 | if [ "$OS_MASTER_VENDOR" ] ; then |
| 147 | OS_MASTER_VENDOR_VERSION="$OS_MASTER_VENDOR/$OS_MASTER_VERSION" |
| 148 | OS_MASTER_VENDOR_ARCH="$OS_MASTER_VENDOR/$MACHINE" |
| 149 | OS_MASTER_VENDOR_VERSION_ARCH="$OS_MASTER_VENDOR/$OS_MASTER_VERSION/$MACHINE" |
| 150 | else |
| 151 | # set fallback values to avoid error exit for 'set -eu' because of unbound variables: |
| 152 | OS_MASTER_VENDOR_VERSION="$OS_MASTER_VERSION" |
| 153 | OS_MASTER_VENDOR_ARCH="$MACHINE" |
| 154 | OS_MASTER_VENDOR_VERSION_ARCH="$OS_MASTER_VERSION/$MACHINE" |
| 155 | fi |
| 156 | |
| 157 | } |
| 158 | |
| 159 | ### Return the template filename |
| 160 | function get_template() { |
| 161 | if [[ -e $CONFIG_DIR/templates/$1 ]] ; then |
| 162 | echo $CONFIG_DIR/templates/$1 |
| 163 | else |
| 164 | echo $SHARE_DIR/conf/templates/$1 |
| 165 | fi |
| 166 | } |
| 167 |