osstuff.sh 4.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
#!/bin/sh
#
# Copyright (c) 2007-2018 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

#
# Figure out which OS, distro. etc.
# Output: TAG=<string> OS=<string> DIST=<string> REL=<string> PKG=<string>
#  where OS is 'FreeBSD', 'Linux', 'Cygwin', etc.
#        DIST for Linux only, is 'Redhat', 'Fedora', 'Ubuntu', etc.
#        REL is the release like '6.2', '9.0', '5.1', etc.
#        TAG is a unique combo of the above like 'freebsd6', 'fedora4', etc.
#        PKG is one of pkg, rpm, deb, apk, etc.
#

if [ $# -ne 1 ]; then
    arg="-a"
else
    arg=$1
fi

# XXX cheezy hack for cross-building: if OSSTUFF env is set, use that info
if [ -n "$OSSTUFF" ]; then
    for _kv in $OSSTUFF; do
	_k=${_kv%%=*}
	_v=${_kv##*=}
	case $_k in
	TAG|OS|DIST|REL|PKG)
	    eval $_k=\"$_v\"
	    ;;
	esac
    done
    if [ -z "$OS" -o -z "$DIST" -o -z "$REL" ]; then
	echo "osstuff: malformed OSSTUFF environment var"
	exit 1
    fi
    if [ -z "$TAG" ]; then
	p1=`echo $DIST | tr '[A-Z]' '[a-z]'`
	p2=`echo $REL | sed -e 's/^\([0-9]*\).*/\1/'`
	TAG="${p1}${p2}"
    fi

    case $arg in
    -a)
	echo "TAG=$TAG OS=$OS DIST=$DIST REL=$REL PKG=$PKG"
	;;
    -t)
	echo "$TAG"
	;;
    -o)
	echo "$OS"
	;;
    -d)
	echo "$DIST"
	;;
    -r)
	echo "$REL"
	;;
    -p)
	echo "$PKG"
	;;
    esac

    exit 0
fi

os=`uname -s`
tag=
dist=

case $os in
FreeBSD)
    dist="FreeBSD"
    rel=`uname -v | sed -e 's/FreeBSD \([0-9][0-9]*\.[0-9][0-9]*\).*/\1/'`
    pkg=bsd
    ;;
Linux)
    if [ -r /etc/lsb-release ]; then
        dist=`grep DISTRIB_ID /etc/lsb-release | awk -F = '{ print $2; }'`
        rel=`grep DISTRIB_RELEASE /etc/lsb-release | awk -F = '{ print $2; }'`
    fi
    if [ -z "$dist" -a -r /etc/os-release ]; then
	dist=`(. /etc/os-release ; echo $ID)`
	if [ "$dist" = "debian" ]; then
	    trel=`(. /etc/os-release ; echo $VERSION_ID)`
	    if [ -n "$trel" ]; then
		dist="Debian"
		rel=$trel
	    else
		grep PRETTY_NAME /etc/os-release | grep -q sid
		if [ $? -eq 0 ]; then
		    rel=S
		    tag=debianS
		fi
	    fi
	fi
    fi
    if [ -n "$dist" -a \( "$dist" = "Ubuntu" -o "$dist" = "Debian" \) ]; then
	pkg=deb
    fi
    if [ -z "$dist" -a -r /etc/redhat-release ]; then
        trel=`grep 'Red Hat' /etc/redhat-release | sed -e 's/Red Hat Linux release \([0-9]\(\.[0-9]\)\?\).*/\1/'`
	if [ -n "$trel" ]; then
            dist="Redhat"
	    rel=$trel
	fi
	trel=`grep 'Fedora' /etc/redhat-release | sed -e 's/Fedora .*release \([0-9.]\+\).*/\1/'`
	if [ -n "$trel" ]; then
	    dist="Fedora"
	    rel=$trel
	fi
	trel=`grep 'CentOS' /etc/redhat-release | sed -e 's/CentOS .*release \([0-9.]\+\).*/\1/'`
	if [ -n "$trel" ]; then
	    dist="CentOS"
	    rel=$trel
	fi
	pkg=rpm
    fi
    if [ -r /etc/centos-release ]; then
	trel=`grep 'CentOS' /etc/centos-release | sed -e 's/CentOS .*release \([0-9.]\+\).*/\1/'` 	
	dist="CentOS"
	rel=$trel
	pkg=rpm
    fi
    # XXX hack check for stargate
    if [ -z "$dist" -a `uname -m` = "armv5tel" ]; then
        dist="Stargate"
        rel=1.0  # XXX probably wrong
    fi
    if [ "$dist" = "Ubuntu" -a `uname -m` = "aarch64" ]; then
	if [ "$rel" = "18.04" ]; then
	    tag=MoonshotUbuntu18
	elif [ "$rel" = "16.04" ]; then
	    tag=MoonshotUbuntu16
	else
	    tag=Moonshot
	fi
    fi
    ;;
CYGWIN_NT-*)	# aka Windows XP/7
    tag=$os	# XXX compat for tmcd makefile
    os="Cygwin"
    dist="NT"
    rel=`echo $tag | sed -e 's/^CYGWIN_NT-\(.*\)/\1/'`
    pkg=win
    ;;
*)
    dist="Unknown"
    rel="0.0"
    pkg="Unknown"
    ;;
esac

if [ -z "$tag" ]; then
    p1=`echo $dist | tr '[A-Z]' '[a-z]'`
    p2=`echo $rel | sed -e 's/^\([0-9]*\).*/\1/'`
    tag="${p1}${p2}"
fi

case $arg in
-a)
    echo "TAG=$tag OS=$os DIST=$dist REL=$rel PKG=$pkg"
    ;;
-t)
    echo "$tag"
    ;;
-o)
    echo "$os"
    ;;
-d)
    echo "$dist"
    ;;
-r)
    echo "$rel"
    ;;
-p)
    echo "$pkg"
    ;;
esac
exit 0