
Many Microsoft employees have contributed to the Windows Subsystem for Linux, this commit is the result of their work since 2016. The entire history of the Windows Subsystem for Linux can't be shared here, but here's an overview of WSL's history after it moved to it own repository in 2021: Number of commits on the main branch: 2930 Number of contributors: 31 Head over https://github.com/microsoft/WSL/releases for a more detailed history of the features added to WSL since 2021.
2.6 KiB
Accessing Windows drives from Linux
WSL offers mountpoints to access Windows drives from Linux. These mountpoints are mounted under /mnt
by default, and point to the root of Windows drives.
Elevated vs non-elevated mountpoints
Within a distribution, WSL separates between Linux processes that have been created from an elevated (as in administrator level) and from a non-elevated (user level) context.
This is done by having two separate mount namespaces within the distribution. One of them offers an elevated access to Windows drives, and the other offers a non-elevated access to Windows drives.
When a Linux process is created, wslservice.exe determines its elevation status, and then tell init to create the process in the appropriate mount namespace.
Mounting a Windows drive
*Note: This section only applies to WSL2 distributions. *
When a session leader is created, wslsevice.exe starts a plan9 file server. This file server can be connected to from the WSL2 virtual machine to mount Windows drives.
When the WSL distribution is created, wslservice.exe uses the LX_INIT_CONFIGURATION_INFORMATION
message to indicate wether the process that created the distribution is elevated or not. Based on this, init will mount either or the elevated, or un-elevated version of the plan9 server.
Later when the first command is created in the namespace that hasn't been mounted yet, (either elevated, or non-elevated), wslservice.exe sends a LxInitMessageRemountDrvfs
to init, which tell init
to mount the other namespace.
See: src/windows/service/exe/WslCoreInstance.cpp
and src/linux/drvfs.cpp
.
Mounting a drive from Linux
As long as the Windows plan9 server is running, drives can be mounted simply by calling mount. For instance mounting the C: drive manually can be done via:
mount -t drvfs C: /tmp/my-mount-point
Internally, this handled by /usr/sbin/mount.drvfs
, which is a symlink to /init
. When /init
starts, it looks at argv[0]
to determine which entrypoint to run. If argv[0]
is mount.drvfs
, then /init
runs the mount.drvfs
entrypoint (see MountDrvfsEntry()
in src/linux/init/drvfs.cpp
).
Depending on the distribution configuration, mount.drvfs
will either mount the drive as drvfs
(WSL1), or plan9
, virtio-plan9
or virtiofs
(WSL), depending on .wslconfig.