Skip to content

Instantly share code, notes, and snippets.

@SirBaeK
Forked from peedy2495/README.md
Created May 15, 2024 20:17
Show Gist options
  • Save SirBaeK/920dfc71f4e0f0a8afb7d3040366e67d to your computer and use it in GitHub Desktop.
Save SirBaeK/920dfc71f4e0f0a8afb7d3040366e67d to your computer and use it in GitHub Desktop.
Deploy USB-devices via Network as simple and stable as possible

Deploy USB-devices via Network as simple and stable as possible

Features:

  • auto-export on host when a defined usb-device has been plugged
  • auto-attachment on client when a disappeared remote-device is available, again.

Files Included:

README.md
[email protected]
[email protected]

Package dependencies

  • usbip
  • lsusb (pre-installed on most distributions)

Installation

install the systemd-files as root.

usb-host:
install -m 644 -o root -g root -t /lib/systemd/system/ /your/filepath/[email protected]

usb-client:
install -m 644 -o root -g root -t /lib/systemd/system/ /your/filepath/[email protected]

execute on both sides:
systemctl daemon-reload

Usage

To export a pysical usb-device you have to determine the vendor and product id of your device with lsusb the format is [idVendor]:[idProduct] like: 301b:56f1

Now, you're able to export/provide your usb-device.
Syntax:
usbip-export@[idVendor]:[idProduct].service e.g:
systemctl start usbip-export@301b:56f1

At last, you're able to attach the usb-device from the remote IP on a client.
Syntax:
[email protected]@[host]_[idVendor]:[idProduct].service e.g.:
systemctl start [email protected]_301b:56f1

Background information for interested fellows ;-)

usbip-export human readable relevant code parts:

starting:

# bind
dev=%i
statePrev=1
state=$(/usr/bin/lsusb|grep -q $dev; echo $?)
while true; do
    if [ $state -ne $statePrev ]; then
        /usr/sbin/usbip bind --busid=$(/usr/sbin/usbip list -p -l | grep "$dev" | cut '-d#' -f1 | cut '-d=' -f2 | tr -d '[:space:]')
    fi
    sleep 1
    statePrev=$state
    state=$(/usr/bin/lsusb|grep -q $dev; echo $?)
done

stopping:

# unbind
/usr/sbin/usbip unbind --busid=$(/usr/sbin/usbip list -p -l | grep "%i" | cut '-d#' -f1 | cut '-d=' -f2 | tr -d '[:space:]')
killall usbipd

usbip-attach human readable relevant code parts:

starting:

# attach
host=$(echo %i|cut '-d_' -f1|tr -d '[:space:]')
dev=$(echo %i|cut '-d_' -f2|tr -d '[:space:]')
while true; do
    /usr/bin/lsusb | grep -q $dev
    if [ $? -ne 0 ]; then
        busid=$(/usr/sbin/usbip list -p -r $host | grep $dev | cut '-d:' -f1 | xargs echo -n)
        /usr/sbin/usbip port|grep -q $dev
        if [ $? -ne 0 ]; then
            /usr/sbin//usr/sbin/usbip attach --remote=$host --busid=$busid
        fi
    fi
    sleep 1
done

stopping:

# detach
dev=$(echo %i|cut '-d_' -f2|tr -d '[:space:]')
/usr/sbin/usbip port | while read i; do
    echo $i | grep -q $dev
    if [ $? -eq 0 ]; then
        /usr/sbin/usbip detach --port=$port
    fi
    echo $i | grep -q Port
    if [ $? -eq 0 ]; then
        port=$(echo $i | cut '-d ' -f2 | cut '-d:' -f1 | tr -d '[:space:]')
    fi
done

Tags: usb usbip tcp teleport gateway ip network systemd service remote deploy export

# Attach remote devices by calling this service with: # systemctl start usbip-attach@[host]_[idVendor]:[idProduct].service
# refer dmesg or lsusb on remote-host to catch your preferred device
[Unit]
Description=attach remote device from usbip-host
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
ExecStart=/bin/sh -c "host=$(echo %i|cut '-d_' -f1|tr -d '[:space:]'); dev=$(echo %i|cut '-d_' -f2|tr -d '[:space:]'); while true; do /usr/bin/lsusb | grep -q $dev; if [ $? -ne 0 ]; then busid=$(/usr/sbin/usbip list -p -r $host | grep $dev | cut '-d:' -f1 $
ExecStop=/bin/sh -c "dev=$(echo %i|cut '-d_' -f2|tr -d '[:space:]'); /usr/sbin/usbip port | while read i; do echo $i | grep -q $dev; if [ $? -eq 0 ]; then /usr/sbin/usbip detach --port=$port; fi; echo $i | grep -q Port; if [ $? -eq 0 ]; then port=$(echo $i $
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
# Export devices by calling this service with: # systemctl start usbip-export@[idVendor]:[idProduct].service
# refer dmesg or lsusb to catch your preferred device
[Unit]
Description=exporting device via usbip host daemon
After=network-online.target
[Service]
Type=forking
ExecStart=/usr/sbin/usbipd -D
ExecStartPost=/bin/sh -c "dev=%i; statePrev=1; state=$(/usr/bin/lsusb|grep -q $dev; echo $?); while true; do if [ $state -ne $statePrev ]; then /usr/sbin/usbip bind --busid=$(/usr/sbin/usbip list -p -l | grep "$dev" | cut '-d#' -f1 | cut '-d=' -f2 | tr -d '[:space:]'); fi; sleep 1; statePrev=$state; state=$(/usr/bin/lsusb|grep -q $dev; echo $?); done &"
ExecStop=/bin/sh -c "/usr/sbin/usbip unbind --busid=$(/usr/sbin/usbip list -p -l | grep "%i" | cut '-d#' -f1 | cut '-d=' -f2 | tr -d '[:space:]'); killall usbipd"
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment