From c0748548e106f5117072179797a1aaf9561beaa7 Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Fri, 9 Aug 2024 16:02:33 +0200
Subject: [PATCH] Set up shared WSL

---
 scripts/Windows/OS/Install.ps1 | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1
index 3bcf86c8..53845e99 100644
--- a/scripts/Windows/OS/Install.ps1
+++ b/scripts/Windows/OS/Install.ps1
@@ -13,6 +13,7 @@ $null = New-Module {
     . "$PSScriptRoot/../Scripts/Hooks.ps1";
     . "$PSScriptRoot/../Scripts/PowerManagement.ps1";
     . "$PSScriptRoot/../Scripts/Registry.ps1";
+    . "$PSScriptRoot/../Scripts/Security.ps1";
     . "$PSScriptRoot/../Scripts/Update.ps1";
     . "$PSScriptRoot/../Scripts/Users.ps1";
     . "$PSScriptRoot/../../Common/Scripts/Config.ps1";
@@ -36,6 +37,8 @@ $null = New-Module {
         Starts the installation loop.
     #>
     function Start-InstallationLoop {
+        $wslLocation = "$env:ProgramData\PortValhalla\Ubuntu";
+
         while (-not (Get-IsFinished)) {
             switch (Get-Stage) {
                 ($null) {
@@ -94,7 +97,15 @@ $null = New-Module {
                     }
 
                     if (-not (& { $null = wsl -l; $?; })) {
-                        ubuntu install --root;
+                        $wslRoot = Split-Path -Parent $wslLocation;
+
+                        if (-not (Test-Path -PathType $wslRoot)) {
+                            $null = New-Item -ItemType Directory $wslRoot;
+                        }
+
+                        Copy-Item -Recurse -Force (Get-AppxPackage "*Ubuntu*").InstallLocation $wslLocation;
+                        Set-UserPermissions $wslLocation;
+                        & "$wslLocation\ubuntu.exe" install --root;
                         continue;
                     }
 
@@ -124,6 +135,11 @@ $null = New-Module {
                     break;
                 }
                 default {
+                    if (-not (& { $null = wsl -l; $? })) {
+                        wsl --import-in-place "PortValhalla" "$wslLocation/ext4.vhdx";
+                        wsl --set-default "PortValhalla";
+                    }
+
                     if (Test-Admin) {
                         $null = Import-Module PSWindowsUpdate;