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

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

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

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

1[root@localhost]# lsusb
2Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub  
3Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub  
4Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  
5Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  
6Bus 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]  
7Bus 001 Device 004: ID 0624:0248 Avocent Corp. Virtual Hub  
8Bus 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

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

1ls -l /dev/bus/usb/001/014  
2crw-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是:

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

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

1lxc.cgroup.devices.allow = c 189:* rwm  
2lxc.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服务根本没在容器里跑啊

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

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

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

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

1/opt/sdk/platform-tools/adb start-server
2/opt/sdk/platform-tools/adb devices
3List of devices attached  
40376b638    unauthorized  

image-20240124103136477

OK搞定。


Dell的idrac Redfish初探
Freelancer任务之八openvpn的DNS分发
comments powered by Disqus