WSL Linux 的 Windows 子系统[笔记]

WSL概要

适用于 Linux 的 Windows 子系统【WSL】可让开发人员按原样运行 GNU/Linux 环境 - 包括大多数命令行工具、实用工具和应用程序 - 且不会产生传统虚拟机或双启动设置开销。

WSL1架构

This layer implements Linux syscalls on top of the Windows kernel and is what enables Linux binaries to run without modifcation; when a Linux binary runs and makes syscalls, it is the WSL translation layer that it is invoking and that makes the conversion into calls to the Windows kernel.

image

WSL2架构

Te big differences come with the use of what the documentation refers to as a Lightweight utility virtual machine (see https://docs.microsoft.com/en-us/windows/wsl/wsl2-about ). This virtual machine has a rapid startup that only consumes a small amount of memory. As you run processes that require memory, the virtual machine dynamically grows its memory usage. Better still, as that memory is freed within the virtual machine, it is returned to the host!

image

最新版本的 WSL 使用 Hyper-V 体系结构来实现其虚拟化。 此体系结构将在“虚拟机平台”可选组件中提供。

 

LINUX内核开源地址https://github.com/microsoft/WSL2-Linux-Kernel

安装

Pre条件

To run WSL 2, you need to be on version 1903 or higher with OS build 18362 or higher.【winver可查看】

  • 方式1:
    • wsl. exe --install
    • 有的还不支持
  • 方式2:
    • dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
    • dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Linux Distribution

http://aka.ms/wsl2kernel 分发版的列表选择安装

或者在Microsof Store选择安装

配置文件

  • wsl.conf:每个linux运行环境的配置,which provides a per-distro confguration
    • wsl. conf fle is located in the /etc/wsl.conf fle in each distro
  • . wslconfig :全局的配置,which provides global confguration options.

wsl.conf

[automount]

enabled = true # control host drive mounting (e. g. /mnt/c)

mountFsTab = true # process /etc/fstab for additional mounts

root = /mnt/ # control where drives are mounted

[interop]

enabled = true # allow WSl to launch Windows processes

appendWindowsPath = true # add Windows PATH to $PATH in WSL

[network]

generateHosts = true

generateResolvConf = true

[user]

default=stuart

 

详细参考地址

https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-per-distro-launch-settings-with-wslconf

. wslconfig

C: \Users\<your username>\. wslconfig

[wsl2]

memory=4GB

processors=2

localhostForwarding=true

swap=6GB

swapFile=D: \\Temp\\WslSwap. Vhdx

 

详细参考地址

https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-global-options-with-wslconfig

 

参考

https://docs.microsoft.com/zh-cn/windows/wsl/install-manual 命令行安装

https://docs.microsoft.com/zh-cn/windows/wsl/wsl2-faq 常见问题

 

https://docs.microsoft.com/en-us/windows/wsl/wsl-config

 

https://hub.fastgit.org/PacktPublishing/Windows-Subsystem-for-Linux-2-WSL-2-Tips-Tricks-and-Techniques/tree/main/chapter-10

Windows LINUX互操作性

Windows查看Linux的文件内容

资源管理器中输入这个可以直接查看运行的linux实例的内容 \\wsl$\<distroname>

 

管道互操作性例子

PS C: \> Get-Childitem $env: SystemRoot | ForEach-Object {$_. Name. Substring(0, 1) . ToUpper() } | wsl bash -c "sort | uniq -c"

PS C: \> wsl bash -c "ls /usr/bin | cut -c1-1" | Group-Object

 

Linux下

默认挂载/mnt/,如c盘/mnt/c

cat /mnt/c/example. Txt

 

Windows的PATH变量在linux可见

$ powershell. exe -C "Get-Content . /wsl. ps1" | wc -l

echo $PWD > clip.exe

posted @ 2021-05-05 17:35  2012  阅读(466)  评论(0编辑  收藏  举报