Remove WSL after operation

This commit is contained in:
Manuel Thalmann 2024-08-24 16:52:41 +02:00
parent 14f348722e
commit de60bf5795
3 changed files with 29 additions and 2 deletions

View file

@ -0,0 +1,7 @@
<#
.SYNOPSIS
Gets the name of the WSL distribution used for reading configuration values.
#>
function Get-DistributionName {
return "ValhallaUbuntu";
}

View file

@ -1,4 +1,5 @@
. "$PSScriptRoot/Config.ps1"; . "$PSScriptRoot/Config.ps1";
. "$PSScriptRoot/Constants.ps1";
. "$PSScriptRoot/../Types/OneShotTask.ps1"; . "$PSScriptRoot/../Types/OneShotTask.ps1";
. "$PSScriptRoot/../../Windows/Scripts/PowerManagement.ps1"; . "$PSScriptRoot/../../Windows/Scripts/PowerManagement.ps1";
. "$PSScriptRoot/../../Windows/Scripts/Registry.ps1"; . "$PSScriptRoot/../../Windows/Scripts/Registry.ps1";
@ -25,9 +26,12 @@ $null = New-Module {
function Start-Operation { function Start-Operation {
param( param(
[switch] $NonInteractive, [switch] $NonInteractive,
[switch] $NoImplicitCleanup,
[scriptblock] $Action [scriptblock] $Action
) )
$cleanup = { };
if (-not $Global:InOperation) { if (-not $Global:InOperation) {
if ($env:DEBUG) { if ($env:DEBUG) {
Set-PSDebug -Trace 1; Set-PSDebug -Trace 1;
@ -46,9 +50,16 @@ $null = New-Module {
} }
New-Alias -Force "sudo" gsudo; New-Alias -Force "sudo" gsudo;
if (-not $NoImplicitCleanup.IsPresent) {
$cleanup = {
Clear-OperationResources;
};
}
} }
& $Action; & $Action;
& $cleanup;
} }
<# <#
@ -174,4 +185,12 @@ $null = New-Module {
Write-EventLog -LogName $logName -Source $logName -EventId $oneShotTrigger -Message "The OneShot task ``$(Get-OneShotTask)`` finished."; Write-EventLog -LogName $logName -Source $logName -EventId $oneShotTrigger -Message "The OneShot task ``$(Get-OneShotTask)`` finished.";
} }
} }
<#
.SYNOPSIS
Clears resources allocated during the operation.
#>
function Clear-OperationResources {
wsl --unregister (Get-DistributionName);
}
}; };

View file

@ -16,6 +16,7 @@ $null = New-Module {
. "$PSScriptRoot/../Scripts/Security.ps1"; . "$PSScriptRoot/../Scripts/Security.ps1";
. "$PSScriptRoot/../Scripts/Update.ps1"; . "$PSScriptRoot/../Scripts/Update.ps1";
. "$PSScriptRoot/../Scripts/Users.ps1"; . "$PSScriptRoot/../Scripts/Users.ps1";
. "$PSScriptRoot/../../Common/Scripts/Constants.ps1";
. "$PSScriptRoot/../../Common/Scripts/Config.ps1"; . "$PSScriptRoot/../../Common/Scripts/Config.ps1";
. "$PSScriptRoot/../../Common/Scripts/Operations.ps1"; . "$PSScriptRoot/../../Common/Scripts/Operations.ps1";
. "$PSScriptRoot/../../Common/Scripts/Software.ps1"; . "$PSScriptRoot/../../Common/Scripts/Software.ps1";
@ -27,7 +28,7 @@ $null = New-Module {
Finishes the installation of a running Windows machine. Finishes the installation of a running Windows machine.
#> #>
function Start-WindowsInstallation { function Start-WindowsInstallation {
Start-Operation { Start-Operation -NoImplicitCleanup {
Start-InstallationLoop; Start-InstallationLoop;
}; };
} }
@ -37,7 +38,7 @@ $null = New-Module {
Starts the installation loop. Starts the installation loop.
#> #>
function Start-InstallationLoop { function Start-InstallationLoop {
$distribution = "ValhallaUbuntu"; $distribution = Get-DistributionName;
$wslLocation = "$env:ProgramData/PortValhalla/$distribution"; $wslLocation = "$env:ProgramData/PortValhalla/$distribution";
$wslDisk = "$wslLocation/ext4.vhdx"; $wslDisk = "$wslLocation/ext4.vhdx";