WSL2使用技巧

WSL2常用的一些配置、使用技巧

WSL2使用技巧

0x00 常用配置

配置.wslconfig(比较常用)

.wslconfig 用于在 WSL 2 上运行的所有已安装发行版中配置全局设置

存放在目录 C:\Users[username] 下

示例文件

 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
# Settings apply across all Linux distros running on WSL 2
[wsl2]

# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB 

# Sets the VM to use two virtual processors
processors=2

# Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
kernel=C:\\temp\\myCustomKernel

# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
kernelCommandLine = vsyscall=emulate

# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB

# Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
swapfile=C:\\temp\\wsl-swap.vhdx

# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
pageReporting=false

# Turn on default connection to bind WSL 2 localhost to Windows localhost
localhostforwarding=true

# Disables nested virtualization
nestedVirtualization=false

# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
debugConsole=true

# Enable experimental features
[experimental]
sparseVhd=true

配置wsl.conf

wsl.conf 用于为在 WSL 1 或 WSL 2 上运行的每个 Linux 发行版按各个发行版配置本地设置

放在发行版的/etc/下

0x01 自编译内核

WSL2支持使用自编译的内核运行

  1. 下载Kernel → microsoft/WSL2-Linux-Kernel: The source for the Linux kernel used in Windows Subsystem for Linux 2 (WSL2) (github.com)

  2. 安装依赖

    $ sudo apt install build-essential flex bison dwarves libssl-dev libelf-dev

  3. 修改需要的配置

  4. 编译安装

0x02 使用USBIP连接USB设备

0x03 使用VHD解决卡顿问题

一些文档

  1. 创建VHD

    打开Windows磁盘管理工具。点击【操作】→ 【创建VHD】 → 选择自己需要的选项

    img

  2. 格式化磁盘为ext4

    • 在Windows使用

    • 在WSL2中使用mkfs.ext4

      sudo mkfs.ext4 /mnt/d/xxx/xxx.vhd

  3. 挂载磁盘

    • 直接挂载

      mkdir workspace

      sudo mount -t auto /mnt/d/workspace.vhd workspace

      sudo chmod 755 -R workspace

    • 使用wsl命令挂载

      wsl –mount –vhd xxx

    挂载脚本 自己切换

     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
    
    @echo off
    :: 检查管理员权限
    net session >nul 2>&1
    if %errorlevel% == 0 (
        echo Running with administrative privileges
    ) else (
        echo This script needs to be run as an Administrator
        exit
    )
    
    :: 挂载 VHD 并获取磁盘号
    for /f "tokens=1" %%i in ('Powershell -Command "Mount-VHD -Path <VHDPATH> -PassThru | Get-Disk | Select -ExpandProperty Number"') do set diskNumber=%%i
    
    :: 检查磁盘号是否设置
    if "%diskNumber%"=="" (
        echo Disk number not found.
        exit
    )
    
    echo Disk number is %diskNumber%
    
    ::  WSL 中挂载 VHD
    Powershell -Command "wsl --mount \\.\PhysicalDrive%diskNumber% --partition 1 --name vhd-data"
    echo VHD mounted at \\.\PhysicalDrive%diskNumber%
    
    echo Done
    

0x04 在WSL2上编译AOSP

0x05 使用WSLg图形界面

0x06 使用CAN/CANFD协议

  1. 下载内核:到https://github.com/microsoft/WSL2-Linux-Kernel下载需要的内核版本

  2. 安装编译内核需要的依赖

    sudo apt install build-essential flex bison dwarves libssl-dev libelf-dev ncurses-dev

  3. 配置内核

    cp Microsoft/config-wsl .config

    make menuconfig → Networking support—>CAN BUS subsystem support 开启相关模块

    对于SLCAN支持等依然需要自己手动修改.config文件开启

    也可以自己手动修改.config文件 为m或者y

     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
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    
    # 编译时添加到config-wsl文件中的配置指令
    CONFIG_CC_CAN_LINK=y
    CONFIG_CC_CAN_LINK_STATIC=y
    
    # CONFIG_HAMRADIO is not set
    CONFIG_CAN=m
    CONFIG_CAN_RAW=m
    CONFIG_CAN_BCM=m
    CONFIG_CAN_GW=m
    CONFIG_CAN_J1939=m
    CONFIG_CAN_ISOTP=m
    
    #
    # CAN Device Drivers
    #
    CONFIG_CAN_VCAN=m
    CONFIG_CAN_VXCAN=m
    CONFIG_CAN_SLCAN=m
    CONFIG_CAN_DEV=m
    CONFIG_CAN_CALC_BITTIMING=y
    CONFIG_CAN_KVASER_PCIEFD=m
    CONFIG_CAN_C_CAN=m
    # CONFIG_CAN_C_CAN_PLATFORM is not set
    # CONFIG_CAN_C_CAN_PCI is not set
    CONFIG_CAN_CC770=m
    # CONFIG_CAN_CC770_ISA is not set
    # CONFIG_CAN_CC770_PLATFORM is not set
    CONFIG_CAN_IFI_CANFD=m
    CONFIG_CAN_M_CAN=m
    # CONFIG_CAN_M_CAN_PCI is not set
    # CONFIG_CAN_M_CAN_PLATFORM is not set
    CONFIG_CAN_PEAK_PCIEFD=m
    CONFIG_CAN_SJA1000=m
    # CONFIG_CAN_EMS_PCI is not set
    # CONFIG_CAN_EMS_PCMCIA is not set
    # CONFIG_CAN_F81601 is not set
    # CONFIG_CAN_KVASER_PCI is not set
    # CONFIG_CAN_PEAK_PCI is not set
    # CONFIG_CAN_PEAK_PCMCIA is not set
    # CONFIG_CAN_PLX_PCI is not set
    # CONFIG_CAN_SJA1000_ISA is not set
    # CONFIG_CAN_SJA1000_PLATFORM is not set
    CONFIG_CAN_SOFTING=m
    CONFIG_CAN_SOFTING_CS=m
    
    #
    # CAN USB interfaces
    #
    CONFIG_CAN_8DEV_USB=m
    CONFIG_CAN_EMS_USB=m
    CONFIG_CAN_ESD_USB2=m
    CONFIG_CAN_ETAS_ES58X=m
    CONFIG_CAN_GS_USB=m
    CONFIG_CAN_KVASER_USB=m
    CONFIG_CAN_MCBA_USB=m
    CONFIG_CAN_PEAK_USB=m
    CONFIG_CAN_UCAN=m
    # end of CAN USB interfaces
    
    CONFIG_CAN_DEBUG_DEVICES=y
    # end of CAN Device Drivers
    
  4. 编译安装内核

    make -j8

    sudo make modules_install

    sudo make install

    cp arch/x86/boot/bzImage /mnt/c/Users/<User>/

    /mnt/c/Users/<User>/下创建.wslconfig文件写入以下内容:

    1
    2
    
    [wsl2]
    kernel=C:\\Users\\[username]\\bzImage
    
  5. 测试安装

    重启wsl2 → wsl --shutdown [Ubuntu] []中是虚拟机系统 可能是kali之类的

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    # 安装can utils 工具
    sudo apt install can-utils
    
    # 使能并创建vcan0
    sudo modprobe can
    sudo modprobe can_raw
    sudo modprobe vcan 
    sudo ip link add dev vcan0 type vcan
    sudo ip link set vcan0 up
    
    # 检查vcan0是否存在,应该能看到vcan0已经存在
    ifconfig
    
    # 在一个窗口向vcan0发送随机数据
    cangen vcan0
    
    # 在另一个窗口查看vcan0是否接收到数据
    candump vcan0
    
    ###能看到数据即安装正常
    

0x07 WSL常用命令

1
2
3
4
5
6
7
8
~ > wsl --version                                                                                 01/11/2024 01:45:41 PM
WSL 版本: 2.0.9.0
内核版本: 5.15.133.1-1
WSLg 版本: 1.0.59
MSRDC 版本: 1.2.4677
Direct3D 版本: 1.611.1-81528511
DXCore 版本: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows 版本: 10.0.19045.2965
  • 使用root作为默认用户

    wsl —

  • 升级WSL2/降级为WSL

  • WSL2备份/恢复

    导出:wsl -export <Distro> <FileName> [选项] → wsl -export Ubuntu D:/bak/wslUbuntu.tar

      选项为 -vhd  可以将指定应将分发版导出为 .vhdx 文件
    

    导入:wsl -import <Distro> <InstallLocation> <FileName>wsl -import Ubuntu D:/wsl2/ubuntu D:/bak/wslUbuntu.tar

0x08 WSL2上使用CUDA

Licensed under CC BY-NC-SA 4.0
最后更新于 2024-08-13 16:06 UTC
(*╹▽╹*)
Built with Hugo
主题 StackJimmy 设计