When a device that is bound and attached remotely is unplugged, the device is automatically detached on the client and unbound on the host. After that the state is the same as if it was never bound or attached.
The usbip commands for binding (on the host) and attaching (on the client) may be run repeatedly with the same arguments. While this issues an error message on already bound or attached devices, nothing bad happens! So one can just install background scripts that will repeatedly bind and attach the devices. Example scripts and systemd units are provided below. Be sure to change the Hostname and Port IDs to your needs.
Host
Skript /opt/usbip/usbip-bind:
#!/bin/bash SPOOL=/var/spool/usbip/bind if [[ $1 == "-q" ]] then exec &>/dev/null fi touch $SPOOL while [[ -e $SPOOL ]] do /usr/bin/usbip bind -b 1-1.2.1 /usr/bin/usbip bind -b 1-1.2.2 sleep 10 done /usr/bin/usbip unbind -b 1-1.2.1 /usr/bin/usbip unbind -b 1-1.2.2 exit 0
Systemd unit /etc/systemd/system/usbip-bind.service:
[Unit] Description=USB-IP Bindings [Service] ExecStart=/opt/usbip/usbip-bind -q ExecStop=/bin/rm /var/spool/usbip/bind ; /bin/bash -c "while [[ -d /proc/"$MAINPID" ]]; do sleep 1; done" [Install] WantedBy=multi-user.target
Be sure do make the directory /var/spool/usbip. Then enable and start the unit:
systemctl daemon-reload systemctl enable usbip-bind systemctl start usbip-bind
Client
Skript /opt/usbip/usbip-attach:
#!/bin/bash SPOOL=/var/spool/usbip/attach if [[ $1 == "-q" ]] then exec &>/dev/null fi touch $SPOOL while [[ -e $SPOOL ]] do /usr/bin/usbip attach -r pi -b 1-1.2.1 /usr/bin/usbip attach -r pi -b 1-1.2.2 sleep 10 done /usr/bin/usbip detach -p 0 /usr/bin/usbip detach -p 1 exit 0
Systemd unit /etc/systemd/system/usbip-attach.service:
[Unit] Description=USB-IP Attach Wants=network-online.target After=network-online.target [Service] ExecStart=/opt/usbip/usbip-attach -q ExecStop=/bin/rm /var/spool/usbip/attach ; /bin/bash -c "while [[ -d /proc/"$MAINPID" ]]; do sleep 1; done" [Install] WantedBy=multi-user.target
Be sure do make the directory /var/spool/usbip. Then enable and start the unit:
systemctl daemon-reload systemctl enable usbip-attach systemctl start usbip-attach
Now you may remove the device whenever needed and at most 20 seconds after plugging it back in the usbip connection is reestablished.