Example #1
0
    def get_interface_by_name(self, name):

        if name == "lo":  # loopback interface is not handled
            raise StorLeverError("Interface(%s) cannot support" % name, 404)

        dev = ifconfig.findif(name, False)
        if dev is None:
            raise StorLeverError("Interface(%s) does not exist" % name, 404)

        encap_type = self._get_if_encap(dev.name)
        if encap_type != 1:
            raise StorLeverError(
                "Interface(%s)'s type(%d) is not supported by storlever" %
                (name, encap_type), 400)

        return EthInterface(dev.name)
Example #2
0
    def get_interface_list(self):

        interfaces_name = []
        for dev in ifconfig.iterifs(False):
            if dev.name == "lo":  # loopback interface is not handled
                continue

            if self._get_if_encap(dev.name) != 1:
                # only support Ethernet
                continue

            interfaces_name.append(dev.name)
        interfaces_name.sort()

        interfaces = []
        for name in interfaces_name:
            interfaces.append(EthInterface(name))

        return interfaces
Example #3
0
    def del_group(self, bond_name, user="******"):
        # check exist
        group_name_list = self.group_name_list()
        if bond_name not in group_name_list:
            raise StorLeverError("%s not found" % bond_name, 404)
        if bond_name == "bond0":
            # if want to delete bond0, there must be no other
            # group, because bonding driver would auto create bond0 interface
            # if there is another bond interface beyond bond0
            if (len(group_name_list) > 1) or \
                    (group_name_list[0] != bond_name):
                raise StorLeverError("Other bonding group must be "
                               "deleted before bond0 can be deleted", 400)

        bond_group = BondGroup(bond_name)
        bond_slaves = bond_group.slaves

        check_output([IFDOWN, bond_name])

        # get mutex
        with self.lock:
            # change bond.conf
            self._del_bond_from_conf(bond_name)

            # modify the slaves conf, especial for the first one,
            # copy the bond ip config to it
            is_first = True
            for slave in bond_slaves:
                slave_object = EthInterface(slave)
                if is_first:
                    slave_object.conf["IPADDR"] = \
                        bond_group.conf.get("IPADDR", "")
                    slave_object.conf["NETMASK"] = \
                        bond_group.conf.get("NETMASK", "")
                    slave_object.conf["GATEWAY"] = \
                        bond_group.conf.get("GATEWAY", "")
                    is_first = False
                else:
                    slave_object.conf["IPADDR"] = ""
                    slave_object.conf["NETMASK"] = ""
                    slave_object.conf["GATEWAY"] = ""

                slave_object.conf.delete("MASTER")
                slave_object.conf.delete("SLAVE")
                slave_object.save_conf()


            # delete ifcfg-bond*
            ifcfg_name = os.path.join(IF_CONF_PATH, "ifcfg-%s" % bond_name)
            if os.path.isfile(ifcfg_name):
                os.remove(ifcfg_name)

        # restart network
        if os.path.exists(BONDING_MASTERS):
            write_file_entry(BONDING_MASTERS, "-%s\n" % bond_name)
        for slave_if in bond_slaves:
            check_output([IFUP, slave_if])

        # if_mgr()._restart_network()

        logger.log(logging.INFO, logger.LOG_TYPE_CONFIG,
                   "bond group %s (slaves:[%s]) "
                   "is deleted by user(%s)" %
                   (bond_name, ",".join(bond_slaves),  user))
Example #4
0
    def add_group(self, miimon, mode, ifs=[],
                  ip="", netmask="", gateway="",
                  user="******"):
        """ add a bond group

        parameters:
        miimon, int, link detect interval in ms
        mode, int, bond mode
        ifs, Array of string, the array of the slave interface name

        return the new bond group name (bond interface name)

        """

        if mode not in modeMap:
            StorLeverError("mdoe(%d) is not supported" % mode, 400)

        # get mutex
        with self.lock:
            # check slave ifs exist and not slave
            exist_if_list = if_mgr().interface_name_list()
            for slave_if in ifs:
                if slave_if not in exist_if_list:
                    raise StorLeverError("%s not found" % slave_if, 404)

                if ifconfig.Interface(slave_if).is_slave():
                    raise StorLeverError("%s is already a slave of other bond group"
                                         % slave_if, 400)

            # find the available bond name
            max_index = self._find_max_index()
            bond_name = "bond%d" % (max_index + 1)

            # change bond.conf
            self._add_bond_to_conf(bond_name)

            # create ifcfg-bond*
            conf = properties(DEVICE=bond_name,
                              IPADDR="",
                              NETMASK="",
                              GATEWAY="",
                              BOOTPROTO="none",
                              NM_CONTROLLED="no",
                              ONBOOT="yes",
                              BONDING_OPTS='"miimon=%d, mode=%d"'
                                           % (miimon, mode))
            ifcfg_name = "ifcfg-%s" % bond_name
            conf.apply_to(os.path.join(IF_CONF_PATH, ifcfg_name))

            # modify the slave's ifcfg
            for slave_if in ifs:
                slave_object = EthInterface(slave_if)
                slave_object.conf.delete("IPADDR")
                slave_object.conf.delete("NETMASK")
                slave_object.conf.delete("GATEWAY")
                slave_object.conf["BOOTPROTO"] = "none"
                slave_object.conf["ONBOOT"] = "yes"
                slave_object.conf["MASTER"] = bond_name
                slave_object.conf["SLAVE"] = "yes"
                slave_object.save_conf()

        # remove the if's ip avoid ip conflict
        # for slave_if in ifs:
            # check_output([IFDOWN, slave_if])
            # ifconfig.Interface(slave_if).set_ip("0.0.0.0")
            # check_output([IFUP, slave_if])

        # restart network
        check_output([IFUP, bond_name])

        # set real ip
        if ip != "" or netmask != "" or gateway != "":
            with self.lock:
                conf = properties(IPADDR=ip,
                                  NETMASK=netmask,
                                  GATEWAY=gateway)
                conf.apply_to(os.path.join(IF_CONF_PATH, ifcfg_name))
            check_output([IFDOWN, bond_name])
            check_output([IFUP, bond_name])

        logger.log(logging.INFO, logger.LOG_TYPE_CONFIG,
                   "New bond group %s (mode:%d, miimon:%d, slaves:[%s]) "
                   "is added by user(%s)" %
                   (bond_name, mode, miimon, ",".join(ifs),  user))

        return bond_name
Example #5
0
    def del_group(self, bond_name, user="******"):
        # check exist
        group_name_list = self.group_name_list()
        if bond_name not in group_name_list:
            raise StorLeverError("%s not found" % bond_name, 404)
        if bond_name == "bond0":
            # if want to delete bond0, there must be no other
            # group, because bonding driver would auto create bond0 interface
            # if there is another bond interface beyond bond0
            if (len(group_name_list) > 1) or \
                    (group_name_list[0] != bond_name):
                raise StorLeverError("Other bonding group must be "
                               "deleted before bond0 can be deleted", 400)

        bond_group = BondGroup(bond_name)
        bond_slaves = bond_group.slaves

        check_output([IFDOWN, bond_name])

        # get mutex
        with self.lock:
            # change bond.conf
            self._del_bond_from_conf(bond_name)

            # modify the slaves conf, especial for the first one,
            # copy the bond ip config to it
            is_first = True
            for slave in bond_slaves:
                slave_object = EthInterface(slave)
                if is_first:
                    slave_object.conf["IPADDR"] = \
                        bond_group.conf.get("IPADDR", "")
                    slave_object.conf["NETMASK"] = \
                        bond_group.conf.get("NETMASK", "")
                    slave_object.conf["GATEWAY"] = \
                        bond_group.conf.get("GATEWAY", "")
                    is_first = False
                else:
                    slave_object.conf["IPADDR"] = ""
                    slave_object.conf["NETMASK"] = ""
                    slave_object.conf["GATEWAY"] = ""

                slave_object.conf.delete("MASTER")
                slave_object.conf.delete("SLAVE")
                slave_object.save_conf()


            # delete ifcfg-bond*
            ifcfg_name = os.path.join(IF_CONF_PATH, "ifcfg-%s" % bond_name)
            if os.path.isfile(ifcfg_name):
                os.remove(ifcfg_name)

        # restart network
        if os.path.exists(BONDING_MASTERS):
            write_file_entry(BONDING_MASTERS, "-%s\n" % bond_name)
        for slave_if in bond_slaves:
            check_output([IFUP, slave_if])

        # if_mgr()._restart_network()

        logger.log(logging.INFO, logger.LOG_TYPE_CONFIG,
                   "bond group %s (slaves:[%s]) "
                   "is deleted by user(%s)" %
                   (bond_name, ",".join(bond_slaves),  user))
Example #6
0
    def add_group(self, miimon, mode, ifs=[],
                  ip="", netmask="", gateway="",
                  user="******"):
        """ add a bond group

        parameters:
        miimon, int, link detect interval in ms
        mode, int, bond mode
        ifs, Array of string, the array of the slave interface name

        return the new bond group name (bond interface name)

        """

        if mode not in modeMap:
            StorLeverError("mdoe(%d) is not supported" % mode, 400)

        # get mutex
        with self.lock:
            # check slave ifs exist and not slave
            exist_if_list = if_mgr().interface_name_list()
            for slave_if in ifs:
                if slave_if not in exist_if_list:
                    raise StorLeverError("%s not found" % slave_if, 404)

                if ifconfig.Interface(slave_if).is_slave():
                    raise StorLeverError("%s is already a slave of other bond group"
                                         % slave_if, 400)

            # find the available bond name
            max_index = self._find_max_index()
            bond_name = "bond%d" % (max_index + 1)

            # change bond.conf
            self._add_bond_to_conf(bond_name)

            # create ifcfg-bond*
            conf = properties(DEVICE=bond_name,
                              IPADDR="",
                              NETMASK="",
                              GATEWAY="",
                              BOOTPROTO="none",
                              NM_CONTROLLED="no",
                              ONBOOT="yes",
                              BONDING_OPTS='"miimon=%d, mode=%d"'
                                           % (miimon, mode))
            ifcfg_name = "ifcfg-%s" % bond_name
            conf.apply_to(os.path.join(IF_CONF_PATH, ifcfg_name))

            # modify the slave's ifcfg
            for slave_if in ifs:
                slave_object = EthInterface(slave_if)
                slave_object.conf.delete("IPADDR")
                slave_object.conf.delete("NETMASK")
                slave_object.conf.delete("GATEWAY")
                slave_object.conf["BOOTPROTO"] = "none"
                slave_object.conf["ONBOOT"] = "yes"
                slave_object.conf["MASTER"] = bond_name
                slave_object.conf["SLAVE"] = "yes"
                slave_object.save_conf()

        # remove the if's ip avoid ip conflict
        # for slave_if in ifs:
            # check_output([IFDOWN, slave_if])
            # ifconfig.Interface(slave_if).set_ip("0.0.0.0")
            # check_output([IFUP, slave_if])

        # restart network
        check_output([IFUP, bond_name])

        # set real ip
        if ip != "" or netmask != "" or gateway != "":
            with self.lock:
                conf = properties(IPADDR=ip,
                                  NETMASK=netmask,
                                  GATEWAY=gateway)
                conf.apply_to(os.path.join(IF_CONF_PATH, ifcfg_name))
            check_output([IFDOWN, bond_name])
            check_output([IFUP, bond_name])

        logger.log(logging.INFO, logger.LOG_TYPE_CONFIG,
                   "New bond group %s (mode:%d, miimon:%d, slaves:[%s]) "
                   "is added by user(%s)" %
                   (bond_name, mode, miimon, ",".join(ifs),  user))

        return bond_name