microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.51k stars 822 forks source link

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

Open diablodale opened 4 years ago

diablodale commented 4 years ago

/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..."
    1. Name wsltest1
    2. Choose 2nd radio Run whether user is logged on or not
    3. Click "Configure for:" pulldown and choose Windows 10
    4. Click top tab Actions
    5. Click bottom button New...
    6. Keep the default Start a program at the top
    7. Under program/script type C:\Windows\System32\wsl.exe
    8. To the right of Add arguments type bash -lc "/home/dale/test.sh"
    9. Click OK
    10. Click top tab Settings
    11. Verify/enable Allow task to be run on demand
    12. Click bottom right "OK"
    13. Enter your password as prompted
  14. Right-click on your newly created task "wsltest1" and choose Run
  15. Wait 2 seconds
  16. 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

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

licanhua commented 4 years ago

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
diablodale commented 4 years ago

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.

Konstantin-Glukhov commented 3 years ago

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

err-unknown-username commented 3 years ago

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

diablodale commented 3 years ago

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.

malumar commented 2 years ago

Try add to /etc/wsl.conf

[boot]
command="mount -a"

or in boot script on windows:

wsl.exe -u root -e mount -u

Konstantin-Glukhov commented 2 years ago
/etc/wsl.conf
[boot]
command="mount -a"

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

ghost commented 1 year ago

see #6073

funnybutnice commented 1 year ago

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.

Konstantin-Glukhov commented 1 year ago

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
optimodzhu commented 11 months ago

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? 😭

amartinr commented 10 months ago

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