[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-API] [PATCH 15 of 33] interface-reconfigure: Various refactoring
Move functions around and rename to reduce diff with vswitch version. (The vswitch version has previously had the function names rationalised and functions reordered into functional groups). - Move check_allowed() to new "Boot from Network filesystem or device." section. Add usage comment. - Move interface_name() to new section "Bare Network Devices" and rename pif_netdev_name(). - Move interface_exists() to "Bare Network Devices" and rename netdev_exists(). - Move configure_static_routes() to "IP device configuration" and rename ipdev_configure_static_routes(). - Move configure_network() to "IP device configuration" and rename ipdev_configure_network(). Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx> diff -r af610bc596af -r b12c62cd92e1 scripts/interface-reconfigure --- a/scripts/interface-reconfigure Fri Dec 18 14:16:32 2009 +0000 +++ b/scripts/interface-reconfigure Fri Dec 18 14:16:32 2009 +0000 @@ -69,7 +69,7 @@ rec['uuid'] = pifrec['uuid'] rec['ip_configuration_mode'] = pifrec['ip_configuration_mode'] rec['action'] = action - rec['pif_netdev_name'] = interface_name(pif) + rec['pif_netdev_name'] = pif_netdev_name(pif) rec['message'] = "Bring %(action)s PIF %(uuid)s" % rec log("%(message)s: %(pif_netdev_name)s configured as %(ip_configuration_mode)s" % rec) @@ -226,24 +226,6 @@ self.__state = self.__STATE['COMMITTED'] -def check_allowed(pif): - pifrec = db.get_pif_record(pif) - try: - f = open("/proc/ardence") - macline = filter(lambda x: x.startswith("HWaddr:"), f.readlines()) - f.close() - if len(macline) == 1: - p = re.compile(".*\s%(MAC)s\s.*" % pifrec, re.IGNORECASE) - if p.match(macline[0]): - log("Skipping PVS device %(device)s (%(MAC)s)" % pifrec) - return False - except IOError: - pass - return True - -def interface_exists(i): - return os.path.exists("/sys/class/net/" + i) - def interface_up(i): operstate = os.path.join("/sys/class/net",i,"operstate") if not os.path.exists(operstate): @@ -258,7 +240,7 @@ return state in ["up", "unknown"] def ifdown(interface): - if not interface_exists(interface): + if not netdev_exists(interface): log("ifdown: interface %s does not exist, ignoring" % interface) return run_command(["/sbin/ifdown", interface]) @@ -268,13 +250,13 @@ def delbr(bridge): - if not interface_exists(bridge): + if not netdev_exists(bridge): log("delbr: bridge %s does not exist, ignoring" % bridge) return run_command(["/usr/sbin/brctl", "delbr", bridge]) def vconfig_rem(vlan): - if not interface_exists(vlan): + if not netdev_exists(vlan): log("vconfig del: vlan %s does not exist, ignoring" % vlan) return run_command(["/sbin/vconfig", "rem", vlan]) @@ -337,41 +319,6 @@ 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 - """ - 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("/etc/sysconfig/network-scripts/route-%s" % interface) - 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. @@ -390,7 +337,7 @@ def open_network_ifcfg(pif): bridge = bridge_name(pif) - interface = interface_name(pif) + interface = pif_netdev_name(pif) if bridge: return __open_ifcfg(bridge) else: @@ -400,9 +347,9 @@ def open_pif_ifcfg(pif): pifrec = db.get_pif_record(pif) - log("Configuring %s (%s)" % (interface_name(pif), pifrec['MAC'])) + log("Configuring %s (%s)" % (pif_netdev_name(pif), pifrec['MAC'])) - f = __open_ifcfg(interface_name(pif)) + f = __open_ifcfg(pif_netdev_name(pif)) if pifrec.has_key('other_config'): configure_ethtool(pifrec['other_config'], f) @@ -779,6 +726,47 @@ else: return None +# +# Boot from Network filesystem or device. +# + +def check_allowed(pif): + """Determine whether interface-reconfigure should be manipulating this PIF. + + Used to prevent system PIFs (such as network root disk) from being interfered with. + """ + + pifrec = db.get_pif_record(pif) + try: + f = open("/proc/ardence") + macline = filter(lambda x: x.startswith("HWaddr:"), f.readlines()) + f.close() + if len(macline) == 1: + p = re.compile(".*\s%(MAC)s\s.*" % pifrec, re.IGNORECASE) + if p.match(macline[0]): + log("Skipping PVS device %(device)s (%(MAC)s)" % pifrec) + return False + except IOError: + pass + return True + +# +# Bare Network Devices -- network devices without IP configuration +# + +def netdev_exists(netdev): + return os.path.exists("/sys/class/net/" + netdev) + +def pif_netdev_name(pif): + """Get the netdev name for a PIF.""" + + pifrec = db.get_pif_record(pif) + + if pif_is_vlan(pif): + return "%(device)s.%(VLAN)s" % pifrec + else: + return pifrec['device'] + def bridge_name(pif): """Return the bridge name associated with pif, or None if network is bridgeless""" pifrec = db.get_pif_record(pif) @@ -790,16 +778,6 @@ else: # TODO: sanity check that nwrec['bridgeless'] == 'true' return None - -def interface_name(pif): - """Construct an interface name from the given PIF record.""" - - pifrec = db.get_pif_record(pif) - - if pif_is_vlan(pif): - return "%(device)s.%(VLAN)s" % pifrec - else: - return pifrec['device'] def load_bonding_driver(): log("Loading bonding driver") @@ -849,7 +827,7 @@ if len(pifrec['bond_master_of']) == 0: return - __create_bond_device(interface_name(pif)) + __create_bond_device(pif_netdev_name(pif)) def __destroy_bond_device(name): if bond_device_exists(name): @@ -879,141 +857,9 @@ if not os.access(sysfs_bonding_masters, os.F_OK): return - name = interface_name(pif) + name = pif_netdev_name(pif) __destroy_bond_device(name) - -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(',', ' ')) - - # There can be only one DNSDEV and 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 DNSDEV 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 = [p for p in db.get_all_pifs() if not p in get_bond_masters_of_pif(pif)] - - # loop through all the pifs on this host looking for one with - # other-config:peerdns = true, and one with - # other-config:default-route=true - peerdns_pif = None - defaultroute_pif = None - 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 - - is_dnsdev = peerdns_pif == pif - is_gatewaydev = defaultroute_pif == pif - - if is_dnsdev or is_gatewaydev: - fnetwork = ConfigurationFile("/etc/sysconfig/network") - for line in fnetwork.readlines(): - if is_dnsdev and line.lstrip().startswith('DNSDEV='): - fnetwork.write('DNSDEV=%s\n' % bridge) - is_dnsdev = False - elif is_gatewaydev and line.lstrip().startswith('GATEWAYDEV='): - fnetwork.write('GATEWAYDEV=%s\n' % bridge) - is_gatewaydev = False - else: - fnetwork.write(line) - - if is_dnsdev: - fnetwork.write('DNSDEV=%s\n' % bridge_name(pif)) - if is_gatewaydev: - fnetwork.write('GATEWAYDEV=%s\n' % bridge_name(pif)) - - fnetwork.close() - f.attach_child(fnetwork) - - return - def configure_physical_interface(pif): """Write the configuration for a physical interface. @@ -1225,7 +1071,7 @@ required when the bond is brought up.""" if pif_is_vlan(pif): - interface = interface_name(pif) + interface = pif_netdev_name(pif) log("bring_down_interface: %s is a VLAN" % interface) ifdown(interface) @@ -1249,13 +1095,13 @@ log("bring_down_interface: vlan slave has other masters") return - log("bring_down_interface: no more masters, bring down vlan slave %s" % interface_name(slave)) + log("bring_down_interface: no more masters, bring down vlan slave %s" % pif_netdev_name(slave)) pif = slave else: vlan_masters = get_vlan_masters_of_pif(pif) - log("vlan masters of %s - %s" % (db.get_pif_record(pif)['device'], [interface_name(m) for m in vlan_masters])) + log("vlan masters of %s - %s" % (db.get_pif_record(pif)['device'], [pif_netdev_name(m) for m in vlan_masters])) if len([m for m in vlan_masters if db.get_pif_record(m)['currently_attached']]) > 0: - log("Leaving %s up due to currently attached VLAN masters" % interface_name(pif)) + log("Leaving %s up due to currently attached VLAN masters" % pif_netdev_name(pif)) return # pif is now either a bond or a physical device which needs to be brought down @@ -1263,9 +1109,9 @@ # Need to bring down bond slaves first since the bond device # must be up to enslave/unenslave. bond_slaves = get_bond_slaves_of_pif(pif) - log("bond slaves of %s - %s" % (db.get_pif_record(pif)['device'], [interface_name(s) for s in bond_slaves])) + log("bond slaves of %s - %s" % (db.get_pif_record(pif)['device'], [pif_netdev_name(s) for s in bond_slaves])) for slave in bond_slaves: - slave_interface = interface_name(slave) + slave_interface = pif_netdev_name(slave) slave_bridge = bridge_name(slave) if db.get_pif_record(slave)['currently_attached']: log("leave bond slave %s up (currently attached)" % slave_interface) @@ -1278,7 +1124,7 @@ if slave_bridge: bring_down_bridge(slave_bridge, destroy=True) - interface = interface_name(pif) + interface = pif_netdev_name(pif) log("Bring interface %s down" % interface) ifdown(interface) @@ -1290,7 +1136,7 @@ def interface_is_up(pif): try: - interface = interface_name(pif) + interface = pif_netdev_name(pif) state = open("/sys/class/net/%s/operstate" % interface).read().strip() return state == "up" except: @@ -1309,12 +1155,186 @@ if not interface_is_up(slave): bring_up_interface(slave) - interface = interface_name(pif) + interface = pif_netdev_name(pif) create_bond_device(pif) log("Bring interface %s up" % interface) ifup(interface) + +# +# IP device configuration +# + +def ipdev_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 + """ + 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("/etc/sysconfig/network-scripts/route-%s" % interface) + 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 ipdev_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 = pif_netdev_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) + ipdev_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" % pif_netdev_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(',', ' ')) + + # There can be only one DNSDEV and 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 DNSDEV 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 = [p for p in db.get_all_pifs() if not p in get_bond_masters_of_pif(pif)] + + # loop through all the pifs on this host looking for one with + # other-config:peerdns = true, and one with + # other-config:default-route=true + peerdns_pif = None + defaultroute_pif = None + 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 + + is_dnsdev = peerdns_pif == pif + is_gatewaydev = defaultroute_pif == pif + + if is_dnsdev or is_gatewaydev: + fnetwork = ConfigurationFile("/etc/sysconfig/network") + for line in fnetwork.readlines(): + if is_dnsdev and line.lstrip().startswith('DNSDEV='): + fnetwork.write('DNSDEV=%s\n' % bridge) + is_dnsdev = False + elif is_gatewaydev and line.lstrip().startswith('GATEWAYDEV='): + fnetwork.write('GATEWAYDEV=%s\n' % bridge) + is_gatewaydev = False + else: + fnetwork.write(line) + + if is_dnsdev: + fnetwork.write('DNSDEV=%s\n' % bridge_name(pif)) + if is_gatewaydev: + fnetwork.write('GATEWAYDEV=%s\n' % bridge_name(pif)) + + fnetwork.close() + f.attach_child(fnetwork) + + return + +# +# Toplevel actions +# def action_up(pif): @@ -1322,19 +1342,19 @@ f = configure_pif(pif) - interface = interface_name(pif) + interface = pif_netdev_name(pif) bridge = bridge_name(pif) mode = pifrec['ip_configuration_mode'] if bridge: log("Configuring %s using %s configuration" % (bridge, mode)) br = open_network_ifcfg(pif) - configure_network(pif, br) + ipdev_configure_network(pif, br) br.close() f.attach_child(br) else: log("Configuring %s using %s configuration" % (interface, mode)) - configure_network(pif, f) + ipdev_configure_network(pif, f) f.close() @@ -1345,7 +1365,7 @@ # Bring down any VLAN masters so that we can reconfigure the slave. vlan_masters = get_vlan_masters_of_pif(pif) for master in vlan_masters: - name = interface_name(master) + name = pif_netdev_name(master) log("action_up: bring down %s" % (name)) ifdown(name) @@ -1374,7 +1394,7 @@ # Bring back any currently-attached VLAN masters (brought down above) for master in [v for v in vlan_masters if db.get_pif_record(v)['currently_attached']]: - name = interface_name(master) + name = pif_netdev_name(master) log("action_up: bring up %s" % (name)) ifup(name) @@ -1393,19 +1413,19 @@ f = configure_pif(pif) - interface = interface_name(pif) + interface = pif_netdev_name(pif) bridge = bridge_name(pif) mode = pifrec['ip_configuration_mode'] if bridge: log("Configuring %s using %s configuration" % (bridge, mode)) br = open_network_ifcfg(pif) - configure_network(pif, br) + ipdev_configure_network(pif, br) br.close() f.attach_child(br) else: log("Configuring %s using %s configuration" % (interface, mode)) - configure_network(pif, f) + ipdev_configure_network(pif, f) f.close() _______________________________________________ xen-api mailing list xen-api@xxxxxxxxxxxxxxxxxxx http://lists.xensource.com/mailman/listinfo/xen-api
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |