Draft: add manual VHDX compaction command for stopped WSL distros#40481
Draft: add manual VHDX compaction command for stopped WSL distros#40481Guayamose wants to merge 19 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 |
|
Sorry for the Copilot mess, I thought it was the automatic review, but it was my settings. |
benhillis
left a comment
There was a problem hiding this comment.
This is looking pretty good, let me discuss with some of the team to see what we want to do here. We've internally been looking at some options to reclaim vhd disk space automatically, but even so it's probably a good idea to have a manual option too.
Thanks for the contribution!
|
All the four above are contradictions to the maintainer |
Thanks. My plan was to start with this manual I understand the team may want to drive the automatic part internally. I’ll keep this PR focused on the manual path and adapt based on the direction you prefer. |
Hello. The best way to reclaim vhd disk space would be mapping VM's BLKDISCARD ioctl to host's FSCTL_SET_ZERO_DATA if sparse mode enabled. This way disk space may be deallocated without manual compaction, without moving data back and forth, unused data will be freed even without stopping VM (on the fly, scheduled or manually by calling fstrim utility). |
So, what you said makes sense and its probably a better long term solution and more comfortable for users, but in my opinion this would be more of an architectural change rather than a small addon or replacement for de manual compaction. Mapping guest BLKDISCARD/TRIM operations directly to host-side sparse deallocation would move space reclamation into the live I/O path. That means WSL would need to handle discard batching, ordering, error handling, host filesystem behavior, and compatibility with existing VHDX files very carefully. It could also make the backing file layout less predictable over time, since space would be punched out dynamically as the guest filesystem frees blocks. Depending on the frequency and pattern of discards, that could introduce host-side fragmentation or metadata overhead that is harder to reason about than an explicit offline compaction pass. So while the idea is technically possible, I think it has a much larger correctness and regression surface than the stopped-VM compaction operation. |
|
@Guayamose hey, I'm just trying to be heard by WSL team member |
@dporf: Thank you for reporting, we hear you. A team member internally has been actively looking at this area and we have shared your PR with them. Manual option is good to have too.. we will get back to you shortly. |
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: