From 7eb0c61cf64d3d5ccdd36b6f93af6e506ac4bc4e Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 24 Aug 2024 16:52:41 +0200 Subject: [PATCH] Remove WSL after operation --- scripts/Common/Scripts/Constants.ps1 | 7 +++++++ scripts/Common/Scripts/Operations.ps1 | 19 +++++++++++++++++++ scripts/Windows/OS/Install.ps1 | 5 +++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 scripts/Common/Scripts/Constants.ps1 diff --git a/scripts/Common/Scripts/Constants.ps1 b/scripts/Common/Scripts/Constants.ps1 new file mode 100644 index 00000000..01217a8d --- /dev/null +++ b/scripts/Common/Scripts/Constants.ps1 @@ -0,0 +1,7 @@ +<# + .SYNOPSIS + Gets the name of the WSL distribution used for reading configuration values. +#> +function Get-DistributionName { + return "ValhallaUbuntu"; +} diff --git a/scripts/Common/Scripts/Operations.ps1 b/scripts/Common/Scripts/Operations.ps1 index 8720953e..cd085a93 100644 --- a/scripts/Common/Scripts/Operations.ps1 +++ b/scripts/Common/Scripts/Operations.ps1 @@ -1,4 +1,5 @@ . "$PSScriptRoot/Config.ps1"; +. "$PSScriptRoot/Constants.ps1"; . "$PSScriptRoot/../Types/OneShotTask.ps1"; . "$PSScriptRoot/../../Windows/Scripts/PowerManagement.ps1"; . "$PSScriptRoot/../../Windows/Scripts/Registry.ps1"; @@ -25,9 +26,12 @@ $null = New-Module { function Start-Operation { param( [switch] $NonInteractive, + [switch] $NoImplicitCleanup, [scriptblock] $Action ) + $cleanup = { }; + if (-not $Global:InOperation) { if ($env:DEBUG) { Set-PSDebug -Trace 1; @@ -46,9 +50,16 @@ $null = New-Module { } New-Alias -Force "sudo" gsudo; + + if (-not $NoImplicitCleanup.IsPresent) { + $cleanup = { + Clear-OperationResources; + }; + } } & $Action; + & $cleanup; } <# @@ -174,4 +185,12 @@ $null = New-Module { 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); + } }; diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index 5eb4d14b..bb4fbb4b 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -18,6 +18,7 @@ $null = New-Module { . "$PSScriptRoot/../Scripts/Security.ps1"; . "$PSScriptRoot/../Scripts/Update.ps1"; . "$PSScriptRoot/../Scripts/Users.ps1"; + . "$PSScriptRoot/../../Common/Scripts/Constants.ps1"; . "$PSScriptRoot/../../Common/Scripts/Config.ps1"; . "$PSScriptRoot/../../Common/Scripts/Operations.ps1"; . "$PSScriptRoot/../../Common/Scripts/Software.ps1"; @@ -30,7 +31,7 @@ $null = New-Module { Finishes the installation of a running Windows machine. #> function Start-WindowsInstallation { - Start-Operation { + Start-Operation -NoImplicitCleanup { Start-InstallationLoop; }; } @@ -40,7 +41,7 @@ $null = New-Module { Starts the installation loop. #> function Start-InstallationLoop { - $distribution = "ValhallaUbuntu"; + $distribution = Get-DistributionName; $wslLocation = "$env:ProgramData/PortValhalla/$distribution"; $wslDisk = "$wslLocation/ext4.vhdx";