Add further functionality to the WSL script

This commit is contained in:
Manuel Thalmann 2024-08-27 00:38:46 +02:00
parent 4b84371500
commit d3cf87500d
3 changed files with 101 additions and 47 deletions

View file

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

View file

@ -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 -Force (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 ($_) {
@ -624,10 +610,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;

View file

@ -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;
}