Skip to content

Instantly share code, notes, and snippets.

@galenseitz
Forked from Leo-PL/10-persistent-serial.sh
Last active May 11, 2024 18:43
Show Gist options
  • Save galenseitz/f3b2c4a45557eebe992dc637a1db070b to your computer and use it in GitHub Desktop.
Save galenseitz/f3b2c4a45557eebe992dc637a1db070b to your computer and use it in GitHub Desktop.
Persistent serial port names for USB-serial on OpenWrt hotplugd
[ "${ACTION}" = "bind" -o "${ACTION}" = "unbind" ] || exit 0
[ "${SUBSYSTEM}" = "usb-serial" ] || exit 0
[ -n "${DEVICENAME}" -a -n "${DEVPATH}" ] || exit 1
if [ "${ACTION}" = "bind" ]; then
subsystem="$(basename $(readlink /sys${DEVPATH}/../subsystem))"
[ "$subsystem" = "usb" ] || exit 0
replace_whitespace="s/^[ \t]*|[ \t]*$//g; s/[ \t]+/_/g"
manufacturer="$(sed -E "${replace_whitespace}" /sys${DEVPATH}/../../manufacturer)"
# If manufacturer is blank, use vendor ID.
: ${manufacturer:=$(cat /sys${DEVPATH}/../../idVendor)}
product="$(sed -E "${replace_whitespace}" /sys${DEVPATH}/../../product)"
# If product is blank, use product ID.
: ${product:=$(cat /sys${DEVPATH}/../../idProduct)}
serial="$(sed -E "${replace_whitespace}" /sys${DEVPATH}/../../serial)"
interface="$(cat /sys${DEVPATH}/../bInterfaceNumber)"
port="$(cat /sys${DEVPATH}/port_number)"
replace_chars="s/[^0-9A-Za-z#+.:=@-]/_/g"
id_link=$(echo "${subsystem}-${manufacturer}_${product}${serial:+_${serial}}-if${interface}${port:+-port${port}}" | \
sed "${replace_chars}")
path_link=$(echo "${DEVPATH}${port:+-${port}}" | \
sed "s%/devices/%%; s%/${DEVICENAME}%%g; ${replace_chars}")
mkdir -p /dev/serial/by-id /dev/serial/by-path
ln -sf "/dev/${DEVICENAME}" "/dev/serial/by-id/${id_link}"
ln -sf "/dev/${DEVICENAME}" "/dev/serial/by-path/${path_link}"
elif [ "${ACTION}" = "unbind" ]; then
for link in $(find /dev/serial -type l); do
[ -L ${link} -a "$(readlink ${link})" = "/dev/$DEVICENAME" ] && rm ${link}
done
fi
@galenseitz
Copy link
Author

Thanks for pointing this out. I've made a change that I believe fixes the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment