From 788a020b696299511fcd042e15f2b40691f58134 Mon Sep 17 00:00:00 2001 From: Tu Dinh Date: Tue, 14 Jan 2025 12:48:36 +0100 Subject: [PATCH] scripts: Add Windows driver installation script Signed-off-by: Tu Dinh --- scripts/guests/windows/install-drivers.ps1 | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 scripts/guests/windows/install-drivers.ps1 diff --git a/scripts/guests/windows/install-drivers.ps1 b/scripts/guests/windows/install-drivers.ps1 new file mode 100644 index 00000000..dc297445 --- /dev/null +++ b/scripts/guests/windows/install-drivers.ps1 @@ -0,0 +1,52 @@ +[CmdletBinding()] +param ( + [Parameter(Mandatory, ParameterSetName = "Drivers")] + [string]$DriverPath, + [Parameter(Mandatory, ParameterSetName = "Msi")] + [string]$MsiPath, + [Parameter()] + [switch]$Shutdown +) + +$ErrorActionPreference = "Stop" + +$signature = @' +[DllImport("Cfgmgr32.dll")] +public static extern uint CMP_WaitNoPendingInstallEvents(uint dwTimeout); +public const uint INFINITE = 0xFFFFFFFF; +public const uint WAIT_OBJECT_0 = 0; +public const uint WAIT_TIMEOUT = 258; +public const uint WAIT_FAILED = 0xFFFFFFFF; +'@ + +$nativeMethods = Add-Type -MemberDefinition $signature -Name NativeMethods -Namespace XenTools -PassThru + +if ($DriverPath) { + foreach ($driver in @("xenbus", "xeniface", "xenvbd", "xenvif", "xennet")) { + $infPath = (Resolve-Path "$DriverPath\$driver\x64\$driver.inf").Path + Write-Output "Attempting install $infPath" + pnputil.exe /add-driver $infPath /install + if ($LASTEXITCODE -ne 0) { + throw "pnputil.exe $LASTEXITCODE" + } + } +} +elseif ($MsiPath) { + $resolvedMsiPath = (Resolve-Path $MsiPath).Path + Write-Output "Attempting install $resolvedMsiPath" + $msiexecProcess = Start-Process -Wait -PassThru msiexec.exe -ArgumentList "/i", "$resolvedMsiPath", "/l*", "C:\other-install.log", "/passive", "/norestart" + if ($msiexecProcess.ExitCode -ne 0 -and $msiexecProcess.ExitCode -ne 1641 -and $msiexecProcess.ExitCode -ne 3010) { + throw "msiexec.exe $($msiexecProcess.ExitCode)" + } + # Some installers like XCP-ng 8.2 don't install their drivers using MSI but through their own service (XenInstall). + # Leave some time for the installation service to do its thing. + Start-Sleep -Seconds 15 +} +Write-Output "Waiting for install events" +$nativeMethods::CMP_WaitNoPendingInstallEvents($nativeMethods::INFINITE) + +if ($Shutdown) { + Write-Output "Shutting down" + Start-Sleep -Seconds 5 + Stop-Computer -Force +}