You are not logged in.
Pages: 1
Topic closed
I am trying to create an udev rule to automatically turn off the builtin Wi-Fi chip of my laptop when my Wi-Fi dongle gets connected. The udev rule I came up with:
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0cf3", ATTRS{idProduct}=="9271", ENV{DEVTYPE}=="usb_device", NAME="wlp0s29", RUN+="/home/roysten/test.sh add"
This works, the test.sh script is executed on connection of my dongle. I managed to turn off the builtin Wi-Fi chip using the command: "ip link set wlp3s0 down". So far so good.
Next I wanted to automatically turn on the just connected dongle using the command: "ip link set wlp0s29 up". This doesn't work, because the device name of the Wi-Fi dongle doesn't get set in time by udev. When my script gets executed the output of "ip link list" shows the Wi-Fi dongle as "wlan0" and not the desired name "wlp0s29". Executing "ip link list" manually after my script has finished does show the correct name!
Defining the device name in a separate udev rule (which gets executed before the rule above) doesn't solve the problem.
The script that gets executed by udev, (test.sh):
#!/bin/bash
if [ $1 == "remove" ]; then
ip link set wlp3s0 up
elif [ $1 == "add" ]; then
ip link set wlp3s0 down
echo `ip link list` >> /home/roysten/output.log
ip link set wlp0s29 up
fi
I think the problem is that I am trying to access the device before it had a chance to properly initialize. I tried adding "sleep 3s" before setting up the dongle but then it still has the incorrect name (probably because udev hangs because of sleep). I tried using nohup but I could not get that to work (script doesn't execute at all). Does someone know what is going on here and how I can get around it?
Offline
Can everyone please stop showing udev rules without showing the *filename*
Because these rules are executed in strict order, and the order is *vital*.
Use e.g. /etc/udev/rules.d/91-local.rules - so that your rule is executed *after* the interface is renamed.
Offline
You are right, I should've mentioned the file names. The rename rule is in a file name "10-local.rules" and my dongle rule is in a file named "60-dongle.rules", so lexicographically the order is correct.
Offline
So have you checked where the rename to "wlp0s29" happens? Could it be in a file named e.g. 70, 80 or 90? If so, your 60 isn't high enough.
60 is stupidly leaving all this to chance. Use 99.
Since udev (well, eudev anyway) now provides "95-udev-late.rules", I'll have to recommend 99 rather than 91
Offline
To turn on the just-connected dongle wouldn't it be simpler to use a udev rule that refers to said dongle's MAC address? That address is immutable and it doesn't seem likely that you would need to "wait" for it to be recognised (I think ...)?
Offline
So have you checked where the rename to "wlp0s29" happens? Could it be in a file named e.g. 70, 80 or 90? If so, your 60 isn't high enough.
60 is stupidly leaving all this to chance. Use 99.
Since udev (well, eudev anyway) now provides "95-udev-late.rules", I'll have to recommend 99 rather than 91
I changed it to 99-dongle.rules, but the dongle still shows up as "wlan0". The file containing the rename rule is still named 10-local.rules.
To turn on the just-connected dongle wouldn't it be simpler to use a udev rule that refers to said dongle's MAC address? That address is immutable and it doesn't seem likely that you would need to "wait" for it to be recognised (I think ...)?
Well I can use the MAC address for udev to detect the device, but this won't solve my problem because I still have to use "ip link set <<devicename>> up" to enable the device.
Offline
Try adding "sleep 10" at the start of your script, to see if it's a race condition.
Offline
I tried that, doesn't work Device name still reads "wlan0", even after waiting ten seconds...
Offline
I had a look at the output of
$ udevadm monitor --subsystem=net
while executing
$ udevadm trigger
in a different terminal.
It seems that the reason the udev rule method doesn't work is because the persistent renaming generates a change action, not an add action. So the rule needs to look like this:
ACTION="change", SUBSYSTEM="net", KERNEL="wlp0s29", ...
Offline
Thanks for sharing. I'm going to go ahead and close this old topic now.
Closing.
Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD
Making lemonade from lemons since 2015.
Offline
Pages: 1
Topic closed