Skip to content

Commit

Permalink
feat(usbip): add entrypoint.sh and README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
prehor committed Jun 18, 2024
1 parent b2862ca commit a940518
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
4 changes: 3 additions & 1 deletion apps/usbip/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ RUN \
hwdata-usb \
linux-tools-usbip~"${VERSION}"

COPY ./apps/usbip/entrypoint.sh /entrypoint.sh

WORKDIR /config

ENTRYPOINT ["/usr/bin/catatonit", "--"]
CMD ["/usr/sbin/usbipd"]
CMD ["/entrypoint.sh","--attach"]

LABEL org.opencontainers.image.source="https://github.com/torvalds/linux/blob/master/tools/usb/usbip/README"
18 changes: 18 additions & 0 deletions apps/usbip/README.md
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

82 changes: 82 additions & 0 deletions apps/usbip/entrypoint.sh
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

0 comments on commit a940518

Please sign in to comment.