From c3509cf341fc5a12338724327e08abbcad991fd3 Mon Sep 17 00:00:00 2001 From: Szymon Osiecki Date: Mon, 9 Oct 2023 22:34:02 +0200 Subject: [PATCH] refactor(ps): wsl setup scripts Use new wsl functions from SetupUtils module. --- wsl/wsl_distro_get.ps1 | 111 ---------------------------------------- wsl/wsl_network_fix.ps1 | 31 +++-------- wsl/wsl_setup.ps1 | 28 ++++++---- wsl/wsl_systemd.ps1 | 32 ++++-------- 4 files changed, 38 insertions(+), 164 deletions(-) delete mode 100644 wsl/wsl_distro_get.ps1 diff --git a/wsl/wsl_distro_get.ps1 b/wsl/wsl_distro_get.ps1 deleted file mode 100644 index 8b99bb69..00000000 --- a/wsl/wsl_distro_get.ps1 +++ /dev/null @@ -1,111 +0,0 @@ -#Requires -PSEdition Core -<# -.SYNOPSIS -Script synopsis. -.EXAMPLE -wsl/wsl_distro_get.ps1 -wsl/wsl_distro_get.ps1 -FromRegistry -wsl/wsl_distro_get.ps1 -Online -#> -[CmdletBinding()] -param ( - [switch]$Online, - - [switch]$FromRegistry -) - -begin { - $ErrorActionPreference = 'Stop' - - # check if the script is running on Windows - if (-not $IsWindows) { - Write-Warning 'Run the script on Windows!' - exit 0 - } - - if ($FromRegistry) { - # specify list of properties to get from Windows registry lxss - $prop = @( - @{ Name = 'Name'; Expression = { $_.DistributionName } } - 'DefaultUid' - @{ Name = 'Version'; Expression = { $_.Flags -lt 8 ? 1 : 2 } } - 'Flags' - @{ Name = 'BasePath'; Expression = { $_.BasePath -replace '^\\\\\?\\' } } - 'PSPath' - ) - } else { - $distros = [Collections.Generic.List[PSCustomObject]]::new() - $outputEncoding = [Console]::OutputEncoding - } -} - -process { - if ($FromRegistry) { - # get list of WSL distros from Windows Registry - $distros = Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss -ErrorAction SilentlyContinue ` - | ForEach-Object { $_ | Get-ItemProperty } ` - | Where-Object { $_.DistributionName -notmatch '^docker-desktop' } ` - | Select-Object $prop - } else { - # change console encoding to utf-16 - [Console]::OutputEncoding = [System.Text.Encoding]::Unicode - if ($Online) { - # get list of online WSL distros - [string[]]$result = wsl.exe --list --online | Where-Object { $_ } - # get distros header - [string]$head = $result | Select-String 'NAME\s+FRIENDLY' -CaseSensitive | Select-Object -ExpandProperty Line - # calculate header line index - if ($head) { - $idx = $result.IndexOf($head) - $dataIdx = if ($idx -ge 0) { - $idx + 1 - } else { - $result.Count - 1 - } - # calculate header columns indexes - $nameIdx = $head.IndexOf('NAME') - $friendlyIdx = $head.IndexOf('FRIENDLY') - # add results to the distros list - for ($i = $dataIdx; $i -lt $result.Count; $i++) { - $distro = [PSCustomObject]@{ - Name = $result[$i].Substring($nameIdx, $friendlyIdx - $nameIdx).TrimEnd() - FriendlyName = $result[$i].Substring($friendlyIdx, $result[$i].Length - $friendlyIdx).TrimEnd() - } - $distros.Add($distro) - } - } - } else { - # get list of installed locally WSL distros - [string[]]$result = wsl.exe --list --verbose - # get distros header - [string]$head = $result | Select-String 'NAME\s+STATE\s+VERSION' -CaseSensitive | Select-Object -ExpandProperty Line - # calculate header line index - if ($head) { - $idx = $result.IndexOf($head) - $dataIdx = if ($idx -ge 0) { - $idx + 1 - } else { - $result.Count - 1 - } - # calculate header columns indexes - $nameIdx = $head.IndexOf('NAME') - $stateIdx = $head.IndexOf('STATE') - $versionIdx = $head.IndexOf('VERSION') - # add results to the distros list - for ($i = $dataIdx; $i -lt $result.Count; $i++) { - $distro = [PSCustomObject]@{ - Name = $result[$i].Substring($nameIdx, $stateIdx - $nameIdx).TrimEnd() - State = $result[$i].Substring($stateIdx, $versionIdx - $stateIdx).TrimEnd() - Version = $result[$i].Substring($versionIdx, $result[$i].Length - $versionIdx).TrimEnd() - } - $distros.Add($distro) - } - } - } - [Console]::OutputEncoding = $outputEncoding - } -} - -end { - return $distros -} diff --git a/wsl/wsl_network_fix.ps1 b/wsl/wsl_network_fix.ps1 index be169240..9497ad6b 100644 --- a/wsl/wsl_network_fix.ps1 +++ b/wsl/wsl_network_fix.ps1 @@ -52,16 +52,11 @@ begin { # set location to workspace folder Push-Location "$PSScriptRoot/.." - # check if the required functions are available, otherwise import SetupUtils module - try { - Get-Command ConvertFrom-Cfg -CommandType Function | Out-Null - Get-Command ConvertTo-Cfg -CommandType Function | Out-Null - } catch { - Import-Module (Resolve-Path './modules/SetupUtils') - } + # import SetupUtils module + Import-Module (Resolve-Path './modules/SetupUtils') # check if distro exist - $distros = wsl/wsl_distro_get.ps1 -FromRegistry + $distros = Get-WslDistro -FromRegistry if ($Distro -notin $distros.Name) { Write-Warning "The specified distro does not exist ($Distro)." exit 1 @@ -81,25 +76,15 @@ begin { process { # *replace wsl.conf Write-Host 'replacing wsl.conf' -ForegroundColor DarkGreen - $wslConf = wsl.exe -d $Distro --exec cat /etc/wsl.conf 2>$null | ConvertFrom-Cfg - if ($wslConf) { - $wslConf.network = [ordered]@{ generateResolvConf = $genResolv } - } else { - $wslConf = [ordered]@{ - automount = [ordered]@{ - enabled = 'true' - options = '"metadata"' - mountFsTab = 'true' - } - network = [ordered]@{ + $param = @{ + Distro = $Distro + ConfDict = [ordered]@{ + network = [ordered]@{ generateResolvConf = $genResolv } } } - $wslConfStr = ConvertTo-Cfg -OrderedDict $wslConf -LineFeed - # save wsl.conf file - $cmd = "rm -f /etc/wsl.conf || true && echo '$wslConfStr' >/etc/wsl.conf" - wsl.exe -d $Distro --user root --exec bash -c $cmd + Set-WslConf @param # *recreate resolv.conf if (-not $Revert) { diff --git a/wsl/wsl_setup.ps1 b/wsl/wsl_setup.ps1 index 0dc21ea0..5e156844 100644 --- a/wsl/wsl_setup.ps1 +++ b/wsl/wsl_setup.ps1 @@ -117,6 +117,8 @@ begin { Push-Location "$PSScriptRoot/.." # import InstallUtils for the Invoke-GhRepoClone function Import-Module (Resolve-Path './modules/InstallUtils') + # import SetupUtils for the Set-WslConf function + Import-Module (Resolve-Path './modules/SetupUtils') # check if repository is up to date Write-Host "`nchecking if the repository is up to date..." -ForegroundColor Cyan @@ -129,12 +131,10 @@ begin { } # *get list of distros - $lxss = wsl/wsl_distro_get.ps1 | Where-Object Name -NotMatch '^docker-desktop' + $lxss = Get-WslDistro | Where-Object Name -NotMatch '^docker-desktop' if ($PsCmdlet.ParameterSetName -ne 'Update') { - if ($Distro -in $lxss.Name) { - $lxss = $lxss | Where-Object Name -EQ $Distro - } else { - $onlineDistros = wsl/wsl_distro_get.ps1 -Online + if ($Distro -notin $lxss.Name) { + $onlineDistros = Get-WslDistro -Online # install online distro if ($Distro -in $onlineDistros.Name) { Write-Host "`nspecified distribution not found ($Distro), proceeding to install..." -ForegroundColor Cyan @@ -143,7 +143,6 @@ begin { Get-Service LxssManagerUser*, WSLService | Out-Null Write-Host "`nSetting up user profile in WSL distro. Type 'exit' when finished to proceed with WSL setup!`n" -ForegroundColor Yellow Invoke-Expression $cmd - $lxss = wsl/wsl_distro_get.ps1 -FromRegistry | Where-Object Name -EQ $Distro } catch { if (Test-IsAdmin) { Invoke-Expression $cmd @@ -160,8 +159,20 @@ begin { exit 1 } } + # *perform initial distro setup + # enable automount in wsl.conf + $param = @{ + Distro = $Distro + ConfDict = [ordered]@{ + automount = [ordered]@{ + enabled = 'true' + mountFsTab = 'true' + } + } + } + Set-WslConf @param # disable appending Windows path inside distro to fix mounting issues - $lxss = wsl/wsl_distro_get.ps1 -FromRegistry | Where-Object Name -EQ $Distro + $lxss = Get-WslDistro -FromRegistry | Where-Object Name -EQ $Distro if ($lxss -and $lxss.Flags -ne 13) { Set-ItemProperty -Path $lxss.PSPath -Name 'Flags' -Value 13 Write-Host "`nrestarting WSL to apply changes..." -ForegroundColor Cyan @@ -450,8 +461,7 @@ process { $builder.AppendLine('git config --global push.autoSetupRemote true') | Out-Null if ($AddCertificate) { # a guess, that if certs are being installed, you're behind MITM proxy without chunked transfer encoding - $builder.AppendLine('git config --global http.postBuffer 512M') | Out-Null - $builder.AppendLine('git config --global http.maxRequestBuffer 128M') | Out-Null + $builder.AppendLine('git config --global http.postBuffer 524288000') | Out-Null } Write-Host 'configuring git...' -ForegroundColor Cyan wsl.exe --distribution $Distro --exec bash -c $builder.ToString().Trim() diff --git a/wsl/wsl_systemd.ps1 b/wsl/wsl_systemd.ps1 index 0c496d71..3c9c46dd 100644 --- a/wsl/wsl_systemd.ps1 +++ b/wsl/wsl_systemd.ps1 @@ -43,16 +43,11 @@ begin { # set location to workspace folder Push-Location "$PSScriptRoot/.." - # check if the required functions are available, otherwise import SetupUtils module - try { - Get-Command ConvertFrom-Cfg -CommandType Function | Out-Null - Get-Command ConvertTo-Cfg -CommandType Function | Out-Null - } catch { - Import-Module (Resolve-Path './modules/SetupUtils') - } + # import SetupUtils module + Import-Module (Resolve-Path './modules/SetupUtils') # check if distro exist - $distros = wsl/wsl_distro_get.ps1 -FromRegistry + $distros = Get-WslDistro -FromRegistry if ($Distro -notin $distros.Name) { Write-Warning "The specified distro does not exist ($Distro)." exit 1 @@ -60,27 +55,22 @@ begin { } process { - $wslConf = wsl.exe -d $Distro --exec cat /etc/wsl.conf 2>$null | ConvertFrom-Cfg - if ($wslConf) { - $wslConf.boot = [ordered]@{ systemd = $Systemd } - } else { - $wslConf = [ordered]@{ + # *set wsl.conf + Write-Host 'replacing wsl.conf' -ForegroundColor DarkGreen + $param = @{ + Distro = $Distro + ConfDict = [ordered]@{ boot = [ordered]@{ systemd = $Systemd } } + ShowConf = $ShowConf ? $true : $false } - $wslConfStr = ConvertTo-Cfg -OrderedDict $wslConf -LineFeed - # save wsl.conf file - $cmd = "rm -f /etc/wsl.conf || true && echo '$wslConfStr' >/etc/wsl.conf" - wsl.exe -d $Distro --user root --exec bash -c $cmd + Set-WslConf @param } end { - if ($ShowConf) { - Write-Host 'wsl.conf' -ForegroundColor Magenta - wsl.exe -d $Distro --exec cat /etc/wsl.conf | Write-Host - } else { + if (-not $ShowConf) { Write-Host "systemd $($Systemd -eq 'true' ? 'enabled' : 'disabled')" } }