USB/IP devices access binding and remote attachement

Hi,

I had a go at creating a snap for USB/IP today. The use case it to start the usbip daemon, bind device(s), and attach them to a remote system via the network.

I got it working, but unfortunately the raw-usb interface is not quite enough, and I had to expose some /sys files using the system-files interface. There a few problems with this technique, it feels like an abuse of the interface and because it does not support any globbing or wildcards it means any snap would have to be very hardware specific in terms of devices and how they connected.

There is also a slight issue with kernel revision support, but I will gloss over that for now :sweat_smile:

Here is the rough snapcraft.yaml I got working for testing:

name: usbip
base: core20
summary: USB over IP
description: |
  USB over IP server and setup
confinement: strict
#adopt-info: ci-metadata
version: 5.4.0-77.000


plugs:
  usb-bind:
    interface: system-files
    write:
    - /sys/bus/usb/drivers/usb/bind
    - /sys/bus/usb/drivers/usb/unbind
    - /sys/bus/usb/drivers/usbip-host
    # This is hardware dependant - required for devices under: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-2/usbip_sockfd
    #- /sys/devices/pci0000:00/0000:00:14.0/usb1
    - /sys/devices/pci0000:00/0000:00:14.0/usb1/1-2/usbip_sockfd

apps:
  module-setup:
    command: sbin/modprobe -a usbip_core usbip_host
    daemon: oneshot
    plugs:
      - kernel-module-control
  daemon:
    command: usr/lib/linux-tools/5.4.0-77-generic/usbipd
    environment:
      PYTHONPATH: $SNAP/usr/lib/python3/dist-packages:/usr/lib/python3/dist-packages
    daemon: simple
    restart-condition: on-failure
    restart-delay: 3s
    after:
      - module-setup
    plugs:
      - raw-usb
      - usb-bind
      - network-bind
  bind:
    command: usr/lib/linux-tools/5.4.0-77-generic/usbip bind -b 1-2
    environment:
      PYTHONPATH: $SNAP/usr/lib/python3/dist-packages:/usr/lib/python3/dist-packages
    plugs:
      - raw-usb
      - usb-bind
      - network
  unbind:
    command: usr/lib/linux-tools/5.4.0-77-generic/usbip unbind -b 1-2
    environment:
      PYTHONPATH: $SNAP/usr/lib/python3/dist-packages:/usr/lib/python3/dist-packages
    plugs:
      - raw-usb
      - usb-bind
      - network
  list:
    command: usr/lib/linux-tools/5.4.0-77-generic/usbip list -l
    plugs:
      - raw-usb
      - network

layout:
  /usr/share/hwdata/usb.ids:
    symlink: $SNAP/usr/share/hwdata/usb.ids

parts:
  usbip:
    plugin: nil
    stage-packages:
      - linux-tools-5.4.0-77-generic
      - linux-tools-common
      - kmod
      - usbutils
      - hwdata

Is this a case for a new snapd interface ?

I guess it would have to support parameters to it could be locked to particular PCI paths and devices.

On a side note, this is where I noticed that globbing is not supported for the system-files interface, so I really can’t do this with that interface in any elegant way: Glob patterns with the system-file interface - #2 by jdstrand

If I am to peruse this, what’s the best way ? Is there something I’m not thinking of here ?

Cheers, Just

as long as the paths have usb in them somewhere, it seems reasonable to include the rules in the raw-usb interface

ok thanks @ijohnson

I will try creating a PR, see how I get on.

Cheers,
Just

1 Like

Let us know if you need any help, we are also happy to file it on your behalf given what you have shared in this forum post

I very much appreciate the offer, I’m using this as an opportunity to get the Contributor Agreement signed off. I have already prepared and tested a patched profile :slight_smile:

We will likely want to be able to contribute more in the future too :+1:

Cheers, Just

2 Likes

@ijohnson After jumping through several hoops, we have signed the entity CLA :sweat_smile:

I created an PR: https://github.com/snapcore/snapd/pull/10821

First-time contributors need a maintainer to approve running workflows

Can you take a look for me ?

Cheers, Just

Seems @mvo beat me to it!

Thanks for checking :+1:

Actually, it seems there is a problem with CLA check now. We signed as an “entity”, but the automated check is looking for individuals. Not sure how best to resolve that. I am emailing the contributor-agreement address to see if they can help.

Cheers, Just