当前阶段windows的WSL系统让人又爱又恨。从WSL一路用到WSL2有很多限制,一路用来也是禁了开,开了禁。 早期不能和VMware共存,还有网络、磁盘性能低下,端口和主机公用等等一堆问题,隔离性没有完整的虚拟机好。
这里记录下WSL2下如何挂载U盘\SD读卡器(如果挂载硬盘官方有相关文档:关于在 WSL 2 中安装 Linux 磁盘的入门 | Microsoft Learn)
参考:
1、让 WSL 2 支持 USB | Dev on Windows with WSL
2、https://blog.csdn.net/G_C_H/article/details/125939941
方案: usbipd
目前来说,其他方式并不怎么好用。 usbip需要宿主机Windows和WSL虚拟系统都安装usbip,一个作为server一个作为client. 本质就是虚拟了一个usb设置然后通过TCP/IP传输。
1、主机安装usbipd
方式1:在cmd中执行:
winget install usbipd方式2:https://github.com/dorssel/usbipd-win
测试usbipd是否可用:
C:\Users\kudzu>usbipd list
Connected:
BUSID VID:PID DEVICE STATE
1-5 24ae:2010 USB 输入设备 Not shared
1-8 8087:0a2a 英特尔(R) 无线 Bluetooth(R) Not shared
1-9 5986:066d BisonCam, NB Pro Not shared
3-4 14cd:1212 USB 大容量存储设备 Attached
3-5 04d8:0b29 USB 输入设备 Not shared
5-2 046d:c542 USB 输入设备 Not shared
5-4 1a86:7523 USB-SERIAL CH340 (COM42) Not shared
Persisted:
GUID DEVICE2、WSL安装USBIP
说明:WSL2内核并没有开启usbip,需要自行编译内核开启
前期准备
//查看系统版本
$ uname -r
4.19.43-microsoft-standard
//安装编译工具链依赖
$ sudo apt install build-essential flex bison libssl-dev libelf-dev libncurses-dev autoconf libudev-dev libtool下载内核源码并安装
$ sudo git clone https://github.com/microsoft/WSL2-Linux-Kernel.git /usr/src/4.19.43-microsoft-standard
$ cd /usr/src/4.19.43-microsoft-standard
#切换分支
sudo git checkout v4.19.43
# 获取内核配置
$ sudo cp /proc/config.gz config.gz
$ sudo gunzip config.gz
$ sudo mv config .config
#配置内核(也可以直接修改.config) 注:按两次 ESC,选择 Yes 保存并退出
sudo make menuconfig
#开启内核模块,这里的M都改为*编译进内核省事 (按空格可进行切换编译为模块还是内核中)
Device Drivers->USB support[*]
Device Drivers->USB support->Support for Host-side USB[*]
Device Drivers->USB support->Enable USB persist by default[*]
Device Drivers->USB support->USB Modem (CDC ACM) support[*]
Device Drivers->USB support->USB Mass Storage support[*]
Device Drivers->USB support->USB/IP support[*]
Device Drivers->USB support->VHCI hcd[*]
Device Drivers->USB support->VHCI hcd->Number of ports per USB/IP virtual host controller(8)
Device Drivers->USB support->Number of USB/IP virtual host controllers(1)
Device Drivers->USB support->USB Serial Converter support[*]
Device Drivers->USB support->USB Serial Converter support->USB FTDI Single Port Serial Driver[*]
Device Drivers->USB support->USB Physical Layer drivers->NOP USB Transceiver Driver[*]
Device Drivers->Network device support->USB Network Adapters[*]
Device Drivers->Network device support->USB Network Adapters->[Deselect everything you don't care about]
Device Drivers->Network device support->USB Network Adapters->Multi-purpose USB Networking Framework[*]
Device Drivers->Network device support->USB Network Adapters->CDC Ethernet support (smart devices such as cable modems)[*]
Device Drivers->Network device support->USB Network Adapters->Multi-purpose USB Networking Framework->Host for RNDIS and ActiveSync devices[*]
#编译
sudo make -j 8 && sudo make modules_install -j 8 && sudo make install -j 8
#报错:
BTF: .tmp_vmlinux.btf: pahole (pahole) is not available
Failed to generate BTF for vmlinux
Try to disable CONFIG_DEBUG_INFO_BTF
$ sudo apt install dwarves
使用编译的内核
#复制内核到指定位置
$ cp arch/x86/boot/bzImage /mnt/c/Users/<user>/usbip-bzImage
# 在 /mnt/c/Users/<user>/ 上创建 .wslconfig 文件,并写入以下内容
$ nano /mnt/c/User/kudzu/.wslconfig
[wsl2]
kernel=c:\\users\\<user>\\usbip-bzImage重启WSL中的系统
C:\Users\kudzu>wsl --shutdown
C:\Users\kudzu>wsl --list --verbose
#再打开一个cmd窗口启动虚拟系统
C:\Users\kudzu>wsl使用
1、windows下执行查看设备列表,正常会有设备连接声音,并显示如下(在windows下执行):
C:\Users\kudzu>usbipd list
Connected:
BUSID VID:PID DEVICE STATE
1-5 24ae:2010 USB 输入设备 Not shared
1-8 8087:0a2a 英特尔(R) 无线 Bluetooth(R) Not shared
1-9 5986:066d BisonCam, NB Pro Not shared
4-4 14cd:1212 USB 大容量存储设备 Attached
3-5 04d8:0b29 USB 输入设备 Not shared
5-2 046d:c542 USB 输入设备 Not shared
5-4 1a86:7523 USB-SERIAL CH340 (COM42) Not shared
2、把usb设备共享给WSL,只需要执行一次(在windows下执行)
C:\Users\kudzu>usbipd bind --busid=4-43、挂载(在windows下执行)
C:\Users\kudzu>usbipd attach --wsl --busid 4-4 4、WSL Ubuntu中查看sh已经挂载到系统
root@DESKTOP-RSAQBV7:/mnt# lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 010: ID 14cd:1212 Super Top microSD card reader (SY-T18) #这个设备
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
root@DESKTOP-RSAQBV7:/mnt# lsblk -d
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 2G 0 loop
sda 8:0 0 388.4M 1 disk
sdb 8:16 0 2G 0 disk [SWAP]
sdc 8:32 0 256G 0 disk /mnt/wslg/distro
/
sde 8:64 1 14.8G 0 disk #这个设备
#查看分区
root@DESKTOP-RSAQBV7:/mnt# fdisk -l
Disk /dev/sde: 14.84 GiB, 15938355200 bytes, 31129600 sectors
Disk model: Storage Device
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x92c785c3
Device Boot Start End Sectors Size Id Type
/dev/sde1 8192 532479 524288 256M ea Linux extended boot
/dev/sde2 532480 8921087 8388608 4G 83 Linux
/dev/sde3 8921088 31129599 22208512 10.6G c W95 FAT32 (LBA)5、WSL中挂载U盘到系统
root@DESKTOP-RSAQBV7:/mnt# mount /dev/sde1 /mnt/xx
root@DESKTOP-RSAQBV7:/mnt# ls /mnt/xx6、卸载(在windows下执行):
root@DESKTOP-RSAQBV7:/mnt# usbipd detach --busid=4-4
评论区