Win10 20H1 发布在即,来看看瞩目的 WSL2。

WSL2,基于 Hyper-V 轻量级虚拟机。

首先,WSL2 要求系统为 19041 或更高,并启用 VirtualMachinePlatform feature。 VirtualMachinePlatform feature 虽然不显式要求 Hyper-V 和 Intel VT-X,但涉及到 Hyper-V 还是打开为好。

然后,设定 WSL2 为默认,手动将之前的 WSL1 distro 升级到 WSL2。

wsl --set-default-version 2
wsl --list --verbose
wsl --set-version <distribution name> <versionNumber>

WSL2 文件

WSL2,使用 9p 挂载 win盘分区,WSL1 则使用 drvfs,都需要对挂载 metadata 做配置,以适应 fmask、umask、dmask 要求。

关于性能,WSL2 本地磁盘的性能会提高(虚拟机挂载 ext4.vhdx);9p 挂载的 win盘分区,和 drvfs 相比,还是要看之后的表现。

WSL2 内存

WSL2 运行一段时间可能会占用可观的内存,观察 vmmem 进程, 刚运行时,占用内存 300MB,使用一段时间,内存可能占用 2000MB 以上, 在退出 WSL2 一段时间后,vmmem 会分阶段退还内存,最后退出。

WSL2 在读写大量本地文件(/mnt/*)时内存可能暴涨,虽然据 build 19013 3 称, 内存在一段时间空闲后可以退还给宿主系统,但触发条件不清楚;可以关闭 WSL2 来强行释放。

内存可通过 .wslconfig 2 中的 memory 等相关参数控制。

WSL2 网络

WSL2 使用 Hyper-V Virtual Ethernet Adapter,在自己的子网内,拥有自己的 IP。

IPv6 尚未支持。

Npcap 0.9991 可以抓取 Hyper-V Virtual Ethernet Adapter.

宿主机 访问 WSL2

  • 18945 之前,不能使用 localhost 访问 WSL2 网络服务,必须使用 ip:port 访问,且每次重启,ip 和 gateway 会重新分配;
  • 18945 开始,可以使用 localhost 或 WSL2.ip 访问 WSL2 网络服务;
  • 从宿主机访问 WSL2 中监听 127.0.0.1:port 的服务,使用 localhost,或 在 WSL2 中将其他请求转发到 127.0.0.1;

WSL2 访问 宿主机

  • WSL2 内部不能使用 localhost 访问宿主机,宿主机 IP 可以从 /etc/resolv.conf 中的 nameserver 得到;
  • DNS 有时出问题,可能需要修改 nameserver,但重启试试也可能恢复正常;
cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }'

配置样例

%USERPROFILE%/.wslconfig

[wsl2]
kernel = <path>               # An absolute Windows path to a custom Linux kernel.
kernelCommandLine = <string>  # Additional kernel command line arguments
memory = <size>               # How much memory to assign to the WSL2 VM.
processors = <number>         # How many processors to assign to the WSL2 VM.
swap = <size>                 # How much swap space to add to the WSL2 VM. 0 for no swap file.
swapFile = <path>             # An absolute Windows path to the swap vhd.
localhostForwarding = <bool>  # Boolean specifying if ports bound to wildcard or localhost in the WSL2 VM should be connectable from the host via localhost:port (default true).
pageReporting = <bool>        # Enable or disable the free memory page reporting feature (default true).
idleThreshold = <integer>     # Set the idle threshold for memory compaction, 0 disables the feature (default 1).
nestedVirtualization = <bool> # >=20175, Enable nested virtualization for WSL2 (default true on amd64).
vmIdleTimeout = <time_in_ms>  # >=20190, Control the VM idle timeout.

# <path> entries must be absolute Windows paths with escaped backslashes, for example C:\\Users\\Ben\\kernel
# <size> entries must be size followed by unit, for example 8GB or 512MB

/etc/wsl.conf

[automount]
enabled = <boolean>             # true causes fixed drives (i.e C:/ or D:/) to be automatically mounted with DrvFs under /mnt. false means drives won't be mounted automatically, but you could still mount them manually or via fstab.
mountFsTab = <boolean>          # true sets /etc/fstab to be processed on WSL start. /etc/fstab is a file where you can declare other filesystems, like an SMB share. Thus, you can mount these filesystems automatically in WSL on start up.
root = <string>                 # Sets the directory where fixed drives will be automatically mounted. (default /mnt)
options = <string>              # This value is appended to the default DrvFs mount options string. Only DrvFs-specific options can be specified.
                                #   Options that the mount binary would normally parse into a flag are not supported. If you want to explicitly specify those options, you must include every drive for which you want to do so in /etc/fstab.
crossDistro = true              # Support Docker Desktop by creating cross distro mounts.

[network]
generateHosts = <boolean>       # true sets WSL to generate /etc/hosts. The hosts file contains a static map of hostnames corresponding IP address. (default true)
generateResolvConf = <boolean>  # true set WSL to generate /etc/resolv.conf. The resolv.conf contains a DNS list that are capable of resolving a given hostname to its IP address. (default true)

[interop]
enabled = <boolean>             # Setting this key will determine whether WSL will support launching Windows processes. (default true)
appendWindowsPath = <boolean>   # Setting this key will determine whether WSL will add Windows path elements to the $PATH environment variable. (default true)

[user]
default = <string>              # The initial username created on first run

Related Posts


Published

WSL

Category

windows

Tags

Contact