Example #1
0
    def testGetIfaceCfg(self):
        deviceName = "___This_could_never_be_a_device_name___"
        ifcfg = ('GATEWAY0=1.1.1.1\n' 'NETMASK=255.255.0.0\n')
        with namedTemporaryDir() as tempDir:
            ifcfgPrefix = os.path.join(tempDir, 'ifcfg-')
            filePath = ifcfgPrefix + deviceName

            with MonkeyPatchScope([(netinfo, 'NET_CONF_PREF', ifcfgPrefix)]):
                with open(filePath, 'w') as ifcfgFile:
                    ifcfgFile.write(ifcfg)
                self.assertEqual(
                    netinfo.getIfaceCfg(deviceName)['GATEWAY'], '1.1.1.1')
                self.assertEqual(
                    netinfo.getIfaceCfg(deviceName)['NETMASK'], '255.255.0.0')
Example #2
0
    def testGetIfaceCfg(self):
        deviceName = "___This_could_never_be_a_device_name___"
        ifcfg = ('GATEWAY0=1.1.1.1\n' 'NETMASK=255.255.0.0\n')
        with namedTemporaryDir() as tempDir:
            ifcfgPrefix = os.path.join(tempDir, 'ifcfg-')
            filePath = ifcfgPrefix + deviceName

            with MonkeyPatchScope([(netinfo, 'NET_CONF_PREF', ifcfgPrefix)]):
                with open(filePath, 'w') as ifcfgFile:
                    ifcfgFile.write(ifcfg)
                self.assertEqual(
                    netinfo.getIfaceCfg(deviceName)['GATEWAY'], '1.1.1.1')
                self.assertEqual(
                    netinfo.getIfaceCfg(deviceName)['NETMASK'], '255.255.0.0')
Example #3
0
 def _getIfaceConfValues(iface):
     ipaddr, netmask, gateway, defaultRoute, ipv6addr, ipv6gateway, \
         ipv6defaultRoute, bootproto, async, ipv6autoconf, dhcpv6 = \
         iface.ipConfig
     defaultRoute = ConfigWriter._toIfcfgFormat(defaultRoute)
     mtu = iface.mtu
     if netinfo.ifaceUsed(iface.name):
         confParams = netinfo.getIfaceCfg(iface.name)
         if not ipaddr and bootproto != 'dhcp':
             ipaddr = confParams.get('IPADDR', None)
             netmask = confParams.get('NETMASK', None)
             gateway = confParams.get('GATEWAY', None)
             bootproto = bootproto or confParams.get('BOOTPROTO', None)
         defaultRoute = defaultRoute or confParams.get('DEFROUTE', None)
         if confParams.get('IPV6INIT', 'no') == 'yes':
             ipv6addr = confParams.get('IPV6ADDR', None)
             ipv6gateway = confParams.get('IPV6_DEFAULTGW', None)
             ipv6autoconf = (confParams.get('IPV6_AUTOCONF', 'no') == 'yes')
             dhcpv6 = (confParams.get('DHCPV6C', 'no') == 'yes')
         if not iface.mtu:
             mtu = confParams.get('MTU', None)
             if mtu:
                 mtu = int(mtu)
     ipconfig = IpConfig.ipConfig(ipaddr, netmask, gateway, defaultRoute,
                                  ipv6addr, ipv6gateway, ipv6defaultRoute,
                                  bootproto, async, ipv6autoconf, dhcpv6)
     return ipconfig, mtu
Example #4
0
 def _getIfaceConfValues(iface):
     ipaddr, netmask, gateway, defaultRoute, ipv6addr, ipv6gateway, \
         ipv6defaultRoute, bootproto, async, ipv6autoconf, dhcpv6 = \
         iface.ipConfig
     defaultRoute = ConfigWriter._toIfcfgFormat(defaultRoute)
     mtu = iface.mtu
     if netinfo.ifaceUsed(iface.name):
         confParams = netinfo.getIfaceCfg(iface.name)
         if not ipaddr and bootproto != 'dhcp':
             ipaddr = confParams.get('IPADDR', None)
             netmask = confParams.get('NETMASK', None)
             gateway = confParams.get('GATEWAY', None)
             bootproto = bootproto or confParams.get('BOOTPROTO', None)
         defaultRoute = defaultRoute or confParams.get('DEFROUTE', None)
         if confParams.get('IPV6INIT', 'no') == 'yes':
             ipv6addr = confParams.get('IPV6ADDR', None)
             ipv6gateway = confParams.get('IPV6_DEFAULTGW', None)
             ipv6autoconf = (confParams.get('IPV6_AUTOCONF', 'no') == 'yes')
             dhcpv6 = (confParams.get('DHCPV6C', 'no') == 'yes')
         if not iface.mtu:
             mtu = confParams.get('MTU', None)
             if mtu:
                 mtu = int(mtu)
     ipconfig = IpConfig.ipConfig(ipaddr, netmask, gateway, defaultRoute,
                                  ipv6addr, ipv6gateway, ipv6defaultRoute,
                                  bootproto, async, ipv6autoconf, dhcpv6)
     return ipconfig, mtu
Example #5
0
    def addNic(self, nic, bonding=None, bridge=None, mtu=None,
               ipaddr=None, netmask=None, gateway=None, bootproto=None,
               onboot='yes', **kwargs):
        """ Create ifcfg-* file with proper fields for NIC """
        _netinfo = netinfo.NetInfo()
        hwaddr = (_netinfo.nics[nic].get('permhwaddr') or
                  _netinfo.nics[nic]['hwaddr'])

        conf = 'HWADDR=%s\n' % pipes.quote(hwaddr)
        if bridge:
            conf += 'BRIDGE=%s\n' % pipes.quote(bridge)
        if bonding:
            conf += 'MASTER=%s\nSLAVE=yes\n' % pipes.quote(bonding)

        if _netinfo.ifaceUsers(nic):
            confParams = netinfo.getIfaceCfg(nic)
            if not ipaddr:
                ipaddr = confParams.get('IPADDR', None)
                netmask = confParams.get('NETMASK', None)
                gateway = confParams.get('GATEWAY', None)
                bootproto = confParams.get('BOOTPROTO', None)
            if not mtu:
                mtu = confParams.get('MTU', None)
                if mtu:
                    mtu = int(mtu)

        self._createConfFile(conf, nic, ipaddr, netmask, gateway,
                             bootproto, mtu, onboot, **kwargs)
Example #6
0
 def _getIfaceConfValues(iface):
     ipconfig = copy.deepcopy(iface.ipconfig)
     ipv4 = ipconfig.ipv4
     ipv6 = ipconfig.ipv6
     mtu = iface.mtu
     if netinfo.ifaceUsed(iface.name):
         confParams = netinfo.getIfaceCfg(iface.name)
         if not ipv4.address and ipconfig.bootproto != 'dhcp':
             ipv4.address = confParams.get('IPADDR')
             ipv4.netmask = confParams.get('NETMASK')
             ipv4.gateway = confParams.get('GATEWAY')
             if not ipconfig.bootproto:
                 ipconfig.bootproto = confParams.get('BOOTPROTO')
         if ipv4.defaultRoute is None and confParams.get('DEFROUTE'):
             ipv4.defaultRoute = _from_ifcfg_bool(confParams['DEFROUTE'])
         if confParams.get('IPV6INIT') == 'yes':
             ipv6.address = confParams.get('IPV6ADDR')
             ipv6.gateway = confParams.get('IPV6_DEFAULTGW')
             ipconfig.ipv6autoconf = (
                 confParams.get('IPV6_AUTOCONF') == 'yes')
             ipconfig.dhcpv6 = confParams.get('DHCPV6C') == 'yes'
         if not iface.mtu:
             mtu = confParams.get('MTU')
             if mtu:
                 mtu = int(mtu)
     return ipconfig, mtu
Example #7
0
    def addBonding(self, bonding, bridge=None, bondingOptions=None, mtu=None,
                   ipaddr=None, netmask=None, gateway=None, bootproto=None,
                   onboot='yes', **kwargs):
        """ Create ifcfg-* file with proper fields for bond """
        if not bondingOptions:
            bondingOptions = 'mode=802.3ad miimon=150'

        conf = 'BONDING_OPTS=%s\n' % pipes.quote(bondingOptions or '')
        if bridge:
            conf += 'BRIDGE=%s\n' % pipes.quote(bridge)

        if netinfo.NetInfo().ifaceUsers(bonding):
            confParams = netinfo.getIfaceCfg(bonding)
            if not ipaddr:
                ipaddr = confParams.get('IPADDR', None)
                netmask = confParams.get('NETMASK', None)
                gateway = confParams.get('GATEWAY', None)
                bootproto = confParams.get('BOOTPROTO', None)
            if not mtu:
                mtu = confParams.get('MTU', None)
                if mtu:
                    mtu = int(mtu)

        self._createConfFile(conf, bonding, ipaddr, netmask, gateway,
                             bootproto, mtu, onboot, **kwargs)

        # create the bonding device to avoid initscripts noise
        if bonding not in open(netinfo.BONDING_MASTERS).read().split():
            open(netinfo.BONDING_MASTERS, 'w').write('+%s\n' % bonding)
Example #8
0
 def _getIfaceConfValues(iface):
     ipconfig = copy.deepcopy(iface.ipconfig)
     ipv4 = ipconfig.ipv4
     ipv6 = ipconfig.ipv6
     mtu = iface.mtu
     if netinfo.ifaceUsed(iface.name):
         confParams = netinfo.getIfaceCfg(iface.name)
         if not ipv4.address and ipconfig.bootproto != 'dhcp':
             ipv4.address = confParams.get('IPADDR')
             ipv4.netmask = confParams.get('NETMASK')
             ipv4.gateway = confParams.get('GATEWAY')
             if not ipconfig.bootproto:
                 ipconfig.bootproto = confParams.get('BOOTPROTO')
         if ipv4.defaultRoute is None and confParams.get('DEFROUTE'):
             ipv4.defaultRoute = _from_ifcfg_bool(confParams['DEFROUTE'])
         if confParams.get('IPV6INIT') == 'yes':
             ipv6.address = confParams.get('IPV6ADDR')
             ipv6.gateway = confParams.get('IPV6_DEFAULTGW')
             ipconfig.ipv6autoconf = (
                 confParams.get('IPV6_AUTOCONF') == 'yes')
             ipconfig.dhcpv6 = confParams.get('DHCPV6C') == 'yes'
         if not iface.mtu:
             mtu = confParams.get('MTU')
             if mtu:
                 mtu = int(mtu)
     return ipconfig, mtu
Example #9
0
 def _getIfaceConfValues(iface, _netinfo):
     ipaddr, netmask, gateway, bootproto, _ = iface.getIpConfig()
     mtu = iface.mtu
     if _netinfo.ifaceUsers(iface.name):
         confParams = netinfo.getIfaceCfg(iface.name)
         if not ipaddr and bootproto != 'dhcp':
             ipaddr = confParams.get('IPADDR', None)
             netmask = confParams.get('NETMASK', None)
             gateway = confParams.get('GATEWAY', None)
             bootproto = confParams.get('BOOTPROTO', None)
         if not iface.mtu:
             mtu = confParams.get('MTU', None)
             if mtu:
                 mtu = int(mtu)
     return ipaddr, netmask, gateway, bootproto, mtu
Example #10
0
 def _getIfaceConfValues(iface, _netinfo):
     ipaddr, netmask, gateway, bootproto, _, defaultRoute = iface.getIpConfig()
     defaultRoute = ConfigWriter._toIfcfgFormat(defaultRoute)
     mtu = iface.mtu
     if _netinfo.ifaceUsers(iface.name):
         confParams = netinfo.getIfaceCfg(iface.name)
         if not ipaddr and bootproto != "dhcp":
             ipaddr = confParams.get("IPADDR", None)
             netmask = confParams.get("NETMASK", None)
             gateway = confParams.get("GATEWAY", None)
             bootproto = confParams.get("BOOTPROTO", None)
         if defaultRoute is None:
             defaultRoute = confParams.get("DEFROUTE", None)
         if not iface.mtu:
             mtu = confParams.get("MTU", None)
             if mtu:
                 mtu = int(mtu)
     return ipaddr, netmask, gateway, bootproto, mtu, defaultRoute
Example #11
0
 def _getIfaceConfValues(iface, _netinfo):
     ipaddr, netmask, gateway, bootproto, _, defaultRoute = \
         iface.getIpConfig()
     defaultRoute = ConfigWriter._toIfcfgFormat(defaultRoute)
     mtu = iface.mtu
     if _netinfo.ifaceUsers(iface.name):
         confParams = netinfo.getIfaceCfg(iface.name)
         if not ipaddr and bootproto != 'dhcp':
             ipaddr = confParams.get('IPADDR', None)
             netmask = confParams.get('NETMASK', None)
             gateway = confParams.get('GATEWAY', None)
             bootproto = confParams.get('BOOTPROTO', None)
         if defaultRoute is None:
             defaultRoute = confParams.get('DEFROUTE', None)
         if not iface.mtu:
             mtu = confParams.get('MTU', None)
             if mtu:
                 mtu = int(mtu)
     return ipaddr, netmask, gateway, bootproto, mtu, defaultRoute
Example #12
0
    def editBonding(self, bond, bondAttrs, bridge, _netinfo):
        # Save MTU for future set on NICs
        confParams = netinfo.getIfaceCfg(bond)
        mtu = confParams.get('MTU', None)
        if mtu:
            mtu = int(mtu)

        ifdown(bond)
        # Take down all bond's NICs.
        for nic in _netinfo.getNicsForBonding(bond):
            ifdown(nic)
            self.configWriter.removeNic(nic)
            if nic not in bondAttrs['nics']:
                ifup(nic)

        # Note! In case we have bridge up and connected to the bond
        # we will get error in log:
        #   (ifdown) bridge XXX is still up; can't delete it
        # But, we prefer this behaviour instead of taking bridge down
        # Anyway, we will not be able to take it down with connected VMs
        self.configureBonding(bond, bondAttrs['nics'], bridge, mtu,
                              bondAttrs.get('options', None))