From 6ec39619ac58f498be80a67a3a087ba404115a0f Mon Sep 17 00:00:00 2001 From: Leonid Amirov Date: Wed, 24 Nov 2021 00:10:41 +0300 Subject: [PATCH 1/5] feat(client): implement scripts for attaching/detaching device --- attach-usbip-device.sh | 3 +++ detach-usbip-device.sh | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 attach-usbip-device.sh create mode 100644 detach-usbip-device.sh 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..064054a --- /dev/null +++ b/detach-usbip-device.sh @@ -0,0 +1,3 @@ +#!/bin/sh +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 From dccc972b62b1a2a5803d8cdeaf095b518674e107 Mon Sep 17 00:00:00 2001 From: Leonid Amirov Date: Wed, 24 Nov 2021 00:11:10 +0300 Subject: [PATCH 2/5] feat(units): implement systemd unit for client device --- usbip-import@.service | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 usbip-import@.service 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 From e53527ae80ff58badfeca8a86ea3f5a43d8d8d48 Mon Sep 17 00:00:00 2001 From: Leonid Amirov Date: Wed, 24 Nov 2021 00:11:40 +0300 Subject: [PATCH 3/5] feat(make): add client scripts and service to Makefile --- Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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: From 5a08d39826fd16eb1d506069e66ec6258c9ff5f7 Mon Sep 17 00:00:00 2001 From: Leonid Amirov Date: Wed, 24 Nov 2021 00:42:21 +0300 Subject: [PATCH 4/5] feat(doc): add client config part to README --- README.md | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) 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 From 9a6dfd05e475e036f9f9fbfd9dfa2027077e4436 Mon Sep 17 00:00:00 2001 From: Leonid Amirov Date: Fri, 3 Dec 2021 10:10:34 +0300 Subject: [PATCH 5/5] fix: use bash instead of sh Otherwise, double-bracketed conditionals do not work --- detach-usbip-device.sh | 2 +- remove-usbip-device.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/detach-usbip-device.sh b/detach-usbip-device.sh index 064054a..0a5d458 100644 --- a/detach-usbip-device.sh +++ b/detach-usbip-device.sh @@ -1,3 +1,3 @@ -#!/bin/sh +#!/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