Skip to content

Commit b83dac8

Browse files
committed
scripts: Update Windows image preparation scripts
Signed-off-by: Tu Dinh <[email protected]>
1 parent 4fbe63e commit b83dac8

File tree

3 files changed

+80
-23
lines changed

3 files changed

+80
-23
lines changed

scripts/guests/windows/install-autotest.ps1

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ param (
55
[Parameter()]
66
[switch]$NoNetReporting,
77
[Parameter()]
8-
[switch]$Cleanup
8+
[switch]$NoCleanup,
9+
[Parameter()]
10+
[switch]$WithTools
911
)
1012

1113
$ErrorActionPreference = "Stop"
@@ -14,6 +16,10 @@ if (!(Test-Path "$PSScriptRoot\id_rsa.pub")) {
1416
throw "Cannot find id_rsa.pub for SSH configuration"
1517
}
1618

19+
if ($WithTools) {
20+
Read-Host -Prompt "Did you install PV tools manually?"
21+
}
22+
1723
# Sometimes enabling updates will disrupt installation and rebooting.
1824
# This is a temporary measure at most, but Microsoft makes disabling updates really difficult...
1925
Write-Output "Disabling updates"
@@ -22,13 +28,10 @@ Stop-Service wuauserv
2228
Set-Service wuauserv -StartupType Disabled
2329

2430
Write-Output "Installing SSH"
25-
$SSHDownloadPath = "$env:TEMP\OpenSSH-Win64-v9.8.1.0.msi"
26-
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
27-
$exitCode = (Start-Process -Wait msiexec.exe -ArgumentList "/i", $SSHDownloadPath, "/passive", "/norestart" -PassThru).ExitCode
31+
$exitCode = (Start-Process -Wait msiexec.exe -ArgumentList "/i `"$PSScriptRoot\OpenSSH-Win64-v9.8.3.0.msi`" /passive /norestart" -PassThru).ExitCode
2832
if ($exitCode -ne 0) {
2933
throw
3034
}
31-
Remove-Item -Force $SSHDownloadPath -ErrorAction SilentlyContinue
3235
Copy-Item "$PSScriptRoot\id_rsa.pub" "$env:ProgramData\ssh\administrators_authorized_keys" -Force
3336
icacls.exe "$env:ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"
3437
if ($LASTEXITCODE -ne 0) {
@@ -37,15 +40,18 @@ if ($LASTEXITCODE -ne 0) {
3740
New-NetFirewallRule -Action Allow -Program "$env:ProgramFiles\OpenSSH\sshd.exe" -Direction Inbound -Protocol TCP -LocalPort 22 -DisplayName sshd
3841

3942
Write-Output "Installing Git Bash"
40-
$GitDownloadPath = "$env:TEMP\Git-2.47.1-64-bit.exe"
41-
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
42-
$exitCode = (Start-Process -Wait $GitDownloadPath -ArgumentList "/silent" -PassThru).ExitCode
43+
$exitCode = (Start-Process -Wait "$PSScriptRoot\Git-2.49.0-64-bit.exe" -ArgumentList "/silent" -PassThru).ExitCode
4344
if ($exitCode -ne 0) {
4445
throw
4546
}
46-
Remove-Item -Force $GitDownloadPath -ErrorAction SilentlyContinue
4747
Set-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Type String -Value "$env:ProgramFiles\Git\bin\bash.exe" -Force
4848

49+
Write-Output "Disabling automatic disk optimization"
50+
schtasks /change /disable /tn "\Microsoft\Windows\Defrag\ScheduledDefrag"
51+
if ($exitCode -ne 0) {
52+
throw
53+
}
54+
4955
if (!$NoNetReporting) {
5056
Write-Output "Installing network reporting script"
5157
Copy-Item "$PSScriptRoot\netreport.ps1" "$env:SystemDrive\" -Force
@@ -56,23 +62,28 @@ if (!$NoNetReporting) {
5662
Register-ScheduledTask -InputObject $task -TaskName "XCP-ng Test Network Report"
5763
}
5864

59-
if ($Cleanup) {
65+
if (!$NoCleanup) {
6066
Read-Host -Prompt "Unplug Internet, run Disk Cleanup and continue"
67+
# You should check at least "Temporary files" in Disk Cleanup
6168

6269
Write-Output "Cleaning up component store"
6370
dism.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase
6471

6572
Write-Output "Cleaning up SoftwareDistribution"
66-
Stop-Service wuauserv, BITS -ErrorAction SilentlyContinue
73+
Stop-Service wuauserv, BITS
6774
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue "$env:windir\SoftwareDistribution\Download\*"
6875

6976
Write-Output "Cleaning up Defender signatures"
7077
& "$env:ProgramFiles\Windows Defender\MpCmdRun.exe" -RemoveDefinitions -All
71-
72-
Write-Output "Optimizing system drive"
73-
defrag.exe $env:SystemDrive /O
7478
}
7579

7680
Write-Output "Resealing"
7781
Stop-Process -Name sysprep -ErrorAction SilentlyContinue
78-
& "$env:windir\System32\Sysprep\sysprep.exe" /generalize /oobe /shutdown /unattend:$PSScriptRoot\unattend.xml
82+
if ($WithTools) {
83+
# WS2025 eval only allows 1 rearm, save this for later
84+
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform" -Name SkipRearm -Type DWord -Value 1
85+
& "$env:windir\System32\Sysprep\sysprep.exe" "/generalize" "/oobe" "/shutdown" "/unattend:$PSScriptRoot\unattend-persisthw.xml"
86+
}
87+
else {
88+
& "$env:windir\System32\Sysprep\sysprep.exe" "/generalize" "/oobe" "/shutdown" "/unattend:$PSScriptRoot\unattend.xml"
89+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<unattend xmlns="urn:schemas-microsoft-com:unattend">
3+
<settings pass="oobeSystem">
4+
<component name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<InputLocale>0409:00000409</InputLocale>
6+
<SystemLocale>en-US</SystemLocale>
7+
<UILanguage>en-US</UILanguage>
8+
<UserLocale>en-US</UserLocale>
9+
</component>
10+
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
11+
<UserAccounts>
12+
<LocalAccounts>
13+
<LocalAccount wcm:action="add">
14+
<Group>Administrators</Group>
15+
<Name>root</Name>
16+
</LocalAccount>
17+
</LocalAccounts>
18+
</UserAccounts>
19+
<OOBE>
20+
<HideEULAPage>true</HideEULAPage>
21+
<HideLocalAccountScreen>true</HideLocalAccountScreen>
22+
<HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
23+
<HideOnlineAccountScreens>true</HideOnlineAccountScreens>
24+
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
25+
<ProtectYourPC>3</ProtectYourPC>
26+
</OOBE>
27+
</component>
28+
</settings>
29+
<settings pass="generalize">
30+
<component name="Microsoft-Windows-PnpSysprep" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
31+
<PersistAllDeviceInstalls>true</PersistAllDeviceInstalls>
32+
</component>
33+
<component name="Microsoft-Windows-Security-SPP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
34+
<SkipRearm>1</SkipRearm>
35+
</component>
36+
</settings>
37+
</unattend>

scripts/guests/windows/win-diskclone.sh

100755100644
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#!/bin/bash
22
set -eu
33

4+
win_diskclean() {
5+
mkdir -p "/run/win-diskclone/$1"
6+
mount $1 "/run/win-diskclone/$1"
7+
rm -vf "/run/win-diskclone/$1/pagefile.sys"
8+
rm -vf "/run/win-diskclone/$1/hiberfil.sys"
9+
rm -vf "/run/win-diskclone/$1/swapfile.sys"
10+
umount "/run/win-diskclone/$1"
11+
}
12+
413
src=$1
514
dst=$2
615

@@ -22,7 +31,7 @@ sfdisk -d $src | sfdisk $dst
2231
echo "Cloning non-data partitions"
2332
for srcpart in $(sfdisk -d $src |
2433
grep start= |
25-
grep -v type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 |
34+
grep -v 'type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7\|type=DE94BBA4-06D1-4D40-A16A-BFD50179D6AC' |
2635
cut -d ':' -f 1)
2736
do
2837
dstpart=${srcpart/"$src"/"$dst"}
@@ -31,16 +40,16 @@ do
3140
done
3241

3342
echo "Cloning data partitions"
43+
mkdir -p /run/win-diskclone
3444
for srcpart in $(sfdisk -d $src |
35-
grep type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 |
45+
grep 'type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7\|type=DE94BBA4-06D1-4D40-A16A-BFD50179D6AC' |
3646
cut -d ':' -f 1)
3747
do
3848
dstpart=${srcpart/"$src"/"$dst"}
39-
echo "Deleting pagefiles"
40-
mkdir -p /mnt/$srcpart &&
41-
mount $srcpart /mnt/$srcpart &&
42-
find /mnt/$srcpart -maxdepth 1 -iname pagefile.sys -or -iname hiberfil.sys -or -iname swapfile.sys -delete
43-
umount /mnt/$srcpart
44-
echo "Cloning $srcpart to $dstpart"
49+
echo "Cleaning $srcpart"
50+
win_diskclean $srcpart
51+
echo "Cloning NTFS $srcpart to $dstpart"
4552
ntfsclone -O $dstpart $srcpart
4653
done
54+
55+
sync

0 commit comments

Comments
 (0)