WSL学习笔记

WSL学习笔记

资料

  1. 手动下载适用于 Linux 的 Windows 子系统发行版包
  2. wsl2-backup-and-restore-images-using-import-and-export
  3. 解决删除文件后 WSL2 磁盘空间不释放的问题
  4. https://github.com/JetBrains/clion-wsl
  5. 使用WSL配置mmdetection环境

SSH

1
2
3
4
5
sudo sed -i '/Port /c Port 23' /etc/ssh/sshd_config
sudo sed -i '/ListenAddress 0.0.0.0/c ListenAddress 0.0.0.0' /etc/ssh/sshd_config
sudo sed -i '/PasswordAuthentication /c PasswordAuthentication yes' /etc/ssh/sshd_config
sudo sed -i '/PermitRootLogin /c PermitRootLogin yes' /etc/ssh/sshd_config
sudo service ssh restart

也可以使用Jetbrains的 https://github.com/JetBrains/clion-wsl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
set -e

SSHD_LISTEN_ADDRESS=127.0.0.1

SSHD_PORT=2222
SSHD_FILE=/etc/ssh/sshd_config
SUDOERS_FILE=/etc/sudoers

# 0. update package lists
sudo apt-get update

# 0.1. reinstall sshd (workaround for initial version of WSL)
sudo apt remove -y --purge openssh-server
sudo apt install -y openssh-server

# 0.2. install basic dependencies
sudo apt install -y cmake ninja-build gcc clang gdb valgrind build-essential

# 1.1. configure sshd
sudo cp $SSHD_FILE ${SSHD_FILE}.`date '+%Y-%m-%d_%H-%M-%S'`.back
sudo sed -i '/^Port/ d' $SSHD_FILE
sudo sed -i '/^ListenAddress/ d' $SSHD_FILE
sudo sed -i '/^UsePrivilegeSeparation/ d' $SSHD_FILE
sudo sed -i '/^PasswordAuthentication/ d' $SSHD_FILE
echo "# configured by CLion" | sudo tee -a $SSHD_FILE
echo "ListenAddress ${SSHD_LISTEN_ADDRESS}" | sudo tee -a $SSHD_FILE
echo "Port ${SSHD_PORT}" | sudo tee -a $SSHD_FILE
echo "UsePrivilegeSeparation no" | sudo tee -a $SSHD_FILE
echo "PasswordAuthentication yes" | sudo tee -a $SSHD_FILE
# 1.2. apply new settings
sudo service ssh --full-restart

# 2. autostart: run sshd
sed -i '/^sudo service ssh --full-restart/ d' ~/.bashrc
echo "%sudo ALL=(ALL) NOPASSWD: /usr/sbin/service ssh --full-restart" | sudo tee -a $SUDOERS_FILE
cat << 'EOF' >> ~/.bashrc
sshd_status=$(service ssh status)
if [[ $sshd_status = *"is not running"* ]]; then
sudo service ssh --full-restart
fi
EOF


# summary: SSHD config info
echo
echo "SSH server parameters ($SSHD_FILE):"
echo "ListenAddress ${SSHD_LISTEN_ADDRESS}"
echo "Port ${SSHD_PORT}"
echo "UsePrivilegeSeparation no"
echo "PasswordAuthentication yes"

32位支持

1
sudo dpkg --add-architecture i386

以下是wsl1,不是wsl2

Exec format error 32-bit executable Windows Subsystem for Linux?

Install qemu and binfmt config:

1
2
sudo apt install qemu-user-static
sudo update-binfmts --install i386 /usr/bin/qemu-i386-static --magic '\x7fELF\x01\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x03\x00\x01\x00\x00\x00' --mask '\xff\xff\xff\xff\xff\xff\xff\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xf8\xff\xff\xff\xff\xff\xff\xff'

You’ll need to reactivate binfmt support every time you start WSL:

1
sudo service binfmt-support start

Enable i386 architecture packages:

1
2
3
sudo dpkg --add-architecture i386
sudo apt update
sudo apt install gcc:i386

Try it out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ file /usr/bin/gcc-5
/usr/bin/gcc-5: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=2637bb7cb85f8f12b40f03cd015d404930c3c790, stripped

$ /usr/bin/gcc-5 --version
gcc-5 (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc helloworld.c -o helloworld

$ ./helloworld
Hello, world!

$ file helloworld
helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=3a0c7be5c6a8d45613e4ef2b7b3474df6224a5da, not stripped

And to prove it really was working, disable i386 support and try again:

1
2
3
4
5
$ sudo service binfmt-support stop
* Disabling additional executable binary formats binfmt-support [ OK ]

$ ./helloworld
-bash: ./helloworld: cannot execute binary file: Exec format error

windows terminal

  1. Window开发】设置windows terminal

  2. Windows Terminal 美化教程

  3. 主题预览 https://ohmyposh.dev/docs/themes

备份

1
2
3
4
5
6
7
8
9
10
wsl --shutdown
wsl --export Ubuntu-18.04 D:\Study\wsl\backup\ubuntu1820221221.tar

wsl --import Ubuntu-18.04 D:\Study\wsl\ D:\Study\wsl\backup\ubuntu1820221221.tar

###
diskpart
select vdisk file="C:\Users\PC-GWJ\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx"
compact vdisk
detach vdisk

问题

Failed to install linux distro with error 0x8007023e #5508

https://github.com/microsoft/WSL/issues/5508

https://github.com/microsoft/WSL/issues/5508#issuecomment-743733155

I was investigating why this failed and messed up with my note taking as I fixed it without having to do all of this, but this does seem to work.

Try this. It’s more in-depth …

  • Make sure Virtualisation is enabled in BIOS.
  • Make sure the Windows Image and the OS are healthy. Follow these instructions for Windows 10: https://docs.microsoft.com/en-us/troubleshoot/windows-server/deployment/fix-windows-update-errors
  • Make a note/backup of ALL Hyper-V settings!
  • Remove all Virtual machine features. This being: Hyper-V, containers, Virtual Machine Platform, Windows Hypervisor Platform, Windows Sandbox and Windows Subsystem for Linux.
  • Reboot the computer!
  • Follow the manual WSL installation instructions provided by Microsoft as shown here: http://aka.ms/wslinstall
  • Reboot the computer!
  • Make sure that your Linux Distro converts to WSL2 if you want your Distro to operate under a full Linux Kernel.
  • There is no harm in invoking Windows Update at this point. I recommend you do.
  • Install the Windows Hypervisor Platform. (The likes of VMware and Virtual Box need it.)
  • No harm in rebooting the computer at this point.
  • No harm in invoking Windows Update at this point.
  • If you use it, install the Windows Sandbox feature and test it.
  • Finally install Hyper-V and put back all your required settings.

At this point everything, for me, started working.

I have noticed that on the completion of installing any of the virtualisation features, when invoking Windows Update then KB4592438 gets reapplied. If you wish, and maybe recommended, invoke Windows Update when you install a Windows Feature. That’s what I did. Installed a feature, rebooted and invoked Window Update. (I think the only one where I didn’t invoke Windows Update is when I installed the Windows Hypervisor Platform.)

语言环境

参考ubuntu命令行下更改默认系统语言方法

1
2
3
4
5
6
7
8
9
10
#安装语言包
sudo apt install language-pack-zh-hans
sudo apt install fonts-droid-fallback
sudo apt install fonts-wqy-zenhei
sudo apt install fonts-wqy-microhei
sudo apt install fonts-arphic-ukai
sudo apt install fonts-arphic-uming
#安装local
sudo locale-gen zh_CN.UTF-8
sudo locale-gen en_US.UTF-8

修改/etc/default/locale

1
2
3
LANG="zh_CN.UTF-8"
LANGUAGE="zh_CN:zh"
LC_ALL="zh_CN.UTF-8"

修改/etc/locale.gen

找到 *zh_CN.UTF-8 UTF-8*取消注释,然后保存并退出

1
sudo update-locale LANG=zh_CN.UTF8

缩放

1
export GDK_DPI_SCALE=2

HiDPI Scaling

修改GPU渲染

GPU selection in WSLg

更改wsl默认登陆用户名

1
2
3
wsl --shutdown
wsl -l -v #查看虚拟机NAME
<wsl> config --default-user <username> #此处<wsl>填上一步要改的虚拟机的NAME

UI界面字体模糊

Blurry fonts on using Windows default scaling with WSL GUI applications (HiDPI)

GUI programs are blurry pixelated in WSL2

Fixing blurry fonts on WSL with X-server

Set dpi of wsl2 windows independently of screen resolution

~/.bashrc添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
wslg_dpi_scale() {
local dpi_scale WindowMetricsAppliedDPI
dpi_scale="${GDK_DPI_SCALE:-${QT_SCALE_FACTOR:-}}"
if [[ -z "${dpi_scale:-}" ]] ; then
WindowMetricsAppliedDPI=$("/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe" '$dpis = @{}; $dpis.add(-6, 100); $dpis.add(-5, 125); $dpis.add(-4, 150); $dpis.add(-3, 175); $dpis.add(-2, 200); $dpis.add(-1, 225); $dpis.add(0, 250); $dpis.add(1, 300); (((Get-ChildItem -Path '"'"'HKCU:\\Control Panel\\Desktop\\PerMonitorSettings\'"'"') | ForEach-Object {$dpis[($_.GetValue('"'"'DpiValue'"'"'))]} | Measure-Object -Maximum).Maximum)' | tr -d '\r\n')
dpi_scale=$(bc <<<"scale=2; $WindowMetricsAppliedDPI / 100")
fi

export GDK_DPI_SCALE=${GDK_DPI_SCALE:-$dpi_scale}
export GTK_SCALE=${GTK_SCALE:-$dpi_scale}

# https://doc.qt.io/qt-5/highdpi.html
# export QT_AUTO_SCREEN_SCALE_FACTOR=${QT_AUTO_SCREEN_SCALE_FACTOR:-1}
# export QT_ENABLE_HIGHDPI_SCALING=${QT_ENABLE_HIGHDPI_SCALING:-1}
export QT_SCALE_FACTOR=${QT_SCALE_FACTOR:-$GDK_DPI_SCALE}

# export MESA_D3D12_DEFAULT_ADAPTER_NAME="Intel(R) UHD Graphics 770"
}

[ -d /mnt/wslg/runtime-dir ] && wslg_dpi_scale

Linux

常用包

1
2
3
4
5
6
7
8
9
10
sudo apt install htop -y#任务管理器
sudo apt install neofetch -y#在终端中显示 Linux 系统信息
sudo apt install build-essential flex bison dwarves libssl-dev libelf-dev #编译依赖组件包
sudo apt install mesa-utils #glxinfo -B 查看显示OpenGL版本
sudo apt-get install libtinfo5 #vitis需要的库
sudo apt install xterm autoconf libtool texinfo gcc-multilib #petalinux需要的
#删除所有已下载的软件包的缓存文件
sudo apt-get clean
#想删除已安装软件包的缓存
sudo apt-get autoclean

WSL内核升级

资料

  1. 官方内核**WSL2-Linux-Kernel**

    • Install the build dependencies:
      $ sudo apt install build-essential flex bison dwarves libssl-dev libelf-dev

    • Build the kernel using the WSL2 kernel configuration:
      $ make KCONFIG_CONFIG=Microsoft/config-wsl

  2. 记录一下 WSL2 更换内核的过程

  3. 如何在 Windows Subsystem for Linux version 2 (WSL2) 上使用 Microsoft Linux 内核 v6

  4. 最省事的方法**WSL2-Linux-Kernel-Rolling**

    进入https://github.com/Nevuly/WSL2-Linux-Kernel-Rolling/releases下载编译好的内核,按下面的步骤配置.wslconfig文件即可

步骤(推荐)

  1. 按**WSL2-Linux-Kernel-Rolling**这个仓库编译或者使用已经编译好的镜像,按步骤(老方法)的第8条修改配置文件即可。

步骤(老方法)

  1. 使用neofetch查看当前系统如下
{% asset_img "image-20240130132455544.png" "image-20240130132455544" %}
  1. The Linux Kernel Archives 下载内核
1
2
3
4
5
6
7
mkdir ./Downloads
cd ~/Downloads
#curl -LO https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.7.2.tar.xz
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.7.2.tar.xz
#wget https://mirrors.edge.kernel.org//pub/linux/kernel/v6.x/linux-6.1.9.tar.gz
tar -zxvf ./linux-6.7.2.tar.gz
cd ./linux-6.7.2
  1. 安装编译依赖组件包
1
2
sudo apt install build-essential flex bison dwarves libssl-dev libelf-dev bc pkg-config libncurses-dev
# Go https://github.com/microsoft/WSL2-Linux-Kernel#build-instructions to see the latest build dependencies
  1. 使用 WSL2-Linux-Kernel 配置文件
这个 `.config` 文件针对 `wsl2` 做出了调整与优化。尽管直到我安装时,它的分支仍然是 `linux-msft-wsl-5.15.y`,也就是针对 `5.15` 内核的编译选项,但是仍然适用于我所安装的 `6.1.9` 版本内核。

1
wget https://raw.githubusercontent.com/microsoft/WSL2-Linux-Kernel/linux-msft-wsl-5.15.y/Microsoft/config-wsl >> .config
  1. 编译 在这里,请按照你自己的配置修改 jobs 的数量,有20线程的话就是-j20或者写-j$(nproc)
1
2
sudo make -j20 && sudo make modules -j20 && sudo make modules_install -j20 && sudo make install -j20
#make -j20 && make modules -j20 && make modules_install -j20 && make install -j20
  1. 在编译前,它会有很多配置项(即在 .config 内没有指定的新内核的配置项)要你指定,没有特殊需求的话一路回车即可。耐心等待安装完成后,可以在标准输出内找到类似字段。 Kernel: 后的路径即为内核路径。

  2. 将编译后其复制到你想要的位置,注意这个位置应该存在于你的主硬盘而不是 wsl 中

    1
    2
    cp arch/x86/boot/bzImage /mnt/e/wsl2/kernel/linux-6.7.2
    #cp arch/x86/boot/bzImage /mnt/e/wsl2/kernel/linux-6.1.9
  3. 打开你的用户目录,修改 .wslconfig 文件,在 [wsl2] 标签中添加 kernel 字段,它的值为带有转义反斜杠的 Windows 路径。注意现在window已经支持wsl.conf为每个linux虚拟机提供单独配置。注意需要提前关闭虚拟机wsl --shutdown

>   可以为已安装的 Linux 发行版配置设置,使它们在你每次启动 WSL 时自动应用,有两种方法:
>
>   -   **[.wslconfig](https://learn.microsoft.com/zh-cn/windows/wsl/wsl-config#wslconfig)** 用于在 WSL 2 上运行的所有已安装发行版中配置**全局设置**。
>   -   **[wsl.conf](https://learn.microsoft.com/zh-cn/windows/wsl/wsl-config#wslconf)** 用于为在 WSL 1 或 WSL 2 上运行的每个 Linux 发行版按各个发行版配置**本地设置**。

具体参阅[WSL 中的高级设置配置](https://learn.microsoft.com/zh-cn/windows/wsl/wsl-config)

例如,在我的案例中, `.wslconfig` 文件长这个样子。

1
2
[wsl2]
kernel=E:\\wsl2\\kernel\\linux-6.7.2
  1. 再次使用neofetch查看当前系统
{% asset_img "image-20240130154703109.png" "image-20240130154703109" %}

GUI

GWSL

作者

GWJ

发布于

2022-12-20

更新于

2024-02-05

许可协议

评论