chiark / gitweb /
rules_generator: skip random MAC addresses
[elogind.git] / extras / rule_generator / write_net_rules
index b709200a351c0f209057a05efe5ac2d86c637f26..62acb8820ce75ff1477866be634c7d1d2b8cccff 100644 (file)
@@ -42,7 +42,7 @@ write_rule() {
        if [ "$PRINT_HEADER" ]; then
                PRINT_HEADER=
                echo "# This file was automatically generated by the $0"
-               echo "# program, probably run by the persistent-net-generator.rules rules file."
+               echo "# program run by the persistent-net-generator.rules rules file."
                echo "#"
                echo "# You can modify it, as long as you keep each rule on a single line."
        fi
@@ -53,51 +53,48 @@ write_rule() {
        } >> $RULES_FILE
 }
 
-# used only if $RULES_FILE is empty, like on installation
-if [ "$1" = "all_interfaces" ]; then
-       if [ -e $RULES_FILE ]; then
-               printf "$RULES_FILE exists, persistent interface names\nnot saved.\n" >&2
-               exit 0
-       fi
-
-       if [ ! -e /sys/class/net/ ]; then
-               echo "/sys/class/net/ is not available, persistent interface names not saved." >&2
-               exit 0
-       fi
-
-       cd /sys/class/net/ || return 0
-
-       for INTERFACE in *; do
-               case $INTERFACE in
-               eth*|ath*|wlan*|ra*|sta*) ;;
-               *) continue ;;
-               esac
-
-               INTERFACE="$INTERFACE" DEVPATH="/class/net/$INTERFACE" \
-                       /lib/udev/write_net_rules || true
-       done
-
-       exit 0
-fi
-
 if [ -z "$INTERFACE" ]; then
        echo "Missing \$INTERFACE." >&2
        exit 1
 fi
 
 if [ "$1" ]; then
-       MAC_ADDR="$1"
+    while [ "$*" ] ; do
+       case $1 in
+           --mac)
+               shift
+               MAC_ADDR=$1
+               shift
+               ;;
+           --driver)
+               shift
+               DRIVER=$1
+               shift
+               ;;
+           --id)
+               shift
+               ID=$1
+               shift
+               ;;
+           *)
+               MAC_ADDR=$1
+               shift
+               ;;
+       esac
+    done
 else
-       MAC_ADDR=$(sysread address)
+    MAC_ADDR=$(sysread address)
 fi
 
-if [ -z "$MAC_ADDR" ]; then
+if [ -z "$DRIVER" ] && [ -z "$ID" ] ; then
+    if [ -z "$MAC_ADDR" ]; then
        echo "No MAC address for $INTERFACE." >&2
        exit 1
-fi
-if [ "$MAC_ADDR" = "00:00:00:00:00:00" ]; then
+    fi
+    if [ "$MAC_ADDR" = "00:00:00:00:00:00" ]; then
        echo "NULL MAC address for $INTERFACE." >&2
        exit 1
+    fi
 fi
 
 # Prevent concurrent processes from modifying the file at the same time.
@@ -117,9 +114,13 @@ if interface_name_taken; then
 fi
 
 # the DRIVERS key is needed to not match bridges and VLAN sub-interfaces
-match="DRIVERS==\"?*\", ATTRS{address}==\"$MAC_ADDR\""
+if [ "$MAC_ADDR" ] ; then
+    match="DRIVERS==\"?*\", ATTR{address}==\"$MAC_ADDR\""
+else
+    match="DRIVERS==\"$DRIVER\", KERNELS==\"$ID\""
+fi
 if [ $basename = "ath" -o $basename = "wlan" ]; then
-       match="$match, ATTRS{type}==\"1\"" # do not match the wifi* interfaces
+       match="$match, ATTR{type}==\"1\"" # do not match the wifi* interfaces
 fi
 
 write_rule "$match" "$INTERFACE" "$COMMENT"