Mounting drives

Under WSL, Windows drives are normally mounted and provided under paths like /mnt/c/ etc. However, there's a major drawback: the driver that converts the Windows file system to the Linux file system is very slow.

If you actually work with WSL, say using a Docker container for development with Apache2/PHP to develop a website, you'll notice this quickly. There's only one solution to this: Your data needs to be moved from the Windows file system to the WSL file system.

You have two options for this:

  1. You use the VHDX file of the WLS distro

    If you take a look at your Windows File Explorer, you'll see - if everything is set up correctly - a little penguin at the bottom of the file tree, next to which you can access the file system of your Linux distro(s). To move your data (in this case, the website files) from the Windows file system to the WSL file system, you can simply drag and drop or copy-paste them into your home directory in the Linux file system.

    Changes to the Linux file system are not shown immediately. Press F5 to update the view and control the copy process!!!

    If you run into file access privilege problems, use the command line for copying.

    Downside of this method: In case you use a dual boot system with Windows and Linux, you cannot use the files you have “buried” in a VHDX with Linux, but only under WSL!

  2. You use a dedicated drive with the ext4 file system and mount it into WSL

    This comes with the advantage that you can very well use these files under Linux, too.

    At first you have to find out the device name of the drive. You can use this Power Shell CMD-let:

    
    GET-CimInstance -query "SELECT * from Win32_DiskDrive"

    If you know the name, you have to mount it into WSL:

    
    wsl --mount \\.\PHYSICALDRIVE2 --bare

    In WSL you have to look for the UUID of the drive. You do this with:

    
    lsblk

    and

    
    sudo blkid

    Now create the mount script:

    
    sudo bash -c "cat > /mount.sh" <<'EOF'
    #!/usr/bin/env bash
    
    /mnt/c/Users/karlheinz/AppData/Local/Microsoft/WindowsApps/wsl.exe -d Ubuntu-24.04 --mount \\\\.\\PHYSICALDRIVE2 --bare
    echo "Physical drive ECHO mounted in WSL"
    
    timeout=30
    elapsed=0
    interval=2
    
    while [ ! -e /dev/disk/by-uuid/<UUID> ]; do
       if [ $elapsed -ge $timeout ]; then
           echo "Timed out waiting for /dev/disk/by-uuid/<UUID> to become ready."
           exit 1
       fi
    
       echo "Waiting for /dev/disk/by-uuid/<UUID> to be ready..."
       sleep $interval
       elapsed=$((elapsed + interval))
    done
    echo "Device is ready"
    
    mount -t ext4 -o defaults /dev/disk/by-uuid/<UUID> /home/karlheinz/linux
    echo "Device mounted to /home/karlheinz/linux"
    EOF

    Finally, you have to change the WSL configuration:

    
    sudo bash -c "cat > /etc/wsl.conf" <<'EOF'
    [boot]
    systemd=true
    
    [boot]
    command="bash /mount-bare.sh > /mount.log"
    command="service docker start"

    Yes, at the start of WSL you call WSL from within WSL to mount the physical drives this way. This is kinda sick and genious at the same time!

Source: