Skip to content

Instantly share code, notes, and snippets.

@itsdotscience
Created May 2, 2024 04:04
Show Gist options
  • Save itsdotscience/6431647a15c9ff626e780066845bb6fe to your computer and use it in GitHub Desktop.
Save itsdotscience/6431647a15c9ff626e780066845bb6fe to your computer and use it in GitHub Desktop.
#!/bin/bash
export WSLKERNEL=`uname -a | cut -d " " -f3 | cut -d "-" -f1`
export CHECKOUT_DIR=`linux-msft-wsl-$WSLKERNEL`
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git --depth=1 -b $CHECKOUT_DIR
cd $WSLKERNEL
sudo make -j$(nproc) KCONFIG_CONFIG=Microsoft/config-wsl CONFIG_USB_STORAGE=m CONFIG_USB_UAS=m
sudo make -j$(nproc) KCONFIG_CONFIG=Microsoft/config-wsl CONFIG_USB_STORAGE=m CONFIG_USB_UAS=m modules
sudo make -j$(nproc) KCONFIG_CONFIG=Microsoft/config-wsl CONFIG_USB_STORAGE=m CONFIG_USB_UAS=m modules_install
cp arch/x86/boot/bzImage wsl-$WSLKERNEL
echo "Copy the above to the root of your USERPROFILE on the Windows sdie then Add in the [wsl] section of your .wslconfig the following, doubling all
kernel=c:\\users\\username\\wsl-$WSLKERNEL
Then run wsl --shutdown from Windows, wait a minut or so and star wsl again. Once you have your usb device passed you should see the storage device if not, try on the wsl side: modprobe usb-storage; modprobe uas"
@StoaferP
Copy link

This made it so easy to add USB mass storage support, thank you!!.

Side note, I had to make a few changes:

line 4 export CHECKOUT_DIR="linux-msft-wsl-$WSLKERNEL"
line 6 git clone https://github.com/microsoft/WSL2-Linux-Kernel.git --depth=1 -b $CHECKOUT_DIR $WSLKERNEL

@StoaferP
Copy link

StoaferP commented May 31, 2024

Also, I had to increase the WSL memory from 4GiB to 6GiB because pandoc kept getting OOM killed

@Komorebi-E
Copy link

Komorebi-E commented Aug 3, 2024

For SD Card / MMC support I needed to add CONFIG_MMC=m for USB devices with SD/MMC card's to show up for lsblk.
This includes the suggestions from @StoaferP and adds the conditional for if the Microsoft WSL2 kernel has already been cloned.

Note that from the WSL2 Kernel README, modules cannot be loaded with modprobe - so removed it from the instructions.

Loadable module support is disabled when using a custom built kernel. Set any modules you want to be built-in before building.

#!/bin/bash

export WSLKERNEL=`uname -a | cut -d " "  -f3 | cut -d "-" -f1`
export CHECKOUT_DIR="linux-msft-wsl-$WSLKERNEL"

if [[ ! -d $WSLKERNEL ]]; then
  git clone https://github.com/microsoft/WSL2-Linux-Kernel.git --depth=1 -b $CHECKOUT_DIR $WSLKERNEL
fi

cd $WSLKERNEL
sudo  make -j$(nproc) KCONFIG_CONFIG=Microsoft/config-wsl CONFIG_USB_STORAGE=m CONFIG_USB_UAS=m CONFIG_MMC=m
sudo  make -j$(nproc) KCONFIG_CONFIG=Microsoft/config-wsl CONFIG_USB_STORAGE=m CONFIG_USB_UAS=m CONFIG_MMC=m modules
sudo  make -j$(nproc) KCONFIG_CONFIG=Microsoft/config-wsl CONFIG_USB_STORAGE=m CONFIG_USB_UAS=m CONFIG_MMC=m modules_install
cp arch/x86/boot/bzImage wsl-$WSLKERNEL

echo -e "Copy the above to the root of your USERPROFILE on the Windows side then add it in the [wsl2] section of your USERPROFILE/.wslconfig like the following - doubling all backslashes, '\\\\\\\\' \n
kernel=c:\\\\\\\\users\\\\\\\\username\\\\\\\\wsl-$WSLKERNEL\n
Then run wsl --shutdown from Windows, wait a minute or so and start wsl again. Once you have your usb device passed you should see the storage device with lsblk\n"

@rs38
Copy link

rs38 commented Oct 18, 2024

@Komorebi-E
why did you comment out the make commands?
may be also worth asking: adding "m" for module makes it avail for usb mass storage, but not via modprobe?

@Komorebi-E
Copy link

@rs38 Commenting was from testing the \ to get the right output. I've removed the comments on the make commands - thank you for pointing it out.

Correct, it is a available fir USB mass storage. WSL2 with a custom built kernel cannot load modules with modprobe as the loadable module support is disabled when WSL2 is configured this way.

@rs38
Copy link

rs38 commented Oct 23, 2024

@Komorebi-E what's strange: to make the USB mass storage actually work with USBIPD and that custom kernel, I have to execute modprobe vhci_hcd in WSL2, which succeeds.
(and docker is failing)

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