如何在启动时自动在 WSL 中挂载 Windows 个文件夹

How to mount Windows folders in WSL automatically on startup

当我进入 WSL 时,C 驱动器自动安装在 /mnt/c。我还想将文件夹 C:\D 挂载到挂载点 /mnt/d.

/etc/fstab的内容:

LABEL=cloudimg-rootfs   /        ext4   defaults        0 0
/mnt/c/D        /mnt/d  none    bind

/etc/wsl.conf的内容:

# Enable extra metadata options by default
[automount]
enabled = true
root = /mnt/
options = "metadata,umask=22,fmask=11"
mountFsTab = true

# Enable DNS – even though these are turned on by default, we’ll specify here just to be explicit.
[network]
generateHosts = true
generateResolvConf = true

当我执行 sudo mount -a 时,它会正确安装。但是它没有在启动时安装并且 运行 mount -a 报告 "mount: only root can use "--all" option".

这个问题很旧,但如果有人仍然回答这个问题,我在 WSL release notes

中找到了答案

WSL now processes the /etc/fstab file during instance start [GH 2636]. This is done prior to automatically mounting DrvFs drives; any drives that were already mounted by fstab will not be remounted automatically, allowing you to change the mount point for specific drives.

因此在绑定挂载之前必须为 windows 驱动器添加挂载: 例如:

# <file system> <dir> <type> <options> <dump> <pass>
C: /mnt/c drvfs rw,noatime,uid=1000,gid=1000,case=off,umask=0027,fmask=0137, 0 0
/mnt/c/directory/for/mount /where/to/mount none bind,default 0 0