-
Notifications
You must be signed in to change notification settings - Fork 11
/
copy_kernel.ps1
executable file
·458 lines (389 loc) · 13.4 KB
/
copy_kernel.ps1
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/usr/bin/powershell
#
# Copy the latest kernel build from the secure share to the local directory,
# then install it, set the default kernel, switch out this script for the
# secondary boot replacement, and reboot the machine.
#
# Author: John W. Fawcett, Principal Software Development Engineer, Microsoft
#
param (
[Parameter(Mandatory=$false)] [string] $pkg_mount_point="Undefined",
[Parameter(Mandatory=$false)] [string] $pkg_mount_source="Undefined",
[Parameter(Mandatory=$false)] [string] $pkg_resourceGroup="smoke_working_resource_group",
[Parameter(Mandatory=$false)] [string] $pkg_storageaccount="smokeworkingstorageacct",
[Parameter(Mandatory=$false)] [string] $pkg_container="last-build-packages",
[Parameter(Mandatory=$false)] [string] $pkg_location="westus"
)
$global:isHyperV = $false
$global:o = New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck
$global:pw=convertto-securestring -AsPlainText -force -string 'P@ssW0rd-'
$global:cred=new-object -typename system.management.automation.pscredential -argumentlist "mstest",$global:pw
$global:session=$null
function ErrOut([string] $failPoint) {
#
# Not really sure what happened. Better let a human have a look...
#
phoneHome("FAILURE in copy_kernel.ps1!! Kernel was not installed and the system may be in an inconsistent state.")
phoneHome("Shutting down the system for examination")
#
# Call the reporting script directly, passing in the failure point. This will cause the install to fail above
#
./report_kernel_version.ps1 $failure_point
sync
sync
sync
# halt
exit 1
}
get-pssession | remove-pssession
$agents = pidof omiagent
#
# Have to specify full path here to avoid
foreach ($agent in $agents) {
Stop-Process -id
@(/bin/kill -9 $agent)
}
function callItIn($c, $m) {
$output_path="c:\temp\progress_logs\$c"
$m | out-file -Append $output_path
return
}
function phoneHome($m) {
echo $m
if ($global:isHyperV -eq $true) {
if ($global:session -eq $null) {
echo "*** Restarting the PowerShell session!" | out-file -Append /opt/microsoft/borg_progress.log
get-pssession | remove-pssession
$global:session=new-PSSession -computername lis-f1637.redmond.corp.microsoft.com -credential $global:cred -authentication Basic -SessionOption $global:o
}
invoke-command -session $global:session -ScriptBlock ${function:callItIn} -ArgumentList $c,$m
$m | out-file -Append /opt/microsoft/borg_progress.log
} else {
$m | out-file -Append /opt/microsoft/borg_progress.log
}
}
function callVersionIn($f,$m) {
$output_path=$f
$m | out-file -Force $output_path
return
}
function phoneVersionHome($m) {
$outFile = "c:\temp\expected_version_deb"
if (Test-Path /bin/rpm) {
$outFile = "c:\temp\expected_version_centos"
}
if ($global:isHyperV -eq $true) {
if ($global:session -eq $null) {
echo "*** Restarting (2) the PowerShell session!" | out-file -Append /opt/microsoft/borg_progress.log
get-pssession | remove-pssession
$global:session=new-PSSession -computername lis-f1637.redmond.corp.microsoft.com -credential $global:cred -authentication Basic -SessionOption $global:o
}
invoke-command -session $global:session -ScriptBlock ${function:callVersionIn} -ArgumentList $outFile,$m
} else {
$output_path="/root/expected_version"
$m | out-file -Append $output_path
}
}
if (Get-Item -ErrorAction SilentlyContinue -Path /opt/microsoft/borg_progress.log ) {
Remove-Item /opt/microsoft/borg_progress.log
}
Stop-Transcript | out-null
Start-Transcript -path /root/borg_install_log -append
#
# Remove the old sentinel file and reset
#
Remove-Item -Force "/root/expected_version"
echo "System Initialization" | Out-File -Path "/root/expected_version"
$failure_point="Setup"
#
# What OS are we on?
#
$linuxInfo = Get-Content /etc/os-release -Raw | ConvertFrom-StringData
$c=hostname
$c | Out-File -FilePath /opt/microsoft/borg_progress.log
#
# Start by cleaning out any existing downloads
#
$global:isHyperV=$true
$lookup=nslookup cdmbuildsna01.redmond.corp.microsoft.com
if ($? -eq $false) {
$global:isHyperV = $false
phoneHome "It looks like we're in Azure"
} else {
phoneHome "It looks like we're in Hyper-V"
}
phoneHome "******************************************************************"
phoneHome "* BORG DRONE $hostName starting conversion..."
phoneHome "******************************************************************"
if ($ENV:PATH -ne "") {
$ENV:PATH=$ENV:PATH + ":/sbin:/bin:/usr/sbin:/usr/bin:/opt/omi/bin:/usr/local:/usr/sbin:/bin"
} else {
$ENV:PATH="/sbin:/bin:/usr/sbin:/usr/bin:/opt/omi/bin:/usr/local:/usr/sbin:/bin"
}
$failure_point="chmod"
@(chmod 777 /opt/microsoft/borg_progress.log)
@(ls -laF /opt/microsoft/borg_progress.log)
$failure_point="cleaning old"
phoneHome "Starting copy file scipt"
cd /root
$kernFolder="/root/latest_kernel"
If (Test-Path $kernFolder) {
Remove-Item -Recurse -Force $kernFolder
}
new-item $kernFolder -type directory
$failure_point="Mounting"
if ($global:isHyperV -eq $true) {
if ($pkg_mount_point -eq "Undefined") {
$pkg_mount_point="/mnt/ostcnix"
$pkg_mount_dir= $pkg_mount_point
} else {
$pkg_mount_dir=$pkg_mount_point
}
if ($pkg_mount_source -eq "Undefined") {
$pkg_mount_source = "cdmbuildsna01.redmond.corp.microsoft.com:/OSTCNix/OSTCNix/Build_Drops/kernel_drops/latest"
}
phoneHome "Package mount point is $pkg_mount_point and Package mount dir is $pkg_mount_dir"
phoneHome "Package source is $pkg_mount_source"
if ((Test-Path $pkg_mount_point) -eq $false) {
phoneHome "Creating the mount point"
New-Item -ItemType Directory -Path $pkg_mount_point
if ($? -eq $false) {
$failure_point="CreateMount"
ErrOut($failure_point)
}
}
echo "Checking for the mount directory..."
if ((Test-Path $pkg_mount_dir/file_list) -eq $false) {
echo "mounting..."
phoneHome "Target directory was not there. Mounting"
echo "Command is mount $pkg_mount_source $pkg_mount_point"
@(mount $pkg_mount_source $pkg_mount_point)
if ($? -eq $false) {
$failure_point="Mount"
ErrOut($failure_point)
}
}
if ((Test-Path $pkg_mount_dir) -eq 0) {
phoneHome "Latest directory $pkg_mount_dir was not on mount point $pkg_mount_point! No kernel to install!"
phoneHome "Mount was from $pkg_mount_source"
$failure_point-"NoSource"
ErrOut($failure_point)
}
#
# Copy the files
#
phoneHome "Copying the kernel from the drop share"
cd /root/latest_kernel
copy-Item -Path $pkg_mount_dir/* -Destination ./
if ($? -eq $false) {
$failure_point="CopyKernelArtifacts"
ErrOut($failure_point)
}
} else {
#
# If we can't mount the drop folder, maybe we can get the files from Azure
#
cd $kernFolder
phoneHome "Copying the kernel from Azure blob storage"
$fileListURIBase = "https://" + $pkg_storageaccount + ".blob.core.windows.net/" + $pkg_container
$fileListURI = $fileListURIBase + "/file_list"
phoneHome "Downloading file list from URI $fileListURI"
Invoke-WebRequest -Uri $fileListURI -OutFile file_list
if ($? -eq $false) {
$failure_point="GetFileListFromAzure"
ErrOut($failure_point)
}
$files=Get-Content file_list
foreach ($file in $files) {
$fileListURIBase = "https://" + $pkg_storageaccount + ".blob.core.windows.net/" + $pkg_container
$fileName=$fileListURIBase + "/" + $file
Invoke-WebRequest -Uri $fileName -OutFile $file
if ($? -eq $false) {
$failure_point="WebDownloadFromAzure"
ErrOut($failure_point)
}
}
}
#
# What OS are we on?
#
$linuxInfo = Get-Content /etc/os-release -Raw | ConvertFrom-StringData
$linuxOs = $linuxInfo.ID
phoneHome "Operating system is $linuxOs"
$linuxVers = $linuxInfo.VERSION_ID
phoneHome "Operating system version is $linuxVers"
#
# Figure out the kernel name
#
$failure_point="PrepareForInstall"
if (Test-Path /bin/rpm) {
$kernel_name_cent=Get-ChildItem -Path /root/latest_kernel/kernel-[0-9].* -Exclude "*.src*"
$kernelNameCent = $kernel_name_cent.Name.split("-")[1]
phoneHome "CentOS Kernel name is $kernelNameCent"
#
# Figure out the kernel version
#
$kernelVersionCent=$kernelNameCent
#
# For some reason, the file is -, but the kernel is _
#
$kernelVersionCent=($kernelVersionCent -replace "_","-")
phoneHome "Expected Kernel version is $kernelVersionCent"
$kernelVersionCent | Out-File -Path "/root/expected_version"
phoneVersionHome $kernelVersionCent
} else {
$kernel_name_deb=Get-ChildItem -Path /root/latest_kernel/linux-image-[0-9].* -Exclude "*-dbg_*"
$kernelNameDeb = $kernel_name_deb.Name.split("image-")[1]
phoneHome "Debian Kernel name is $kernelNameDeb"
#
# Figure out the kernel version
#
$kernelVersionDeb=($kernelNameDeb -split "_")[0]
#
# For some reason, the file is -, but the kernel is _
#
$kernelVersionDeb=($kernelVersionDeb -replace "_","-")
phoneHome "Expected Kernel version is $kernelVersionDeb"
$kernelVersionDeb | Out-File -Path "/root/expected_version"
phoneVersionHome $kernelVersionDeb
}
#
# Do the right thing for the platform
#
cd $kernFolder
$failure_point="Installing"
if (Test-Path /bin/rpm) {
#
# rpm-based system
#
$kerneldevelName = Get-Childitem -Path /root/latest_kernel/kernel-devel-[0-9].*.rpm
phoneHome "Kernel Devel Package name is $kerneldevelName"
$kernelPackageName = Get-ChildItem -Path /root/latest_kernel/kernel-[0-9].*.rpm
phoneHome "Making sure the firewall is configured"
@(firewall-cmd --zone=public --add-port=443/tcp --permanent)
if ($? -eq $false) {
$failure_point="FirewallSetPort"
ErrOut($failure_point)
}
#
# Don't care if we fail to stop it
#
@(systemctl stop firewalld)
@(systemctl start firewalld)
if ($? -eq $false) {
$failure_point="FirewallStart"
ErrOut($failure_point)
}
#
# Install the new kernel
#
phoneHome "Installing the rpm kernel devel package $kernelDevelName"
@(rpm -ivh $kernelDevelName)
if ($? -eq $false) {
$failure_point="RPMInstallDevel"
ErrOut($failure_point)
}
phoneHome "Installing the rpm kernel package $kernelPackageName"
@(rpm -ivh $kernelPackageName)
if ($? -eq $false) {
$failure_point="RPMInstall"
ErrOut($failure_point)
}
#
# Now set the boot order to the first selection, so the new kernel comes up
#
phoneHome "Setting the reboot for selection 0"
$foo = @(/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg)
if ($? -eq $false) {
$failure_point="GrubSetBootSelection"
ErrOut($failure_point)
}
$foo = @(/sbin/grub2-set-default 0)
if ($? -eq $false) {
$failure_point="GrubSetBootDefault"
ErrOut($failure_point)
}
} else {
#
# Figure out the kernel name
#
$debKernName=(get-childitem linux-image-*.deb -exclude "-dgb_")[0].Name
phoneHome "Kernel Package name is $debKernName"
#
# Debian
#
$kernDevName=(get-childitem linux-image-*.deb -Exclude ".src.")[1].Name
phoneHome "Kernel Devel Package name is $kernDevName"
#
# In an ideal world, neither of these would be necessary. However,
# experience has shown that there are many more broken images that
# good, so let's at least try and get the system consistent before
# installing the kernel.
#
phoneHome "Trying to make sure the dpkg repository is in a conistent state"
Remove-Item -Path /var/lib/dpkg/lock
@(dpkg --configure -a)
if ($? -eq $false) {
ErrOut($failure_point)
fail
}
@(apt-get install -f)
if ($? -eq $false) {
$failure_point="Install_F"
ErrOut($failure_point)
}
@(apt autoremove -y)
if ($? -eq $false) {
$failure_point="SetAutoRemove"
ErrOut($failure_point)
}
#
# Now make sure the system is current
#
phoneHome "Getting the system current"
@(apt-get -y update)
if ($? -eq $false) {
$failure_point="AptGetUpdate"
ErrOut($failure_point)
}
phoneHome "Installing the DEB kernel devel package"
@(dpkg -i $kernDevName)
if ($? -eq $false) {
$failure_point="DpkgInstallDevel"
ErrOut($failure_point)
}
phoneHome "Installing the DEB kernel package"
@(dpkg -i $debKernName)
if ($? -eq $false) {
$failure_point="DpkgInstall"
ErrOut($failure_point)
}
#
# Now set the boot order to the first selection, so the new kernel comes up
#
phoneHome "Setting the reboot for selection 0"
@(grub-mkconfig -o /boot/grub/grub.cfg)
if ($? -eq $false) {
$failure_point="GrubMkConfig"
ErrOut($failure_point)
}
@(grub-set-default 0)
if ($? -eq $false) {
$failure_point="GrubSetDefault"
ErrOut($failure_point)
}
}
#
# Copy the post-reboot script to RunOnce
#
copy-Item -Path "/root/Framework-Scripts/report_kernel_version.ps1" -Destination "/root/runonce.d"
phoneHome "Rebooting now..."
if ($global:isHyperV -eq $true) {
remove-pssession $global:session
}
Stop-Transcript
#
# This will reboot the system
#
shutdown -r
exit 0