Add further functionality to the WSL script

This commit is contained in:
Manuel Thalmann 2024-08-27 00:38:46 +02:00
parent 380b70322c
commit 6ab0df4dc8
3 changed files with 101 additions and 47 deletions

View file

@ -194,6 +194,6 @@ $null = New-Module {
Clears resources allocated during the operation. Clears resources allocated during the operation.
#> #>
function Clear-OperationResources { function Clear-OperationResources {
wsl --unregister (Get-DistributionName); Uninstall-WslDistribution;
} }
}; };

View file

@ -41,10 +41,6 @@ $null = New-Module {
Starts the installation loop. Starts the installation loop.
#> #>
function Start-InstallationLoop { function Start-InstallationLoop {
$distribution = Get-DistributionName;
$wslLocation = Get-DistributionPath;
$wslDisk = Get-DistributionDisk;
while (-not (Get-IsFinished)) { while (-not (Get-IsFinished)) {
switch (Get-Stage) { switch (Get-Stage) {
($null) { ($null) {
@ -151,23 +147,14 @@ $null = New-Module {
continue; continue;
} }
if (-not (& { $null = wsl --status; $?; })) { if (-not (Test-Wsl)) {
wsl --install --no-launch; Install-Wsl;
Restart-Intermediate; Restart-Intermediate;
return; return;
} }
if (-not (& { $null = wsl -l; $?; })) { if (-not (Test-WslDistributions)) {
$wslRoot = Split-Path -Parent $wslLocation; Install-WslDistribution;
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;
continue; continue;
} }
@ -201,9 +188,8 @@ $null = New-Module {
break; break;
} }
default { default {
if (-not (& { $null = wsl -l; $? })) { if (-not (Test-Wsl)) {
wsl --import-in-place $distribution "$wslDisk"; Register-WslDistribution;
wsl --set-default $distribution;
} }
switch ($_) { switch ($_) {
@ -627,10 +613,7 @@ $null = New-Module {
$user = Get-SetupUser; $user = Get-SetupUser;
Enable-LocalUser $user; Enable-LocalUser $user;
Set-AutologinUser $user; Set-AutologinUser $user;
wsl --shutdown; Unregister-WslDistribution;
$tempDisk = Rename-Item -Force $wslDisk "ext4_.vhdx" -PassThru;
wsl --unregister $distribution;
Move-Item $tempDisk $wslDisk;
Set-UserStage ([UserStage]::Completed); Set-UserStage ([UserStage]::Completed);
Restart-Intermediate; Restart-Intermediate;
exit; exit;

View file

@ -1,25 +1,96 @@
$null = New-Module { . "$PSScriptRoot/../Scripts/Security.ps1";
<#
.SYNOPSIS <#
Gets the name of the WSL distribution used for managing the configuration. .SYNOPSIS
#> Gets the name of the WSL distribution used for managing the configuration.
function Get-DistributionName { #>
return "ValhallaUbuntu"; 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;
} }
<# Copy-Item -Recurse -Force (Get-AppxPackage "*Ubuntu*").InstallLocation $dir;
.SYNOPSIS Set-UserPermissions $dir;
Gets the path to the directory containing the WSL distribution. & "$dir\ubuntu.exe" install --root;
#> wsl --shutdown;
function Get-DistributionPath { }
return "$env:ProgramData/PortValhalla/$distribution";
}
<# <#
.SYNOPSIS .SYNOPSIS
Gets the path to the virtual hard disk of the WSL distribution. Uninstalls the managed WSL distribution.
#> #>
function Get-DistributionDisk { function Uninstall-WslDistribution {
return "$(Get-DistributionPath)/ext4.vhdx"; 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;
}