| 1 |
# |
| 2 |
# This is OCF Linux distribution query support |
| 3 |
# |
| 4 |
# Currently needed for the nfsserver RA which has some already |
| 5 |
# released RH specific stuff (/etc/sysconfig/nfs editing) |
| 6 |
# |
| 7 |
# These functions are intended to be POSIX-compliant for portability. |
| 8 |
# |
| 9 |
|
| 10 |
# systemd-based systems should all have an os-release file. |
| 11 |
_ETC_OS_RELEASE_FILE="/etc/os-release" |
| 12 |
_USR_OS_RELEASE_FILE="/usr/lib/os-release" |
| 13 |
|
| 14 |
# Legacy distro-specific files |
| 15 |
_DEBIAN_VERSION_FILE="/etc/debian_version" |
| 16 |
_REDHAT_RELEASE_FILE="/etc/redhat-release" |
| 17 |
_SUSE_RELEASE_FILE="/etc/SuSE-release" |
| 18 |
|
| 19 |
|
| 20 |
# Converts OS release ID to a standard form regardless of source. |
| 21 |
_process_os_release_id() { |
| 22 |
_os="$1" |
| 23 |
|
| 24 |
# Convert to lowercase, isolate distro name, remove whitespace |
| 25 |
_os=$(echo "$_os" \ |
| 26 |
| tr "[:upper:]" "[:lower:]" \ |
| 27 |
| sed -e "s|\(gnu/\)*linux||" -e "s/server//" \ |
| 28 |
-e "s/release.*//" -e "s/[[:digit:]].*//" \ |
| 29 |
-e "s/[[:blank:]]//") |
| 30 |
|
| 31 |
# Normalize known distros to os-release names |
| 32 |
case "$_os" in |
| 33 |
*alma*) |
| 34 |
_os="almalinux" |
| 35 |
;; |
| 36 |
*centos*) |
| 37 |
_os="centos" |
| 38 |
;; |
| 39 |
*debian*) |
| 40 |
_os="debian" |
| 41 |
;; |
| 42 |
*fedora*) |
| 43 |
_os="fedora" |
| 44 |
;; |
| 45 |
*ol*) |
| 46 |
_os="ol" |
| 47 |
;; |
| 48 |
*redhat*|*rhel*|*scientific*) |
| 49 |
_os="rhel" |
| 50 |
;; |
| 51 |
*rocky*) |
| 52 |
_os="rocky" |
| 53 |
;; |
| 54 |
*opensuse*) |
| 55 |
_os="opensuse" |
| 56 |
;; |
| 57 |
*suseenterprise*) |
| 58 |
_os="sles" |
| 59 |
;; |
| 60 |
*ubuntu*) |
| 61 |
_os="ubuntu" |
| 62 |
;; |
| 63 |
esac |
| 64 |
|
| 65 |
echo "$_os" |
| 66 |
} |
| 67 |
|
| 68 |
# Converts OS version ID to a form that ocf_version_cmp() can handle. |
| 69 |
# Strips any garbage. |
| 70 |
_process_os_version_id() { |
| 71 |
_ver="$1" |
| 72 |
_fmt="[[:digit:]][[:digit:].-]*[[:alnum:].\+-]*" |
| 73 |
|
| 74 |
echo "$_ver" | sed -e "s/[^[:digit:]]*\(${_fmt}\).*/\1/" |
| 75 |
} |
| 76 |
|
| 77 |
# Gets OS release ID (i.e., distro) or version ID from os-release file. |
| 78 |
# $_ETC_OS_RELEASE_FILE takes precedence over $_USR_OS_RELEASE_FILE. |
| 79 |
_get_val_from_os_release_file() { |
| 80 |
_key="" |
| 81 |
_value="" |
| 82 |
_func="" |
| 83 |
|
| 84 |
case "$1" in |
| 85 |
id) |
| 86 |
_key="ID" |
| 87 |
_func="_process_os_release_id" |
| 88 |
;; |
| 89 |
version_id) |
| 90 |
_key="VERSION_ID" |
| 91 |
_func="_process_os_version_id" |
| 92 |
;; |
| 93 |
esac |
| 94 |
|
| 95 |
if [ -n "$_key" ]; then |
| 96 |
if [ -f "$_ETC_OS_RELEASE_FILE" ]; then |
| 97 |
_value=$(awk -F "=" -v k="$_key" '$1 == k {print $2}' \ |
| 98 |
"$_ETC_OS_RELEASE_FILE" | tr -d \") |
| 99 |
fi |
| 100 |
|
| 101 |
if [ -z "$_value" ] && [ -f "$_USR_OS_RELEASE_FILE" ]; then |
| 102 |
_value=$(awk -F "=" -v k="$_key" '$1 == k {print $2}' \ |
| 103 |
"$_USR_OS_RELEASE_FILE" | tr -d \") |
| 104 |
fi |
| 105 |
fi |
| 106 |
|
| 107 |
# Ensure the value is in the correct format |
| 108 |
[ -n "$_func" ] && _value=$("$_func" "$_value") |
| 109 |
|
| 110 |
echo "$_value" |
| 111 |
} |
| 112 |
|
| 113 |
# Gets OS release ID from lsb_release command or legacy *-release files |
| 114 |
_get_os_from_legacy_source() { |
| 115 |
_os="" |
| 116 |
|
| 117 |
if which lsb_release >/dev/null 2>&1; then |
| 118 |
_os=$(lsb_release -si) |
| 119 |
|
| 120 |
elif [ -f "$_DEBIAN_VERSION_FILE" ]; then |
| 121 |
_os="debian" |
| 122 |
|
| 123 |
elif [ -f "$_REDHAT_RELEASE_FILE" ]; then |
| 124 |
_os=$(head -n 1 "$_REDHAT_RELEASE_FILE") |
| 125 |
|
| 126 |
elif [ -f "$_SUSE_RELEASE_FILE" ]; then |
| 127 |
_os=$(head -n 1 "$_SUSE_RELEASE_FILE") |
| 128 |
|
| 129 |
else |
| 130 |
_os=$(uname -s) |
| 131 |
fi |
| 132 |
|
| 133 |
_process_os_release_id "$_os" |
| 134 |
} |
| 135 |
|
| 136 |
# Gets OS version from lsb_release command or legacy *-release files |
| 137 |
_get_version_from_legacy_source() { |
| 138 |
_ver="" |
| 139 |
|
| 140 |
if which lsb_release >/dev/null 2>&1; then |
| 141 |
_ver=$(lsb_release -sr) |
| 142 |
|
| 143 |
elif [ -f "$_DEBIAN_VERSION_FILE" ]; then |
| 144 |
_ver=$(cat "$_DEBIAN_VERSION_FILE") |
| 145 |
|
| 146 |
elif [ -f "$_REDHAT_RELEASE_FILE" ]; then |
| 147 |
_ver=$(head -1 "$_REDHAT_RELEASE_FILE") |
| 148 |
|
| 149 |
elif [ -f "$_SUSE_RELEASE_FILE" ]; then |
| 150 |
_ver=$(awk '$1 == "VERSION" {print $3}' "$_SUSE_RELEASE_FILE") |
| 151 |
_patchlevel=$(awk '$1 == "PATCHLEVEL" {print $3}' \ |
| 152 |
"$_SUSE_RELEASE_FILE") |
| 153 |
|
| 154 |
[ -n "$_patchlevel" ] && _ver="${_ver}.${_patchlevel}" |
| 155 |
|
| 156 |
else |
| 157 |
_ver=$(uname -r) |
| 158 |
fi |
| 159 |
|
| 160 |
_process_os_version_id "$_ver" |
| 161 |
} |
| 162 |
|
| 163 |
# Prints OS release ID (i.e., distro name) |
| 164 |
get_release_id() { |
| 165 |
_os=$(_get_val_from_os_release_file id) |
| 166 |
|
| 167 |
if [ -z "$_os" ]; then |
| 168 |
_os=$(_get_os_from_legacy_source) |
| 169 |
fi |
| 170 |
|
| 171 |
echo "$_os" |
| 172 |
} |
| 173 |
|
| 174 |
# Prints OS version ID |
| 175 |
get_os_version_id() { |
| 176 |
_ver=$(_get_val_from_os_release_file version_id) |
| 177 |
|
| 178 |
if [ -z "$_ver" ] || [ "$(get_release_id)" = "debian" ]; then |
| 179 |
# Debian only includes major release in os-release. |
| 180 |
# $_DEBIAN_VERSION_FILE has ${major}.${minor}. |
| 181 |
_ver=$(_get_version_from_legacy_source) |
| 182 |
fi |
| 183 |
|
| 184 |
echo "$_ver" |
| 185 |
} |
| 186 |
|
| 187 |
# Returns true if the OS is Debian-based, otherwise false |
| 188 |
is_debian_based() { |
| 189 |
get_release_id | grep -i -e "debian" -e "ubuntu" >/dev/null 2>&1 |
| 190 |
} |
| 191 |
|
| 192 |
# Returns true if the OS is Red Hat-based, otherwise false |
| 193 |
is_redhat_based() { |
| 194 |
get_release_id | grep -i -e "almalinux" -e "centos" -e "fedora" -e "ol" \ |
| 195 |
-e "redhat" -e "rhel" -e "rocky" -e "scientific" >/dev/null 2>&1 |
| 196 |
} |
| 197 |
|
| 198 |
# Returns true if the OS is SUSE-based, otherwise false |
| 199 |
is_suse_based() { |
| 200 |
get_release_id | grep -i -e "sles" -e "suse" >/dev/null 2>&1 |
| 201 |
} |
| 202 |
|
| 203 |
# Sets global variables OS and VER. |
| 204 |
# get_os_ver() is currently unused upstream; maintained for backwards |
| 205 |
# compatibility. |
| 206 |
get_os_ver() { |
| 207 |
OS=$(get_release_id) |
| 208 |
VER=$(get_os_version_id) |
| 209 |
} |