From e42b6077901597c22dba43992955ad9f9ab7c4aa Mon Sep 17 00:00:00 2001 From: Aleksandr Chebotov <47745270+al-cheb@users.noreply.github.com> Date: Fri, 17 Jun 2022 09:24:02 +0200 Subject: [PATCH] [Windows] Set WSLv1 version on Windows Server 2022 (#5762) * Set WSLv1 version on Windows Server 2022 * Create path if missing --- .../scripts/ImageHelpers/ImageHelpers.psm1 | 1 + .../win/scripts/ImageHelpers/PathHelpers.ps1 | 21 ++++++++++++++++++- images/win/scripts/Installers/Finalize-VM.ps1 | 7 +++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 index 94cd2ea4b..cdd4ea5b3 100644 --- a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 +++ b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 @@ -17,6 +17,7 @@ Export-ModuleMember -Function @( 'Set-DefaultPath' 'Add-MachinePathItem' 'Add-DefaultPathItem' + 'Add-DefaultItem' 'Get-SystemVariable' 'Get-DefaultVariable' 'Set-SystemVariable' diff --git a/images/win/scripts/ImageHelpers/PathHelpers.ps1 b/images/win/scripts/ImageHelpers/PathHelpers.ps1 index f8dc0ecb9..df7ce10ed 100644 --- a/images/win/scripts/ImageHelpers/PathHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/PathHelpers.ps1 @@ -75,7 +75,7 @@ function Set-DefaultVariable { $key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($Name, $Writable) $key.SetValue($DefaultVariable, $Value, $Kind) - Get-DefaultVariable $DefaultVariable + Get-DefaultVariable -DefaultVariable $DefaultVariable -Name $Name $key.Handle.Close() [System.GC]::Collect() } @@ -135,6 +135,25 @@ function Add-DefaultPathItem { Disconnect-Hive } +function Add-DefaultItem { + param( + [string]$DefaultVariable, + [string]$Value, + [string]$Name = "DEFAULT\Environment", + [string]$Kind = "ExpandString", + [bool]$Writable = $true + ) + + Connect-Hive + $regPath = Join-Path "HKLM:\" $Name + if (-not (Test-Path $Name)) { + Write-Host "Creating $regPath key" + New-Item -Path $regPath -Force | Out-Null + } + Set-DefaultVariable -DefaultVariable $DefaultVariable -Value $Value -Name $Name -Kind $Kind -Writable $Writable + Disconnect-Hive +} + function New-ItemPath { param ( [string]$Path diff --git a/images/win/scripts/Installers/Finalize-VM.ps1 b/images/win/scripts/Installers/Finalize-VM.ps1 index dd78ba826..f759a0980 100644 --- a/images/win/scripts/Installers/Finalize-VM.ps1 +++ b/images/win/scripts/Installers/Finalize-VM.ps1 @@ -6,6 +6,13 @@ Write-Host "Cleanup WinSxS" Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase +# Sets the default install version to v1 for new distributions +# https://github.com/actions/virtual-environments/issues/5760 +if (Test-IsWin22) { + Write-Host "Sets the default install version to v1 for new distributions" + Add-DefaultItem -DefaultVariable "DefaultVersion" -Value 1 -Name "DEFAULT\Software\Microsoft\Windows\CurrentVersion\Lxss" -Kind "DWord" +} + Write-Host "Clean up various directories" @( "$env:SystemDrive\Recovery",