File: //var/opt/nydus/ops/customer_local_ops/operating_system/powershell/install_devcon.ps1
param(
[Parameter(Mandatory)] [String] $package_url,
[Parameter(Mandatory)] [String] $vertDriver_url
)
function Retry-Download {
[cmdletbinding()]
param([parameter(ValueFromPipeline)][ValidateNotNullorEmpty()][string]$url,
[ValidateNotNullorEmpty()][string]$storageDir = "C:\\Windows\\Temp",
[ValidateNotNullorEmpty()][int]$maxDownloadAttemptsCount = 20)
begin {
# We need this change to be able to make requests to urls (e.g. http://hfs-public.secureserver.net), as non-encrypted requests are blocked
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12;
$webclient = New-Object System.Net.WebClient
}
Process {
$filename = $url.Substring($url.LastIndexOf('/') + 1)
if ($filename.indexof('?') -gt -1) {
$filename = $filename.split('?')[0]
}
$file = "$storageDir\\$filename"
$count = 0
do {
$lastErrMsg = ""
start-sleep -seconds ($count * 10)
"Downloading: $file Attempt: $($count+1)"
try {
$webclient.DownloadFile($url, $file)
}
catch {
$lastErrMsg = $_.Exception.Message
"$lastErrMsg"
}
$count++
}while (($count -lt $maxDownloadAttemptsCount) -and (($lastErrMsg -like "*The remote name could not be resolved:*") -or ($lastErrMsg -like "*Unable to connect to the remote server*") -or ($lastErrMsg -like "*The remote server returned an error: (404) Not Found.*")))
if ($lastErrMsg -ne "") {
"$lastErrMsg"
}
}
}
function InstallDriver {
try {
Start-Process -Wait -FilePath "C:\Windows\Temp\virtio-win-gt-x64.msi" -ArgumentList "/quiet"
}
catch {
$_.Exception.Message
exit 1
}
}
function IsDevInitialized {
$state = 1
try {
$state = (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\vioscsi' -Name Start).Start
}
catch {
$state = 1
}
return $state
}
function RemoveDriver {
try {
Start-Process -FilePath "C:\Windows\Temp\devcon.exe" -ArgumentList "install", '"C:\Program Files\Virtio-Win\Vioscsi\vioscsi.inf"', '"PCI\VEN_1AF4&DEV_1048&SUBSYS_11001AF4&REV_01"' -NoNewWindow
$devID = (Get-PnpDevice -Class SCSIAdapter -FriendlyName 'Red Hat VirtIO*' | Select-Object *).HardwareID[0]
if ($devID -ne $null) {
Start-Process -Wait -FilePath "C:\Windows\Temp\devcon.exe" -ArgumentList "remove $devID" -NoNewWindow
$status = IsDevInitialized
if ($status -ne 0) {
return $false
}
}
else {
return $false
}
}
catch {
return $false
}
return $true
}
Retry-Download $package_url
Retry-Download $vertDriver_url
$status = IsDevInitialized
if ($status -ne 0) {
echo "not Initialize"
InstallDriver
}
$path = Test-Path 'C:\Program Files\Virtio-Win\Vioscsi\vioscsi.inf'
if ($path -is [bool] ) {
$dev = RemoveDriver
if ($dev -is [bool]) {
exit 0
}
else {
exit 1
}
}