注目イベント!
アドベントカレンダー2024開催します(12/1~12/25)!
一年を締めくくる特別なイベント、アドベントカレンダーを今年も開催します!
初心者からベテランまで楽しめる内容で、毎日新しい技術トピックをお届けします。
詳細はこちらから!
event banner

Using USB Devices on Linux via USB/IP Shared from Windows

| 5 min read
Author: shigeki-shoji shigeki-shojiの画像
Information

To reach a broader audience, this article has been translated from Japanese.
You can find the original version here.

Hello, I'm Shoji.

The advent of Windows Subsystem for Linux (WSL) has made it very convenient for those who want to run a Linux environment with Windows as the host. Another method is using Hyper-V, a standard feature. I wanted a Linux environment (Ubuntu 22.04.3 LTS) that could start without logging into Windows, so I built one using Hyper-V. I've also installed "amazon-ssm-agent" on this Ubuntu to enable monitoring and updating via AWS System Manager.

To use USB devices connected to the PC, both WSL and Hyper-V can connect using the method introduced in Microsoft's document "Connect USB devices". Although this article describes the case for using it in a WSL environment, here, I will introduce how to use it in Ubuntu 22.04.3 LTS on Hyper-V.

Preparation on the Windows Side

#

Follow the steps written in "Connect USB devices" and install the USBIPD-WIN project on Windows.

Let's check the USB devices to share. Run PowerShell as an administrator.

usbipd list

This will display the BUSID, device name, and status of the connected USB devices.
Next, to share the device, execute the following command.

usbipd bind -b BUSID of the device to share

If you run usbipd list again, you can confirm that the status of the specified device has changed to "Shared".

To stop sharing, execute usbipd unbind -b BUSID of the device.

Example

Here is an example from my environment.

> usbipd list
Connected:
BUSID  VID:PID    DEVICE                                                        STATE
1-17   0411:01f0  USB Mass Storage Device                                       Not shared

> usbipd bind -b 1-17

> usbipd list
Connected:
BUSID  VID:PID    DEVICE                                                        STATE
1-17   0411:01f0  USB Mass Storage Device                                       Shared

On the Ubuntu Side

#

Attach the device shared from Windows.

sudo modprobe vhci-hcd
sudo usbip attach -r Windows IP address -b device BUSID
In my case

In my environment, some preparations were necessary to execute usbip. And since it didn't work as described in the aforementioned document, I'll introduce the steps I took.

sudo apt install linux-tools-6.2.0-39-generic hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/6.2.0-39-generic 20

To detach the device, either execute usbipd detach -b device BUSID on the Windows side or follow the steps below.

Checking the Port

#
usbip port

Detaching

#
usbip detach -p port confirmed
Example

Here is an example from my environment.

When attached, /dev/sdb is recognized, and when detached, /dev/sdb disappears, confirming that the USB-HDD is recognized.

$ sudo ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3

$ sudo usbip attach -r lachesis.local -b 1-17

$ sudo ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb  /dev/sdb1

$ sudo usbip port
Imported USB devices
====================
Port 08: <Port in Use> at Super Speed(5000Mbps)
       BUFFALO INC. (formerly MelCo., Inc.) : unknown product (0411:01f0)
       2-1 -> usbip://lachesis.local:3240/1-17
           -> remote bus/dev 001/017

$ sudo usbip detach -p 8
usbip: info: Port 8 is now detached!

$ sudo ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3

Since I have iTunes installed on Windows, I use Bonjour, or dns-sd, to specify the Windows host (lachesis.local) instead of the IP address.

When attached, it can also be confirmed with the lsusb command.

$ sudo lsusb
Bus 002 Device 004: ID 0411:01f0 BUFFALO INC. (formerly MelCo., Inc.) HD-LBU3
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

About USB/IP

#

USB/IP was developed by the Nara Institute of Science and Technology (NAIST), and its achievements were merged into the Linux 2.6.38 kernel. Since then, it has been included in the Linux kernel, so it is considered to be reliable.

Performance

#

I connected an external HDD via USB to a Windows PC and mounted it on the same PC's Ubuntu for use. It worked without any issues.

However, when connecting a USB-HDD device on Windows to a Raspberry Pi via Wifi, it became unstable. It seems there might be problems when using unstable or low-quality network connections.

Conclusion

#

This article explained how to share USB devices connected to a Windows PC. Not only Windows - Linux but also devices like Raspberry Pi can be used for Linux - Linux, or even the opposite, using USB devices connected to Linux on Windows.

References

#

豆蔵では共に高め合う仲間を募集しています!

recruit

具体的な採用情報はこちらからご覧いただけます。