title: Usbip in Docker
body: <ol>
  <li><p>I have a hardware dev kit on my desk attached to my Windows laptop via USB. The dev kit allows you to flash a chip via USB. I would like to expose this device to my server environment using usbip.</p></li>
  <li><p>On my Windows PC I run a virtualbox with USBIP server. I have bound the hw dev kit. The server is listening on port 3240 which is forwarded by Virtual Box to the host (laptop) port 7023</p></li>
  <li><p>Using SSH I login into my server. I have setup an SSH remote tunnel so that port 3240 on the server will sent requests to port 7023 on the laptop.</p></li>
  </ol>
  
  <p>Summary</p>
  
  <blockquote>
    <p>USBIP server port 3240 --Virtual Box--> laptop port 7023 &lt;--SSH -R :3240:localhost:7023 </p>
  </blockquote>
  
  <p>On the server this is working:</p>
  
  <pre><code>sudo /usr/lib/linux-tools-4.4.0-66/usbip --tcp-port 3240 attach -r 127.0.0.1 -b 1-2
  dinne@vidar:~$ lsusb
  Bus 005 Device 002: ID 04f2:b39a Chicony Electronics Co., Ltd
  </code></pre>
  
  <p>Now on the server I have a some Docker containers running. These containers have a development environment with which I can compile programs for the hw dev kit. And I would like to flash the compiled application to the dev kit.</p>
  
  <p>Each user will have his/her own dev container on the server and a locally installed dev kit. So I would like to avoid using usbip to attach the virtual USB device directly on the server. Rather it would be ideal if the user could run usbip inside the Docker container.</p>
  
  <p>Inside the Docker container I tried this:</p>
  
  <pre><code>sudo usbip --tcp-port 3240 list -r 192.168.96.101
  
  Exportable USB devices
  ======================
   - 192.168.96.101
          1-2: Chicony Electronics Co., Ltd : unknown product (04f2:b39a)
             : /sys/devices/pci0000:00/0000:00:06.0/usb1/1-2
             : Miscellaneous Device / ? / Interface Association (ef/02/01)
  
  sudo usbip --debug --tcp-port 3240 attach -r 192.168.96.101 -b 1-2
  
  usbip: info: using port 3250 ("3250")
  usbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/src/usbip_network.c:40:[usbip_setup_port_number] parsing port arg '3250'
  usbip: info: using port 3250 ("3250")
  usbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/src/usbip.c:141:[run_command] running command: `attach'
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:242:[usbip_vhci_driver_open] available ports: 7
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:71:[parse_status] port 0 status 4 speed 0 devid 0
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:72:[parse_status] socket 0 lbusid 0000000000000000
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:71:[parse_status] port 1 status 4 speed 0 devid 0
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:72:[parse_status] socket 0 lbusid 0000000000000000
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:71:[parse_status] port 2 status 4 speed 0 devid 0
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:72:[parse_status] socket 0 lbusid 0000000000000000
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:71:[parse_status] port 3 status 4 speed 0 devid 0
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:72:[parse_status] socket 0 lbusid 0000000000000000
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:71:[parse_status] port 4 status 4 speed 0 devid 0
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:72:[parse_status] socket 0 lbusid 0000000000000000
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:71:[parse_status] port 5 status 4 speed 0 devid 0
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:72:[parse_status] socket 0 lbusid 0000000000000000
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:71:[parse_status] port 6 status 4 speed 0 devid 0
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:72:[parse_status] socket 0 lbusid 0000000000000000
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:71:[parse_status] port 7 status 4 speed 0 devid 0
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:72:[parse_status] socket 0 lbusid 0000000000000000
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:105:[parse_status] exit
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:311:[usbip_vhci_attach_device2] writing: 0 3 65539 2
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:316:[usbip_vhci_attach_device2] attach attribute path: /sys/devices/platform/vhci_hcd/attach
  usbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/sysfs_utils.c:17:[write_sysfs_attribute] error opening attribute /sys/devices/platform/vhci_hcd/attach
  libusbip: debug: /build/linux-tools-SJqEHJ/linux-tools-3.16.7-ckt20/drivers/staging/usbip/userspace/libsrc/vhci_driver.c:320:[usbip_vhci_attach_device2] write_sysfs_attribute failed
  usbip: error: import device
  usbip: error: query
  </code></pre>
  
  <p>It seems that open("/sys/devices/platform/vhci_hcd/attach", O_WRONLY) is failing. </p>
  
  <pre><code>sudo -- sh -c "ls -al /sys/devices/platform/vhci_hcd/"
  drwxr-xr-x  4 root root    0 Mar 21 22:00 .
  drwxr-xr-x 19 root root    0 Mar 21 21:21 ..
  --w-------  1 root root 4096 Mar 21 22:00 attach
  --w-------  1 root root 4096 Mar 21 22:00 detach
  lrwxrwxrwx  1 root root    0 Mar 21 22:00 driver -&gt; ../../../bus/platform/drivers/vhci_hcd
  -rw-r--r--  1 root root 4096 Mar 21 22:00 driver_override
  -r--r--r--  1 root root 4096 Mar 21 22:00 modalias
  drwxr-xr-x  2 root root    0 Mar 21 22:00 power
  -r--r--r--  1 root root 4096 Mar 21 21:21 status
  lrwxrwxrwx  1 root root    0 Mar 21 22:00 subsystem -&gt; ../../../bus/platform
  -rw-r--r--  1 root root 4096 Mar 21 21:21 uevent
  drwxr-xr-x  5 root root    0 Mar 21 22:00 usb5
  -rw-r--r--  1 root root 4096 Mar 21 22:00 usbip_debug
  </code></pre>
  
  <p>How to get an USBIP client working in a Docker container?</p>