Mar 152017
 

CentOS7,网卡名字,采用consistent network device naming,简单的说,网络的名字,已经不是以前的eth0,eth1,你装系统前,你根本就不知道他叫啥名字。

这样的命名,好处就是你机器重启,增加pci设备,不会导致原来的设备名称发生改变。如果我们通过grub,修改内核的方式,改变回到以前的网卡命名,这种方式在新的设备里,会出现很多问题,你会发现每次重启机器,网卡名字都是会改变,一直都是在不停的变化,让你疯掉。

所以最佳的办法,就是还是使用装系统的时候默认网卡是啥名字就是什么名字。那么对于cobbler来说,

cobbler system  add \
--name=vm1 \
--hostname=vm1 \
--profile=CentOS7-x86_64 \
--interface=eth0 \
--mac=00:50:56:35:61:5C \
--ip-address=192.168.27.100 \
--subnet=255.255.255.0 \
--gateway=192.168.27.0 \
--static=1

你是必须指定网卡名字的。

我们可以考虑通过cobbler提供的snippet来解决这个问题。

目前cobbler提供 pre_install_network_config,还不能很好解决这个问题。

cd /var/lib/cobbler/snippets
mv pre_install_network_config pre_install_network_config.save

我们创建一个新的 pre_install_network_config

#if $getVar("system_name","") != ""
# Start pre_install_network_config generated code
#raw
# generic functions to be used later for discovering NICs
mac_exists() {
  [ -z "$1" ] && return 1

  if which ip 2>/dev/null >/dev/null; then
    ip -o link | grep -i "$1" 2>/dev/null >/dev/null
    return $?
  elif which esxcfg-nics 2>/dev/null >/dev/null; then
    esxcfg-nics -l | grep -i "$1" 2>/dev/null >/dev/null
    return $?
  else
    ifconfig -a | grep -i "$1" 2>/dev/null >/dev/null
    return $?
  fi
}
get_ifname() {
  if which ip 2>/dev/null >/dev/null; then
    IFNAME=$(ip -o link | grep -i "$1" | sed -e 's/^[0-9]*: //' -e 's/:.*//')
  elif which esxcfg-nics 2>/dev/null >/dev/null; then
    IFNAME=$(esxcfg-nics -l | grep -i "$1" | cut -d " " -f 1)
  else
    IFNAME=$(ifconfig -a | grep -i "$1" | cut -d " " -f 1)
    if [ -z $IFNAME ]; then
      IFNAME=$(ifconfig -a | grep -i -B 2 "$1" | sed -n '/flags/s/:.*$//p')
    fi
  fi
}
#end raw
    #set ikeys = $interfaces.keys()
    #import re
    #set $vlanpattern = $re.compile("[a-zA-Z0-9]+[\.][0-9]+")
    #set $routepattern = $re.compile("[0-9/.]+:[0-9.]+")
    ##
    ## Determine if we should use the MAC address to configure the interfaces first
    ## Only physical interfaces are required to have a MAC address
    #set $configbymac = True
    #for $iname in $ikeys
        #set $idata = $interfaces[$iname]
        #if $idata["mac_address"] == "" and not $vlanpattern.match($iname) and not $idata["interface_type"].lower() in ("master","bond","bridge","bonded_bridge_slave")
            #set $configbymac = False
        #end if
    #end for
    #set $i = 0

    #if $configbymac
        ## Output diagnostic message
# Start of code to match cobbler system interfaces to physical interfaces by their mac addresses
    #end if
    #for $iname in $ikeys
#  Start $iname
        #set $idata         = $interfaces[$iname]
        #set $mac           = $idata["mac_address"]
        #set $static        = $idata["static"]
        #set $ip            = $idata["ip_address"]
        #set $netmask       = $idata["netmask"]
        #set $iface_type    = $idata["interface_type"]
        #set $iface_master  = $idata["interface_master"]
        #set $if_gateway    = $idata["if_gateway"]
        #set $static_routes = $idata["static_routes"]
        #set $devfile       = "/etc/sysconfig/network-scripts/ifcfg-" + $iname
        #if $vlanpattern.match($iname)
            ## If this is a VLAN interface, skip it, anaconda doesn't know
            ## about VLANs.
            #set $is_vlan = "true"
        #else
            #set $is_vlan = "false"
        #end if
        #if ($configbymac and $is_vlan == "false" and $iface_type.lower() not in ("slave","bond_slave","bridge_slave","bonded_bridge_slave")) or $iface_type.lower() in ("master","bond","bridge")
            ## This is a physical interface, hand it to anaconda. Do not
            ## process slave interface here.
            #if $iface_type.lower() in ("master","bond","bridge","bonded_bridge_slave")
                ## Find a slave for this interface
                #for $tiname in $ikeys
                    #set $tidata = $interfaces[$tiname]
                    #if $tidata["interface_type"].lower() in ("slave","bond_slave","bridge_slave") and $tidata["interface_master"].lower() == $iname
                        #if $tidata["mac_address"] != '':
                            #set $mac = $tidata["mac_address"]
#  Found a slave for this interface: $tiname ($mac)
                            #break
                        #end if
                    #else if $tidata["interface_type"].lower() == "bonded_bridge_slave" and $tidata["interface_master"].lower() == $iname
                        ## find a slave for this slave interface...
                        #for $stiname in $ikeys
                            #set $stidata = $interfaces[$stiname]
                            #if $stidata["interface_type"].lower() in ("slave","bond_slave","bridge_slave") and $stidata["interface_master"].lower() == $tiname
                                #if $stidata["mac_address"] != '':
                                    #set $mac = $stidata["mac_address"]
#  Found a slave for this interface: $tiname -> $stiname ($mac)
                                    #break
                                #end if
                            #end if
                        #end for
                    #end if
                #end for
            #end if
            #if $static and $ip != ""
                #if $netmask == ""
                    ## Netmask not provided, default to /24.
                    #set $netmask = "255.255.255.0"
                #end if
                #set $netinfo = "--bootproto=static --ip=%s --netmask=%s" % ($ip, $netmask)
                #if $if_gateway != ""
	            #set $netinfo = "%s --gateway=%s" % ($netinfo, $if_gateway)
                #else
                    #set $netinfo = "%s --gateway=0.0.0.0" % ($netinfo)
                #end if
    	        #if $len($name_servers) > 0
    	            #set $netinfo = "%s --nameserver=%s" % ($netinfo, $name_servers[0])
                #end if
            #else if not $static
                #set $netinfo = "--bootproto=dhcp"
            #else
                ## Skip this interface, it's set as static, but without
                ## networking info.
#  Skipping (no configuration)...
                #continue
            #end if
            #if $hostname != ""
                #set $netinfo = "%s --hostname=%s" % ($netinfo, $hostname)
            #end if
# Configuring $iname ($mac)
if mac_exists $mac
then
  get_ifname $mac
  echo "network --device=\$IFNAME $netinfo " >> /tmp/pre_install_network_config
            #for $route in $static_routes
                #if $routepattern.match($route)
                    #set $routebits = $route.split(":")
                    #set [$network, $router] = $route.split(":")
  ip route add $network via $router dev \$IFNAME
                #else
  # Warning: invalid route "$route"
                #end if
            #end for
fi
        #else
            #if $iface_type.lower() in ("slave","bond_slave","bridge_slave","bonded_bridge_slave")
#  Skipping (slave-interface)
            #else
#  Skipping (not a physical interface)...
            #end if
        #end if
    #end for
# End pre_install_network_config generated code
#end if

这样cobbler默认使用的sample.ks来安装centos 7.x,那么网卡名字,就会和系统默认是一致的。

上面的snippet是我同事 http://zhanguo1110.blog.51cto.com/5750817/1671442 写的。

 

参考文档

https://access.redhat.com/discussions/916973

 Leave a Reply

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.