2009-10-29 74 views
1

我寫的udev規則自動重命名和編號具有特定MAC地址的網卡。使用udev自動重命名和編號nics使用udev

產生的規則應該這樣做幾乎相同75-persistent-net-generator.rules不(符合前3個字節網卡的MAC地址,這取決於如何此供應商的多卡安裝它命名爲「mycard *」,寫重命名規則爲70-persistent-net.rules )。

這是多遠我來直到如今

# udev rules to name rename cards to mycard 

ACTION!="add", GOTO="persistent_mycard_generator_end" 
SUBSYSTEM!="net", GOTO="persistent_mycard_generator_end" 

# ignore the interface if a name has already been set 
NAME=="mycard*", GOTO="persistent_mycard_generator_end" 

# device name whitelist 
KERNEL!="eth*", GOTO="persistent_mycard_generator_end" 

# read MAC address 
ENV{MATCHADDR}="$attr{address}" 

# match interface type 
ENV{MATCHIFTYPE}="$attr{type}" 

# ignore non mycard MAC addresses 
ENV{MATCHADDR}!="00:11:22:*", GOTO="persistent_mycard_generator_end" 

# default comment 
ENV{COMMENT}=="", ENV{COMMENT}="mycard connected through ($attr{driver})" 

#### THIS IS THE PART I DON'T GET #### 

# write rule 
DRIVERS=="?*", IMPORT{program}="write_net_rules" 

# rename interface if needed 
ENV{INTERFACE_NEW}=="?*", NAME="mycard*" 

#### THIS IS THE END OF THE PART I DON'T GET #### 

LABEL="persistent_mycard_generator_end 

任務「部分我不明白」應該做的是重命名卡(可以說這是ETH3)如果要mycard0或它是系統中具有匹配的MAC地址mycard1等的第二張卡。

由於提前, flokra

回答

0

如果設置了ENV {接口}爲「mycard0」呼叫write_net_rules之前,它會找到第一個未使用mycardN你,寫出它的規則,並返回名稱在ENV {INTERFACE_NEW}中。

+0

幾乎就是這樣。留下一個問題:編寫的規則具有'KERNEL == mycard *'而不是'KERNEL == eth *',因此不適用。我今天早上寫了一個完整的解決方案,並在我回家時發佈。 – flokra 2009-11-06 15:19:25

0

OK,這裏是我的解決方案(我已經在Debian 5.0和Ubuntu 9.04進行了測試,所以我不知道這是否與其他發行版的udev進行的實現工作):

  1. 75-持久mycard-generator.rules

    ACTION!="add", GOTO="persistent_mycard_generator_end" 
    SUBSYSTEM!="net", GOTO="persistent_mycard_generator_end" 
    
    # ignore the interface if a name has already been set 
    NAME=="?*", GOTO="persistent_mycard_generator_end" 
    
    # device name whitelist 
    KERNEL!="eth*", GOTO="persistent_mycard_generator_end" 
    
    # by default match on the MAC address and interface type 
    ENV{MATCHADDR}="$attr{address}" 
    ENV{MATCHIFTYPE}="$attr{type}" 
    
    # match interface dev_id 
    ATTR{dev_id}=="?*", ENV{MATCHDEVID}="$attr{dev_id}" 
    
    # by default match on the MAC address and interface type 
    ENV{MATCHADDR}="$attr{address}" 
    ENV{MATCHIFTYPE}="$attr{type}" 
    
    # match interface dev_id 
    ATTR{dev_id}=="?*", ENV{MATCHDEVID}="$attr{dev_id}" 
    
    # terminate processing if card is not a mycard 
    ENV{MATCHADDR}!="AA:BB:CC:*", GOTO="persistent_mycard_generator_end" 
    
    # provide nice comments for the generated rules 
    SUBSYSTEMS=="pci", ENV{COMMENT}="PCI device $attr{vendor}:$attr{device}" 
    SUBSYSTEMS=="pcmcia", ENV{COMMENT}="PCMCIA device $attr{card_id}:$attr{manf_id}" 
    SUBSYSTEMS=="usb", ENV{COMMENT}="USB device 0x$attr{idVendor}:0x$attr{idProduct}" 
    ENV{COMMENT}=="", ENV{COMMENT}="Unknown $env{SUBSYSTEM} device($env{DEVPATH})" 
    ATTRS{driver}=="?*", ENV{COMMENT}="$env{COMMENT} ($attr{driver})" 
    
    # add mycard to comment 
    ENV{COMMENT}="$env{COMMENT} (mycard)" 
    
    # set interface name to mycard0 (initially) 
    ENV{INTERFACE}="mycard0" 
    
    # generate and write the rule 
    DRIVERS=="?*", IMPORT{program}="write_mycard_rules" 
    
    # rename the interface if requested 
    ENV{INTERFACE_NEW}=="?*",NAME="$env{INTERFACE_NEW}" 
    
    LABEL="persistent_mycard_generator_end" 
    
  2. write_mycard_rules

    RULES_FILE='/etc/udev/rules.d/70-persistent-net.rules' 
    
    . /lib/udev/hotplug.functions 
    
    interface_name_taken() { 
        local value="$(find_all_rules 'NAME=' $INTERFACE)" 
        if [ "$value" ]; then 
         return 0 
        fi 
        return 1 
    
    } 
    
    find_next_available() { 
        raw_find_next_available "$(find_all_rules 'NAME=' "$1")" 
    } 
    
    write_rule() { 
        local match="$1" 
        local name="$2" 
        local comment="$3" 
    
        { 
        if [ "$PRINT_HEADER" ]; then 
         PRINT_HEADER= 
         echo "# This file was automatically generated by the $0" 
         echo "# program run by the persistent-mycard-generator.rules rules file." 
         echo "#" 
         echo "# You can modify it, as long as you keep each rule on a single line." 
        fi 
    
        echo "" 
        [ "$comment" ] && echo "# $comment" 
        echo "SUBSYSTEM==\"net\", ACTION==\"add\"$match, NAME=\"$name\"" 
        } >> $RULES_FILE 
    } 
    
    if [ -z "$INTERFACE" ]; then 
        echo "missing \$INTERFACE" >&2 
        exit 1 
    fi 
    
    # Prevent concurrent processes from modifying the file at the same time. 
    lock_rules_file 
    
    # Check if the rules file is writeable. 
    choose_rules_file 
    
    # the DRIVERS key is needed to not match bridges and VLAN sub-interfaces 
    if [ "$MATCHADDR" ]; then 
        match="$match, DRIVERS==\"?*\", ATTR{address}==\"$MATCHADDR\"" 
    fi 
    
    
    if [ "$MATCHDRV" ]; then 
        match="$match, DRIVERS==\"$MATCHDRV\"" 
    fi 
    
    if [ "$MATCHDEVID" ]; then 
        match="$match, ATTR{dev_id}==\"$MATCHDEVID\"" 
    fi 
    
    
    if [ "$MATCHID" ]; then 
        match="$match, KERNELS==\"$MATCHID\"" 
    fi   
    
    if [ "$MATCHIFTYPE" ]; then 
        match="$match, ATTR{type}==\"$MATCHIFTYPE\"" 
    fi 
    
    
    if [ -z "$match" ]; then 
        echo "missing valid match" >&2 
        unlock_rules_file 
        exit 1 
    fi 
    
    basename=${INTERFACE%%[0-9]*} 
    match="$match, KERNEL==\"eth*\"" 
    if [ "$INTERFACE_NAME" ]; then 
        # external tools may request a custom name 
        COMMENT="$COMMENT (custom name provided by external tool)" 
    
        if [ "$INTERFACE_NAME" != "$INTERFACE" ]; then 
         INTERFACE=$INTERFACE_NAME; 
         echo "INTERFACE_NEW=$INTERFACE" 
        fi 
    else 
        # if a rule using the current name already exists, find a new name 
        if interface_name_taken; then 
         INTERFACE="$basename$(find_next_available "$basename[0-9]*")" 
         echo "INTERFACE_NEW=$INTERFACE" 
        fi 
    fi 
    
    write_rule "$match" "$INTERFACE" "$COMMENT" 
    unlock_rules_file 
    exit 0 
    

75-persistent-mycard-generator.rules中最重要的更改是ENV{INTERFACE}="mycard0",它設置卡片應該獲得的名稱以及match="$match, KERNEL==\"eth*\""位於write_mycard_rules中,這迫使udev不使用新名稱覆蓋已使用的內核子系統。