Repository navigation
Draft: add manual VHDX compaction command for stopped WSL distros - #40481
Ben Hillis (benhillis) merged 38 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a manual-only VHDX compaction path for stopped WSL 2 distributions, exposed via wsl --manage <Distro> --compact, with service plumbing and a Virtual Disk API helper.
Changes:
- Introduces
--compactinwsl.exe --manage, routed throughSvcCommto the service. - Implements a service-side
CompactDistributionpath that rejects non-WSL2 and running/in-use distros and invokesCompactVirtualDisk. - Updates help/localization text and adds a WSL2 unit test covering the compact flow.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| test/windows/UnitTests.cpp | Adds help text expectation and a new WSL2 test for --manage --compact. |
| src/windows/service/inc/wslservice.idl | Extends ILxssUserSession with CompactDistribution RPC method. |
| src/windows/service/exe/LxssUserSession.h | Declares CompactDistribution on the COM class + impl. |
| src/windows/service/exe/LxssUserSession.cpp | Implements CompactDistribution validation and VHD compaction call path. |
| src/windows/inc/wsl.h | Adds --compact manage option constant. |
| src/windows/common/WslCoreFilesystem.h | Declares CompactVhd helper. |
| src/windows/common/WslCoreFilesystem.cpp | Implements CompactVhd via OpenVirtualDisk + CompactVirtualDisk. |
| src/windows/common/WslClient.cpp | Adds CLI parsing/dispatch for --manage --compact. |
| src/windows/common/svccomm.hpp | Adds SvcComm::CompactDistribution declaration. |
| src/windows/common/svccomm.cpp | Wires SvcComm::CompactDistribution to ILxssUserSession. |
| localization/strings/en-US/Resources.resw | Adds --compact help text + locked token list update. |
| localization/strings/es-ES/Resources.resw | Adds Spanish --compact help text + locked token list update. |
|
@microsoft-github-policy-service agree |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Blue (@OneBlue) Formatting is fixed. Could you run "/azp run" again please? |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Blue (@OneBlue) Sorry again, It should pass now. |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Blue (@OneBlue) Sorry for the repeated pings. WSLC failed; it may be flaky or an interaction with the recent WSLC changes in master. I can’t access the detailed result could you check it? |
Blue (OneBlue)
left a comment
There was a problem hiding this comment.
LGTM. Test failure was a known issue that's unrelated to this change
|
Thanks again for your patience here, I'm going to go ahead and merge. Nice job. |
…n tools; reclaim WSL VHDX with wsl --manage --compact (#753) ### Scope One anti-pattern log row in `docs/harness-defaults.md`, following the user's 2026-10-05 request to resolve the core reasons for NativeStack's disk growth. Lane: foundation. The row's facts: - NativeStack's root fell to 191 GB free, and its VHDX on Z: grew to 1,065 GB for 765 GB used. - The bulk was finished work: 403 worktrees, 124 GB of `/var/tmp` build scratch from a session that had ended, 56.5 GB of 1,802 byte-identical fixture-binary copies, 27.8 GB of per-session index databases, about 75 GB of superseded gateway builds, and 30 GB of unused images. - The cleanup reached 529 GB free, with every uncommitted change and the one local-only gateway patch preserved first. The rule closes each job with each tool's own command. It reclaims VHDX space with WSL 3.0.1's native `wsl --manage <distro> --compact`, and leaves sparse VHD off while WSL refuses it without `--allow-unsafe`. ### SOTA sources - [git-worktree](https://git-scm.com/docs/git-worktree): `remove` refuses unclean worktrees without `--force`. - [uv `cache prune`](https://docs.astral.sh/uv/reference/cli/#uv-cache-prune) and [docker `image prune`](https://docs.docker.com/reference/cli/docker/image/prune/). - microsoft/WSL: - [PR #40481](microsoft/WSL#40481): `--compact` force-terminates the target distro and runs an offline `e2fsck -E discard` before `CompactVirtualDisk`; - tag 3.0.1, `src/windows/service/exe/LxssUserSession.cpp:1761-1921`, where `--set-sparse true` is refused without `--allow-unsafe`; - [PR #41726](microsoft/WSL#41726): lifts that refusal in tag 3.0.2 and marks sparse experimental. ### Evidence-class table | Claim | Evidence class | Source | | --- | --- | --- | | Sizes and counts | host measurement on NativeStack (`du`, `df`, `git worktree list`) | the coordinator's private actions log | | WSL compact and sparse behaviour | upstream source review at tag 3.0.1, plus PRs | the links above | | git, uv and docker commands | upstream docs | the links above | ### Local commands run - `uv run --no-project --with pyyaml python -m unittest` over the 416 tests that reference `harness-defaults.md`: OK (2 skipped). - `python3 scripts/validate.py`: exit 0, after the registry-last commit. - `python3 scripts/validate_convergence.py --all-recorded`: exit 0. - The login-name scan is clean. ### Decision record None new; the anti-pattern log row is the record. ### Host evidence Sanitized counts only. No host paths or user names. ### Checklist - [x] No GitHub Actions or workflows changed. - [x] No secrets printed, logged or committed. - [x] No paid hosting or billing surface. - [x] Peer-owned untracked files and worktrees preserved. Each peer pruned its own data; the coordinator removed only data from ended sessions and its own lane, by literal target. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Summary of the Pull Request
Related to #40243 and #4699.
This is an exploratory draft PR for a manual-only VHDX compaction path.
It adds a new manual command:
wsl --manage <Distro> --compactThe command compacts the VHDX backing file for a stopped WSL 2 distro using the Windows Virtual Disk API.
This PR intentionally does not add automatic compaction yet.
PR Checklist
Detailed Description of the Pull Request / Additional comments
Scope
Included:
OpenVirtualDiskandCompactVirtualDisk.wsl --manage <Distro> --compact.ERROR_SHARING_VIOLATION.Not included:
.wslconfigpolicy.Motivation
The broader issue in #4699 is that WSL 2 distro VHDX files can grow significantly and not release space back to the host automatically.
In #40243 I proposed a safer path around shutdown/offline compaction. Before attempting any automatic policy, this PR adds a smaller manual command first, so the Virtual Disk API integration and safety model can be reviewed independently.
Validation
Built successfully on Windows:
cmake .cmake --build . --config Debug --target wsl wslservice wsltests -- /mcmake --build . --config Debug --target msixinstallerpackage -- /mTested:
UnitTests::UnitTests::Compactpassed.Wsl/Service/WSL_E_DISTRO_NOT_STOPPED.wsl --manage Ubuntu --compact --resize 10GBdid not compact and failed argument validation/help as expected.Manual validation result:
wsl --manage Ubuntu --compactLa operación se completó correctamente.0-2,097,152 byteswsl -d Ubuntu -- echo okok0Running distro validation:
wsl --manage Ubuntu --compactWsl/Service/WSL_E_DISTRO_NOT_STOPPED-10Notes
This is opened as a draft because the intended first step is design and maintainer feedback on the manual command surface and service/helper integration.
Open questions:
wsl --manage <Distro> --compactthe right CLI surface?Future work, if this direction is acceptable: