Skip to content

Commit 2e20c32

Browse files
committed
detect-environment: Document the detect_distribution
Ticket: ENT-12600 Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
1 parent bfd346d commit 2e20c32

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

build-scripts/detect-environment

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,18 @@ detect_os()
107107
export OS OS_VERSION
108108
}
109109

110+
# The uname command does not reveal the specific distribution on Linux. Hence,
111+
# we'll need to parse it from different files located in the /etc/ directory.
112+
# Unfortunately, there is no standard across the existing distributions. Thus,
113+
# this is going to be a bit messy.
110114
detect_distribution()
111115
{
112116
if [ -f /etc/redhat-release ]; then
113117
REL=$(cat /etc/redhat-release)
114118
case "$REL" in
115119
"CentOS "*)
120+
# Example output for CentOS:
121+
# CentOS Linux release 7.6.1810 (Core)
116122
VER="$(echo "$REL" | sed -e 's/^CentOS.* release \([0-9][0-9]*\.[0-9][0-9]*\).*$/\1/')"
117123
if ! echo "$VER" | grep -E '^[0-9]+\.[0-9]+$' > /dev/null
118124
then
@@ -180,8 +186,29 @@ detect_distribution()
180186
;;
181187
esac
182188
elif [ -f /etc/lsb-release ] && grep -q Ubuntu /etc/lsb-release; then
189+
# This file was introduced by Linux Standard Base (LSB) which an attempt to
190+
# standardize the Linux ecosystem. Unfortunately it was not adopted by many
191+
# Linux distributions. Ubuntu dropped the support for LSB in 2015. However,
192+
# the /etc/lsb-release file is still available as of Ubuntu 24.
193+
#
194+
# It might be naive to assume the file will continue to exist and that the
195+
# existence of the file is only present in Ubuntu. Hence, if this breaks in
196+
# the future, you'll know why.
197+
#
198+
# Example output of /etc/lsb-release:
199+
#
200+
# DISTRIB_ID=Ubuntu
201+
# DISTRIB_RELEASE=24.04
202+
# DISTRIB_CODENAME=noble
203+
# DISTRIB_DESCRIPTION="Ubuntu 24.04.2 LTS"
204+
205+
# Get the line containing 'DISTRIB_RELEASE='
183206
REL=$(grep DISTRIB_RELEASE= /etc/lsb-release)
207+
208+
# Remove the 'DISTRIB_RELEASE=' part
184209
REL=${REL#DISTRIB_RELEASE=}
210+
211+
# Verify that we can find a valid version number
185212
case "$REL" in
186213
[0-9][0-9].[0-9][0-9])
187214
;;
@@ -194,6 +221,11 @@ detect_distribution()
194221
OS=ubuntu
195222
OS_VERSION="$REL"
196223
elif [ -f /etc/debian_version ]; then
224+
# This file contains only the version number.
225+
#
226+
# Example output of /etc/debian_version
227+
# 12.11
228+
197229
REL=$(cat /etc/debian_version)
198230
if ! echo "$REL" | grep -E '^[0-9]+\.[0-9]+(\.[0-9]+)?$' > /dev/null
199231
then
@@ -233,6 +265,17 @@ detect_distribution()
233265
elif [ -f /etc/os-release ]; then
234266
# see https://en.opensuse.org/SDB:Find_openSUSE_version for rules of
235267
# parsing this file
268+
269+
# Example output for /etc/os-release:
270+
#
271+
# NAME="SLES"
272+
# VERSION="12-SP5"
273+
# VERSION_ID="12.5"
274+
# PRETTY_NAME="SUSE Linux Enterprise Server 12 SP5"
275+
# ID="sles"
276+
# ANSI_COLOR="0;32"
277+
# CPE_NAME="cpe:/o:suse:sles:12:sp5"
278+
236279
os="$(sh -c ". /etc/os-release; echo \$ID")"
237280
ver="$(sh -c ". /etc/os-release; echo \$VERSION_ID")"
238281
if [ "$os" = "sles" ]; then

0 commit comments

Comments
 (0)