4

lxc下如何让usb设备pass through直接到达虚机

 7 months ago
source link: https://bajie.dev/posts/20240124-lxc_usb_pass_through/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Lxc下如何让usb设备pass Through直接到达虚机

2024-01-24 2 分钟阅读

测试的同事要在测试机上安装android studio,adb直接调试手机。

这下麻烦了,测试机实际是个lxc的容器,需要把插在宿主机usb上的手机直接过给容器。

说下做法: 首先在宿主机上执行lsusb,查出手机USB:

[root@localhost]# lsusb
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub  
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub  
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  
Bus 001 Device 014: ID 04e8:6860 Samsung Electronics Co., Ltd GT-I9100 Phone [Galaxy S II], GT-I9300 Phone [Galaxy S III], GT-P7500 [Galaxy Tab 10.1] , GT-I9500 [Galaxy S 4]  
Bus 001 Device 004: ID 0624:0248 Avocent Corp. Virtual Hub  
Bus 001 Device 005: ID 0624:0249 Avocent Corp. Virtual Keyboard/Mouse  

看最长的带GT-I9100那一行,ID 04e8:6860,VendorID:ProdID,说明Vendor=04e8 ProdID=6860,记下来。

并且记下来Bus 001,Device 014

一切设备皆文件,看看具体的文件吧:

ls -l /dev/bus/usb/001/014  
crw-rw-r--. 1 root root 189, 13 8月  29 10:31 /dev/bus/usb/001/014  

记下来这个189

然后去虚机的配置文件里 注意,我们的lxc虚机配置文件在/var/lib/docker/containers/bc2c9887fa72a59ab59f78b50677612608d40a2b73e8fd8fe0ffe25baa35edaa/config.lxc中,我们的rootfs是:

lxc.rootfs = /var/lib/docker/devicemapper/mnt/bc2c9887fa72a59ab59f78b50677612608d40a2b73e8fd8fe0ffe25baa35edaa/rootfs  

So,在rootfs目录下的config.lxc文件中增加2行,让usb设备直通虚机:

lxc.cgroup.devices.allow = c 189:* rwm  
lxc.mount.entry = /dev/bus/usb/001/014 /var/lib/docker/devicemapper/mnt/bc2c9887fa72a59ab59f78b50677612608d40a2b73e8fd8fe0ffe25baa35edaa/rootfs/dev/bus/usb/001/014 none bind,optional,create=file  

还没完: 重启虚机,进入容器,写个udev的rules,这个没测试是否有必要,因为其实udev服务根本没在容器里跑啊

vi /etc/udev/rules.d/51-android.rules  
SUBSYSTEMS=="usb", ATTRS{idVendor}=="04e8", ATTRS{idProduct}=="6860", MODE="0666", OWNER="root"  

继续,修改容器中的adb_usb.ini,把VendorID加进去

vi ~/.android/adb_usb.ini  
# ANDROID 3RD PARTY USB VENDOR ID LIST -- DO NOT EDIT.
# USE 'android update adb' TO GENERATE.
# 1 USB VENDOR ID PER LINE.
0x04e8  

重启容器,adb被装在/opt/sdk/platform-tools/路径下,进去后执行:

/opt/sdk/platform-tools/adb start-server
/opt/sdk/platform-tools/adb devices
List of devices attached  
0376b638    unauthorized  
image-20240124103136477

OK搞定。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK