wsl2 is very slow at loading files from mounted windows directory (/mnt/h/...), how to…
- User
wsl2 is very slow at loading files from mounted windows directory (/mnt/h/...), how to change that?
- Assistant
wsl2 is very slow at loading files from mounted windows directory (/mnt/h/...), how to change that?
The slowness you're experiencing when accessing files in mounted Windows directories (/mnt/h/...) from WSL2 is a common issue due to the way WSL2 handles file I/O between the Linux environment and the Windows filesystem. WSL2 uses a lightweight virtual machine with its own Linux kernel, and accessing files across the virtual machine boundary (from the Linux environment to the Windows filesystem) can introduce significant overhead.
Here's how you can improve the file access performance in WSL2:
Best Practice: For optimal performance, keep your project files within the Linux file system used by WSL2, which resides in a virtual hard disk (VHD) file. This means working with files under your Linux home directory, such as /home/your-username/.
Steps:
Move Your Files to the Linux File System:
Copy your files from the Windows directory to the Linux file system. For example:
cp -r /mnt/h/your-project-folder ~/your-project-folder
Work on Files in the Linux File System:
Navigate to your project directory within the Linux file system:
cd ~/your-project-folder
Access Files from Windows (if needed):
You can access the Linux file system from Windows using the \\wsl$ network share path in File Explorer:
\\wsl$\YourDistroName\home\your-username\your-project-folder
Replace YourDistroName and your-username with your actual WSL distribution name and Linux username.
Benefits:
If you need to work with files located on the Windows file system, you can adjust the mount options to improve performance.
Note: Modifying mount options can lead to potential data integrity issues, especially if files are accessed simultaneously from both Windows and WSL2. Use these options cautiously.
Description: Enabling caching can improve read/write performance by reducing the overhead of file operations. However, it may cause data to become out of sync between Windows and WSL2.
Steps:
Edit the WSL Configuration File:
Create or edit the /etc/wsl.conf file in your WSL2 distribution:
sudo nano /etc/wsl.conf
Add the Following Configuration:
[automount]
enabled = true
options = "metadata,umask=22,fmask=11,cache"
metadata: Enables support for Linux metadata (permissions, symlinks).umask=22,fmask=11: Sets default permissions (adjust as needed).cache: Enables caching of file contents.Restart WSL2:
Exit WSL and shut it down:
wsl --shutdown
Then restart your WSL2 distribution.
Considerations:
You can further tweak mount options to suit your needs. For example:
Disable Metadata if Not Needed:
Removing metadata can improve performance but will disable Linux permissions and symlink support.
options = "umask=22,fmask=11,cache"
wsl --mount FeatureIf you're accessing an entire disk or partition (like your H: drive), you can mount it directly in WSL2, bypassing some of the overhead.
Steps:
Identify the Disk:
Use Disk Management in Windows to find the device name (e.g., \\.\PhysicalDrive1).
Mount the Disk in WSL2:
In an administrative PowerShell, run:
wsl --mount \\.\PhysicalDrive1
Access the Disk in WSL2:
The disk will appear under /mnt/.
Considerations:
Ensure that you have the latest version of WSL installed, as updates often include performance improvements.
Steps:
Update WSL:
In an administrative PowerShell, run:
wsl --update
Update Your WSL Kernel:
wsl --update --kernel
Restart WSL:
wsl --shutdown
WSL1 can offer better performance for file operations on the Windows file system because it doesn't involve the VM layer.
Steps:
Set Your Distribution to Use WSL1:
wsl --set-version YourDistroName 1
Replace YourDistroName with your distribution's name (e.g., Ubuntu).
Verify the Version:
wsl --list --verbose
Considerations:
Adjusting how you access and manipulate files can also help.
Antivirus or security software on Windows can interfere with file operations.
Add Exclusions:
Test Performance Without Antivirus:
Temporarily disable your antivirus software to see if performance improves (ensure you understand the risks before doing so).
Engage with the WSL community to share your experiences and learn from others.
WSL GitHub Repository:
Report issues or search for similar problems:
Community Forums:
Participate in discussions on platforms like Reddit’s r/bashonubuntuonwindows or Stack Overflow.
Note: Modifying system configurations and mount options can carry risks. Always back up important data before making changes, and ensure you understand the implications of each change.
Let me know if you need further assistance or have any questions about these steps!