File: //var/opt/nydus/ops/customer_local_ops/operating_system/powershell/install_qemu_agent.ps1
param([Parameter(Mandatory)] [String] $package_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"
}
}
}
# Remove mount to ISO - in case previous attempt failed
Dismount-DiskImage -ImagePath "C:\\Windows\\Temp\\virtio-win.iso"
Retry-Download $package_url
Mount-DiskImage -ImagePath "C:\\Windows\\Temp\\virtio-win.iso"
$driveLetter=gwmi Win32_LogicalDisk | Where {$_.DriveType -eq 5 -and $_.VolumeName -like 'virtio-win*'} | select -ExpandProperty DeviceId
# Install qemu agent
Start-Process msiexec.exe -Wait -ArgumentList "/I ${driveLetter}\guest-agent\qemu-ga-x86_64.msi /quiet /norestart" -NoNewWindow
# Remove mount to ISO
Dismount-DiskImage -ImagePath "C:\\Windows\\Temp\\virtio-win.iso"
# Enable qemu to start on boot
Set-Service QEMU-GA -StartupType Automatic