-
Notifications
You must be signed in to change notification settings - Fork 835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to automount partition/drive using wsl --mount after restart? #6073
Comments
WSL doesn't support that per-say, but you can achieve this behavior by creating an auto-start entry. For your scenario, something like:
Should do the trick. |
@OneBlue , this seams like a valid feature request. Having to modify the system registry seems like a hack. Having the ability for WSL2 to auto mount an ext4 volume would be very useful. |
I do agree that this feature would be nice to have, but for now running wsl --mount at startup is the simplest way to achieve this. This doesn't have to be done in the registry though, a startup app or a scheduled task would work as well. |
@OneBlue I feel like @wandersonca was attempting to subtly object to closing this issue. Can this be reopened to serve as a feature request (or maybe you have a link to where it's already being tracked)? |
Yup, not sure why this was closed. It's a valid feature request. Having users muck around in the registry to automount is not a good experience. |
The workaround proposed by @OneBlue doesn't work when restarting the WSL instance. Can we reopen this feature request? |
The reg hack workaround doesn't work because "Administrator access is needed to mount a disk". |
No, it does not work. But i find a workaround. Here's my trick.
|
Any updates on this ? |
+1 |
i'm very, very upset with being unable to do this properly |
Q: If I'm in a shell, and I do 'wsl --shutdown` - which one of these workaround options given above will re-mount when WSL is invoked again? A: None. Please re-open this and track as a feature request at least, or point us to the place it is being tracked and we'll pile on there. I have MANY ext4/etc. drives I need access to, and it's becoming imperative that they stay attached, or at the very least re-attach themselves like a modern OS knows how to do. If we can trick it via 'fstab' - I'm in .. I just need SOME kind of mechanism to ensure these drives stay mounted. Thanks. |
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.
Use
Something to note:
Note: only available in Windows 11
Inside #!/bin/bash
/bin/bash /wsl-boot.sh > /wsl-boot.log 2>&1
#!/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. |
Thanks a lot for the workaround.
|
I wish that this feature can be added natively. will make the life quite easy |
Although @charlie0129 method works, this really should be a feature of WSL. |
I agree. I've been using his method as well as creating a PowerShell script to start docker containers that depend on the drive being mounted as restart: unless-stopped starts before the drive is mounted. Below is what I've been using to start the docker containers after the drive is mounted as a scheduled task to start 1 minute after login. |
I have a oneline bat file that i use to detect my ocz trion drive and automount it: it uses awk and grep. i use chocolatey's awk and wimm's wit grep. just what I had onhand. mountsdd.bat
.:edit:.
|
@PaintOnBrush Thanks buddy, It's simple and neat. will try it
|
Hi @charlie0129 , I did all your steps (Windows 11) and I am getting issues, the log file indicates
I know the task work as I have run it from the TaskScheduler. I have follow exactly your steps. To test the start yo task I have restarted the wsl service Lxssmanager. Any input? Thanks. |
@charlie0129's solution won't work if systemd is enabled ( /etc/wsl.conf
mount-vhdx.service
/usr/local/sbin/mount-vhdx.sh#!/bin/bash
wsl='/mnt/c/Users/<username>/AppData/Local/Microsoft/WindowsApps/wsl.exe'
vhdx='<path to vhdx>'
export WSLENV="$WSLENV:WSL_UTF8/wp" WSL_UTF8=1
"$wsl" --mount --vhd "$vhdx" --bare > >(tr -d $'\r') 2> >(tr -d $'\r' >&2) |
Thank you for writing up these amazing workarounds! I discovered that for mounting a physical disk, it can be necessary to ask for elevated permissions. This can be done by changing @eternalphane's #!/bin/bash
powershell="/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"
"$powershell" \
Start-Process \
-Verb "RunAs" \
-FilePath "wsl.exe" \
-ArgumentList \"--mount\",\"--bare\",\"\\\\.\\PHYSICALDRIVE0\" \
-Wait |
I have no idea what caused the conflict. I have been using |
This works, but it makes the UAC appear. |
This method works but only when running as logged in user, which creates annoying terminal popups every time the task runs. If set to run when any user is logged in, it does nothing. No error returned on the wsl log nor the task scheduler. It just doesn't work. What gives? |
To mount a physical disk, simply create sh script with the code given by @LetrixZ and @eternalphane and add it to ~/.profile, that works for me
|
Thanks @charlie0129 for the nice workaround. Works great. Can use belowlogic to add wait for mount to appear.
|
Based on all of these excellent comments i ended up with the following for mounting bare-disks automatically Creating Task for mounting disk to wsl
Boot script in linuxConfigure mount point and disk partition.
Configure wsl.conf
This works flawlessly on my system. Creds goes to everyone above :) |
Thanks for the script version of creating task however I am getting "Access denied" when running task in mount-wsl-disks. This is due to "NT AUTHORITY\SYSTEM". Error goes away when using regular user name to create task. Another modification could be to replace onstart with 'once' else the task will trigger on every boot even before wsl is loaded. The start date can be any far future date. USER_NAME="username" |
I create a full guide on how to set this up with an encrypted disk: https://medium.com/@stefan.berkner/automatically-starting-an-external-encrypted-ssd-in-windows-subsystem-wsl-6403c34e9680 |
following on @stefan-berkner-tilotech's script based solution, i made a gist that integrates this with an openrc init. you can do something similar if you're running w/ systemd and adapt my openrc solution. https://gist.github.com/razamatan/dcdc7188b0bd643401adeeccbc8164c8 |
Wow, just passing by, but this thread right here is a great example of what's wrong with Tech.
This clearly needs to be a feature, it has been stated multiple times, and in spite of this extremely clear comment, someone proposed a (broken) workaround, and then closed the entire thread, never to be seen again. GG WP What's the excuse for not implementing this as a clean feature, instead making the community run around in circles for 3.5 years? Is it because Microsoft is a cash-strapped non-profit that's making WSL for the public good? Big thanks to everyone else here for their solutions and ideas. |
This here I used as a single solution. I'm ok with accepting UAC whenever wsl restarts. In my case, since I mounted the bare drive and then initialized it to gpt with fdisk and then formatted it to ext4, I had to provide some additional options. The simple path check was so that I wouldn't get duplicate prompts with every new session started (launching vscode multiple times). The path to check is the autogenerated one that wsl creates from the PhysicalDrive + number + partition. This won't work if you have things that require the volume sooner, but it gets the job done for me with minimal effort. I went the PhysicalDrive route even though this is a vhdx, because it stays mounted through restarts, though I suppose passing if [ ! -d "/mnt/wsl/PhysicalDrive12p1" ]; then
powershell="/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"
"$powershell" \
Start-Process \
-Verb "RunAs" \
-FilePath "wsl.exe" \
-ArgumentList \"--mount\",\"\\\\.\\PhysicalDrive12\",\"--partition\",\"1\",\"--type\",\"ext4\" \
-Wait
fi |
I use this as a workaround to mount two VHDX drives and it is working perfectly. If you do not have a NOPASSWD for your user in sudoers the WSL instance will ask for your password at startup since sudo commands is needed to make this work. Put this in your ~/.profile or ~/.bashrc or ~/.zshrc or what default shell you are using.
|
wsl.exe also takes a |
In my case, I chose¹ to use a separate VHD file just for the home folder. I felt I needed this after losing and having to recreate the entire environment when reinstalling a new WSL distribution. I mixed some really good solutions given here and I was able to use a mounted NTFS disk/partition² for my home. ¹ The fastest I/O option I could verify. ² I chose an NTFS partition to make it easier to transfer (often large numbers of) files to and from the WSL distribution mounting the VHD on Windows. wsl.conf[boot]
systemd=true
command="bash /boot.sh > /boot.log" /etc/fstab
/boot.sh#!/usr/bin/env bash
# General values
username=bernardo
distribution="Ubuntu-24.04"
vhd_file_path="C:/Users/$username/ubuntu-home.vhd"
# Timeout and interval in seconds
timeout=10
interval=1
# Drive to check and mount
target_drive="/dev/sdc1"
# Mount point
mount_point="/home/$username"
# Execute mount
/mnt/c/Users/$username/AppData/Local/Microsoft/WindowsApps/wsl.exe -d $distribution --mount --vhd $vhd_file_path --bare
# Initialize elapsed time counter
elapsed=0
# Wait for the target drive to be ready
while [ ! -e $target_drive ]; do
if [ $elapsed -ge $timeout ]; then
echo "Timed out waiting for $target_drive to be ready."
exit 1
fi
echo "Waiting for $target_drive to be ready..."
sleep $interval
elapsed=$((elapsed + interval))
done
# Mount the target drive
mount -o defaults,permissions $target_drive $mount_point
# Check the mounted target drive
mount -l | grep $target_drive |
Nice, I didn't know you could mount the user home directory like this. I always just back up the whole disk when i know the appx is going to change. It's To be clear, it's not a dig at ntfs, it's just the overhead of the translation the ntfs driver in wsl has to do. Mind you it was way way worse in WSL1, but it's still not native. |
Hello @SheepReaper! Yes, you are right about the lower performance of NTFS compared to ext4. The ways I checked performance:
If I have problems with NTFS or realize that I don't really need NTFS, I will definitely will use an ext4 partition. Thanks for sharing your experience! 🙏 |
Is your feature request related to a problem?
I am able to mount my ext4 partition in wsl-2 using the following command in powershell:
wsl --mount \.\PHYSICALDRIVE4 --partition 1
However when I either restart my computer or run wsl --restart the partition is unmounted and I have to run the above command again. Is there a way of automounting the partition and if not could this feature be added?
Describe the solution you'd like
Have a parameter where I can set automount after restart.
Describe alternatives you've considered
A powershell script that triggers when I start Windows.
The text was updated successfully, but these errors were encountered: