Skip to content

There are some scenarios in which the software running in your Virtual Machine requires a usb device to be attached to the machine. There are different solutions for this, in software and in hardware, and the best solution depends on the details of each case.

The recipe below is an example solution, using a USBIP that allows sharing usb devices between Virtual Machines.

Disclaimer: This procedure has been adapted from USB/IP Wiki and tested in CERN CentOS 7. If you are using a different Linux flavor, please modify it accordingly.

This procedure is available for Linux machines. If you are running Windows, please contact the cloud team via opening a service desk ticket.

Configure server machine to share its usb devices

  • Install the software

    $ yum install -y usbip-utils kmod-usbip
    

  • In CentOS 7 there is no systemd unit, we can create one for this particular purpose

    $ cat > /usr/lib/systemd/system/usbip-server.service <<EOF
    [Unit]
    Description=USB/IP server daemon
    
    [Service]
    ExecStartPre=/sbin/modprobe -qab usbip_host
    ExecStart=/usr/sbin/usbipd
    
    [Install]
    WantedBy=multi-user.target
    EOF
    $ chown root:root /usr/lib/systemd/system/usbip-server.service
    $ chcon -u system_u -t systemd_unit_file_t /usr/lib/systemd/system/usbip-server.service
    $ systemctl daemon-reload
    

  • Enable and start the systemd unit
    $ systemctl enable usbip-server
    $ systemctl start usbip-server
    
  • Set the firewall rules to allow the client to connect

    $ firewall-cmd --zone=public --add-port=3240/tcp --permanent
    $ firewall-cmd --reload
    

  • To list the exportable devices, you can type

    $ usbip list -l
    - busid 3-2 (046d:0836)
       Logitech, Inc. : B525 HD Webcam (046d:0836)
    

  • In order to export the device you need to bind it with the busid information such

    $ usbip bind --busid=3-2
    usbip: info: bind device on busid 3-2: complete
    

Configure client machine to access exported usb devices

  • Install the software

    $ yum install -y usbip-utils kmod-usbip
    

  • In CentOS7 there is no systemd unit, we can create one for this particular purpose

    $ cat > /usr/lib/systemd/system/usbip-client.service <<EOF
    [Unit]
    Description=USB/IP client modules
    
    [Service]
    Type=oneshot
    RemainAfterExit=true
    ExecStart=/sbin/modprobe -qab vhci-hcd
    
    [Install]
    WantedBy=multi-user.target
    EOF
    $ chown root:root /usr/lib/systemd/system/usbip-client.service
    $ chcon -u system_u -t systemd_unit_file_t /usr/lib/systemd/system/usbip-client.service
    $ systemctl daemon-reload
    

  • Enable and start the systemd unit

    $ systemctl enable usbip-client; systemctl start usbip-client
    

  • You can list the registered devices on the server by

    $ usbip list --remote=<server IP address>
    Exportable USB devices
    ======================
     - xxx.xxx.xxx.xxx
            3-2: Logitech, Inc. : B525 HD Webcam (046d:0836)
               : /sys/devices/pci0000:00/0000:00:1d.0/0000:06:00.0/0000:07:01.0/0000:09:00.0/0000:0a:00.0/0000:0b:00.0/usb3/3-2
               : Miscellaneous Device / ? / Interface Association (ef/02/01)
    

  • Let's attach the selected device

    $ usbip attach --remote=<server IP address> --busid=<device bus id>
    

  • List the ports in use (attached devices)

    $ usbip port
    Imported USB devices
    ====================
    Port 00: <Port in Use> at High Speed(480Mbps)
           unknown vendor : unknown product (046d:0836)
           2-1 -> usbip://xxx.xxx.xxx.xxx:3240/3-2
               -> remote bus/dev 003/005
    

  • Detach the device

    $ usbip detach --port=00
    

Advanced configuration

If you need automatic rebind/reattach for usb devices to follow reconnections please use the following recipe