Monday, May 20, 2024

Mounting USB devices in WSL-2

My goal was to deduplicate all the images and videos I have in my large backup USB hard drive. Obviously this was a task for Linux but the only Linux I have ready access to these days is WSL-2 running under Windows 11.

By default USB devices connected on the Windows side are not accessible from WSL-2. The solution is laid out in this how-to from Microsoft.

Setup


Prequisite: WSL needs to have kernel which is USBIP capable. This can be achieved by updating WSL in Powershell: wsl --update.


Windows side


Step 1: Install the (open-source) usbipd-win utility (msi) from the project's releases page. Verify the installation by launching Windows Powershell and running usbipd list.


WSL side


We now need to compile the Kernel to enable CONFIG_USB_STORAGE.

Step 1: Figure out the current kernel version. In Powershell run: wsl --version. In our example the kernel version is 5.15.153.1-2.

Step 2: Clone the WSL kernel source code for the version installed: git clone --depth 1 --branch linux-msft-wsl-5.15.153.1 https://github.com/microsoft/WSL2-Linux-Kernel.git

Step 3: Install build tools: sudo apt-get install git bc build-essential flex bison libssl-dev libelf-dev dwarves

Step 4: Install usbip tools on the WSL side: sudo apt-get install usbutils hwdata

Step 5: 

cd WSL2-Linux-Kernel
cp Microsoft/config-wsl .config
scripts/config --enable USB_STORAGE
make oldconfig

The last command will prompt for a bunch of information. Answer y to everything except "verbose debug" (and possibly Rio karma music player).

make -j$(nproc)
cp arch/x86/boot/bzImage /c/Users/<windows-user>/

Step 6: Modify wsl config to use this compiled kernel by modifying the /c/Users/<windows-user>/.wslconfig file and pointing the kernel property to C:\\Users\\<windows-user>\\bzImage

Step 7: Restart WSL (in Powershell run: wsl --shutdown and then launch WSL again)

Sources:

Mounting USB device in WSL


Step 1: In Powershell run usbipd list and take a look at the devices that are already attached. We will rerun this command and look at the change to discover our device.

Now attach your USB device and eject it (while keeping it plugged in). Attachment will fail if Windows is accessing the USB device.

Step 2: In Powershell run usbipd list and determine the busid (BUS ID) of the attached device. It is of the form d-d (where d is an integer). In our example the busid will be 2-6.

We will now bind the USB devise to WSL (make sure that the WSL VM is up, opening a WSL terminal should suffice). 

Step 3: In an Admin Powershell: usbipd bind --busid 2-6. To verify run usbipd list and check that the STATE for your device now says Shared.

Step 4: In (a normal) Powershell: usbipd attach --wsl --busid 2-6. Verify by running usbipd list and checking that the STATE for your device now says Attached.

Step 5: In WSL run lsusb to see the USB device showing up. Then run dmesg and look at the last bit of output. We are looking for usb, scsi, and sd output, in particular any mention of a disk partition e.g. sdd1.

Step 6: Mount the partition: mount /dev/sdd1 /mnt/usb


Unmounting USB device in WSL


Step 1: Unmount partition in WSL: umount /mnt/usb

Step 2: Unattach device in Powershell: usbipd unattach --busid 2-6

Step 3: Unbind device in Powershell: usbipd unbind --basuid 2-6