Differences
This shows you the differences between two versions of the page.
| 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] vgaetera | docs: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 ''/ | + | Procd (the init system and process management daemon) executes scripts located in ''/ |
| 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 |
| - | | **block** | Block device events: device connected/ | + | | **block** |
| - | | **button** | Buttons: not created by default, see [[docs: | + | | **button** |
| - | | **dhcp** | DHCP-related events | | + | | **dhcp** |
| - | | **dsl** | DSL modem events | | + | | **dsl** |
| - | | **firewall** | Firewall-related events | | + | | **firewall** |
| - | | **iface** | Interface events: LAN/ | + | | **iface** |
| - | | **neigh** | Neighbor discovery | | + | | **neigh** |
| - | | **net** | Network-related events | | + | | **net** |
| - | | **ntp** | Time sync events: time step, time server stratum change | | + | | **ntp** |
| - | | **tftp** | TFTP-related events | | + | | **tftp** |
| - | | **usb** | USB devices like 3g-modem and tty* | | + | | **tty** |
| + | | **usb** | ||
| + | | **usbmisc** | ||
| 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 | " | ||
| + | | DEVICENAME | configured interface names (br-lan, wlan0, phy1-ap0 | | ||
| + | | PATH | Full path | | ||
| + | | DEVPATH | full device path (for example "/ | ||
| + | | DEVTYPE | what the DEVICENAME are names of, ie. br-lan, phy1-ap0 | | ||
| + | | INTERFACE | configured interfaces as in DEVTYPE | | ||
| + | | SEQNUM | seqnum (a number) | | ||
| + | | SUBSYSTEM | always = " | ||
| + | | 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 " | + | Even without NTP sync, you will receive a " |
| + | |||
| + | ==== tty ==== | ||
| + | ^ Variable name ^ description ^ | ||
| + | | ACTION | add, remove, bind, unbind as above | | ||
| + | | DEVICENAME | eg " | ||
| + | | DEVNAME | only for bind/ | ||
| + | | DEVPATH | eg, "/ | ||
| + | | SEQNUM | (action number since boot for this subsystem) eg 335 | | ||
| + | | SUBSYSTEM | Specific type of device, eg, " | ||
| + | | MAJOR | eg 189 | | ||
| + | | MINOR | eg 1 | | ||
| ==== usb ==== | ==== usb ==== | ||
| ^ Variable name ^ description ^ | ^ Variable name ^ description ^ | ||
| - | | ACTION | add, remove as above | | + | | ACTION | add, remove, bind, unbind |
| + | | DEVICENAME | eg " | ||
| | DEVNAME | eg, " | | DEVNAME | eg, " | ||
| + | | DEVNUM | eg 002 | | ||
| | DEVPATH | eg, "/ | | DEVPATH | eg, "/ | ||
| - | | DEVICENAME | + | | DEVTYPE |
| - | | DEVNUM | eg 002 | | + | |
| - | | DRIVER | "usb" | | + | |
| | TYPE | eg 9/0/1 | | | TYPE | eg 9/0/1 | | ||
| | PRODUCT | the vendor/ | | PRODUCT | the vendor/ | ||
| - | | SEQNUM | ? eg 335 | | + | | SEQNUM | (action number since boot for this subsystem) |
| | BUSNUM | eg 001 | | | BUSNUM | eg 001 | | ||
| | MAJOR | eg 189 | | | MAJOR | eg 189 | | ||
| | MINOR | eg 1 | | | MINOR | eg 1 | | ||
| + | |||
| + | ==== usbmisc ==== | ||
| + | ^ Variable name ^ description | ||
| + | | ACTION | ||
| + | | DEVNAME | ||
| + | | DEVPATH | ||
| + | | DEVICENAME | ||
| + | | SEQNUM | ||
| + | | 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 | + | An other script to create |
| - | Reconnect until you get the one matching a specific regexp. | + | |
| - | Match '' | + | |
| - | Delay for 10 seconds between reconnects. | + | |
| <code bash> | <code bash> | ||
| - | cat << " | + | cat << " |
| - | if [ " | + | CP210_PRODID='10c4/ea60/100' |
| - | && [ " | + | SYMLINK="my_link" |
| - | then exit 0 | + | |
| - | fi | + | |
| - | if [ " | + | |
| - | && [ -z " | + | |
| - | && [ -z " | + | |
| - | then exit 0 | + | |
| - | fi | + | |
| - | . /lib/functions/ | + | |
| - | network_flush_cache | + | |
| - | network_find_wan NET_IF | + | |
| - | network_get_ipaddr NET_ADDR " | + | |
| - | if [ " | + | |
| - | then exit 0 | + | |
| - | fi | + | |
| - | if [ " | + | |
| - | || [[ " | + | |
| - | then exit 0 | + | |
| - | fi | + | |
| - | sleep 10 | + | |
| - | ifup " | + | |
| - | EOF | + | |
| - | </ | + | |
| - | ==== 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 [ " |
| + | [ " | ||
| + | if [ " | ||
| + | if [ -L "/ | ||
| + | logger -t hotplug " | ||
| + | exit 0 | ||
| + | fi | ||
| - | <code bash> | + | DEVICE_NAME=" |
| - | # lsusb -v | + | if [ -z " |
| - | | + | logger -t hotplug ' |
| - | | + | exit 0 |
| - | | + | fi |
| - | </ | + | |
| - | <code bash> | + | logger |
| - | mkdir -p /etc/hotplug.d/usb | + | ln -s "/dev/${DEVICE_NAME}" "/dev/${SYMLINK}" |
| - | cat << | + | logger -t hotplug |
| - | if [ "${PRODUCT}" | + | fi |
| - | && [ "${ACTION}" | + | |
| - | then wifi | + | |
| - | fi | + | |
| - | EOF | + | |
| - | </ | + | |
| - | ==== 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 [ " |
| - | cat << " | + | rm "/ |
| - | CP210_PRODID=" | + | logger -t hotplug " |
| - | SYMLINK=" | + | fi |
| - | + | ||
| - | if [ " | + | |
| - | | + | |
| - | | + | |
| - | if [ -z ${DEVICE_NAME} ]; then | + | |
| - | logger -t hotplug | + | |
| - | exit 0 | + | |
| - | fi | + | |
| - | logger -t hotplug " | + | |
| - | ln -s / | + | |
| - | logger -t hotplug | + | |
| - | fi | + | |
| - | fi | + | |
| - | + | ||
| - | if [ " | + | |
| - | if [ " | + | |
| - | rm / | + | |
| - | logger -t hotplug " | + | |
| - | fi | + | |
| fi | fi | ||
| EOF | EOF | ||
| </ | </ | ||
| - | + | ==== Script that detects | |
| - | ==== Script that detects | + | |
| <code bash> | <code bash> | ||
| cat << " | cat << " | ||
| Line 277: | Line 265: | ||
| add) | add) | ||
| # start process | # start process | ||
| - | | + | |
| ;; | ;; | ||
| remove) | remove) | ||
| # stop process | # stop process | ||
| - | | + | |
| ;; | ;; | ||
| esac | esac | ||
| Line 315: | Line 303: | ||
| ==== Coldplug ==== | ==== Coldplug ==== | ||
| - | If you had notice | + | You may have noticed that the udev and eudev were removed in the openwrt 18.0.* release. Don' |
| You can use hotplug scripts as coldplug. | You can use hotplug scripts as coldplug. | ||
| - | Pay attention | + | Pay attention |
| - | So, just add this option to hotplug run accordinly. | + | So, just add this option to hotplug |
| In my case I used this: | In my case I used this: | ||
| Line 370: | Line 359: | ||
| esac | esac | ||
| fi | fi | ||
| + | EOF | ||
| + | </ | ||
| + | |||
| + | ==== 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 << " | ||
| + | case " | ||
| + | (DSL_INTERFACE_STATUS) | ||
| + | logger -p daemon.notice -t dsl-notify " | ||
| + | (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 | ||
| + | </ | ||
| + | |||
| + | 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 / | ||
| + | cat << " | ||
| + | if [ " | ||
| + | && [ " | ||
| + | then wifi | ||
| + | fi | ||
| + | EOF | ||
| + | </ | ||
| + | |||
| + | 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 | ||
| + | </ | ||
| + | |||
| + | ==== 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: | ||
| + | |||
| + | <code bash> | ||
| + | mkdir -p / | ||
| + | cat << " | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan WAN_IF | ||
| + | network_get_ipaddr WAN_ADDR " | ||
| + | if [ " | ||
| + | then exit 0 | ||
| + | fi | ||
| + | case ${WAN_ADDR} in | ||
| + | (??.???.*) exit 0 ;; | ||
| + | esac | ||
| + | sleep 10 | ||
| + | ifup ${INTERFACE} | ||
| EOF | EOF | ||
| </ | </ | ||
| ==== Rename interfaces by MAC address ==== | ==== Rename interfaces by MAC address ==== | ||
| - | A simple method to configure | + | Assuming pre-configured upstream interfaces '' |
| + | Set up persistent interface names by MAC address. | ||
| <code bash> | <code bash> | ||
| Line 380: | Line 439: | ||
| dev_rename() { | dev_rename() { | ||
| local DEV_CONF=" | local DEV_CONF=" | ||
| - | local DEV_ONAME DEV_NAME | + | local DEV_MAC DEV_NAME |
| - | config_get | + | |
| config_get DEV_MAC " | config_get DEV_MAC " | ||
| + | config_get DEV_NAME " | ||
| DEV_ONAME=" | DEV_ONAME=" | ||
| $(find / | $(find / | ||
| | sed -e " | | sed -e " | ||
| | awk -F '/' | | awk -F '/' | ||
| - | if [ " | + | if [ -n " |
| + | && | ||
| then ip link set " | then ip link set " | ||
| fi | fi | ||
| Line 396: | Line 456: | ||
| EOF | EOF | ||
| - | uci set network.wana.device=" | + | while read -r DEV_NAME DEV_MAC |
| - | uci set network.wanb.device=" | + | do |
| - | uci set network.wana6.device=" | + | uci set network.${DEV_NAME}.device=" |
| - | uci set network.wanb6.device=" | + | uci set network.${DEV_NAME}6.device=" |
| - | uci -q delete network.wana_dev | + | uci -q delete network.${DEV_NAME}_dev |
| - | uci set network.wana_dev=" | + | uci set network.${DEV_NAME}_dev=" |
| - | uci set network.wana_dev.name="wana" | + | uci set network.${DEV_NAME}_dev.mac="${DEV_MAC}" |
| - | uci set network.wana_dev.mac=" | + | uci set network.${DEV_NAME}_dev.name="${DEV_NAME}" |
| - | uci -q delete network.wanb_dev | + | done << EOI |
| - | uci set network.wanb_dev=" | + | wana 11: |
| - | uci set network.wanb_dev.name=" | + | wanb aa: |
| - | uci set network.wanb_dev.mac=" | + | EOI |
| uci commit network | uci commit network | ||
| - | / | + | service |
| </ | </ | ||
| - | ==== log DSL status changes | + | ==== Create deterministic/ |
| - | If you have a DSL modem you can add logging of DSL status changes | + | Resulting links will be placed in / |
| - | + | with a name structure closely resembling links created | |
| - | This can be helpful if your DSL connection is affected | + | |
| - | 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> | <code bash> | ||
| - | cat << " | + | set -o pipefail |
| - | case "${DSL_NOTIFICATION_TYPE}" | + | [ "${ACTION}" |
| - | | + | [ "${SUBSYSTEM}" |
| - | " | + | [ -n "${DEVICENAME}" -a -n "${DEVPATH}" |
| - | " | + | |
| - | esac | + | |
| - | EOF | + | |
| - | </ | + | |
| - | Note that this example script assumes that channel bonding is not compiled in to the DSL support. | + | if [ " |
| + | subsystem=" | ||
| + | [ " | ||
| + | |||
| + | replace_whitespace=" | ||
| + | manufacturer=" | ||
| + | product=" | ||
| + | serial=" | ||
| + | interface=" | ||
| + | port=" | ||
| + | |||
| + | replace_chars=" | ||
| + | id_link=$(echo " | ||
| + | path_link=$(echo " | ||
| + | |||
| + | mkdir -p / | ||
| + | ln -sf "/ | ||
| + | ln -sf "/ | ||
| + | elif [ " | ||
| + | for link in $(find /dev/serial -type l); do | ||
| + | [ -L ${link} -a " | ||
| + | done | ||
| + | fi | ||
| + | </ | ||
| + | [[https:// | ||