From 44362a087bf1429af0e59ca7bba7ecad9dc4f0d3 Mon Sep 17 00:00:00 2001 From: Tu Dinh Date: Fri, 6 Dec 2024 11:46:17 +0100 Subject: [PATCH] scripts: Add Windows guest preparation scripts Signed-off-by: Tu Dinh --- .gitignore | 1 + scripts/guests/windows/install-autotest.ps1 | 57 +++++++++++++++++++++ scripts/guests/windows/netreport.ps1 | 21 ++++++++ scripts/guests/windows/unattend.xml | 29 +++++++++++ 4 files changed, 108 insertions(+) create mode 100644 scripts/guests/windows/install-autotest.ps1 create mode 100644 scripts/guests/windows/netreport.ps1 create mode 100644 scripts/guests/windows/unattend.xml diff --git a/.gitignore b/.gitignore index 8cd7e13a0..8aa60aada 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__ */__pycache__ data.py vm_data.py +/scripts/guests/windows/id_rsa.pub diff --git a/scripts/guests/windows/install-autotest.ps1 b/scripts/guests/windows/install-autotest.ps1 new file mode 100644 index 000000000..daabc144e --- /dev/null +++ b/scripts/guests/windows/install-autotest.ps1 @@ -0,0 +1,57 @@ +[CmdletBinding()] +param ( + [Parameter()] + [switch]$NoNetReporting +) + +$ErrorActionPreference = "Stop" + +if (!(Test-Path "$PSScriptRoot\id_rsa.pub")) { + throw "Cannot find id_rsa.pub for SSH configuration" +} + +# Sometimes enabling updates will disrupt installation and rebooting. +# This is a temporary measure at most, but Microsoft makes disabling updates really difficult... +Write-Output "Disabling updates" +Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name AUOptions -Type DWord -Value 2 -Force +Stop-Service wuauserv +Set-Service wuauserv -StartupType Disabled + +Write-Output "Installing SSH" +$SSHDownloadPath = "$env:TEMP\OpenSSH-Win64-v9.8.1.0.msi" +Invoke-WebRequest -UseBasicParsing -Uri "https://github.com/PowerShell/Win32-OpenSSH/releases/download/v9.8.1.0p1-Preview/OpenSSH-Win64-v9.8.1.0.msi" -OutFile $SSHDownloadPath +$exitCode = (Start-Process -Wait msiexec.exe -ArgumentList "/i", $SSHDownloadPath, "/passive", "/norestart" -PassThru).ExitCode +if ($exitCode -ne 0) { + throw +} +Remove-Item -Force $SSHDownloadPath -ErrorAction SilentlyContinue +Copy-Item "$PSScriptRoot\id_rsa.pub" "$env:ProgramData\ssh\administrators_authorized_keys" -Force +icacls.exe "$env:ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F" +if ($LASTEXITCODE -ne 0) { + throw +} +New-NetFirewallRule -Action Allow -Program "$env:ProgramFiles\OpenSSH\sshd.exe" -Direction Inbound -Protocol TCP -LocalPort 22 -DisplayName sshd + +Write-Output "Installing Git Bash" +$GitDownloadPath = "$env:TEMP\Git-2.47.1-64-bit.exe" +Invoke-WebRequest -UseBasicParsing -Uri "https://github.com/git-for-windows/git/releases/download/v2.47.1.windows.1/Git-2.47.1-64-bit.exe" -OutFile $GitDownloadPath +$exitCode = (Start-Process -Wait $GitDownloadPath -ArgumentList "/silent" -PassThru).ExitCode +if ($exitCode -ne 0) { + throw +} +Remove-Item -Force $GitDownloadPath -ErrorAction SilentlyContinue +Set-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Type String -Value "$env:ProgramFiles\Git\bin\bash.exe" -Force + +if (!$NoNetReporting) { + Write-Output "Installing network reporting script" + Copy-Item "$PSScriptRoot\netreport.ps1" "$env:SystemDrive\" -Force + $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-executionpolicy bypass $env:SystemDrive\netreport.ps1" + $trigger = New-ScheduledTaskTrigger -AtStartup + $principal = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -RunLevel Highest + $task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal + Register-ScheduledTask -InputObject $task -TaskName "XCP-ng Test Network Report" +} + +Write-Output "Resealing" +Stop-Process -Name sysprep -ErrorAction SilentlyContinue +& "$env:windir\System32\Sysprep\sysprep.exe" /generalize /oobe /shutdown /unattend:$PSScriptRoot\unattend.xml diff --git a/scripts/guests/windows/netreport.ps1 b/scripts/guests/windows/netreport.ps1 new file mode 100644 index 000000000..1be116824 --- /dev/null +++ b/scripts/guests/windows/netreport.ps1 @@ -0,0 +1,21 @@ +do { + Start-Sleep -Seconds 2 + $Adapter = Get-NetAdapter -Physical | Where-Object Status -eq Up | Select-Object -First 1 +} while (!$Adapter) + +$Address = Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $Adapter.InterfaceIndex +# write the full `r`n sequence so that grep could catch it as a full line +$ReportString = "~xcp-ng-tests~$($Adapter.MacAddress)=$($Address.IPv4Address)~end~`r`n" + +$Port = [System.IO.Ports.SerialPort]::new("COM1") +try { + $Port.Open() + for ($i = 0; $i -lt 300; $i++) { + $Port.Write($ReportString) + Start-Sleep -Seconds 1 + } + $Port.Close() +} +finally { + $Port.Dispose() +} diff --git a/scripts/guests/windows/unattend.xml b/scripts/guests/windows/unattend.xml new file mode 100644 index 000000000..d93801243 --- /dev/null +++ b/scripts/guests/windows/unattend.xml @@ -0,0 +1,29 @@ + + + + + 0409:00000409 + en-US + en-US + en-US + + + + + + Administrators + root + + + + + true + true + true + true + true + 3 + + + +