Skip to content

Commit

Permalink
First release to public
Browse files Browse the repository at this point in the history
  • Loading branch information
psct committed May 3, 2022
1 parent de310e4 commit 45bf947
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 1 deletion.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
# usbip
systemd units for sharing usb devices by usb/ip

ZIP-Download: Release-Page

## German

Dieses Repository enthält Dateien zu einem Artikel in c't 11/2022, S. 66 (USB-Streckbank mit Raspi, USB über LAN und WLAN verlängern: USB/IP). Die u.g. englischen Hinweise finden Sie im Artikel – der Artikel versäumt allerdings den Hinweis, die Befehle als root oder mit sudo auszuführen. Er sei hiermit nachgereicht.

## English

systemd-Units for USB/IP - let a Raspberry Pi (or any Linux host) share USB devices on a network by using Linux kernel components named "USB/IP". The units have to be copied to server and client and registered with `systemctl daemon-reload`.

The server starts sharing devices after their USB vendor and device IDs have been added to /etc/udev/rules.d/90-usbip.rules and those rules have been put in place by calling `udevadm control --reload-rules && sudo udevadm trigger` (all commands have to be prefixed with sudo or be called as root user).

On the client side you (as user root or with sudo prefixed) have to connect to the shared devices by calling `systemctl enable usbip@srv:03f0:3d17` and `systemctl start usbip@srv:03f0:3d17` – "srv" may be a valid host name or an IPv4/IPv6 address. "03f0:3d17" are vendor and device ID for a HP laser printer placed in mentioned 90-usbip.rules.

Beware: Those systemd units and udev rules only support sharing one device with the same vendor and device ID. Furthermore they don't try to unload the whole stack on inactivity (no device shared) because that led to kernel panics while trying to do so.

The units and rules have been tested on Pi OS Buster, Debian Buster and Linux Mint 20.3 - they should work with minor modifications on any systemd based distribution.

29 changes: 29 additions & 0 deletions client/etc/systemd/system/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Part of an article in german c't magazine (https://ct.de)
# more on https://github.com/psct/usbip
[Unit]
Description=USB/IP attach/detach usb dev %i
After=network-online.target usbipd.service
Wants=network-online.target usbipd.service
PartOf=usbipd.service

[Service]
Type=simple
Restart=on-failure
RestartSec=1
RemainAfterExit=yes

# %i contains IPv4/IPv6/hostname:vendor:product
# connect to remote usb dev
# list shared devices, pick (first) bus id of dev with :vendor:productid
# get IPv4/IPv6/hostname from parameter: echo %i | rev | cut -d: -f1,2 --complement | rev
# get :vendor:product from parameter: echo %i | rev | cut -d: -f1,2 | rev
ExecStart=/bin/sh -c "usbip attach -r $(echo %i | rev | cut -d: -f1,2 --complement | rev) -b $(usbip list -r $(echo %i | rev | cut -d: -f1,2 --complement | rev) | grep $(echo %i | rev | cut -d: -f1,2 | rev) | cut -d: -f1)"

# disconnect from remote usb dev
# throw away header, concat to one line (paste), pick IPv4/IPv6, cut port
# but: beforehand check if dev connected at all. Otherwise `usbip detach --port=` would disconnect any device attached on port 0 (if no port specified).
ExecStop=-/bin/sh -c "usbip port | grep -v "^I" | grep -v "^=" | paste - - - - | grep $(echo %i | rev | cut -d: -f1,2 | rev) | grep $(echo %i | rev | cut -d: -f1,2 --complement | rev) | grep -qP "[0-9]+" && usbip detach --port=$(usbip port | grep -v "^I" | grep -v "^=" | paste - - - - | grep $(echo %i | rev | cut -d: -f1,2 | rev) | grep $(echo %i | rev | cut -d: -f1,2 --complement | rev) | cut -b6-7)"

[Install]
WantedBy=multi-user.target

23 changes: 23 additions & 0 deletions client/etc/systemd/system/usbipd.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Part of an article in german c't magazine (https://ct.de)
# more on https://github.com/psct/usbip
[Unit]
Description=USB/IP client module loader
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
Restart=always
RemainAfterExit=yes

#ExecStartPre=/bin/sh -c "/sbin/insmod /lib/modules/$(uname -r)/kernel/drivers/usb/usbip/usbip-core.ko"
#ExecStart=/bin/sh -c "/sbin/insmod /lib/modules/$(uname -r)/kernel/drivers/usb/usbip/vhci-hcd.ko"
ExecStart=/sbin/modprobe vhci-hcd

# unload previously loaded modules
# unloading produces ooopses
#ExecStop=/usr/sbin/modprobe -r usbip-core

[Install]
WantedBy=multi-user.target

21 changes: 21 additions & 0 deletions server/etc/systemd/system/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Part of an article in german c't magazine (https://ct.de)
# more on https://github.com/psct/usbip
[Unit]
Description=USB/IP share usb device on given usb port %i; called by device specific udev rule
After=network-online.target usbipd.service
Wants=network-online.target usbipd.service
PartOf=usbipd.service

[Service]
Type=simple
RemainAfterExit=yes

# share device by bind
ExecStart=/bin/sh -c "/usr/sbin/usbip bind -b %i"

# stop sharing
ExecStop=-/bin/sh -c "/usr/sbin/usbip unbind -b %i"

[Install]
WantedBy=multi-user.target

24 changes: 24 additions & 0 deletions server/etc/systemd/system/usbipd.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Part of an article in german c't magazine (https://ct.de)
# more on https://github.com/psct/usbip
[Unit]
Description=USB/IP load modules and start daemon for sharing
After=network-online.target
Wants=network-online.target

# bad idea, kernel panics sometimes, so leave it alone
# StopWhenUnneeded=true

[Service]
Type=simple
Restart=always

# usbip-host will start usbip-core by itself
ExecStartPre=/sbin/modprobe usbip-host
ExecStart=/bin/sh -c "/usr/sbin/usbipd"

# unload modules
ExecStop=/usr/sbin/rmmod usbip-host
ExecStop=/usr/sbin/rmmod usbip-core

[Install]
WantedBy=multi-user.target
14 changes: 14 additions & 0 deletions server/etc/udev/rules.d/90-usbip.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Part of an article in german c't magazine (https://ct.de)
# more on https://github.com/psct/usbip
SUBSYSTEM!="usb", GOTO="usbipexit"

# start service for mentioned usb device so they get shared by usb/ip
# uncomment line and insert your id for vendor and product lsusb shows for your usb device
#ACTION=="add", ATTR{idVendor}=="1a86", ATTR{idProduct}=="7523", TAG+="usbip", TAG+="systemd", ENV{SYSTEMD_WANTS}+="usbip@%k.service"
#ACTION=="add", ATTR{idVendor}=="0403", ATTR{idProduct}=="6001", TAG+="usbip", TAG+="systemd", ENV{SYSTEMD_WANTS}+="usbip@%k.service"

# remove by SYSTEMD_WANTS does not work according to https://github.com/systemd/systemd/issues/7587, so use systemctl
ACTION=="remove", TAG=="usbip", RUN+="/usr/bin/systemctl --no-block stop usbip@%k.service"

#End Of File
LABEL="usbipexit"

0 comments on commit 45bf947

Please sign in to comment.