-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Run WSL from Removable Media #13556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Run WSL from Removable Media #13556
Conversation
This commit implements support for portable WSL distributions that can run directly from removable media (USB drives, external disks) without requiring local installation. This feature enables users to carry their complete development environments on external storage and use them across different Windows machines without leaving traces on the host system. Key Changes: 1. New Portable Distribution API (PortableDistribution.h/cpp) - Metadata management with JSON-based configuration (wsl-portable.json) - Mount/unmount operations for portable distributions - Drive type validation with security-focused restrictions (removable media by default) - --allow-fixed flag for development/testing on fixed drives - Registry-based tracking for portable distribution state and temporary flags 2. Command-Line Interface Extensions - wsl --mount-removable <path> - Mount a portable distribution from external media - wsl --unmount-removable <distro> - Unmount and cleanup portable distribution - wsl --import <name> <path> <file> --portable - Create new portable distribution - --allow-fixed flag for both mount and import commands 3. Command-Line Arguments (wsl.h) - --mount-removable with -d/--distro, -t/--temporary, and -f/--allow-fixed options - --portable flag for import command with optional --allow-fixed - --unmount-removable for cleanup operations 4. Localization (Resources.resw) - Added 7 new localized message strings for portable operations - Proper multi-language support for all user-facing messages - Removed temporary inline message helpers 5. Integration (WslClient.cpp) - Command routing for new portable operations - Proper localization integration with Localization::MessagePortable* calls - Error handling for portable-specific scenarios - --allow-fixed flag support in command handlers 6. Build System (CMakeLists.txt) - Added PortableDistribution.cpp and PortableDistribution.h to build Technical Implementation: - Metadata Format: JSON-based storage of distribution configuration including name, version, VHDX path, and portable flag - Drive Validation: Uses Windows GetDriveTypeW() API to verify removable media by default, supports Volume GUID paths - Security: Restricts to DRIVE_REMOVABLE by default; --allow-fixed explicitly required for fixed drives - Transient Registration: Distributions are registered only during active use, cleaned up on unmount - Registry Tracking: Portable flag, base path, and temporary flag stored in LXSS registry for state management - Localization: Full multi-language support using WSL's localization infrastructure - Error Handling: Comprehensive validation and cleanup on failure scenarios Benefits: - True Portability: Run complete Linux environments from any USB drive - Zero Host Footprint: No local installation or registry traces after unmount - Cross-Machine Compatibility: Same distribution works on any Windows 10/11 machine with WSL - Secure by Default: Restricts to removable media; fixed drives require explicit opt-in - Developer Flexibility: --allow-fixed flag enables testing without removable media - Temporary Mounts: Support for ephemeral distributions with automatic cleanup tracking Testing Notes: - Test on actual removable media (USB 3.0+ recommended) - Verify Volume GUID path support for drive-letter-agnostic mounting - Test cross-machine portability with different Windows versions - Verify proper cleanup leaves no registry artifacts - Test --allow-fixed flag behavior for development scenarios - Verify all localized messages display correctly in different languages Closes microsoft#12804 Signed-off-by: Giovanni Magliocchetti <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements support for portable WSL distributions that can run directly from removable media without requiring local disk installation. It introduces a complete portable WSL workflow with three new commands and supporting infrastructure to address the lack of true portability and host system contamination issues.
Key changes:
- Adds three new WSL commands:
--mount-removable
,--unmount-removable
, and--import
with--portable
flag - Implements security-focused path validation restricting to removable media by default with
--allow-fixed
override - Provides comprehensive localization support with 7 new message strings
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
src/windows/inc/wsl.h | Adds command constants and option flags for portable distribution functionality |
src/windows/common/WslClient.cpp | Implements command handlers, argument parsing, and localization integration for portable operations |
src/windows/common/PortableDistribution.h | Defines API and data structures for portable WSL distribution management |
src/windows/common/PortableDistribution.cpp | Core implementation of portable distribution functionality including validation, mounting, and metadata handling |
src/windows/common/CMakeLists.txt | Adds new portable distribution source files to build system |
localization/strings/en-US/Resources.resw | Adds 7 new localized message strings for portable distribution operations |
Co-authored-by: Copilot <[email protected]>
Signed-off-by: Giovanni Magliocchetti <[email protected]>
// Unregister immediately as we only wanted to create the VHDX | ||
try | ||
{ | ||
service.UnregisterDistribution(&guid); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't unregistering the distribution delete the VHDX as well on this code path ?
Signed-off-by: Giovanni Magliocchetti <[email protected]>
…thout deleting VHDX Signed-off-by: Giovanni Magliocchetti <[email protected]>
Summary
This PR implements support for portable WSL distributions that can run directly from removable media (USB drives, external disks, etc.) without requiring local disk installation. This addresses feature request #12804 and enables true portability for WSL environments.
Problem Statement
Currently, WSL forces all distributions to be installed on local disks, creating two major limitations:
Solution Overview
This PR introduces a complete portable WSL workflow with three new commands and supporting infrastructure:
New Commands
1.
wsl --mount-removable <path> [options]
Mount a portable WSL distribution from removable media.
Options:
--distro <name>
or-d <name>
: Override distribution name--temporary
or-t
: Mark as temporary (registry-tracked for future auto-cleanup)--allow-fixed
or-f
: Allow mounting from fixed drives (for development/testing)Examples:
2.
wsl --import <name> <location> <file> --portable [--allow-fixed]
Create a new portable distribution on external storage.
Examples:
3.
wsl --unmount-removable <distro-name>
Cleanly unmount a portable distribution and remove system traces.
Example:
Technical Implementation
Architecture
Metadata Format
The
wsl-portable.json
file contains distribution configuration:Key Components
PortableDistribution.h/cpp
allowFixed
parameter for development/testing flexibilityEnhanced WslClient.cpp
Localization::MessagePortable*()
calls--allow-fixed
flag supportLocalization (Resources.resw)
Registry Integration
IsTemporary
DWORD)Path Handling
E:\wsl\
\\?\Volume{UUID}\wsl\
GetDriveTypeW()
APIDRIVE_REMOVABLE
by default--allow-fixed
flag requiredBenefits
✅ True Portability: Carry complete Linux environments on USB drives
✅ Zero Local Footprint: No contamination of host system
✅ Clean Removal: Automatic cleanup on unmount
✅ Easy Sharing: Share pre-configured environments
✅ Secure by Default: Removable media only; fixed drives require explicit flag
✅ Developer Flexibility:
--allow-fixed
enables testing without USB drives✅ Temporary Mounts: Registry-tracked for future auto-cleanup features
Usage Workflow
Creating a Portable Distribution
Using a Portable Distribution
Sharing Portable Distributions
Simply copy the entire portable directory (containing
wsl-portable.json
and.vhdx
file) to another USB drive or share with others.Implementation Details
Files Modified
src/windows/inc/wsl.h
- Added command constants including--allow-fixed
flagssrc/windows/common/WslClient.cpp
- Command handlers, routing, localization integrationsrc/windows/common/WslClient.h
- Function declarationssrc/windows/common/CMakeLists.txt
- Added new source fileslocalization/strings/en-US/Resources.resw
- Added 7 new localized message stringsFiles Added
src/windows/common/PortableDistribution.h
- Portable WSL APIsrc/windows/common/PortableDistribution.cpp
- ImplementationBackward Compatibility
Testing Recommendations
Basic Functionality
Security & Path Validation
--allow-fixed
flag enables fixed drives--allow-fixed
Localization Testing
Temporary Flag Tracking
--temporary
flagIsTemporary=1
in registryMulti-Device Testing
Edge Cases
--allow-fixed
(should fail with helpful message)Registry Cleanup
Future Enhancements (Out of Scope)
Documentation Updates Needed
--allow-fixed
flag behavior and security rationaleExamples
Creating a Portable Distribution
Mounting a Portable Distribution
Unmounting a Portable Distribution
Checklist