You are not logged in.

#1 2015-04-30 17:34:38

Roysten
Member
Registered: 2014-10-30
Posts: 14

Wi-Fi dongle udev rule

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

#2 2015-04-30 19:10:00

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: Wi-Fi dongle udev rule

Can everyone please stop showing udev rules without showing the *filename* wink

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

#3 2015-04-30 19:41:52

Roysten
Member
Registered: 2014-10-30
Posts: 14

Re: Wi-Fi dongle udev rule

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. wink

Offline

#4 2015-04-30 20:26:28

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: Wi-Fi dongle udev rule

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 smile

Offline

#5 2015-04-30 22:33:24

MoonSwan
Member
From: Great White North
Registered: 2008-01-23
Posts: 881

Re: Wi-Fi dongle udev rule

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

#6 2015-05-01 15:08:17

Roysten
Member
Registered: 2014-10-30
Posts: 14

Re: Wi-Fi dongle udev rule

brebs wrote:

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 smile

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.

MoonSwan wrote:

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

#7 2015-05-01 15:51:27

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: Wi-Fi dongle udev rule

Try adding "sleep 10" at the start of your script, to see if it's a race condition.

Offline

#8 2015-05-01 16:35:58

Roysten
Member
Registered: 2014-10-30
Posts: 14

Re: Wi-Fi dongle udev rule

I tried that, doesn't work sad Device name still reads "wlan0", even after waiting ten seconds...

Offline

#9 2017-03-25 01:47:39

huba
Member
Registered: 2017-03-25
Posts: 1

Re: Wi-Fi dongle udev rule

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

#10 2017-03-25 04:37:14

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,788
Website

Re: Wi-Fi dongle udev rule

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

Board footer

Powered by FluxBB