diff --git a/scripts/Common/Scripts/Operations.ps1 b/scripts/Common/Scripts/Operations.ps1 index b7f76b8b..05220d0a 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 371e6791..796665f2 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -38,10 +38,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) { @@ -148,23 +144,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 (Get-AppxPackage "*Ubuntu*").InstallLocation $wslLocation; - Set-UserPermissions $wslLocation; - & "$wslLocation\ubuntu.exe" install --root; - wsl --shutdown; + if (-not (Test-WslDistributions)) { + Install-WslDistribution; continue; } @@ -198,9 +185,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 ($_) { @@ -620,10 +606,7 @@ $null = New-Module { $user = Get-SetupUser; Enable-LocalUser $user; Set-AutologinUser $user; - wsl --shutdown; - $tempDisk = Rename-Item $wslDisk "ext4_.vhdx" -PassThru; - wsl --unregister $distribution; - Move-Item $tempDisk $wslDisk; + Unregister-WslDistribution; Set-UserStage ([UserStage]::Completed); Restart-Intermediate; } diff --git a/scripts/Windows/Scripts/WSL.ps1 b/scripts/Windows/Scripts/WSL.ps1 index 7a92a48a..240c0dbb 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-DistributionPath { - return "$env:ProgramData/PortValhalla/$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-DistributionDisk { - return "$(Get-DistributionPath)/ext4.vhdx"; +<# + .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; } -}; + + Copy-Item -Recurse (Get-AppxPackage "*Ubuntu*").InstallLocation $dir; + Set-UserPermissions $dir; + & "$dir\ubuntu.exe" install --root; + wsl --shutdown; +} + +<# + .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 $wslDisk "ext4_.vhdx" -PassThru; + Uninstall-WslDistribution; + Move-Item $tempDisk $wslDisk; +}