-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathGetLinuxOS.sh
More file actions
executable file
·343 lines (294 loc) · 12.5 KB
/
GetLinuxOS.sh
File metadata and controls
executable file
·343 lines (294 loc) · 12.5 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/sh
#####################################################################
## Copyright (c) Microsoft Corporation. All rights reserved. See license.txt for license information.
## Name: GetLinuxOS.sh
## Date: 7/1/2012
## Version: 1.0
## Description: Release file parsing for Linux Operating Systems
#####################################################################
Hostname=`uname -n`
OSName=`uname -s`
Version=`uname -r` # Version
Arch=`uname -m` # Overridden as uname -p on some platforms
ReleaseFile=`echo $PLATFORM_RELEASE_FILE_SCX_INSTALLER_INPUT`
EtcPath="/etc"
# Create destination directory if it does not exist
RelDir="<RelDir>"
RelFile="$RelDir/scx-release"
DisableFile="$RelDir/disablereleasefileupdates"
if [ ! -e ${RelDir} ]; then
mkdir -p $RelDir
fi
## Function used to get Linux Distro and Version
######################################################################
GetKitType() {
# The os-release file doesn't tell us the type of kit, so try to determine
if [ `rpm -q rpm 2> /dev/null | /bin/egrep '^rpm-[0-9].*' | wc -l` = 1 ]; then
OSAlias="UniversalR"
elif [ `dpkg -l dpkg 2> /dev/null | egrep "^ii.*"| wc -l` = 1 ]; then
OSAlias="UniversalD"
else
OSAlias="Universal?"
fi
}
GetLinuxInfo() {
IsLinux="true"
# Return kernel version as OS version - will be updated for specific cases below
Version=`uname -r | cut -d. -f1,2`
# Determine release file
# Try to find -release file
if [ -z ${ReleaseFile} ]; then
ReleaseFile=`ls -F ${EtcPath}/*-release 2>/dev/null | grep -v lsb-release |grep -v release@| grep -v scx-release| sed -n '1p'`
fi
# Fall back to lsb-release (e.g. Ubunutu)
if [ -z ${ReleaseFile} ]; then
if [ -e "${EtcPath}/lsb-release" ]; then ReleaseFile="${EtcPath}/lsb-release"; fi
fi
# Debian (no lsb-release, but debian_version exists)
if [ -z ${ReleaseFile} ]; then
TestFile="${EtcPath}/debian_version"
if [ -e $TestFile ]; then ReleaseFile=$TestFile; fi
fi
# Try RHEL/CentOS
TestFile="${EtcPath}/redhat-release"
if [ -f $TestFile ]; then ReleaseFile=$TestFile; fi
# Try OEL
TestFile="${EtcPath}/oracle-release"
if [ -f $TestFile ]; then ReleaseFile=$TestFile; fi
# Try NeoKylin
TestFile="${EtcPath}/neokylin-release"
if [ -f $TestFile ]; then ReleaseFile=$TestFile; fi
# Try SLES
TestFile="${EtcPath}/SuSE-release"
if [ -f $TestFile ]; then ReleaseFile=$TestFile; fi
# Get OS Name
if [ ! -z $ReleaseFile ]; then
OSName=`/bin/egrep -o 'Red Hat Enterprise Linux|SUSE Linux Enterprise Server|SUSE LINUX Enterprise Server' $ReleaseFile`
fi
## Extract Version from file
## Could also use /etc/*release (not Ubuntu)
if [ "${OSName}" = "Red Hat Enterprise Linux" ]; then
Version=`grep 'Red Hat Enterprise' $ReleaseFile | sed s/.*release\ // | sed s/\ .*//`
if [ "${Version}" != "" ]; then
OSAlias="RHEL"
OSManufacturer="Red Hat, Inc."
OSFullName=`cat $ReleaseFile`
OSShortName="RHEL_"
fi
elif [ "${OSName}" = "SUSE Linux Enterprise Server" ]; then
# SLES 10 uses "Linux". Need to parse the minor version as SLES 10.0 is not supported, only 10.1 and up
# SLES 15 uses /etc/os-release as $ReleaseFile. It contains all information inside "" so Version appears like 15". Need to parse the Version to remove "
Version=`grep 'SUSE Linux Enterprise Server' $ReleaseFile | sed 's/.*Server\ \| (.*\|\"//g'`
# Discovery Wizard wants "10.X" not "10 SPX"
if [ `echo ${Version} | grep 'SP' | wc -l` -eq 1 ]; then
Version=`echo ${Version} | awk '{print $1"."$2}'| sed s/SP//`
else
VersionPL=`grep PATCHLEVEL $ReleaseFile | sed s/.*PATCHLEVEL\ =\ //`
if [ "${VersionPL}" != "" ]; then
Version=`echo ${Version}.${VersionPL}`
fi
fi
if [ "${Version}" != "" ]; then
OSAlias="SLES"
OSManufacturer="SUSE GmbH"
OSShortName="SUSE_"
fi
elif [ "${OSName}" = "SUSE LINUX Enterprise Server" ]; then
# SLES 9 uses "LINUX". No need to parse minor version as Agent supports 9.0 and up.
Version=`grep 'SUSE LINUX Enterprise Server' $ReleaseFile | sed s/.*Server\ // | sed s/\ \(.*//`
if [ "${Version}" != "" ]; then
OSAlias="SLES"
OSManufacturer="SUSE GmbH"
OSShortName="SUSE_"
fi
else
OSAlias="Universal"
OSName="Linux"
Version=`uname -r | cut -d. -f1,2`
# Do we have the (newer) os-release standard file?
# If so, that trumps everything else
if [ -e "${EtcPath}/os-release" ]; then
ReleaseFile="${EtcPath}/os-release"
GetKitType
# The os-release files contain TAG=VALUE pairs; just read it in
. "$ReleaseFile"
# Some fields are optional, for details see the WWW site:
# http://www.freedesktop.org/software/systemd/man/os-release.html
[ ! -z "$NAME" ] && OSName="$NAME"
[ ! -z "$VERSION_ID" ] && Version="$VERSION_ID"
# Set the manufacturer if we know this ID
# (Set OSAlias for unit test purposes; test injection won't inject that)
[ -z "$ID" ] && ID="linux"
case $ID in
debian)
OSManufacturer="Software in the Public Interest, Inc."
OSAlias="UniversalD"
if [ "${Version}" != "" ]; then
OSShortName="Debian_"
else
OSShortName="Debian"
fi
;;
opensuse)
OSManufacturer="SUSE GmbH"
OSAlias="UniversalR"
if [ "${Version}" != "" ]; then
OSShortName="OpenSUSE_"
else
OSShortName="OpenSUSE"
fi
;;
centos)
OSManufacturer="Central Logistics GmbH"
OSAlias="UniversalR"
if [ "${Version}" != "" ]; then
OSShortName="CentOS_"
else
OSShortName="CentOS"
fi
;;
ubuntu)
OSManufacturer="Canonical Group Limited"
OSAlias="UniversalD"
if [ "${Version}" != "" ]; then
OSShortName="Ubuntu_"
else
OSShortName="Ubuntu"
fi
;;
ol)
OSManufacturer="Oracle Corporation"
OSAlias="UniversalR"
if [ "${Version}" != "" ]; then
OSShortName="Oracle_"
else
OSShortName="Oracle"
fi
;;
esac
elif [ ! -z $ReleaseFile ]; then
# Set OSName to release file contents for evaluation. If parsing logic is not known, Release File contents will be used as OSName.
OSName=`sed '/^$/d' ${ReleaseFile} | head -1`
# Try known cases for OSName/Version
# ALT Linux
if [ `echo $OSName | grep "ALT Linux" | wc -l` -gt 0 ]; then
OSName="ALT Linux"
OSAlias="UniversalR"
OSManufacturer="ALT Linux Ltd"
Version=`grep 'ALT Linux' $ReleaseFile | sed s/.*Linux\ // | sed s/\ \.*//`
OSShortName="ALTLinux_"
fi
# Enterprise Linux Server
if [ `echo $OSName | grep "Enterprise Linux Enterprise Linux Server" | wc -l` -gt 0 ]; then
OSName="Enterprise Linux Server"
OSAlias="UniversalR"
OSManufacturer="Oracle Corporation"
Version=`grep 'Enterprise Linux Enterprise Linux Server' $ReleaseFile | sed s/.*release\ // | sed s/\ \(.*//`
OSShortName="Oracle_"
fi
# Oracle Enterprise Linux Server
if [ `echo $OSName | grep "Oracle Linux Server" | wc -l` -gt 0 ]; then
OSName="Oracle Linux Server"
OSAlias="UniversalR"
OSManufacturer="Oracle Corporation"
Version=`grep 'Oracle Linux Server release' $ReleaseFile | sed s/.*release\ // | sed s/\ \(.*//`
OSShortName="Oracle_"
fi
# NeoKylin Linux Advanced Server
if [ `echo $OSName | grep "NeoKylin Linux Advanced Server" | wc -l` -gt 0 ]; then
OSName="NeoKylin Linux Server"
OSAlias="UniversalR"
OSManufacturer="China Standard Software Co., Ltd."
Version=`grep 'NeoKylin Linux Advanced Server release' $ReleaseFile | sed s/.*release\ // | sed s/\ \(.*//`
OSShortName="NeoKylin_"
fi
# OpenSUSE
if [ `echo $OSName | grep -i "openSUSE" | wc -l` -gt 0 ]; then
Version=`echo $OSName | awk '{print $2}'`
OSName="openSUSE"
OSAlias="UniversalR"
OSManufacturer="SUSE GmbH"
OSShortName="OpenSUSE_"
fi
# Debian
if [ "$ReleaseFile" = "${EtcPath}/debian_version" ]; then
OSName="Debian"
OSAlias="UniversalD"
OSManufacturer="Software in the Public Interest, Inc."
Version=`cat ${EtcPath}/debian_version`
OSShortName="Debian_"
fi
# Ubuntu
if [ `echo $OSName | grep "Ubuntu" | wc -l` -gt 0 ]; then
OSName="Ubuntu"
OSAlias="UniversalD"
OSManufacturer="Canonical Group Limited "
Version=`grep 'DISTRIB_RELEASE' $ReleaseFile | cut -d'=' -f2`
OSShortName="Ubuntu_"
fi
# Fedora
if [ `echo $OSName | grep "Fedora" | wc -l` -gt 0 ]; then
OSName="Fedora"
OSAlias="UniversalR"
OSManufacturer="Red Hat, Inc."
Version=`grep 'Fedora' $ReleaseFile | sed s/.*release\ // | sed s/\ .*//`
OSShortName="Fedora_"
fi
# CentOS
if [ `echo $OSName | grep "CentOS" | wc -l` -gt 0 ]; then
OSName="CentOS"
OSAlias="UniversalR"
OSManufacturer="Central Logistics GmbH"
Version=`grep 'CentOS' $ReleaseFile | sed s/.*release\ // | sed s/\ .*//`
OSShortName="CentOS_"
fi
# If distro is not known, determine whether RPM or DPKG is installed
if [ "${OSAlias}" = "Universal" ]; then
# Identify package manager
GetKitType
fi
# If Version is null, something went wrong in release file parsing, reset to kernel version
if [ "$Version" = "" ]; then
Version=`uname -r`
fi
# If OSName is null, something went wrong in release file parsing, reset to Linux
if [ "$OSName" = "" ]; then
OSName="Linux"
OSManufacturer="Universal"
OSShortName="$OSName"
fi
else
GetKitType
Version=`uname -r`
OSName="Linux"
OSManufacturer="Universal"
OSShortName="${OSName}_"
fi
fi
if [ -z `echo ${Version} | grep '\.'` ]; then
Version="$Version.0"
fi
# Tack the version number onto the OSShortName if we have one
if [ -n "$Version" ]; then
OSShortName="${OSShortName}${Version}"
fi
if [ -z "$OSFullName" ]; then
# Construct OSFullName string
OSFullName="$OSName $Version ($Arch)"
fi
}
## End Linux distro function
######################################################################
GetLinuxInfo
# If the touch file does not exist or RelFile does not exist, write the rel file
if [ ! -e $DisableFile ] || [ ! -e $RelFile ]; then
# Update scx-release
printf "OSName=$OSName\n" > $RelFile
printf "OSVersion=$Version\n" >> $RelFile
printf "OSFullName=$OSFullName\n" >>$RelFile
printf "OSAlias=$OSAlias\n" >>$RelFile
printf "OSManufacturer=$OSManufacturer\n" >>$RelFile
printf "OSShortName=$OSShortName\n" >> $RelFile
# Verify that it's W:R so non-priv'ed users can read
chmod 644 $RelFile
fi