From c4c24171bfbffd7bb75a75b8ceae18ce7296cac6 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Fri, 24 Oct 2014 15:47:24 +0200 Subject: [PATCH] ifcfg/write-ifcfg: only write DEVICE for non-kernel names Rename an interface to the kernel namespace is not allowed, so don't add DEVICE="", if HWADDR is given. (cherry picked from commit 3947f07d93cde5e1cf0d788537e93b135d6c27b0) --- modules.d/40network/net-lib.sh | 46 ++++++++++++++++++++++++++++++- modules.d/45ifcfg/write-ifcfg.sh | 59 ++++++++++++++++++++++++++-------------- 2 files changed, 84 insertions(+), 21 deletions(-) diff --git a/modules.d/40network/net-lib.sh b/modules.d/40network/net-lib.sh index 22f77546..337817e3 100755 --- a/modules.d/40network/net-lib.sh +++ b/modules.d/40network/net-lib.sh @@ -553,7 +553,19 @@ find_iface_with_link() { } is_persistent_ethernet_name() { - case "$1" in + local _netif="$1" + local _name_assign_type="0" + + [ -f "/sys/class/net/$_netif/name_assign_type" ] \ + && _name_assign_type=$(cat "/sys/class/net/$_netif/name_assign_type") + + # NET_NAME_ENUM 1 + [ "$_name_assign_type" = "1" ] && return 1 + + # NET_NAME_PREDICTABLE 2 + [ "$_name_assign_type" = "2" ] && return 0 + + case "$_netif" in # udev persistent interface names eno[0-9]|eno[0-9][0-9]|eno[0-9][0-9][0-9]*) ;; @@ -573,3 +585,35 @@ is_persistent_ethernet_name() { esac return 0 } + +is_kernel_ethernet_name() { + local _netif="$1" + local _name_assign_type="1" + + if [ -e "/sys/class/net/$_netif/name_assign_type" ]; then + _name_assign_type=$(cat "/sys/class/net/$_netif/name_assign_type") + + case "$_name_assign_type" in + 2|3|4) + # NET_NAME_PREDICTABLE 2 + # NET_NAME_USER 3 + # NET_NAME_RENAMED 4 + return 1 + ;; + 1|*) + # NET_NAME_ENUM 1 + return 0 + ;; + esac + fi + + # fallback to error prone manual name check + case "$_netif" in + eth[0-9]|eth[0-9][0-9]|eth[0-9][0-9][0-9]*) + return 0 + ;; + *) + return 1 + esac + +} diff --git a/modules.d/45ifcfg/write-ifcfg.sh b/modules.d/45ifcfg/write-ifcfg.sh index aed30698..a1bae72f 100755 --- a/modules.d/45ifcfg/write-ifcfg.sh +++ b/modules.d/45ifcfg/write-ifcfg.sh @@ -82,18 +82,41 @@ print_s390() { return 0 } +hw_bind() { + local _netif="$1" + local _macaddr="$2" + + [ -n "$_macaddr" ] \ + && echo "MACADDR=\"$_macaddr\"" + + print_s390 "$_netif" \ + && return 0 + + [ -n "$_macaddr" ] && return 0 + + is_persistent_ethernet_name "$_netif" && return 0 + + [ -f "/sys/class/net/$_netif/addr_assign_type" ] \ + && [ "$(cat "/sys/class/net/$_netif/addr_assign_type")" != "0" ] \ + && return 1 + + [ -f "/sys/class/net/$_netif/address" ] \ + || return 1 + + echo "HWADDR=\"$(cat /sys/class/net/$_netif/address)\"" +} + interface_bind() { - local netif="$1" - local macaddr="$2" - if ! print_s390 $netif; then - if [ -z "$macaddr" ] && \ - ! is_persistent_ethernet_name "$netif" && \ - [ -f /sys/class/net/$netif/addr_assign_type ] && \ - [ "$(cat /sys/class/net/$netif/addr_assign_type)" = "0" ] && \ - [ -f /sys/class/net/$netif/address ]; then - echo "HWADDR=\"$(cat /sys/class/net/$netif/address)\"" - fi + local _netif="$1" + local _macaddr="$2" + + # see, if we can bind it to some hw parms + if hw_bind "$_netif" "$_macaddr"; then + # only print out DEVICE, if it's user assigned + is_kernel_ethernet_name "$_netif" && return 0 fi + + echo "DEVICE=\"$_netif\"" } for netup in /tmp/net.*.did-setup ; do @@ -131,7 +154,8 @@ for netup in /tmp/net.*.did-setup ; do { echo "# Generated by dracut initrd" - echo "DEVICE=\"$netif\"" + echo "NAME=\"$netif\"" + interface_bind "$netif" "$macaddr" echo "ONBOOT=yes" echo "NETBOOT=yes" echo "UUID=\"$uuid\"" @@ -177,10 +201,7 @@ for netup in /tmp/net.*.did-setup ; do if [ -z "$bridge" ] && [ -z "$bond" ] && [ -z "$vlan" ]; then # standard interface { - [ -n "$macaddr" ] && echo "MACADDR=\"$macaddr\"" - interface_bind "$netif" "$macaddr" echo "TYPE=Ethernet" - echo "NAME=\"$netif\"" [ -n "$mtu" ] && echo "MTU=\"$mtu\"" } >> /tmp/ifcfg/ifcfg-$netif fi @@ -207,16 +228,15 @@ for netup in /tmp/net.*.did-setup ; do # write separate ifcfg file for the raw eth interface ( echo "# Generated by dracut initrd" - echo "DEVICE=\"$slave\"" + echo "NAME=\"$slave\"" echo "TYPE=Ethernet" echo "ONBOOT=yes" echo "NETBOOT=yes" echo "SLAVE=yes" echo "MASTER=\"$netif\"" - echo "NAME=\"$slave\"" echo "UUID=\"$(cat /proc/sys/kernel/random/uuid)\"" + unset macaddr [ -e /tmp/net.$slave.override ] && . /tmp/net.$slave.override - [ -n "$macaddr" ] && echo "MACADDR=\"$macaddr\"" interface_bind "$slave" "$macaddr" ) >> /tmp/ifcfg/ifcfg-$slave done @@ -232,15 +252,14 @@ for netup in /tmp/net.*.did-setup ; do # write separate ifcfg file for the raw eth interface ( echo "# Generated by dracut initrd" - echo "DEVICE=\"$slave\"" + echo "NAME=\"$slave\"" echo "TYPE=Ethernet" echo "ONBOOT=yes" echo "NETBOOT=yes" echo "BRIDGE=\"$bridgename\"" - echo "NAME=\"$slave\"" echo "UUID=\"$(cat /proc/sys/kernel/random/uuid)\"" + unset macaddr [ -e /tmp/net.$slave.override ] && . /tmp/net.$slave.override - [ -n "$macaddr" ] && echo "MACADDR=\"$macaddr\"" interface_bind "$slave" "$macaddr" ) >> /tmp/ifcfg/ifcfg-$slave done