From 6ab0df4dc8d8803d14240f0c97c22317f0867fbb Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 27 Aug 2024 00:38:46 +0200 Subject: [PATCH] Add further functionality to the WSL script --- scripts/Common/Scripts/Operations.ps1 | 2 +- scripts/Windows/OS/Install.ps1 | 31 ++----- scripts/Windows/Scripts/WSL.ps1 | 115 +++++++++++++++++++++----- 3 files changed, 101 insertions(+), 47 deletions(-) diff --git a/scripts/Common/Scripts/Operations.ps1 b/scripts/Common/Scripts/Operations.ps1 index b6ada82a..29dacee4 100644 --- a/scripts/Common/Scripts/Operations.ps1 +++ b/scripts/Common/Scripts/Operations.ps1 @@ -194,6 +194,6 @@ $null = New-Module { Clears resources allocated during the operation. #> function Clear-OperationResources { - wsl --unregister (Get-DistributionName); + Uninstall-WslDistribution; } }; diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index 125409ec..9a3c63d6 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -41,10 +41,6 @@ $null = New-Module { Starts the installation loop. #> function Start-InstallationLoop { - $distribution = Get-DistributionName; - $wslLocation = Get-DistributionPath; - $wslDisk = Get-DistributionDisk; - while (-not (Get-IsFinished)) { switch (Get-Stage) { ($null) { @@ -151,23 +147,14 @@ $null = New-Module { continue; } - if (-not (& { $null = wsl --status; $?; })) { - wsl --install --no-launch; + if (-not (Test-Wsl)) { + Install-Wsl; Restart-Intermediate; return; } - if (-not (& { $null = wsl -l; $?; })) { - $wslRoot = Split-Path -Parent $wslLocation; - - if (-not (Test-Path $wslRoot)) { - $null = New-Item -ItemType Directory $wslRoot; - } - - Copy-Item -Recurse -Force (Get-AppxPackage "*Ubuntu*").InstallLocation $wslLocation; - Set-UserPermissions $wslLocation; - & "$wslLocation\ubuntu.exe" install --root; - wsl --shutdown; + if (-not (Test-WslDistributions)) { + Install-WslDistribution; continue; } @@ -201,9 +188,8 @@ $null = New-Module { break; } default { - if (-not (& { $null = wsl -l; $? })) { - wsl --import-in-place $distribution "$wslDisk"; - wsl --set-default $distribution; + if (-not (Test-Wsl)) { + Register-WslDistribution; } switch ($_) { @@ -627,10 +613,7 @@ $null = New-Module { $user = Get-SetupUser; Enable-LocalUser $user; Set-AutologinUser $user; - wsl --shutdown; - $tempDisk = Rename-Item -Force $wslDisk "ext4_.vhdx" -PassThru; - wsl --unregister $distribution; - Move-Item $tempDisk $wslDisk; + Unregister-WslDistribution; Set-UserStage ([UserStage]::Completed); Restart-Intermediate; exit; diff --git a/scripts/Windows/Scripts/WSL.ps1 b/scripts/Windows/Scripts/WSL.ps1 index 7a92a48a..8f7b3b88 100644 --- a/scripts/Windows/Scripts/WSL.ps1 +++ b/scripts/Windows/Scripts/WSL.ps1 @@ -1,25 +1,96 @@ -$null = New-Module { - <# - .SYNOPSIS - Gets the name of the WSL distribution used for managing the configuration. - #> - function Get-DistributionName { - return "ValhallaUbuntu"; +. "$PSScriptRoot/../Scripts/Security.ps1"; + +<# + .SYNOPSIS + Gets the name of the WSL distribution used for managing the configuration. +#> +function Get-WslDistributionName { + return "ValhallaUbuntu"; +} + +<# + .SYNOPSIS + Gets the path to the directory containing the WSL distribution. +#> +function Get-WslDistributionPath { + return "$env:ProgramData/PortValhalla/$(Get-WslDistributionName)"; +} + +<# + .SYNOPSIS + Gets the path to the virtual hard disk of the WSL distribution. +#> +function Get-WslDistributionDisk { + return "$(Get-WslDistributionPath)/ext4.vhdx"; +} + +<# + .SYNOPSIS + Checks whether `wsl` is installed properly. +#> +function Test-Wsl { + & { $null = wsl --status; $?; }; +} + +<# + .SYNOPSIS + Checks whether any WSL distributions are installed for the current user. +#> +function Test-WslDistributions { + & { $null = wsl -l; $?; }; +} + +<# + .SYNOPSIS + Installs `wsl` on the system. +#> +function Install-Wsl { + wsl --install --no-launch; +} + +<# + .SYNOPSIS + Installs a Ubuntu distribution to a shared directory. +#> +function Install-WslDistribution { + $dir = Get-WslDistributionPath; + $root = Split-Path -Parent $dir; + + if (-not (Test-Path $root)) { + $null = New-Item -ItemType Directory $root; } - <# - .SYNOPSIS - Gets the path to the directory containing the WSL distribution. - #> - function Get-DistributionPath { - return "$env:ProgramData/PortValhalla/$distribution"; - } + Copy-Item -Recurse -Force (Get-AppxPackage "*Ubuntu*").InstallLocation $dir; + Set-UserPermissions $dir; + & "$dir\ubuntu.exe" install --root; + wsl --shutdown; +} - <# - .SYNOPSIS - Gets the path to the virtual hard disk of the WSL distribution. - #> - function Get-DistributionDisk { - return "$(Get-DistributionPath)/ext4.vhdx"; - } -}; +<# + .SYNOPSIS + Uninstalls the managed WSL distribution. +#> +function Uninstall-WslDistribution { + wsl --unregister (Get-WslDistributionName); +} + +<# + .SYNOPSIS + Registers the managed WSL distribution. +#> +function Register-WslDistribution { + wsl --import-in-place (Get-WslDistributionName) (Get-WslDistributionDisk); + wsl --set-default (Get-WslDistributionName); +} + +<# + .SYNOPSIS + Unregisters the managed WSL distribution. +#> +function Unregister-WslDistribution { + $wslDisk = Get-WslDistributionDisk; + wsl --shutdown; + $tempDisk = Rename-Item -Force $wslDisk "ext4_.vhdx" -PassThru; + Uninstall-WslDistribution; + Move-Item $tempDisk $wslDisk; +}