Skip to content

Commit a072b0b

Browse files
committed
WSL compact script and instructions
1 parent e4b174e commit a072b0b

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

devops/utils/compact_wsl_disks.ps1

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,50 @@
66
# 2. You have Hyper-V management tools:
77
# Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All -All -NoRestart
88

9+
# Check if the script is running with administrator privileges
10+
$IsAdmin = [bool]([System.Security.Principal.WindowsIdentity]::GetCurrent().Owner.IsWellKnown([System.Security.Principal.WellKnownSidType]::BuiltinAdministratorsSid))
11+
12+
if (-not $IsAdmin) {
13+
# Relaunch the script with administrator privileges
14+
$argList = "$($myinvocation.MyCommand.Path)" # Get the script path
15+
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File $argList" -Verb RunAs
16+
exit
17+
}
918

10-
# Define the base folder path using the LOCALAPPDATA environment variable
11-
$baseFolderPath = Join-Path -Path $env:LOCALAPPDATA -ChildPath "Packages"
12-
13-
# Search for all VHDX files matching the pattern
14-
$vdiskFiles = Get-ChildItem -Path $baseFolderPath -Recurse -Filter "ext4.vhdx"
15-
19+
# Shutdown WSL
1620
Write-Host "Shutting down WSL..."
1721
wsl --shutdown
1822
Start-Sleep -Seconds 5 # Wait a few seconds to ensure WSL is fully shut down
1923
Write-Host "WSL shut down successfully."
2024

21-
# Loop through each VHDX file found
22-
foreach ($vdisk in $vdiskFiles) {
23-
Write-Host "Processing VHD/VHDX: $($vdisk.FullName)"
24-
25-
# Mount the VHD/VHDX file
26-
Mount-VHD -Path $vdisk.FullName -PassThru | Out-Null
27-
Write-Host "VHD mounted: $($vdisk.Name)"
25+
# Search for all VHDX files under LOCALAPPDATA\...\Packages\...
26+
$baseFolderPath = Join-Path -Path $env:LOCALAPPDATA -ChildPath "Packages"
27+
$vdiskFiles = Get-ChildItem -Path $baseFolderPath -Recurse -Filter "ext4.vhdx"
2828

29-
# Compact the VHD/VHDX file to reclaim unused space
30-
Optimize-VHD -Path $vdisk.FullName -Mode Full
31-
Write-Host "VHD compacted: $($vdisk.Name)"
29+
# Loop through each VHDX file
30+
foreach ($vdisk in $vdiskFiles) {
3231

33-
# Dismount the VHD
34-
Dismount-VHD -Path $vdisk.FullName
35-
Write-Host "VHD dismounted: $($vdisk.Name)"
32+
$diskName = $vdisk.Name
33+
if ($vdisk.FullName -like "*Ubuntu*") {
34+
$diskName = "Ubuntu VHDX"
35+
} elseif ($vdisk.FullName -like "*Debian*") {
36+
$diskName = "Debian VHDX"
37+
}
38+
39+
Write-Host "Found ${diskName} at $($vdisk.FullName)"
40+
41+
# Ask the user if they want to compact this VHD/VHDX
42+
$userInput = Read-Host "Do you want to compact this disk? (Y/N)"
43+
44+
if ($userInput -match "^[Yy]$") {
45+
# Compact the VHD/VHDX file to reclaim unused space
46+
Optimize-VHD -Path $vdisk.FullName -Mode Full
47+
Write-Host "VHD compacted: $($vdisk.Name)"
48+
}
49+
# else
50+
# {
51+
# Write-Host "Skipping VHD/VHDX: $($vdisk.Name)"
52+
}
3653
}
3754

3855
Write-Host "VHD/VHDX compression complete."

docs/WSL-README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ cleaned.
109109
To actually realise the freed up space in WSL you will need to compact the VHD
110110
in which your WSL instance resides.
111111

112-
In a Windows terminal:
112+
You can use the `/devops/utils/compact_wsl_disks.ps1` script to achieve this. Or
113+
to do it old school, in a Windows terminal:
113114

114115
```cmd
115116
wsl --shutdown

0 commit comments

Comments
 (0)