Skip to content

Instantly share code, notes, and snippets.

@ayyybe
Last active June 5, 2023 00:24
Show Gist options
  • Save ayyybe/b57704fc29b91fdd92b9ebf96270adb4 to your computer and use it in GitHub Desktop.
Save ayyybe/b57704fc29b91fdd92b9ebf96270adb4 to your computer and use it in GitHub Desktop.
wsl2 + docker desktop tomfoolery

Update WSL (to use "store version" features) without the Microsoft Store:

  1. Download the latest WSL app package release: https://github.com/microsoft/WSL/releases
  2. wsl --shutdown
  3. 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.

      1. Create a task that will mount the drives

      Use Task Scheduler to create a task called mount-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 to Actions
      1. Edit /etc/wsl.conf to execute a script whenever WSL starts

      Note: only available in Windows 11

      [boot]
      command="bash /boot.sh"
      
      1. 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
      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

  1. Find registry entry for distro under HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\
  2. Rename: edit "DistributionName"
  3. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment