[systemd-devel] Rename NIC by using dynamic MAC

Richard Maw richard.maw at codethink.co.uk
Thu Dec 3 05:55:26 PST 2015


On Thu, Dec 03, 2015 at 01:25:03PM +0000, Kaesbauer Michael wrote:
> Hi,
> 
> up to now I found no way to write an udev rule which renames a specific NIC by using a MAC address that is returned by a bash script. The reason for getting the MAC address by the bash script is that the renaming NIC rule should be applied to different CPUs (with different MACs).

I'm finding it a bit difficult to follow what you mean here.

You have different machines with different numbers of NICs
and want a particular interface to be given a specific name
based on which machine it is running on;
and your current implementation wants to do it by the number of NICs?

http://www.freedesktop.org/software/systemd/man/systemd.link.html#[Match]%20Section%20Options
may be of interest,
since you could collect the contents of /etc/machine-id or /etc/hostname
and have networkd .link config files set the interface name that way.

Something like the following:

    # /etc/systemd/network/machine1.link
    [Match]
    OriginalName=net0 # name the kernel gave the interface initially
    Host=machine1 # from /etc/hostname
    [Link]
    Name=enp1s0
    # /etc/systemd/network/machine2.link
    [Match]
    OriginalName=net0 # name the kernel gave the interface initially
    Host=machine2 # from /etc/hostname
    [Link]
    Name=enp3s0
    # /etc/systemd/network/machine3.link
    [Match]
    OriginalName=net0 # name the kernel gave the interface initially
    Host=machine3 # from /etc/hostname
    [Link]
    Name=enp3s0

> 
> 
> Here is my udev rule: /etc/udev/rules.d/81-net-rename.rules
> 
> SUBSYSTEM!="net", GOTO="net_name_slot_end"
> ACTION!="add|change", GOTO="net_name_slot_end"
> KERNEL=="*", ATTR{type}=="1", PROGRAM="/bin/getMAC_DHCP.sh", ATTR{address}=="%c", NAME="net0"
> LABEL="net_name_slot_end"
> 
> 
> Here is my bash script:
> 
> #!/bin/bash
> 
> NIC_ID=
> MACS=(`cat /sys/class/net/*/address`)
> 
> len=${#MACS[*]}
> 
> NIC_CNT=0
> for ((i=0; i != len; i++)); do
> mac_str=${MACS[$i]}
> if [ $mac_str != "00:00:00:00:00:00" ]; then
>   let NIC_CNT=$NIC_CNT+1
> fi
> done

By the way, you could simplify this to:

    NIC_CNT=0
    for mac_str in `cat /sys/class/net/*/address`; do
        if [ "$mac_str" != "00:00:00:00:00:00" ]; then
            let NIC_CNT=$NIC_CNT+1
        fi
    done

or maybe even

    NIC_CNT=$(cat /sys/class/net/*/address | grep -v '00:00:00:00:00:00' | wc -l)

> 
> if [ $NIC_CNT -eq 2 ]; then
>   NIC_ID="enp1s0"
> fi
> if [ $NIC_CNT -eq 3 ]; then
> NIC_ID="enp3s0"
> fi
> if [ $NIC_CNT -eq 4 ]; then
>   NIC_ID="enp3s0"
> fi
> echo $NIC_ID >> $LOG
> 
> cat /sys/class/net/$NIC_ID/address
> 
> 
> Kind regards
> Michael
> 
> ______________________________________________________________
> 
> KRONES AG
> Vorstand: Volker Kronseder (Vors.),
> Christoph Klenk, Rainulf Diepold, Thomas Ricker, Markus Tischer, Ralf Goldbrunner
> Vors. des Aufsichtsrates: Ernst Baumann
> Sitz Neutraubling
> Registergericht Regensburg HRB 2344
> ______________________________________________________________
> 
> Der Inhalt dieser E-Mail und jeder Anhang ist vertraulich.
> Diese Inhalte sind nur fuer die benannten Adressaten.
> Wenn Sie diese E-Mail durch einen Fehler erhalten haben,
> benachrichtigen Sie sofort Ihren Administrator oder den Absender.
> Behandeln Sie die E-Mail vertraulich.
> 
> * Diese E-Mail wurde auf Viren und gefaehrlichen Inhalt geprueft. *
> ______________________________________________________________
> 
> The contents of this email and any attachments are confidential.
> They are intended for the named recipient(s) only.
> If you have received this email in error please notify the system manager
> or the sender immediately and do not disclose the contents to anyone or
> make copies.
> 
> * This e-Mail was scanned for viruses, vandals and malicious content. *

> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel



More information about the systemd-devel mailing list