WSL icon indicating copy to clipboard operation
WSL copied to clipboard

/etc/fstab not automounted when WSL2 used from Windows Task Scheduler

Open diablodale opened this issue 6 years ago • 13 comments

/etc/fstab is not automounted when a WSL2 Ubuntu distro is used from Windows Task Scheduler via wsl.exe.

This issue does not reproduce in WSL1 Ubuntu. I discovered it during my WSL1->2 migration. It reminds me of the old WSL1 issue https://github.com/microsoft/WSL/issues/1602 Perhaps related to https://github.com/microsoft/WSL/issues/4226

Environment

Windows build number: Microsoft Windows [Version 10.0.19042.541]
Your Distribution version: Ubuntu 20.04.1 LTS on WSL2
Whether the issue is on WSL 2 and/or WSL 1: Linux version 4.19.128-microsoft-standard (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Tue Jun 23 12:58:10 UTC 2020

Steps to reproduce

  1. Install Windows 10, WSL2, and Ubuntu 20.04 distribution on WSL2.
  2. Note your username when you create your first and primary Ubuntu user on install. You must replace dale username in below steps with your own username.
  3. apt update and apt upgrade
  4. Create /etc/wsl.conf to have content of
    [automount]
    options = "fmask=0027,dmask=0027,metadata"
    
  5. Edit /etc/fstab to have 3 additional lines
    1. C: /mnt/c drvfs rw,noatime,uid=1000,gid=1000,fmask=0027,dmask=0027,metadata 0 0
    2. The second line should be a bind mount which binds a folder on your Windows C: drive (which was mounted on the above line) to your Linux home directory. Remember to change usernames! I chose to do with with my .ssh directory. Therefore, I first mkdir /home/dale/.ssh and then added the line /mnt/c/Users/Dale/.ssh /home/dale/.ssh none bind 0 0
    3. The third line should mount a network share, For example //home-nas/dale /mnt/n drvfs rw,noatime,uid=1000,gid=1000,fmask=0027,dmask=0027,_netdev,nofail 0 0
  6. Exit your WSL session(s).
  7. Open a cmd prompt and run wsl --shutdown
  8. Open a WSL2 Ubuntu 20.04 prompt
  9. Verify your 3 mounts from /etc/fstab are successful; C drive, your bind mount, and your network share.
  10. Create a file test.sh in your Linux home directory, e.g. /home/dale/test.sh, with the below content. Remember to change username and use the directory you bind mounted in step 5.ii above.
    #!/usr/bin/env bash
    LOGFILE="/home/dale/testout.log"
    whoami &> $LOGFILE
    ls -la $HOME &>> $LOGFILE
    ls -la ~/.ssh &>> $LOGFILE
    mount &>> $LOGFILE
    
  11. chmod 755 test.sh
  12. Run Windows Task Scheduler
  13. On the right, click "Create Task..."
  14. Name wsltest1
  15. Choose 2nd radio Run whether user is logged on or not
  16. Click "Configure for:" pulldown and choose Windows 10
  17. Click top tab Actions
  18. Click bottom button New...
  19. Keep the default Start a program at the top
  20. Under program/script type C:\Windows\System32\wsl.exe
  21. To the right of Add arguments type bash -lc "/home/dale/test.sh"
  22. Click OK
  23. Click top tab Settings
  24. Verify/enable Allow task to be run on demand
  25. Click bottom right "OK"
  26. Enter your password as prompted
  27. Right-click on your newly created task "wsltest1" and choose Run
  28. Wait 2 seconds
  29. Inspect the output at /home/dale/testout.log

Actual Behavior

mounts from /etc/fstab did not successfully occur. The testout.log output will show in sequence

  • your username
  • your home directory listing. However, notice that the bind mount for /home/dale/.ssh is not corrected listed. Instead, you only see the /home/dale/.ssh directory that you created. This is easily seen by looking at the datetime and in the following...
  • the .ssh directory shows only the . and .. entries. It does not list the files that should be from /mnt/c/Users/Dale/.ssh due to your bind mount
  • a long list of the mounts, however, there is no entry for your bind mount or network share mount

Expected Behavior

All mounts from /etc/fstab to successfully occur. The testout.log should show the correct contents of your bind mount, and all three mounts should appear in the list of mounts.

WSL logs

Available at https://aka.ms/AA9ubkh

diablodale avatar Sep 30 '20 12:09 diablodale

I didn't find something interesting in the logs, but I guess:

  1. wsl distros are user specific, so maybe run only when user is logged on is supported.
  2. mount file system may take time, I would suggest you mount >> $LOGFILE 2>&1 other than mount &>> $LOGFILE.
  3. It's also possible you need to execute mount first, then do ls in test.sh

licanhua avatar Sep 30 '20 16:09 licanhua

It is unclear to be what your 1-3 comments are. If you are suggesting workarounds for the bug/regression, then that is not needed. I already have a workaround for the actual scenario on my machines. The issue report I provide above is a short, clear, and isolated scenario so you can reproduce the WSL2 regression/bug.

  1. Thanks for the comment. Please follow my repro steps to reproduce the regression on your test machines.
  2. a. All executables take CPU time. And a bind mount should take nanoseconds. What are you asking or suggesting? b. &>> works fine in bash as it is a deprecated feature not eliminated. It correctly concats stdout/err. No action needed here.
  3. That is not needed. My repro steps should be followed as written so you can reproduce the regression/bug.

If you have questions of me, please do ask them and I'll endeavor to get answers.

diablodale avatar Sep 30 '20 18:09 diablodale

I can also confirm that /etc/fstab is ignored with the following configuration:

cat /etc/wsl.conf

[automount] enabled = true options = metadata,umask=22,fmask=11 mountFsTab = true

Konstantin-Glukhov avatar Dec 20 '20 08:12 Konstantin-Glukhov

Is there any update on this? @diablodale what is your workaround?

err-unknown-username avatar Oct 15 '21 23:10 err-unknown-username

Last time I tried, still doesn't work as expected. As my current workaround, I have a task in task scheduler that runs at logon of my user, and that task does wsl.exe bash -lc exit. Doing it at this time (when I am also interactively logged in). In this scenario, the mounts occur.

So on my computer, I have to always be interactively logged in, therefore I use a short screen lock and Windowskey-L now by habit.

diablodale avatar Oct 16 '21 12:10 diablodale

Try add to /etc/wsl.conf

[boot]
command="mount -a"

or in boot script on windows:

wsl.exe -u root -e mount -u

malumar avatar Apr 26 '22 11:04 malumar

/etc/wsl.conf
[boot]
command="mount -a"

This has no effect. Automount on boot still does not happen.

Konstantin-Glukhov avatar Oct 02 '22 04:10 Konstantin-Glukhov

see #6073

ghost avatar May 06 '23 02:05 ghost

Any update on this problem. Windows 11 still has the problem. It looks like the system ignores the file. Even if I change it for e.g swappiness or swapon... It cannot make it permanent. I made the change in the file, but everytime i reboot It does not read that change. Those files does not make sense in wsl booting.

funnybutnice avatar Jul 20 '23 02:07 funnybutnice

It seems like /etc/fstab works fine with the following configuration:

WSL version: 1.2.5.0 Kernel version: 5.15.90.1 Windows version: 10.0.22000.2176

/etc/wsl.conf:


[automount]
# Automatically mount Windows drive when the distribution is launched plus /etc/fstab
enabled = false
# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
# Redundant if enabled = true
mountFsTab = true

/etc/fstab:

# Mount a network drive
//JoeHost/E$ /mnt/re cifs uid=joe,gid=joe,user=joe,pass=shmo 0 0
# Mount local E:\ drive
drvfs /mnt/e 9p rw,noatime,dirsync,aname=drvfs;path=E:\;uid=1000;gid=1000;symlinkroot=/mnt/,mmap,access=client,msize=262144,trans=virtio 0 0

Konstantin-Glukhov avatar Jul 20 '23 08:07 Konstantin-Glukhov

wsl.conf is ignored when wsl2 is booted by task scheduler in condition "Run whether user is logged on or not". Is there any solution now? 😭

optimozhu avatar Dec 27 '23 10:12 optimozhu

If you have systemd enabled with WSL 2 on Windows 10, there's no need to enable automount, as systemd will parse /etc/fstab and generate the corresponding mount units.

Here follows my current settings.

/etc/wsl.conf

[boot]
systemd=true

[automount]
enabled = false
mountFsTab = false

/etc/fstab

drvfs /mnt/c 9p rw,noatime,dirsync,aname=drvfs;path=C:\;uid=1000;gid=1000;symlinkroot=/mnt/,mmap,access=client,msize=262144,trans=virtio 0 0
drvfs /mnt/debian 9p ro,aname=drvfs;path=D:\,mmap,access=client,trans=virtio 0 0
$ uname -r -m
5.15.133.1-microsoft-standard-WSL2 x86_64

$ sudo systemctl list-units --type mount mnt-*
  UNIT                        LOAD   ACTIVE SUB     DESCRIPTION
  mnt-c.mount                 loaded active mounted /mnt/c
  mnt-debian.mount            loaded active mounted /mnt/debian
  mnt-wsl.mount               loaded active mounted /mnt/wsl
  mnt-wslg-distro.mount       loaded active mounted /mnt/wslg/distro
  mnt-wslg-doc.mount          loaded active mounted /mnt/wslg/doc
  mnt-wslg-versions.txt.mount loaded active mounted /mnt/wslg/versions.txt
  mnt-wslg.mount              loaded active mounted /mnt/wslg

amartinr avatar Jan 25 '24 09:01 amartinr

This issue has been automatically closed since it has not had any activity for the past year. If you're still experiencing this issue please re-file this as a new issue or feature request.

Thank you!