diff --git a/Makefile b/Makefile index 9997092..bb9a77f 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,14 @@ unitdir=$(sysconfdir)/systemd/system UNITS = \ usbipd.service \ - usbip-device@.service + usbip-device@.service \ + usbip-import@.service SCRIPTS = \ configure-usbip-device.sh \ - remove-usbip-device.sh + remove-usbip-device.sh \ + attach-usbip-device.sh \ + detach-usbip-device.sh all: diff --git a/README.md b/README.md index 885c9ab..8d86227 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,14 @@ To install the systemd unit, run `make install` in the source directory, which will place the support scripts into `/sbin` and the systemd unit into `/etc/systemd/system`. -## Configuration +## Server Configuration Create one or files in `/etc/usbip-device` named `.conf`. These files **must** contain the following configuration keys: -- `USB_IDVENDOR` -- Vendor ID. -- `USB_IDPRODUCT` -- Product ID. +- `USB_IDVENDOR` -- Vendor ID. +- `USB_IDPRODUCT` -- Product ID. For example, to share a device with vendor:product 03f0:8607, create a file called /etc/usbip_devices/mouse.conf @@ -34,3 +34,36 @@ To share the device immediately: To stop sharing it: systemctl stop usbip-device@mouse + + +## Client Configuration + +Client part is configured very similar to server one, with a few additions: + +* files in `/etc/usbip-devices` must be named `.import.conf` +* besides `USB_IDVENDOR` and `USB_IDPRODUCT` there should also be `HOST` key, identifying hostname or IP address of the server that shares USB device + +Example: +``` +USB_IDVENDOR=03f0 +USB_IDPRODUCT=8607 +HOST=192.168.1.54 +``` + +To enable attaching to remote device at boot, run: + +``` +systemctl enable usbip-import@mouse +``` + +To attach device: + +``` +systemctl start usbip-import@mouse +``` + +To detach device: + +``` +systemctl stop usbip-import@mouse +``` \ No newline at end of file diff --git a/attach-usbip-device.sh b/attach-usbip-device.sh new file mode 100644 index 0000000..7908dcb --- /dev/null +++ b/attach-usbip-device.sh @@ -0,0 +1,3 @@ +#!/bin/bash -e +BUS_ID=`usbip list -r ${HOST}|grep ${USB_IDVENDOR}:${USB_IDPRODUCT}|cut -d\: -f 1|xargs` +[[ -z "$BUS_ID" ]] || /usr/bin/usbip attach -r ${HOST} -b $BUS_ID diff --git a/detach-usbip-device.sh b/detach-usbip-device.sh new file mode 100644 index 0000000..0a5d458 --- /dev/null +++ b/detach-usbip-device.sh @@ -0,0 +1,3 @@ +#!/bin/bash -e +PORT_ID=`usbip port |awk -F'[ /]+' '/^Port/ { printf $2 } /\(.+:.+\)$/ {printf $NF"#"} /usbip:/ { print $NF}'|grep ${USB_IDVENDOR}:${USB_IDPRODUCT}|cut -d\: -f1` +[[ -z "$PORT_ID" ]] || /usr/bin/usbip detach -p $PORT_ID diff --git a/remove-usbip-device.sh b/remove-usbip-device.sh index 23b274b..7aab224 100644 --- a/remove-usbip-device.sh +++ b/remove-usbip-device.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash -e BUS_ID=`/usr/sbin/usbip list -p -l | grep -i "#usbid=${USB_IDVENDOR}:${USB_IDPRODUCT}#" | cut '-d#' -f1` [[ -z "$BUS_ID" ]] || /usr/sbin/usbip unbind --$BUS_ID diff --git a/usbip-import@.service b/usbip-import@.service new file mode 100644 index 0000000..40daac0 --- /dev/null +++ b/usbip-import@.service @@ -0,0 +1,12 @@ +[Unit] +Description=USB/IP Device import %I + +[Service] +EnvironmentFile=/etc/usbip-devices/%i.import.conf +RemainAfterExit=yes +ExecStartPre=/sbin/modprobe vhci_hcd +ExecStart=/usr/sbin/attach-usbip-device %i +ExecStop=/usr/sbin/detach-usbip-device %i + +[Install] +WantedBy=multi-user.target