[systemd-devel] Rename NIC by using dynamic MAC

Kaesbauer Michael Michael.Kaesbauer at krones.com
Thu Dec 3 07:13:38 PST 2015


Hi again, 

thanks for your quick reply.

Yes, I have many machines and each of those machines can have different number of NICs [2-4]. I want that the first NIC, at a specific port, has always the same NIC name. 

As the NIC name, produced by udev, changes when adding more NICs, I tried to solve that problem by adding my 81-net-rename.rules rule. By the way udev does not provide a predictable NIC name in my case, because the NICs do not supply any information to SMBIOS and therefore udev uses that PCI-name based NIC naming scheme. This is reason why the same NIC appears with NIC name " enp1s0"  when having 2 NICs in the machine, and appears with NIC name " enp3s0"  when having more than 2 NICs.

Is your suggestion with systemd.link files available when just having udev and no systemd (I have eudev)? At this case I think my script getMAC_DHCP.sh is also be needed.

Michael


-----Ursprüngliche Nachricht-----
Von: Richard Maw [mailto:richard.maw at codethink.co.uk] 
Gesendet: Donnerstag, 3. Dezember 2015 14:55
An: Kaesbauer Michael
Cc: systemd-devel at lists.freedesktop.org
Betreff: Re: [systemd-devel] Rename NIC by using dynamic MAC

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

______________________________________________________________

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



More information about the systemd-devel mailing list