-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
399 lines (329 loc) · 8.99 KB
/
install
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/bin/bash
#
# Copyright (C) Microsoft Corporation. All rights reserved.
#
# Install or remove Microsoft System Center Virtual Machine Manager Agent
#
#
# internal function to create log entry. Timestamps with UTC
#
function WriteLog {
logfile="/var/opt/microsoft/scvmmguestagent/log/scvmm-install.log"
if [ ! -d `dirname ${logfile}` ]
then
mkdir -p `dirname ${logfile}`
fi
echo -e `date -u '+%D %T'`" UTC\t$1\t${caller}"
echo -e `date -u '+%D %T'`" UTC\t$1\t${caller}">>"${logfile}"
}
function usage {
WriteLog "Usage:"
WriteLog "Agent installation:"
WriteLog "install [-d installation_directory] scvmmguestagent.x.y-z.[x86|x64].tar"
WriteLog "Remove agent:"
WriteLog "install -r"
WriteLog "Upgrade agent:"
WriteLog "install -u -v x.y-z"
exit 1
}
function installExec {
ERROR=$((eval ${1}) 2>&1)
rc=$?
if [ $rc != 0 ]; then
WriteLog ${ERROR}
WriteLog "Failed to install SCVMM Guest Agent"
exit -1
fi
}
function getArch {
arch=`uname -p`
if [ ${arch} = "unknown" ]
then
arch=`uname -m`
fi
if [ `echo $arch | grep -c '^i[3-6]86$'` -eq 1 ]
then
archfriendly="x86"
elif [ ${arch} = "x86_64" ]
then
archfriendly="x64"
else
WriteLog "Failed to identify architecture. Exiting."
exit -1
fi
}
function installAgent {
if [ ! -n "${installdir}" ]
then
installdir="/opt/microsoft"
fi
installdir="${installdir}/scvmmguestagent"
#Untar kit
if [ ! -d "${installdir}" ]
then
mkdir -p "${installdir}"
else
if [ -d "${installdir}" ]
then
rm -fr "${installdir}/bin"
rm -fr "${installdir}/etc"
fi
fi
WriteLog "Installing SCVMM Guest Agent to ${installdir}"
installExec 'tar -xf "${agentkit}" -C ${installdir} --overwrite'
#Create default guest agent log dir
if [ ! -d "/var/opt/microsoft/scvmmguestagent/log" ]
then
mkdir -p "/var/opt/microsoft/scvmmguestagent/log"
fi
#Move init.d script to /etc/init.d
sed -i".bak" 's,<SCVMMHOME>,'$installdir',g' ${installdir}/bin/${servicename}
installExec "mv ${installdir}/bin/${servicename} /etc/init.d/"
chmod 744 "/etc/init.d/${servicename}"
if [ ! "${nomount}" = "true" ]
then
if [ -n "$(uname -v | awk '/(Debian|Ubuntu)/')" ]
then
sed -i".bak" 's/^# Default-Start:.*/# Default-Start: 2 3 4 5/g' /etc/init.d/${servicename}
sed -i".bak" 's/^# Default-Stop:.*/# Default-Stop: 0 1 6/g' /etc/init.d/${servicename}
rm -f /etc/init.d/${servicename}.bak
fi
#Daemon registration
if [ -x /usr/sbin/update-rc.d ]
then
update-rc.d ${servicename} defaults
elif [ -x /usr/lib/lsb/install_initd ]
then
/usr/lib/lsb/install_initd /etc/init.d/${servicename}
else
if [ -x /sbin/chkconfig ]
then
/sbin/chkconfig --add ${servicename}
/sbin/chkconfig --level 3 ${servicename} on
else
WriteLog "Unable to find service control mechanism. Exiting."
exit -1
fi
fi
else
#Start agent
WriteLog "Starting daemon"
installExec '/etc/init.d/${servicename} startnomount "${mntpath}"'
fi
WriteLog "Successfully installed SCVMM Guest Agent"
}
function removeAgent {
WriteLog "Removing SCVMM Guest Agent"
if [ -e /etc/init.d/${servicename} ]
then
#Remove daemon
if [ -x /usr/sbin/update-rc.d ]
then
update-rc.d -f ${servicename} remove > /dev/null
elif [ -x /usr/lib/lsb/install_initd ]
then
/usr/lib/lsb/remove_initd /etc/init.d/${servicename}
else
if [ -x /sbin/chkconfig ]
then
/sbin/chkconfig --del ${servicename}
fi
fi
rm -f /etc/init.d/${servicename}
fi
#Disable waagent daemon
WriteLog "Disabling waagent daemon"
#SystemD
if [ -x /bin/systemctl -o -x /usr/bin/systemctl ]
then
if [ `systemctl is-enabled walinuxagent 2>/dev/null|grep enabled|wc -l` -eq 1 ]
then
systemctl disable walinuxagent
systemctl stop walinuxagent
fi
fi
if [ -x /sbin/insserv ]
then
/sbin/insserv -r waagent
elif [ -x /sbin/chkconfig ]
then
/sbin/chkconfig waagent off
fi
if [ -e /etc/init/waagent.conf -o -e /etc/init/walinuxagent.conf ]
then
if [ -e /etc/init/waagent.conf ];then mv /etc/init/waagent.conf /etc/init/waagent.conf.disabled;fi
if [ -e /etc/init/walinuxagent.conf ];then mv /etc/init/walinuxagent.conf /etc/init/walinuxagent.conf.disabled;fi
elif [ -x /usr/sbin/update-rc.d ]
then
/usr/sbin/update-rc.d -f waagent remove
fi
if [ -d "${SCVMM_HOME}" ]
then
#Uninstall agent
rm -rf "${SCVMM_HOME}"
fi
}
function upgradeAgent {
#Check for upgrade viability
doUpgrade=0
currentversion=`cat ${SCVMM_HOME}/etc/version`
WriteLog "Current version: ${currentversion}"
if [ `echo $currentversion |grep -c .` -eq 1 ]
then
currentmajor=`echo ${currentversion}|cut -f1 -d.`
currentminor=`echo ${currentversion}|cut -f2 -d.`
currentpatch=`echo ${currentversion}|cut -f3 -d.`
currentbuild=`echo ${currentversion}|cut -f4 -d.`
newmajor=`echo ${version}|cut -f1 -d.`
newminor=`echo ${version}|cut -f2 -d.`
newpatch=`echo ${version}|cut -f3 -d.`
newbuild=`echo ${version}|cut -f4 -d.`
if [ ${newmajor} -gt ${currentmajor} ]
then
doUpgrade=1
elif [ ${newmajor} -eq ${currentmajor} -a ${newminor} -gt ${currentminor} ]
then
doUpgrade=1
elif [ ${newmajor} -eq ${currentmajor} -a ${newminor} -eq ${currentminor} -a ${newpatch} -gt ${currentpatch} ]
then
doUpgrade=1
elif [ ${newmajor} -eq ${currentmajor} -a ${newminor} -eq ${currentminor} -a ${newpatch} -eq ${currentpatch} -a ${newbuild} -gt ${currentbuild} ]
then
doUpgrade=1
fi
else
WriteLog "Unable to parse installed agent version. Exiting"
exit -1
fi
if [ ${doUpgrade} -eq 1 ]
then
WriteLog "Agent requires upgrade"
#Back up customizations
configfile="${SCVMM_HOME}/etc/scvmm.conf"
if [ -e "${configfile}" ]
then
customconf=`cat ${configfile}`
fi
WriteLog "Stopping service"
proc=`ps -aef |grep -v grep | grep ${servicename}`
if [ $? -eq 0 ]
then
WriteLog "Stopping service ${servicename}"
installExec "/etc/init.d/${servicename} stop"
else
WriteLog "Nothing to stop"
fi
#Remove
WriteLog "Removing agent"
removeAgent
#Install
WriteLog "Starting install"
installdir=`echo ${SCVMM_HOME} |sed 's/\/scvmmguestagent//g'`
installAgent
#Start daemon
WriteLog "Starting daemon"
installExec "/etc/init.d/${servicename} start"
#Rewrite config file
echo -e "${customconf}" > ${configfile}
else
WriteLog "Current agent version is the latest."
exit 0
fi
exit 1
}
#Main
archfriendly=""
agentkit=""
installdir=""
remove=0
upgrade=0
servicename="scvmmguestagent"
whoami=`whoami`
# Check root privelege
if [ "$whoami" != "root" ]; then
WriteLog "You must be root to install this product."
exit 2
fi
while getopts d:v:p:ur opt
do
case "$opt" in
d) installdir=${OPTARG};;
v) version=${OPTARG};;
r) remove=1;;
u) upgrade=1;;
p) mntpath=${OPTARG};;
[?]) usage;;
esac
done
shift $(($OPTIND - 1))
#Main
getArch
#Check for SCVMM_HOME
if [[ -z "${SCVMM_HOME}" && -f /etc/init.d/${servicename} ]]
then
SCVMM_HOME=`cat /etc/init.d/${servicename}|grep -e '^SCVMM_HOME='|cut -f2 -d=`
fi
if [ `echo ${mntpath}|grep /|wc -l` -ge 1 ]
then
WriteLog "Install called with mount path provided"
nomount="true"
agentkit=`ls $(dirname ${0})/${servicename}.*.${archfriendly}.tar|head -n 1`
else
if [ ${upgrade} -eq 0 -a ${remove} -eq 0 ]
then
if [ $# -lt 1 ]
then
usage
fi
fi
fi
if [ ${remove} -eq 1 ]
then
removeAgent
elif [ ${upgrade} -eq 1 ]
then
if [ ! ${version} ]
then
WriteLog "Upgrade specified but no supplied version. Exiting."
usage
fi
agentkit="$(dirname ${0})/${servicename}.${version}.${archfriendly}.tar"
if [ -f "${agentkit}" ]
then
upgradeAgent
else
WriteLog "Agent tar file ${agentkit} not found. Exiting."
exit -1
fi
else
#Use specified agent kit
if [ "${nomount}" = "true" ]
then
agentkit=`ls $(dirname ${0})/${servicename}.*.${archfriendly}.tar|head -n 1`
else
#Check for existing SCVMM guest agent and exit if found, unless mntpath provided
if [ -n "${SCVMM_HOME}" ]
then
if [ -d "${SCVMM_HOME}" ]
then
WriteLog "Found existing SCVMM guest agent, nothing to do."
exit 1
fi
fi
agentkit="${1}"
fi
if [ -f "${agentkit}" ]
then
archtest=`tar -O -f ${agentkit} -x etc/buildarch |grep -c ${archfriendly}`
if [ ${archtest} -eq 1 ]
then
installAgent
else
WriteLog "Specified agent tar file does not match machine architecture. Exiting."
exit -1
fi
else
WriteLog "Agent tar file ${agentkit} not found. Exiting."
exit -1
fi
fi