Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
docs:guide-user:base-system:hotplug [2023/10/03 22:02] – [Restart WLAN on USB Wi-Fi event] vgaeteradocs:guide-user:base-system:hotplug [2024/08/23 10:25] – Avoid duplication/duplicate execution oliv3r
Line 1: Line 1:
 ====== Hotplug ====== ====== Hotplug ======
-Procd (the init system and process management daemon) executes scripts located in ''/etc/hotplug.d/'' when certain events happen, like for example when an interface goes up or down, when a new storage drive is detected, or when a button is pressed.+Procd (the init system and process management daemon) executes scripts located in ''/etc/hotplug.d'' when certain events happen, like for example when an interface goes up or down, when a new storage drive is detected, or when a button is pressed.
 It can be very useful with PPPoE connection or in an unstable network, or to use hardware buttons. It can be very useful with PPPoE connection or in an unstable network, or to use hardware buttons.
  
Line 11: Line 11:
 Which is why most scripts in there use a numeric prefix. Which is why most scripts in there use a numeric prefix.
  
-^ Directory ^ Description ^ +^ Directory     ^ Description                                                                                               
-| **block** | Block device events: device connected/disconnected | +| **block**     | Block device events: device connected/disconnected                                                        
-| **button** | Buttons: not created by default, see [[docs:guide-user:hardware:hardware.button|/etc/rc.button]] instead | +| **button**    | Buttons: not created by default, see [[docs:guide-user:hardware:hardware.button|/etc/rc.button]] instead  
-| **dhcp** | DHCP-related events | +| **dhcp**      | DHCP-related events                                                                                       
-| **dsl** | DSL modem events | +| **dsl**       | DSL modem events                                                                                          
-| **firewall** | Firewall-related events | +| **firewall**  | Firewall-related events                                                                                   
-| **iface** | Interface events: LAN/WAN/etc. is connected/disconnected | +| **iface**     | Interface events: LAN/WAN/etc. is connected/disconnected                                                  
-| **neigh** | Neighbor discovery | +| **neigh**     | Neighbor discovery                                                                                        
-| **net** | Network-related events | +| **net**       | Network-related events                                                                                    
-| **ntp** | Time sync events: time step, time server stratum change | +| **ntp**       | Time sync events: time step, time server stratum change                                                   
-| **tftp** | TFTP-related events | +| **tftp**      | TFTP-related events                                                                                       | 
-| **usb** | USB devices like 3g-modem and tty* |+| **tty**       | TTY-related events, including but not limited to WWAN modems, but pointing directly to TTY interface      
 +| **usb**       | USB devices like 3g-modem and tty*                                                                        | 
 +| **usbmisc**   | Special USB peripherals like printers (scripts in this subsystem have fewer variables available)          |
  
 See also: See also:
Line 49: Line 51:
  
 ===== Event classes / Directories ===== ===== Event classes / Directories =====
 +==== net ====
 +For scripts in **net** directory, these are the (relevant) environmental variables
 +
 +^ Variable name ^ Description ^ 
 +| ACTION | "add" or "remove" noted |
 +| DEVICENAME | configured interface names (br-lan, wlan0, phy1-ap0 |
 +| PATH | Full path |
 +| DEVPATH | full device path (for example "/devices/pci0000:00/0000:00:0b.0/usb1/1-1/1-1:1.0/host7/target7:0:0/7:0:0:0/block/sdc/sdc1 " |
 +| DEVTYPE | what the DEVICENAME are names of, ie. br-lan, phy1-ap0 |
 +| INTERFACE | configured interfaces as in DEVTYPE |
 +| SEQNUM | seqnum (a number) |
 +| SUBSYSTEM | always = "net" |
 +| IFINDEX | appears to be related to the configured interfaces. See `ifconfig` |
 +
 ==== block ==== ==== block ====
 For scripts in **block** directory, these are the (relevant) environmental variables For scripts in **block** directory, these are the (relevant) environmental variables
Line 121: Line 137:
  
 ==== ntp ==== ==== ntp ====
- 
 Variables based on busybox ntpd: Variables based on busybox ntpd:
  
Line 131: Line 146:
 | poll_interval | ntp variables | | poll_interval | ntp variables |
  
-Even without NTP sync, you will receive a "periodic" hotplug event, with stratum=16, about every 11 minutes out of the box.+Even without NTP sync, you will receive a "periodic" hotplug event, with ''stratum=16'', about every 11 minutes out of the box. 
 + 
 +==== tty ==== 
 +^ Variable name ^ description ^ 
 +| ACTION | add, remove, bind, unbind as above | 
 +| DEVICENAME | eg "ttyUSB2"
 +| DEVNAME | only for bind/unbind, eg, "ttyUSB2"
 +| DEVPATH | eg, "/devices/platform/ahb/1b400000.usb/usb2/2-1/2-1:1.3/ttyUSB2"
 +| SEQNUM | (action number since boot for this subsystem) eg 335 | 
 +| SUBSYSTEM | Specific type of device, eg, "usb-serial"
 +| MAJOR | eg 189 | 
 +| MINOR | eg 1 |
  
 ==== usb ==== ==== usb ====
 ^ Variable name ^ description ^ ^ Variable name ^ description ^
-| ACTION | add, remove as above |+| ACTION | add, remove, bind, unbind as above 
 +| DEVICENAME | eg "1-1" |
 | DEVNAME | eg, "bus/usb/001/002" | | DEVNAME | eg, "bus/usb/001/002" |
 +| DEVNUM | eg 002 |
 | DEVPATH | eg, "/devices/platform/ehci-platform/usb1/1-1" | | DEVPATH | eg, "/devices/platform/ehci-platform/usb1/1-1" |
-DEVICENAME | eg "1-1" | +DEVTYPE | eg "usb_device" |
-| DEVNUM | eg 002 | +
-| DRIVER | "usb" |+
 | TYPE | eg 9/0/1 | | TYPE | eg 9/0/1 |
 | PRODUCT | the vendor/productcode/version, eg "424/2640/0" see lsusb | | PRODUCT | the vendor/productcode/version, eg "424/2640/0" see lsusb |
-| SEQNUM | eg 335 |+| SEQNUM | (action number since boot for this subsystem) eg 335 |
 | BUSNUM | eg 001 | | BUSNUM | eg 001 |
 | MAJOR | eg 189 | | MAJOR | eg 189 |
 | MINOR | eg 1 | | MINOR | eg 1 |
 +
 +==== usbmisc ====
 +^ Variable name  ^ description                                             ^
 +| ACTION         | add, remove as above                                    |
 +| DEVNAME        | eg, "bus/usb/001/002"                                   |
 +| DEVPATH        | eg, "/devices/platform/ehci-platform/usb1/1-1"          |
 +| DEVICENAME     | eg "1-1"                                                |
 +| SEQNUM          (action number since boot for this subsystem) eg 335   |
 +| MAJOR          | eg 189                                                  |
 +| MINOR          | eg 1                                                    |
 +
  
 ===== Examples ===== ===== Examples =====
Line 159: Line 196:
 Every time an interface goes up then the if/fi statement will be executed. Every time an interface goes up then the if/fi statement will be executed.
  
-==== Get specific IP address ==== +==== Symlink instead of rename a device ==== 
-Assuming your ISP provides dynamic IP address. +An other script to create symlink instead of renaming the device.
-Reconnect until you get the one matching a specific regexp. +
-Match ''10'' in the first octet, or 2 digits in the 1-st octet and 3 digits in the 2-nd octet. +
-Delay for 10 seconds between reconnects.+
  
 <code bash> <code bash>
-cat << "EOF" > /etc/hotplug.d/iface/10-fix-wan-ip +cat << "EOF" > /etc/hotplug.d/usb/20-cp210x 
-if [ "${ACTION}" !"ifup" ] \ +CP210_PRODID='10c4/ea60/100' 
-&& [ "${ACTION}" != "ifupdate"+SYMLINK="my_link"
-then exit 0 +
-fi +
-if [ "${ACTION}" = "ifupdate" ] \ +
-&& [ -z "${IFUPDATE_ADDRESSES}" ] \ +
-&& [ -z "${IFUPDATE_DATA}"+
-then exit 0 +
-fi +
-/lib/functions/network.sh +
-network_flush_cache +
-network_find_wan NET_IF +
-network_get_ipaddr NET_ADDR "${NET_IF}" +
-if [ "${NET_IF}" != "${INTERFACE}+
-then exit 0 +
-fi +
-if [ "${NET_ADDR%%.*}" = "10" ] \ +
-|| [[ "${NET_ADDR}" =~ "^[0-9]{2}[.][0-9]{3}[.]" ]] +
-then exit 0 +
-fi +
-sleep 10 +
-ifup "${NET_IF}" +
-EOF +
-</code>+
  
-==== Restart WLAN on USB Wi-Fi event ==== +set -eu
-Niii has posted this quick example for a USB WiFi device hotplug event to trigger an init.d network restart wlan0 script.+
  
-To determine RTL8188SU_PRODID variable:+if [ "${DEVTYPE:-}" = 'usb_interface' ] && \ 
 +   [ "${PRODUCT:-}" = "${CP210_PRODID}" ]; then 
 + if [ "${ACTION:-}" = 'bind' ]; then 
 + if [ -L "/dev/${SYMLINK}" ]; then 
 + logger -t hotplug "Symlink '/dev/${SYMLINK}' already exists" 
 + exit 0 
 + fi
  
-<code bash> + DEVICE_NAME="$(find /sys${DEVPATH:-} -maxdepth 1 -type d -iname 'ttyUSB*' -exec basename {} \;)" 
-# lsusb -v + if [ -z "${DEVICE_NAME}" ]; then 
-  idVendor           0x0bda Realtek Semiconductor Corp. + logger -t hotplug 'Warning: DEVICE_NAME is empty' 
-  idProduct          0x8171 RTL8188SU 802.11n WLAN Adapter + exit 0 
-  bcdDevice            2.00 + fi
-</code>+
  
-<code bash> + logger -hotplug "Device name of cp210 is '${DEVICE_NAME}'
-mkdir -p /etc/hotplug.d/usb + ln -s "/dev/${DEVICE_NAME}" "/dev/${SYMLINK}
-cat << "EOF> /etc/hotplug.d/usb/20-rtl8188su + logger -t hotplug "Symlink from '/dev/${DEVICE_NAME}' to '/dev/${SYMLINK}' created
-if [ "${PRODUCT}" "bda/8171/200] \ + fi
-&& [ "${ACTION}" = "add"+
-then wifi +
-fi +
-EOF +
-</code>+
  
-==== Symlink instead of rename a device ==== 
-An other script to create a symlink instead of renaming the device. 
-I test if DEVICE_NAME is empty because when I plug usb device I retrieve two add event, and the first come before created device, so symlink fails. 
  
-<code bash> + if [ "${ACTION:-}" = 'unbind' ]; then 
-cat << "EOF" > /etc/hotplug.d/usb/20-cp210x + rm "/dev/${SYMLINK}" 
-CP210_PRODID="10c4/ea60/100" + logger -t hotplug "Symlink '/dev/${SYMLINK}removed" 
-SYMLINK="my_link" + fi
- +
-if [ "${PRODUCT}" = "${CP210_PRODID}" ]; then +
-    if [ "${ACTION}""add" ]; then +
-          DEVICE_NAME=$(ls /sys/${DEVPATH} | grep tty) +
-          if [ -z ${DEVICE_NAME} ]; then +
-              logger -t hotplug "Warning DEVICE_NAME is empty" +
-              exit 0 +
-          fi +
-          logger -t hotplug "Device name of cp210 is ${DEVICE_NAME}" +
-          ln -s /dev/${DEVICE_NAME} /dev/${SYMLINK} +
-          logger -t hotplug "Symlink from /dev/${DEVICE_NAME} to /dev/${SYMLINK} created" +
-    fi +
-fi +
- +
-if [ "${PRODUCT}" = "${CP210_PRODID}" ]; then +
-    if [ "${ACTION}" = "remove" ]; then +
-        rm /dev/${SYMLINK} +
-        logger -t hotplug "Symlink /dev/${SYMLINK} removed" +
-    fi+
 fi fi
 EOF EOF
 </code> </code>
- +==== Script that detects whether plugged usb device is bluetooth or not ====
-==== Script that detects if plugged usb device is bluetooth or not ====+
 <code bash> <code bash>
 cat << "EOF" > /etc/hotplug.d/usb/20-bt_test cat << "EOF" > /etc/hotplug.d/usb/20-bt_test
Line 277: Line 265:
     add)     add)
             # start process             # start process
-        /etc/init.d/mjpg-streamer start+        service mjpg-streamer start
             ;;             ;;
     remove)     remove)
             # stop process             # stop process
-        /etc/init.d/mjpg-streamer stop+        service mjpg-streamer stop
             ;;             ;;
 esac esac
Line 315: Line 303:
  
 ==== Coldplug ==== ==== Coldplug ====
-If you had notice the udev and eudev were removed in the openwrt 18.0.* release, don'be afraid because you still can make the things works.+You may have noticed that the udev and eudev were removed in the openwrt 18.0.* release. Don'worry, because you still can make things work.
 You can use hotplug scripts as coldplug. You can use hotplug scripts as coldplug.
-Pay attention at the ACTION environment variable, at the boot are executed 'bind' actions. +Pay attention to the ACTION environment variable, at boot 'bind' actions are executed
-So, just add this option to hotplug run accordinly.+So, just add this option to hotplug and run accordingly. 
 In my case I used this: In my case I used this:
  
Line 370: Line 359:
   esac   esac
 fi fi
 +EOF
 +</code>
 +
 +==== Log DSL status changes ====
 +If you have a DSL modem you can add logging of DSL status changes and connection data rates.
 +
 +This can be helpful if your DSL connection is affected by Seamless Rate Adaptation (SRA) or
 +Dynamic Line Management (DLM) events; for SRA events data rate changes will be logged, while
 +for DLM events connection status changes will be logged as the modem is forced to retrain and then the new
 +data rates will be logged.
 +
 +<code bash>
 +cat << "EOF" > /etc/hotplug.d/dsl/20-dsl_status
 +case "${DSL_NOTIFICATION_TYPE}" in
 +(DSL_INTERFACE_STATUS)
 +logger -p daemon.notice -t dsl-notify "${DSL_XTU_STATUS} link status: ${DSL_INTERFACE_STATUS}" ;;
 +(DSL_DATARATE_STATUS_US)
 +logger -p daemon.notice -t dsl-notify "DSL upstream actual data rate: ${DSL_DATARATE_US_BC0}" ;;
 +(DSL_DATARATE_STATUS_DS)
 +logger -p daemon.notice -t dsl-notify "DSL downstream actual data rate: ${DSL_DATARATE_DS_BC0}" ;;
 +esac
 +EOF
 +</code>
 +
 +This script assumes that channel bonding is not compiled in to the DSL support.
 +
 +==== Utilize wireless USB adapter ====
 +Restart Wi-Fi when plugging in a wireless USB adapter.
 +
 +<code bash>
 +mkdir -p /etc/hotplug.d/usb
 +cat << "EOF" > /etc/hotplug.d/usb/20-rtl8188su
 +if [ "${PRODUCT}" = "bda/8171/200" ] \
 +&& [ "${ACTION}" = "add" ]
 +then wifi
 +fi
 +EOF
 +</code>
 +
 +The above code matches the following device.
 +
 +<code bash>
 +# lsusb -v
 + idVendor 0x0bda Realtek Semiconductor Corp.
 + idProduct 0x8171 RTL8188SU 802.11n WLAN Adapter
 + bcdDevice 2.00
 +</code>
 +
 +==== Get specific IP address ====
 +Assuming your ISP provides a dynamic IP address.
 +Reconnect until you get the one matching a specific regexp.
 +Delay for 10 seconds between reconnects.
 +Set up [[docs:guide-user:advanced:hotplug_extras|Hotplug extras]] to trigger the script upon connecting WAN interface.
 +
 +<code bash>
 +mkdir -p /etc/hotplug.d/online
 +cat << "EOF" > /etc/hotplug.d/online/10-wan-ipaddr
 +. /lib/functions/network.sh
 +network_flush_cache
 +network_find_wan WAN_IF
 +network_get_ipaddr WAN_ADDR "${WAN_IF}"
 +if [ "${WAN_IF}" != "${INTERFACE}" ]
 +then exit 0
 +fi
 +case ${WAN_ADDR} in
 +(??.???.*) exit 0 ;;
 +esac
 +sleep 10
 +ifup ${INTERFACE}
 EOF EOF
 </code> </code>
  
 ==== Rename interfaces by MAC address ==== ==== Rename interfaces by MAC address ====
-A simple method to configure persistent interface names by MAC address.+Assuming pre-configured upstream interfaces ''wana'' and ''wanb''
 +Set up persistent interface names by MAC address.
  
 <code bash> <code bash>
Line 380: Line 439:
 dev_rename() { dev_rename() {
 local DEV_CONF="${1}" local DEV_CONF="${1}"
-local DEV_ONAME DEV_NAME DEV_MAC +local DEV_MAC DEV_NAME DEV_ONAME
-config_get DEV_NAME "${DEV_CONF}" name+
 config_get DEV_MAC "${DEV_CONF}" mac config_get DEV_MAC "${DEV_CONF}" mac
 +config_get DEV_NAME "${DEV_CONF}" name
 DEV_ONAME="$(grep -l -e "${DEV_MAC}" \ DEV_ONAME="$(grep -l -e "${DEV_MAC}" \
 $(find /sys/class/net/*/device/uevent \ $(find /sys/class/net/*/device/uevent \
 | sed -e "s|/device/uevent$|/address|") \ | sed -e "s|/device/uevent$|/address|") \
 | awk -F '/' '{print $5}')" | awk -F '/' '{print $5}')"
-if [ "${DEV_ONAME}" != "${DEV_NAME}" ]+if [ -n "${DEV_MAC}" ] \ 
 +&& [ "${DEV_ONAME}" != "${DEV_NAME}" ]
 then ip link set "${DEV_ONAME}" name "${DEV_NAME}" then ip link set "${DEV_ONAME}" name "${DEV_NAME}"
 fi fi
Line 396: Line 456:
 EOF EOF
  
-uci set network.wana.device="wana" +while read -r DEV_NAME DEV_MAC 
-uci set network.wanb.device="wanb" +do 
-uci set network.wana6.device="wana+uci set network.${DEV_NAME}.device="${DEV_NAME}
-uci set network.wanb6.device="wanb+uci set network.${DEV_NAME}6.device="${DEV_NAME}
-uci -q delete network.wana_dev +uci -q delete network.${DEV_NAME}_dev 
-uci set network.wana_dev="device" +uci set network.${DEV_NAME}_dev="device" 
-uci set network.wana_dev.name="wana+uci set network.${DEV_NAME}_dev.mac="${DEV_MAC}
-uci set network.wana_dev.mac="11:22:33:44:55:66" +uci set network.${DEV_NAME}_dev.name="${DEV_NAME}" 
-uci -q delete network.wanb_dev +done << EOI 
-uci set network.wanb_dev="device" +wana 11:22:33:44:55:66 
-uci set network.wanb_dev.name="wanb+wanb aa:bb:cc:dd:ee:ff 
-uci set network.wanb_dev.mac="aa:bb:cc:dd:ee:ff"+EOI
 uci commit network uci commit network
-/etc/init.d/network restart+service network restart
 </code> </code>
  
-==== log DSL status changes ==== +==== Create deterministic/persistent links to USB-serial devices ==== 
-If you have a DSL modem you can add logging of DSL status changes and connection data rates. +Resulting links will be placed in /dev/serial/by-id and /dev/serial/by-path, 
- +with a name structure closely resembling links created by udevbut not fully exact.
-This can be helpful if your DSL connection is affected by Seamless Rate Adaptation (SRA) or +
-Dynamic Line Management (DLM) events; for SRA events data rate changes will be loggedwhile +
-for DLM events connection status changes will be logged as the modem is forced to retrain and then the new +
-data rates will be logged.+
  
 <code bash> <code bash>
-cat << "EOF" > /etc/hotplug.d/dsl/20-dsl_status +set -o pipefail 
-case "${DSL_NOTIFICATION_TYPE}" in +"${ACTION}" "bind" -"${ACTION}" = "unbind" ] || exit 0 
-    "DSL_INTERFACE_STATUS")    logger -p daemon.notice -t "dsl-notify" "${DSL_XTU_STATUSlink status: ${DSL_INTERFACE_STATUS}";; +[ "${SUBSYSTEM}" "usb-serial] || exit 0 
-    "DSL_DATARATE_STATUS_US")  logger -p daemon.notice -t "dsl-notify" "DSL upstream actual data rate: ${DSL_DATARATE_US_BC0}";; +-"${DEVICENAME}" --"${DEVPATH}" ] || exit 1
-    "DSL_DATARATE_STATUS_DS" logger -p daemon.notice -"dsl-notify" "DSL downstream actual data rate: ${DSL_DATARATE_DS_BC0}";; +
-esac +
-EOF +
-</code>+
  
-Note that this example script assumes that channel bonding is not compiled in to the DSL support.+if [ "${ACTION}" = "bind" ]; then 
 +        subsystem="$(basename $(readlink /sys${DEVPATH}/../subsystem))"
  
 +        [ "$subsystem" = "usb" ] || exit 0
 +
 +        replace_whitespace="s/^[ \t]*|[ \t]*$//g; s/[ \t]+/_/g"
 +        manufacturer="$(cat /sys${DEVPATH}/../../manufacturer | sed -E "${replace_whitespace}")" || manufacturer="$(cat /sys${DEVPATH}/../../idVendor)"
 +        product="$(cat /sys${DEVPATH}/../../product | sed -E "${replace_whitespace}")" || product="$(cat /sys${DEVPATH}/../../idProduct)"
 +        serial="$(cat /sys${DEVPATH}/../../serial | sed -E "${replace_whitespace}")"
 +        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}${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
 +</code>
 +[[https://gist.github.com/Leo-PL/b5ee737e49b34c1551dba6c182707c8e|(Source)]]
  • Last modified: 2024/12/19 04:42
  • by pythonic