Update WSL (to use "store version" features) without the Microsoft Store:
- Download the latest WSL app package release: https://github.com/microsoft/WSL/releases
wsl --shutdown
Add-AppxPackage Microsoft.WSL_1.2.5.0_x64_ARM64.msixbundle
Optionally, reboot before and/or after installing the package if features aren't working properly (not required for --mount, maybe required for WSLg)
Attach bare vhdx disk (useful for formatting to ext4)
wsl --mount --vhd F:\ml\wsl2-mldata.vhdx --bare
Mount ext4 partition from vhdx
wsl --mount --vhd F:\ml\wsl2-mldata.vhdx --partition 1 --name mldata
# mount is available at /mnt/wsl/mldata by default
# may be different if root path is changed in /etc/wsl.conf; it's at /mnt/host/wsl/mldata in the "docker-desktop" distro
Auto-mount on wsl (re)start
- not actually supported, needs task scheduler hack
- microsoft/WSL#6073 (comment)
-
mirror
I have a workaround which can handle manual WSL restarts. Since WSL can execute Windows commands, we can let WSL mount a drive when WSL starts.By doing this, the drive can always be automatically mounted whenever WSL starts (including manually restating WSL). The WSL experience will stay the same.
Here's how.
- Create a task that will mount the drives
Use
Task Scheduler
to create a task calledmount-wsl-disks
. WSL will execute this task later to mount the drives.This is just to get elevated permissions without seeing UAC dialogs (so that we can mount something). If you have UAC completely disabled, this is not required.
Something to note:
- Check
Gerneral->Run with highest privileges
- Add your mount command
wsl --mount xxx
toActions
- Edit
/etc/wsl.conf
to execute a script whenever WSL starts
Note: only available in Windows 11
[boot] command="bash /boot.sh"
- Create
/boot.sh
Inside
/boot.sh
, just execute another script called/wsl-boot.sh
, so we can keep its output inside/wsl-boot.log
for debugging purposes.#!/bin/bash /bin/bash /wsl-boot.sh > /wsl-boot.log 2>&1
- Create
/wsl-boot.sh
#!/bin/bash # This will be executed whenever WSL starts. # Execute the task we created before. /mnt/c/Windows/system32/schtasks.exe /run /tn "mount-wsl-disks" # Since mounting will take some time, if you need to do something with the mounted drives later, # you will need to write some logic to wait for it to be ready.
By doing these 4 steps, the drive should be able to automatically mount.
-
Mount wsl path in Docker
-v /run/desktop/<wsl path in "docker-desktop" distro>:/coolmount
Example: forward "mldata" wsl mount
-v /run/desktop/mnt/host/wsl/mldata:/mldata
Rename distro and/or move .vhdx
- Find registry entry for distro under HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\
- Rename: edit "DistributionName"
- Move: edit "BasePath" to the folder containing ext4.vhdx
WARNING: the vscode WSL extension uses a distribution whitelist, and will only recognize names defined in this file: https://github.com/microsoft/WSL/blob/master/distributions/DistributionInfo.json