-
- return 0
-\f
-# The following code allows interface-reconfigure to keep Centos
-# network configuration files up-to-date, even though the vswitch
-# never uses them. In turn, that means that "rpm -e vswitch" does not
-# have to update any configuration files.
-
-def configure_ethtool(oc, f):
- # Options for "ethtool -s"
- settings = None
- setting_opts = ["autoneg", "speed", "duplex"]
- # Options for "ethtool -K"
- offload = None
- offload_opts = ["rx", "tx", "sg", "tso", "ufo", "gso"]
-
- for opt in [opt for opt in setting_opts + offload_opts if oc.has_key("ethtool-" + opt)]:
- val = oc["ethtool-" + opt]
-
- if opt in ["speed"]:
- if val in ["10", "100", "1000"]:
- val = "speed " + val
- else:
- log("Invalid value for ethtool-speed = %s. Must be 10|100|1000." % val)
- val = None
- elif opt in ["duplex"]:
- if val in ["half", "full"]:
- val = "duplex " + val
- else:
- log("Invalid value for ethtool-duplex = %s. Must be half|full." % val)
- val = None
- elif opt in ["autoneg"] + offload_opts:
- if val in ["true", "on"]:
- val = opt + " on"
- elif val in ["false", "off"]:
- val = opt + " off"
- else:
- log("Invalid value for ethtool-%s = %s. Must be on|true|off|false." % (opt, val))
- val = None
-
- if opt in setting_opts:
- if val and settings:
- settings = settings + " " + val
- else:
- settings = val
- elif opt in offload_opts:
- if val and offload:
- offload = offload + " " + val
- else:
- offload = val
-
- if settings:
- f.write("ETHTOOL_OPTS=\"%s\"\n" % settings)
- if offload:
- f.write("ETHTOOL_OFFLOAD_OPTS=\"%s\"\n" % offload)
-
-def configure_mtu(oc, f):
- if not oc.has_key('mtu'):
- return
-
- try:
- mtu = int(oc['mtu'])
- f.write("MTU=%d\n" % mtu)
- except ValueError, x:
- log("Invalid value for mtu = %s" % mtu)
-
-def configure_static_routes(interface, oc, f):
- """Open a route-<interface> file for static routes.
-
- Opens the static routes configuration file for interface and writes one
- line for each route specified in the network's other config "static-routes" value.
- E.g. if
- interface ( RO): xenbr1
- other-config (MRW): static-routes: 172.16.0.0/15/192.168.0.3,172.18.0.0/16/192.168.0.4;...
-
- Then route-xenbr1 should be
- 172.16.0.0/15 via 192.168.0.3 dev xenbr1
- 172.18.0.0/16 via 192.168.0.4 dev xenbr1
- """
- fname = "route-%s" % interface
- if oc.has_key('static-routes'):
- # The key is present - extract comma seperates entries
- lines = oc['static-routes'].split(',')
- else:
- # The key is not present, i.e. there are no static routes
- lines = []
-
- child = ConfigurationFile(fname)
- child.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
- (os.path.basename(child.path()), os.path.basename(sys.argv[0])))
-
- try:
- for l in lines:
- network, masklen, gateway = l.split('/')
- child.write("%s/%s via %s dev %s\n" % (network, masklen, gateway, interface))
-
- f.attach_child(child)
- child.close()
-
- except ValueError, e:
- log("Error in other-config['static-routes'] format for network %s: %s" % (interface, e))
-
-def __open_ifcfg(interface):
- """Open a network interface configuration file.
-
- Opens the configuration file for interface, writes a header and
- common options and returns the file object.
- """
- fname = "ifcfg-%s" % interface
- f = ConfigurationFile(fname)
-
- f.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
- (os.path.basename(f.path()), os.path.basename(sys.argv[0])))
- f.write("XEMANAGED=yes\n")
- f.write("DEVICE=%s\n" % interface)
- f.write("ONBOOT=no\n")
-
- return f
-
-def open_network_ifcfg(pif):
- bridge = bridge_name(pif)
- interface = interface_name(pif)
- if bridge:
- return __open_ifcfg(bridge)
- else:
- return __open_ifcfg(interface)
-
-
-def open_pif_ifcfg(pif):
- pifrec = db.get_pif_record(pif)
-
- log("Configuring %s (%s)" % (interface_name(pif), pifrec['MAC']))
-
- f = __open_ifcfg(interface_name(pif))
-
- if pifrec.has_key('other_config'):
- configure_ethtool(pifrec['other_config'], f)
- configure_mtu(pifrec['other_config'], f)
-
- return f
-
-def configure_network(pif, f):
- """Write the configuration file for a network.
-
- Writes configuration derived from the network object into the relevant
- ifcfg file. The configuration file is passed in, but if the network is
- bridgeless it will be ifcfg-<interface>, otherwise it will be ifcfg-<bridge>.
-
- This routine may also write ifcfg files of the networks corresponding to other PIFs
- in order to maintain consistency.
-
- params:
- pif: Opaque_ref of pif
- f : ConfigurationFile(/path/to/ifcfg) to which we append network configuration
- """
-
- pifrec = db.get_pif_record(pif)
- nw = pifrec['network']
- nwrec = db.get_network_record(nw)
- oc = None
- bridge = bridge_name(pif)
- interface = interface_name(pif)
- if bridge:
- device = bridge
- else:
- device = interface
-
- if nwrec.has_key('other_config'):
- configure_ethtool(nwrec['other_config'], f)
- configure_mtu(nwrec['other_config'], f)
- configure_static_routes(device, nwrec['other_config'], f)
-
-
- if pifrec.has_key('other_config'):
- oc = pifrec['other_config']
-
- if device == bridge:
- f.write("TYPE=Bridge\n")
- f.write("DELAY=0\n")
- f.write("STP=off\n")
- f.write("PIFDEV=%s\n" % interface_name(pif))
-
- if pifrec['ip_configuration_mode'] == "DHCP":
- f.write("BOOTPROTO=dhcp\n")
- f.write("PERSISTENT_DHCLIENT=yes\n")
- elif pifrec['ip_configuration_mode'] == "Static":
- f.write("BOOTPROTO=none\n")
- f.write("NETMASK=%(netmask)s\n" % pifrec)
- f.write("IPADDR=%(IP)s\n" % pifrec)
- f.write("GATEWAY=%(gateway)s\n" % pifrec)
- elif pifrec['ip_configuration_mode'] == "None":
- f.write("BOOTPROTO=none\n")
- else:
- raise Error("Unknown ip-configuration-mode %s" % pifrec['ip_configuration_mode'])
-
- if pifrec.has_key('DNS') and pifrec['DNS'] != "":
- ServerList = pifrec['DNS'].split(",")
- for i in range(len(ServerList)): f.write("DNS%d=%s\n" % (i+1, ServerList[i]))
- if oc and oc.has_key('domain'):
- f.write("DOMAIN='%s'\n" % oc['domain'].replace(',', ' '))
-
- # We only allow one ifcfg-xenbr* to have PEERDNS=yes and there can be only one GATEWAYDEV in /etc/sysconfig/network.
- # The peerdns pif will be the one with pif::other-config:peerdns=true, or the mgmt pif if none have this set.
- # The gateway pif will be the one with pif::other-config:defaultroute=true, or the mgmt pif if none have this set.
-
- # Work out which pif on this host should be the one with PEERDNS=yes and which should be the GATEWAYDEV
- #
- # Note: we prune out the bond master pif (if it exists).
- # This is because when we are called to bring up an interface with a bond master, it is implicit that
- # we should bring down that master.
- pifs_on_host = [ __pif for __pif in db.get_all_pifs() if
- not __pif in get_bond_masters_of_pif(pif) ]
- other_pifs_on_host = [ __pif for __pif in pifs_on_host if __pif != pif ]
-
- peerdns_pif = None
- defaultroute_pif = None
-
- # loop through all the pifs on this host looking for one with
- # other-config:peerdns = true, and one with
- # other-config:default-route=true
- for __pif in pifs_on_host:
- __pifrec = db.get_pif_record(__pif)
- __oc = __pifrec['other_config']
- if __oc.has_key('peerdns') and __oc['peerdns'] == 'true':
- if peerdns_pif == None:
- peerdns_pif = __pif
- else:
- log('Warning: multiple pifs with "peerdns=true" - choosing %s and ignoring %s' % \
- (db.get_pif_record(peerdns_pif)['device'], __pifrec['device']))
- if __oc.has_key('defaultroute') and __oc['defaultroute'] == 'true':
- if defaultroute_pif == None:
- defaultroute_pif = __pif
- else:
- log('Warning: multiple pifs with "defaultroute=true" - choosing %s and ignoring %s' % \
- (db.get_pif_record(defaultroute_pif)['device'], __pifrec['device']))
-
- # If no pif is explicitly specified then use the mgmt pif for peerdns/defaultroute
- if peerdns_pif == None:
- peerdns_pif = management_pif
- if defaultroute_pif == None:
- defaultroute_pif = management_pif
-
- # Update all the other network's ifcfg files and ensure consistency
- for __pif in other_pifs_on_host:
- __f = open_network_ifcfg(__pif)
- peerdns_line_wanted = 'PEERDNS=%s\n' % ((__pif == peerdns_pif) and 'yes' or 'no')
- lines = __f.readlines()
-
- if not peerdns_line_wanted in lines:
- # the PIF selected for DNS has changed and as a result this ifcfg file needs rewriting
- for line in lines:
- if not line.lstrip().startswith('PEERDNS'):
- __f.write(line)
- log("Setting %s in %s" % (peerdns_line_wanted.strip(), __f.path()))
- __f.write(peerdns_line_wanted)
- __f.close()
- f.attach_child(__f)
-
- else:
- # There is no need to change this ifcfg file. So don't attach_child.
- pass
-
- # ... and for this pif too
- f.write('PEERDNS=%s\n' % ((pif == peerdns_pif) and 'yes' or 'no'))
-
- # Update gatewaydev
- fnetwork = ConfigurationFile("network", "/etc/sysconfig")
- for line in fnetwork.readlines():
- if line.lstrip().startswith('GATEWAY') :
- continue
- fnetwork.write(line)
- if defaultroute_pif:
- gatewaydev = bridge_name(defaultroute_pif)
- if not gatewaydev:
- gatewaydev = interface_name(defaultroute_pif)
- fnetwork.write('GATEWAYDEV=%s\n' % gatewaydev)
- fnetwork.close()
- f.attach_child(fnetwork)
-
- return
-
-
-def configure_physical_interface(pif):
- """Write the configuration for a physical interface.
-
- Writes the configuration file for the physical interface described by
- the pif object.
-
- Returns the open file handle for the interface configuration file.
- """
-
- pifrec = db.get_pif_record(pif)
-
- f = open_pif_ifcfg(pif)
-
- f.write("TYPE=Ethernet\n")
- f.write("HWADDR=%(MAC)s\n" % pifrec)