-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usbip): add entrypoint.sh and README.md
- Loading branch information
Showing
3 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# usbip | ||
|
||
USB over IP: | ||
* https://github.com/torvalds/linux/blob/master/tools/usb/usbip/README | ||
* https://man.archlinux.org/man/extra/usbip/usbipd.8.en | ||
* https://man.archlinux.org/man/extra/usbip/usbip.8.en | ||
|
||
## Usbip server | ||
|
||
Systemd service & udev rules for device binding: | ||
* https://github.com/prehor/home-ops/blob/main/ansible/main/playbooks/cluster-prepare.yaml | ||
|
||
## Usbip client | ||
|
||
Kubernetes sidecar container: | ||
* https://github.com/prehor/home-ops/blob/main/ansible/storage/playbooks/cluster-prepare.yaml | ||
* https://github.com/prehor/home-ops/blob/main/kubernetes/main/apps/home-automation/home-assistant/app/helmrelease.yaml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#/bin/bash | ||
|
||
: "${USBIP_HOST:=localhost}" # Remote host | ||
: "${USBIP_PORT:=3240}" # Remote port | ||
: "${USBIP_RETRY:=5}" # Attach retry period | ||
: "${USBIP_LOOP_FILE:=/tmp/usbip-attach-loop}" | ||
|
||
# Attach device | ||
attach_device() { | ||
local ID_LIST=$1 | ||
|
||
for ID in ${ID_LIST}; do | ||
detach_device "${ID}" | ||
usbip -t "${USBIP_PORT}" attach -r "${USBIP_HOST}" -b "${ID}" 2>/dev/null | ||
echo "[INFO] Device usbip://${USBIP_HOST}:${USBIP_PORT}/${ID} was attached" | ||
done | ||
} | ||
|
||
# Detach devices on unreachable host | ||
clean_unreachable_devices() { | ||
ping -W 5 -c 1 "${USBIP_HOST}" 2>&1 > /dev/null && return | ||
echo -n | timeout 1 cat > "/dev/tcp/${USBIP_HOST}/${USBIP_PORT}" 2>&1 > /dev/null && return | ||
echo "[WARNING] Device usbip://${USBIP_HOST}:${USBIP_PORT} is unreachable" | ||
detach_device | ||
} | ||
|
||
# Detach device | ||
detach_device() { | ||
# Empty ID will remove all remote host devices | ||
local ID=$1 | ||
|
||
PORTS=$(usbip port | grep -B2 "\-> usbip://${USBIP_HOST}:${USBIP_PORT}/${ID}" | \ | ||
grep '^Port' | cut -d' ' -f 2 | cut -d':' -f1) | ||
for PORT in ${PORTS}; do | ||
usbip detach -p "${PORT}" 2>&1 > /dev/null | ||
echo "[INFO] Device usbip://${USBIP_HOST}:${USBIP_PORT}/${ID} was detached" | ||
done | ||
} | ||
|
||
# Get remote devices id list | ||
get_device_list() { | ||
echo $(usbip -t "${USBIP_PORT}" list -r "${USBIP_HOST}" 2>/dev/null | \ | ||
grep -oP '^[[:blank:]]+[[:digit:]]+-.*?:' | \ | ||
awk -F' ' '{print $1}' | cut -d':' -f 1) | ||
} | ||
|
||
# Attach all devices | ||
start_attach_loop () { | ||
touch "${USBIP_LOOP_FILE}" | ||
while :; do | ||
local ID_LIST=$(get_device_list) | ||
if [ -z "${ID_LIST}" ]; then | ||
clean_unreachable_devices | ||
else | ||
if [-e "${USBIP_LOOP_FILE}"]; then | ||
attach_device "${ID_LIST}" | ||
fi | ||
fi | ||
|
||
sleep "${USBIP_RETRY}" | ||
done | ||
} | ||
|
||
# Detach all devices | ||
stop_attach_loop() { | ||
rm -f "${USBIP_LOOP_FILE}" | ||
sleep "${USBIP_RETRY}" # Be sure the main loop is stopped | ||
detach_device | ||
} | ||
|
||
# Main | ||
case "${1}" in | ||
'--attach') | ||
start_attach_loop | ||
;; | ||
'--detach') | ||
stop_attach_loop | ||
;; | ||
*) | ||
echo "[ERROR] Unknown option '${1}'" > /dev/stderr | ||
exit 1 | ||
esac |