From dab17a102fe90c2b6c02bfea852552b2d5e19e5c Mon Sep 17 00:00:00 2001 From: Alexey-Ayupov Date: Wed, 11 Dec 2024 15:00:05 +0100 Subject: [PATCH] [Windows] Add WSL2 to Windows-2025 image --- .../windows/scripts/build/Configure-System.ps1 | 4 ++-- images/windows/scripts/build/Install-WSL2.ps1 | 16 ++++++++++++++++ .../scripts/docs-gen/Generate-SoftwareReport.ps1 | 7 ++++++- .../scripts/docs-gen/SoftwareReport.Tools.psm1 | 4 ++++ .../scripts/tests/WindowsFeatures.Tests.ps1 | 6 ++++++ images/windows/templates/windows-2025.pkr.hcl | 3 ++- 6 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 images/windows/scripts/build/Install-WSL2.ps1 diff --git a/images/windows/scripts/build/Configure-System.ps1 b/images/windows/scripts/build/Configure-System.ps1 index 1291b7b47b38..5e144f2d58df 100644 --- a/images/windows/scripts/build/Configure-System.ps1 +++ b/images/windows/scripts/build/Configure-System.ps1 @@ -93,11 +93,11 @@ $servicesToDisable = @( 'wuauserv' 'DiagTrack' 'dmwappushservice' - 'PcaSvc' + $(if(-not (Test-IsWin25)){'PcaSvc'}) 'SysMain' 'gupdate' 'gupdatem' - 'StorSvc' + $(if(-not (Test-IsWin25)){'StorSvc'}) ) | Get-Service -ErrorAction SilentlyContinue Stop-Service $servicesToDisable $servicesToDisable.WaitForStatus('Stopped', "00:01:00") diff --git a/images/windows/scripts/build/Install-WSL2.ps1 b/images/windows/scripts/build/Install-WSL2.ps1 new file mode 100644 index 000000000000..4134b2afd134 --- /dev/null +++ b/images/windows/scripts/build/Install-WSL2.ps1 @@ -0,0 +1,16 @@ +Write-Host "Install WSL2" + +$version = (Get-GithubReleasesByVersion -Repo "microsoft/WSL" -Version "latest").version +$downloadUrl = Resolve-GithubReleaseAssetUrl ` + -Repo "microsoft/WSL" ` + -Version $version ` + -UrlMatchPattern "wsl.*.x64.msi" + +Install-Binary -Type MSI ` + -Url $downloadUrl ` + -ExpectedSHA256Sum "CD3F2A68A1A5836F6A1CC9965A7F5F54DB267CA221EAA87DF29345AB7957AEC4" + +Write-Host "Performing wsl --install --no-distribution" +wsl.exe --install --no-distribution + +Invoke-PesterTests -TestFile "WindowsFeatures" -TestName "WSL2" diff --git a/images/windows/scripts/docs-gen/Generate-SoftwareReport.ps1 b/images/windows/scripts/docs-gen/Generate-SoftwareReport.ps1 index 7077a39f3af6..a1eb81b09f11 100644 --- a/images/windows/scripts/docs-gen/Generate-SoftwareReport.ps1 +++ b/images/windows/scripts/docs-gen/Generate-SoftwareReport.ps1 @@ -20,7 +20,12 @@ Import-Module (Join-Path $PSScriptRoot "SoftwareReport.VisualStudio.psm1") -Disa # Software report $softwareReport = [SoftwareReport]::new($(Build-OSInfoSection)) $optionalFeatures = $softwareReport.Root.AddHeader("Windows features") -$optionalFeatures.AddToolVersion("Windows Subsystem for Linux (WSLv1):", "Enabled") + if (-not (Test-IsWin25)) { + $optionalFeatures.AddToolVersion("Windows Subsystem for Linux (WSLv1):", "Enabled") +} else { + $optionalFeatures.AddToolVersion("Windows Subsystem for Linux (WSLv1):", "Enabled") + $optionalFeatures.AddToolVersion("Windows Subsystem for Linux (WSLv2):", $(Get-WSL2Version)) +} $installedSoftware = $softwareReport.Root.AddHeader("Installed Software") # Language and Runtime diff --git a/images/windows/scripts/docs-gen/SoftwareReport.Tools.psm1 b/images/windows/scripts/docs-gen/SoftwareReport.Tools.psm1 index bd83432c0c89..c4672f82d348 100644 --- a/images/windows/scripts/docs-gen/SoftwareReport.Tools.psm1 +++ b/images/windows/scripts/docs-gen/SoftwareReport.Tools.psm1 @@ -318,3 +318,7 @@ function Get-ImageMagickVersion { function Get-MongoshVersion { return $(mongosh --version) } + +function Get-WSL2Version { + return $((Get-AppxPackage -Name "MicrosoftCorporationII.WindowsSubsystemForLinux").version) +} diff --git a/images/windows/scripts/tests/WindowsFeatures.Tests.ps1 b/images/windows/scripts/tests/WindowsFeatures.Tests.ps1 index 03b56a2108d1..09e682498bdb 100644 --- a/images/windows/scripts/tests/WindowsFeatures.Tests.ps1 +++ b/images/windows/scripts/tests/WindowsFeatures.Tests.ps1 @@ -82,3 +82,9 @@ Describe "Windows Updates" { $State | Should -BeIn $expect } } + +Describe "WSL2" { + It "WSL status should return zero exit code" { + "wsl --status" | Should -ReturnZeroExitCode + } +} diff --git a/images/windows/templates/windows-2025.pkr.hcl b/images/windows/templates/windows-2025.pkr.hcl index a4ba4ed9ed0a..f1f3fdc55007 100644 --- a/images/windows/templates/windows-2025.pkr.hcl +++ b/images/windows/templates/windows-2025.pkr.hcl @@ -243,7 +243,7 @@ build { inline = ["if (-not ((net localgroup Administrators) -contains '${var.install_user}')) { exit 1 }"] } - provisioner "powershell" { +provisioner "powershell" { elevated_password = "${var.install_password}" elevated_user = "${var.install_user}" inline = ["bcdedit.exe /set TESTSIGNING ON"] @@ -256,6 +256,7 @@ build { "${path.root}/../scripts/build/Configure-WindowsDefender.ps1", "${path.root}/../scripts/build/Configure-PowerShell.ps1", "${path.root}/../scripts/build/Install-PowerShellModules.ps1", + "${path.root}/../scripts/build/Install-WSL2.ps1", "${path.root}/../scripts/build/Install-WindowsFeatures.ps1", "${path.root}/../scripts/build/Install-Chocolatey.ps1", "${path.root}/../scripts/build/Configure-BaseImage.ps1",