distr_info 13.2 KB
Newer Older
1 2
#!/bin/sh
# Author: Vitaly Lipatov <lav@etersoft.ru>
3 4
# 2007, 2009, 2010, 2012, 2016, 2017, 2018 (c) Etersoft
# 2007-2018 Public domain
5 6 7 8 9 10 11

# Detect the distro and version
# Welcome to send updates!

# You can set ROOTDIR to root system dir
#ROOTDIR=

12 13
# TODO: check /etc/system-release

14 15 16 17 18 19 20 21 22 23 24 25 26
# Check for DISTRO specific file in /etc
distro()
{
	#[ -n "$ROOTDIR" ] || return
	# fill global DISTROFILE
	DISTROFILE="$ROOTDIR/etc/$1"
	[ -f "$DISTROFILE" ]
}

# Has a distro file the specified word?
has()
{
	[ -n "$DISTROFILE" ] || exit 1
27
	grep "$*" "$DISTROFILE" >/dev/null 2>&1
28 29
}

30 31 32 33 34 35
# Has a system the specified command?
hascommand()
{
	which $1 2>/dev/null >/dev/null
}

36 37 38 39 40
firstupper()
{
	echo "$*" | sed 's/.*/\u&/'
}

41 42 43 44 45 46 47
tolower()
{
	# tr is broken in busybox (checked with OpenWrt)
	#echo "$*" | tr "[:upper:]" "[:lower:]"
	echo "$*" | awk '{print tolower($0)}'
}

48 49 50 51
# Translate DISTRIB_ID to vendor name (like %_vendor does)
rpmvendor()
{
	[ "$DISTRIB_ID" = "ALTLinux" ] && echo "alt" && return
52
	[ "$DISTRIB_ID" = "AstraLinux" ] && echo "astra" && return
53
	[ "$DISTRIB_ID" = "LinuxXP" ] && echo "lxp" && return
54
	[ "$DISTRIB_ID" = "TinyCoreLinux" ] && echo "tcl" && return
55
	[ "$DISTRIB_ID" = "VoidLinux" ] && echo "void" && return
56
	[ "$DISTRIB_ID" = "OpenSUSE" ] && echo "suse" && return
57
	tolower "$DISTRIB_ID"
58 59 60 61 62 63 64 65 66 67 68 69
}

# Translate DISTRIB_ID name to package manner (like in the package release name)
pkgvendor()
{
	[ "$DISTRIB_ID" = "Mandriva" ] && echo "mdv" && return
	rpmvendor
}

# Print pkgtype (need DISTRIB_ID var)
pkgtype()
{
70
# TODO: try use generic names
Vitaly Lipatov's avatar
Vitaly Lipatov committed
71
    case $(pkgvendor) in
72 73 74
		freebsd) echo "tbz" ;;
		sunos) echo "pkg.gz" ;;
		slackware|mopslinux) echo "tgz" ;;
75
		archlinux|manjaro) echo "pkg.tar.xz" ;;
76 77
		gentoo) echo "tbz2" ;;
		windows) echo "exe" ;;
78
		android) echo "apk" ;;
79
		alpine) echo "apk" ;;
80
		tinycorelinux) echo "tcz" ;;
81
		voidlinux) echo "xbps" ;;
82
		openwrt) echo "ipk" ;;
83
		cygwin) echo "tar.xz" ;;
84
		debian|ubuntu|mint|runtu|mcst|astra) echo "deb" ;;
85
		alt|asplinux|suse|mandriva|rosa|mandrake|pclinux|sled|sles)
86
			echo "rpm" ;;
87
		fedora|redhat|scientific|centos|rhel|goslinux|amzn)
88 89 90 91 92 93 94
			echo "rpm" ;;
		*)  echo "rpm" ;;
	esac
}

get_var()
{
95 96
	# get first variable and print it out, drop quotes if exists
	grep -i "^$1 *=" | head -n 1 | sed -e "s/^[^=]*[ \t]*=[ \t]*//" | sed -e "s/^[\'\"]\(.*\)[\'\"]/\1/"
97 98 99 100 101 102 103 104 105 106 107
}

# 2010.1 -> 2010
get_major_version()
{
	echo "$1" | sed -e "s/\..*//g"
}

# Default values
DISTRIB_ID="Generic"
DISTRIB_RELEASE=""
108
DISTRIB_CODENAME=""
109 110 111

# Default with LSB
if distro lsb-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
112 113 114
	DISTRIB_ID=$(cat $DISTROFILE | get_var DISTRIB_ID)
	DISTRIB_RELEASE=$(cat $DISTROFILE | get_var DISTRIB_RELEASE)
	DISTRIB_CODENAME=$(cat $DISTROFILE | get_var DISTRIB_CODENAME)
115 116 117 118
fi

# ALT Linux based
if distro altlinux-release ; then
119
	# TODO: use os-release firsly
120
	DISTRIB_ID="ALTLinux"
121
	if has Sisyphus ; then DISTRIB_RELEASE="Sisyphus"
122
	elif has "ALT Linux 7." ; then DISTRIB_RELEASE="p7"
123
	elif has "ALT Linux t7." ; then DISTRIB_RELEASE="t7"
124
	elif has "ALT Linux 8." ; then DISTRIB_RELEASE="p8"
125
	elif has "ALT .*8.[0-9]" ; then DISTRIB_RELEASE="p8"
126
	elif has "ALT .*9.[0-9]" ; then DISTRIB_RELEASE="p9"
127
	elif has "ALT p9 p9" ; then DISTRIB_RELEASE="p9"
128
	elif has "Simply Linux 6." ; then DISTRIB_RELEASE="p6"
129 130
	elif has "Simply Linux 7." ; then DISTRIB_RELEASE="p7"
	elif has "Simply Linux 8." ; then DISTRIB_RELEASE="p8"
131
	elif has "Simply Linux 9." ; then DISTRIB_RELEASE="p9"
132
	elif has "ALT Linux 6." ; then DISTRIB_RELEASE="p6"
133
	elif has "ALT Linux p8"  ; then DISTRIB_RELEASE="p8"
134 135 136 137 138
	elif has "ALT Linux p7"  ; then DISTRIB_RELEASE="p7"
	elif has "ALT Linux p6"  ; then DISTRIB_RELEASE="p6"
	elif has "ALT Linux p5"  ; then DISTRIB_RELEASE="p5"
	elif has "ALT Linux 5.1" ; then DISTRIB_RELEASE="5.1"
	elif has "ALT Linux 5.0" ; then DISTRIB_RELEASE="5.0"
139
	elif has "ALT Linux 4.1" ; then DISTRIB_RELEASE="4.1"
140 141
	elif has "ALT Linux 4.0" ; then DISTRIB_RELEASE="4.0"
	elif has Walnut          ; then DISTRIB_RELEASE="4.0"
142 143
	elif has Hypericum       ; then DISTRIB_RELEASE="p8"
	elif has "starter kit"   ; then DISTRIB_RELEASE="p8"
144 145 146 147
	elif has 20070810 ; then DISTRIB_RELEASE="4.0"
	elif has Ajuga    ; then DISTRIB_RELEASE="4.0"
	elif has 20050723 ; then DISTRIB_RELEASE="3.0"
	elif has Citron   ; then DISTRIB_RELEASE="2.4"
148 149 150 151
	fi

elif distro gentoo-release ; then
	DISTRIB_ID="Gentoo"
152
	MAKEPROFILE=$(readlink $ROOTDIR/etc/portage/make.profile 2>/dev/null) || MAKEPROFILE=$(readlink $ROOTDIR/etc/make.profile)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
153
	DISTRIB_RELEASE=$(basename $MAKEPROFILE)
154
	echo $DISTRIB_RELEASE | grep -q "[0-9]" || DISTRIB_RELEASE=$(basename "$(dirname $MAKEPROFILE)") #"
155 156 157

elif distro slackware-version ; then
	DISTRIB_ID="Slackware"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
158
	DISTRIB_RELEASE="$(grep -Eo '[0-9]+\.[0-9]+' $DISTROFILE)"
159

160
elif distro os-release && hascommand apk ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
161
	# shellcheck disable=SC1090
162
	. $ROOTDIR/etc/os-release
163
	DISTRIB_ID="$(firstupper "$ID")"
164 165
	DISTRIB_RELEASE="$VERSION_ID"

166
elif distro os-release && hascommand tce-ab ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
167
	# shellcheck disable=SC1090
168 169 170 171
	. $ROOTDIR/etc/os-release
	DISTRIB_ID="TinyCoreLinux"
	DISTRIB_RELEASE="$VERSION_ID"

172
elif distro os-release && hascommand xbps-query ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
173
	# shellcheck disable=SC1090
174 175 176 177
	. $ROOTDIR/etc/os-release
	DISTRIB_ID="VoidLinux"
	DISTRIB_RELEASE="Live"

178 179 180 181 182 183 184
elif distro arch-release ; then
	DISTRIB_ID="ArchLinux"
	DISTRIB_RELEASE="2010"
	if grep 2011 -q $ROOTDIR/etc/pacman.d/mirrorlist ; then
		DISTRIB_RELEASE="2011"
	fi

185
# Elbrus
186 187 188 189
elif distro mcst_version ; then
	DISTRIB_ID="MCST"
	DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "release" | sed -e "s|.*release \([0-9]*\).*|\1|g")

190 191 192 193 194
# OpenWrt
elif distro openwrt_release ; then
	. $DISTROFILE
	DISTRIB_RELEASE=$(cat $ROOTDIR/etc/openwrt_version)

195 196 197 198 199 200
elif distro astra_version ; then
	#DISTRIB_ID=`cat $DISTROFILE | get_var DISTRIB_ID`
	DISTRIB_ID="AstraLinux"
	#DISTRIB_RELEASE=$(cat "$DISTROFILE" | head -n1 | sed -e "s|.* \([a-z]*\).*|\1|g")
	DISTRIB_RELEASE=$DISTRIB_CODENAME

201 202 203 204 205 206 207 208
# for Ubuntu use standard LSB info
elif [ "$DISTRIB_ID" = "Ubuntu" ] && [ -n "$DISTRIB_RELEASE" ]; then
	# use LSB version
	true

# Debian based
elif distro debian_version ; then
	DISTRIB_ID="Debian"
209
	DISTRIB_RELEASE=$(cat $DISTROFILE | sed -e "s/\..*//g")
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229


# Mandriva based
elif distro pclinuxos-release ; then
	DISTRIB_ID="PCLinux"
	if   has "2007" ; then DISTRIB_RELEASE="2007"
	elif has "2008" ; then DISTRIB_RELEASE="2008"
	elif has "2010" ; then DISTRIB_RELEASE="2010"
	fi

elif distro mandriva-release || distro mandrake-release ; then
	DISTRIB_ID="Mandriva"
	if   has 2005 ; then DISTRIB_RELEASE="2005"
	elif has 2006 ; then DISTRIB_RELEASE="2006"
	elif has 2007 ; then DISTRIB_RELEASE="2007"
	elif has 2008 ; then DISTRIB_RELEASE="2008"
	elif has 2009.0 ; then DISTRIB_RELEASE="2009.0"
	elif has 2009.1 ; then DISTRIB_RELEASE="2009.1"
	else
		# use /etc/lsb-release info by default
230 231 232
		if has ROSA ; then
			DISTRIB_ID="ROSA"
		fi
233 234 235 236 237 238 239 240 241 242 243 244
	fi

# Fedora based

elif distro MCBC-release ; then
	DISTRIB_ID="MCBC"
	if   has 3.0 ; then DISTRIB_RELEASE="3.0"
	elif has 3.1 ; then DISTRIB_RELEASE="3.1"
	fi

elif distro fedora-release ; then
	DISTRIB_ID="Fedora"
245
	DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "release" | sed -e "s|.*release \([0-9]*\).*|\1|g") #"
246 247 248 249 250 251 252 253 254

elif distro redhat-release ; then
	# FIXME if need
	# actually in the original RHEL: Red Hat Enterprise Linux .. release N
	DISTRIB_ID="RHEL"
	if has CentOS ; then
		DISTRIB_ID="CentOS"
	elif has Scientific ; then
		DISTRIB_ID="Scientific"
255 256
	elif has GosLinux ; then
		DISTRIB_ID="GosLinux"
257 258 259 260 261 262 263 264
	fi
	if has Beryllium ; then
		DISTRIB_ID="Scientific"
		DISTRIB_RELEASE="4.1"
	elif has "release 4" ; then DISTRIB_RELEASE="4"
	elif has "release 5" ; then DISTRIB_RELEASE="5"
	elif has "release 6" ; then DISTRIB_RELEASE="6"
	elif has "release 7" ; then DISTRIB_RELEASE="7"
265
	elif has "release 8" ; then DISTRIB_RELEASE="8"
266 267 268 269 270 271 272 273 274 275 276 277
	fi

# SUSE based
elif distro SuSe-release || distro SuSE-release ; then
	DISTRIB_ID="SUSE"
	DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "VERSION" | sed -e "s|^VERSION = ||g")
	if   has "SUSE Linux Enterprise Desktop" ; then
		DISTRIB_ID="SLED"
	elif has "SUSE Linux Enterprise Server" ; then
		DISTRIB_ID="SLES"
	fi

278 279 280 281 282 283 284
# https://www.freedesktop.org/software/systemd/man/os-release.html
elif distro os-release ; then
	# shellcheck disable=SC1090
	. $ROOTDIR/etc/os-release
	DISTRIB_ID="$(firstupper "$ID")"
	DISTRIB_RELEASE="$VERSION_ID"
	[ -n "$DISTRIB_RELEASE" ] || DISTRIB_RELEASE="CUR"
285 286 287
	if [ "$ID" = "opensuse-leap" ] ; then
		DISTRIB_ID="SUSE"
	fi
288

289
# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
290
elif [ "$(uname)" = "FreeBSD" ] ; then
291 292 293 294 295
	DISTRIB_ID="FreeBSD"
	UNAME=$(uname -r)
	DISTRIB_RELEASE=$(echo "$UNAME" | grep RELEASE | sed -e "s|\([0-9]\.[0-9]\)-RELEASE|\1|g")

# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
296
elif [ "$(uname)" = "SunOS" ] ; then
297 298 299
	DISTRIB_ID="SunOS"
	DISTRIB_RELEASE=$(uname -r)

Vitaly Lipatov's avatar
Vitaly Lipatov committed
300
# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
301
elif [ "$(uname -s 2>/dev/null)" = "Darwin" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
302 303 304
	DISTRIB_ID="MacOS"
	DISTRIB_RELEASE=$(uname -r)

305
# fixme: move to up
306
elif [ "$(uname)" = "Linux" ] && hascommand guix ; then
307 308 309
	DISTRIB_ID="GNU/Linux/Guix"
	DISTRIB_RELEASE=$(uname -r)

310
# fixme: move to up
Vitaly Lipatov's avatar
Vitaly Lipatov committed
311
elif [ "$(uname)" = "Linux" ] && [ -x $ROOTDIR/system/bin/getprop ] ; then
312 313 314
	DISTRIB_ID="Android"
	DISTRIB_RELEASE=$(getprop | awk -F": " '/build.version.release/ { print $2 }' | tr -d '[]')

Vitaly Lipatov's avatar
Vitaly Lipatov committed
315
elif [ "$(uname -o 2>/dev/null)" = "Cygwin" ] ; then
316 317 318
        DISTRIB_ID="Cygwin"
        DISTRIB_RELEASE="all"

319 320 321
# try use standart LSB info by default
elif distro lsb-release && [ -n "$DISTRIB_RELEASE" ]; then
	# use LSB
322 323

	# fix distro name
324 325
	case "$DISTRIB_ID" in
		"openSUSE Tumbleweed")
326 327
			DISTRIB_ID="SUSE"
			DISTRIB_RELEASE="Tumbleweed"
328 329
			;;
	esac
330 331
fi

332 333 334 335 336
get_uname()
{
    tolower $(uname $1) | tr -d " \t\r\n"
}

337 338 339 340
get_base_os_name()
{
local DIST_OS
# Resolve the os
341
DIST_OS="$(get_uname -s)"
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
case "$DIST_OS" in
    'sunos')
        DIST_OS="solaris"
        ;;
    'hp-ux' | 'hp-ux64')
        DIST_OS="hpux"
        ;;
    'darwin' | 'oarwin')
        DIST_OS="macosx"
        ;;
    'unix_sv')
        DIST_OS="unixware"
        ;;
    'freebsd' | 'openbsd' | 'netbsd')
        DIST_OS="freebsd"
        ;;
esac
echo "$DIST_OS"
}

362

363 364 365 366
get_arch()
{
local DIST_ARCH
# Resolve the architecture
367
DIST_ARCH="$(get_uname -m)"
368
case "$DIST_ARCH" in
369
    'ia32' | 'i386' | 'i486' | 'i586' | 'i686')
370 371
        DIST_ARCH="x86"
        ;;
372 373 374
    'amd64' | 'x86_64')
        DIST_ARCH="x86_64"
        ;;
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    'ia64' | 'ia-64')
        DIST_ARCH="ia64"
        ;;
    'ip27' | 'mips')
        DIST_ARCH="mips"
        ;;
    'powermacintosh' | 'power' | 'powerpc' | 'power_pc' | 'ppc64')
        DIST_ARCH="ppc"
        ;;
    'pa_risc' | 'pa-risc')
        DIST_ARCH="parisc"
        ;;
    'sun4u' | 'sparcv9')
        DIST_ARCH="sparc"
        ;;
    '9000/800')
        DIST_ARCH="parisc"
        ;;
    armv*)
394
        if [ -z "$(readelf -A /proc/self/exe | grep Tag_ABI_VFP_args)" ] ; then
395 396 397 398 399 400 401 402 403 404 405 406 407
            DIST_ARCH="armel"
        else
            DIST_ARCH="armhf"
        fi
        ;;
esac
echo "$DIST_ARCH"
}

get_bit_size()
{
local DIST_BIT
# Check if we are running on 64bit platform, seems like a workaround for now...
408
DIST_BIT="$(get_uname -m)"
409 410 411 412
case "$DIST_BIT" in
    'amd64' | 'ia64' | 'x86_64' | 'ppc64')
        DIST_BIT="64"
        ;;
413 414 415
    'aarch64')
        DIST_BIT="64"
        ;;
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
#    'pa_risc' | 'pa-risc') # Are some of these 64bit? Least not all...
#       BIT="64"
#        ;;
    'sun4u' | 'sparcv9') # Are all sparcs 64?
        DIST_BIT="64"
        ;;
#    '9000/800')
#       DIST_BIT="64"
#        ;;
    *) # In any other case default to 32
        DIST_BIT="32"
        ;;
esac
echo "$DIST_BIT"
}

432
# TODO: check before calc
433 434
get_memory_size() {
    local detected=0
435 436 437 438 439 440 441 442 443 444 445 446 447 448
    local DIST_OS="$(get_base_os_name)"
    case "$DIST_OS" in
        macosx)
            detected=$((`sysctl hw.memsize | sed s/"hw.memsize: "//`/1024/1024))
            ;;
        freebsd)
            detected=$((`sysctl hw.physmem | sed s/"hw.physmem: "//`/1024/1024))
            ;;
        linux)
            [ -r /proc/meminfo ] && detected=$((`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`/1024))
            ;;
    esac

    # Exit codes only support values between 0 and 255. So use stdout.
449 450 451 452
    echo $detected
}


453 454 455 456 457 458 459 460 461 462 463 464
case $1 in
	-p)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		pkgtype
		exit 0
		;;
	-h)
		echo "distr_vendor - system name and version detection"
		echo "Usage: distr_vendor [options] [args]"
		echo "-p [SystemName] - print type of packaging system"
		echo "-d - print distro name"
465 466 467 468
		echo "-a - print hardware architecture"
		echo "-b - print size of arch bit (32/64)"
		echo "-m - print system memory size (in MB)"
		echo "-o - print base os name"
469 470 471 472 473 474 475 476 477 478 479
		echo "-v - print version of distro"
		echo "-e - print full name of distro with version (by default)"
		echo "-s [SystemName] - print name of distro for build system (like in the package release name)"
		echo "-n [SystemName] - print vendor name (as _vendor macros in rpm)"
		echo "-V - print the version of $0"
		echo "-h - this help"
		exit 0
		;;
	-d)
		echo $DISTRIB_ID
		;;
480 481 482 483 484 485 486 487 488 489 490 491
	-a)
		get_arch
		;;
	-b)
		get_bit_size
		;;
	-m)
		get_memory_size
		;;
	-o)
		get_base_os_name
		;;
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	-v)
		echo $DISTRIB_RELEASE
		;;
	-s)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		pkgvendor
		exit 0
		;;
	-n)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		rpmvendor
		exit 0
		;;
	-V)
508
		echo "20171010"
509 510 511 512 513 514 515 516
		exit 0
		;;
	*)
		# if run without args, just printout Name/Version of the current system
		[ -n "$DISTRIB_RELEASE" ] && echo $DISTRIB_ID/$DISTRIB_RELEASE || echo $DISTRIB_ID
		;;
esac