WSL编译kernel

1)下载kernel

https://github.com/microsoft/WSL2-Linux-Kernel/

$ git clone https://github.com/microsoft/WSL2-Linux-Kernel.git

Cloning into 'WSL2-Linux-Kernel'...
remote: Enumerating objects: 11190886, done.
remote: Total 11190886 (delta 0), reused 0 (delta 0), pack-reused 11190886 (from 1)
Receiving objects: 100% (11190886/11190886), 2.41 GiB | 1.46 MiB/s, done.
Resolving deltas: 100% (9467086/9467086), done.
Updating files: 100% (81819/81819), done.

文件比较多,可能需要下载很久

2)配置内核

终端到kernel目录下,运行

$ make menuconfig KCONFIG_CONFIG=Microsoft/config-wsl

选择Device Drivers-->Input device support-->Joystick interface

选择Device Drivers-->Input device support-->Joystick/Gamepads-->Xbox Gamepad support

然后退出到最上一层菜单,选择Save,可以保存一个单独的配置名称,比如:Microsoft/config-wsl-joystick

然后退出menuconfig

3)编译内核

$ make -j$(nproc) bzImage KCONFIG_CONFIG=Microsoft/config-wsl-joystick

等待编译完成,如果提示缺少工具,就apt install。

本人遇到的:

$ sudo apt install flex

$ sudo apt install binson

$ sudo apt install pahole

典型的log:

DESCEND objtool
DESCEND bpf/resolve_btfids
INSTALL libsubcmd_headers
INSTALL libsubcmd_headers
CALL scripts/checksyscalls.sh
UPD include/generated/utsversion.h
CC init/version-timestamp.o
LD .tmp_vmlinux.btf
BTF .btf.vmlinux.bin.o
LD .tmp_vmlinux.kallsyms1
NM .tmp_vmlinux.kallsyms1.syms
KSYMS .tmp_vmlinux.kallsyms1.S
AS .tmp_vmlinux.kallsyms1.S
LD .tmp_vmlinux.kallsyms2
NM .tmp_vmlinux.kallsyms2.syms
KSYMS .tmp_vmlinux.kallsyms2.S
AS .tmp_vmlinux.kallsyms2.S
LD vmlinux
BTFIDS vmlinux
NM System.map
SORTTAB vmlinux
RELOCS arch/x86/boot/compressed/vmlinux.relocs
RSTRIP vmlinux
CC arch/x86/boot/a20.o
AS arch/x86/boot/bioscall.o
CC arch/x86/boot/cmdline.o
AS arch/x86/boot/copy.o
HOSTCC arch/x86/boot/mkcpustr
CC arch/x86/boot/cpuflags.o
CC arch/x86/boot/cpucheck.o
CC arch/x86/boot/early_serial_console.o
CC arch/x86/boot/edd.o
CC arch/x86/boot/main.o
CC arch/x86/boot/memory.o
LDS arch/x86/boot/compressed/vmlinux.lds
AS arch/x86/boot/compressed/kernel_info.o
CC arch/x86/boot/pm.o
AS arch/x86/boot/compressed/head_64.o
VOFFSET arch/x86/boot/compressed/../voffset.h
CC arch/x86/boot/compressed/string.o
AS arch/x86/boot/pmjump.o
CC arch/x86/boot/printf.o
CC arch/x86/boot/regs.o
CC arch/x86/boot/compressed/cmdline.o
CC arch/x86/boot/string.o
CC arch/x86/boot/tty.o
CC arch/x86/boot/video.o
CC arch/x86/boot/compressed/error.o
CC arch/x86/boot/video-mode.o
CC arch/x86/boot/version.o
CC arch/x86/boot/video-vga.o
CC arch/x86/boot/video-vesa.o
OBJCOPY arch/x86/boot/compressed/vmlinux.bin
CC arch/x86/boot/video-bios.o
HOSTCC arch/x86/boot/tools/build
CPUSTR arch/x86/boot/cpustr.h
HOSTCC arch/x86/boot/compressed/mkpiggy
CC arch/x86/boot/cpu.o
CC arch/x86/boot/compressed/cpuflags.o
CC arch/x86/boot/compressed/early_serial_console.o
CC arch/x86/boot/compressed/kaslr.o
CC arch/x86/boot/compressed/idt_64.o
CC arch/x86/boot/compressed/ident_map_64.o
AS arch/x86/boot/compressed/idt_handlers_64.o
AS arch/x86/boot/compressed/mem_encrypt.o
CC arch/x86/boot/compressed/pgtable_64.o
CC arch/x86/boot/compressed/sev.o
CC arch/x86/boot/compressed/acpi.o
CC arch/x86/boot/compressed/mem.o
CC arch/x86/boot/compressed/efi.o
CC arch/x86/boot/compressed/misc.o
GZIP arch/x86/boot/compressed/vmlinux.bin.gz
MKPIGGY arch/x86/boot/compressed/piggy.S
AS arch/x86/boot/compressed/piggy.o
LD arch/x86/boot/compressed/vmlinux
ZOFFSET arch/x86/boot/zoffset.h
OBJCOPY arch/x86/boot/vmlinux.bin
AS arch/x86/boot/header.o
LD arch/x86/boot/setup.elf
OBJCOPY arch/x86/boot/setup.bin
BUILD arch/x86/boot/bzImage
Kernel: arch/x86/boot/bzImage is ready (#2)

编译驱动:

$ make -j$(nproc) modules KCONFIG_CONFIG=Microsoft/config-wsl-joystick

$ sudo make -j$(nproc) modules_install KCONFIG_CONFIG=Microsoft/config-wsl-joystick

 

4)应用内核

编译出来的bzImage,复制到Windows的一个目录中,

$ cp arch/x86/boot/bzImage /mnt/d/vm/wsl/Kernel-Images/bzImage-6.6-joystick

然后编辑.wslconfig文件(一般都是在C:\Users\username目录下)

[wsl2]
kernel=D:\\VM\\WSL\\Kernel-Images\\bzImage-6.6-joystick

重启wsl,如果内核选项,关于vhci_hcd的部分没有加入内核(预览版kernel一般都默认是编译成模块(M)可能是为了提高编译速度吧),因此需要用下面的命令加载vhci_hcd的驱动。

$ sudo modprobe vhci_hcd

如果每次都运行可以放入.bashrc,或者重新编译,将选项选为编入内核。

5)测试joystick

WSL2要在Linux内访问Windows的USB设备,推荐使用usbidp软件。

dorssel/usbipd-win: Windows software for sharing locally connected USB devices to other machines, including Hyper-V guests and WSL 2.

可以直接下载msi安装,usbipd 4.3.0 下载地址

安装后,可以在windown 管理员终端运行以下命令设置

> usbipd list

Connected:
BUSID VID:PID DEVICE STATE
2-1 04f2:b73b HP HD Camera, HP IR Camera, Camera DFU Device Not shared
2-6 045e:028e 支持 Windows 的 XBOX 360 手柄 Shared (forced)
2-7 04f3:0c7e ELAN WBF Fingerprint Sensor Not shared
2-10 8087:0033 英特尔(R) 无线 Bluetooth(R) Not shared

Persisted:
GUID DEVICE
1140268c-65fe-4b4e-82d3-6c5fab78d32c 支持 Windows 的 XBOX 360 手柄
f4e49be7-17f7-4826-9853-47ad7aa5ee67 支持 Windows 的 XBOX 360 手柄

> usbipd bind --force --busid 2-6

WIN11好像有后台服务占用joystick,所以用了force,可能需要重启电脑


> usbipd attach --wsl --busid 2-6

usbipd: info: Using WSL distribution 'Ubuntu-22.04' to attach; the device will be available in all WSL 2 distributions.
usbipd: info: Using IP address 127.0.0.1 to reach the host.

这样,在WSL2的Ubuntu主机内,可以通过lsusb来查看:

$ lsusb

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 045e:028e Microsoft Corp. Xbox360 Controller
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

$ ls /dev/input

by-id by-path event0 js0

其中js0就是joystick的句柄

可以安装一个测试程序来试验是否OK

$ sudo apt install joystick

$ sudo jstest /dev/input/js0

Driver version is 2.1.0.
Joystick (Microsoft X-Box 360 pad) has 8 axes (X, Y, Z, Rx, Ry, Rz, Hat0X, Hat0Y)
and 11 buttons (BtnA, BtnB, BtnX, BtnY, BtnTL, BtnTR, BtnSelect, BtnStart, BtnMode, BtnThumbL, BtnThumbR).
Testing ... (interrupt to exit)
Axes: 0: 0 1: 0 2:-32767 3: 0 4: 0 5:-32767 6: 0 7: 0 Buttons: 0:off 1:off 2:off 3:off 4:off 5:off 6:off 7:off 8:off 9:off 10:off ^C

按下游戏手柄的按键,相应的轴或者按键会有变化。这样,内核就满足joystick使用要求了。

 

 

 

 


欢迎转载,本文地址: https://blog.prodrich.com/detail/92/

带着使命来到世上的你,给他人提供价值,才有价值