Example #1
0
    def get_map(self):
        if not self._MultiPath__map is None:
            return self._MultiPath__map

        table = self.get_table()

        import dm as _dm

        for map in _dm.maps():
            # XXX wtf?  why's it have a space at the end sometimes?
            if str(map.table).strip() == str(table).strip():
                if self.name != map.name:
                    self.name = nameCache.rename(self.name, map.name)

                self._MultiPath__map = map
                self.buildParts()
                self.active = True
                del _dm
                return self._MultiPath__map

        # all else has failed, make a new map...
        self._MultiPath__map = _dm.map(name=self.name, table=table)
        self.buildParts()
        self.active = True
        del _dm
        return self._MultiPath__map
Example #2
0
def getNameFromDmNode(dm_node):
    """ Return the related name for the specified node.

    Expects a device node with or without the "/dev" prefix.

    Returns a String representing the name.  None if the major, minor
    pair was not found in the maps list.
    """

    if not dm_node.startswith("/dev"):
        import os.path as _path
        dm_node = _path.join("/dev", dm_node)
        del _path

    import os as _os
    stat = _os.stat(dm_node)
    major = long(_os.major(stat.st_rdev))
    minor = long(_os.minor(stat.st_rdev))
    del _os

    for map in dm.maps():
        if map.dev.major == major and map.dev.minor == minor:
            return map.name

    # In case the major, minor pair is not found in maps.
    return None
Example #3
0
def getMap(uuid = None, major = None, minor = None, name = None):
    """ Return a map that matches the given parameters.

    uuid and name are strings.  major and minor are converted to long before
    being compared.

    major and minor should be specified as a pair -- that is to say one
    should either give both of them or neither of them.

    Returns None if the map is not found.
    """
    # don't bother if there are no specs to search for
    if uuid is None and major is None and minor is None and name is None:
        return None

    # Return None if we don't find the map.
    map = None
    for _map in dm.maps():
        if (name is None or \
            (_map.name is not None and _map.name == name)) and\
           (uuid is None or \
            (_map.uuid is not None and _map.uuid == uuid)) and\
           ((major is None or minor is None) or \
            (_map.dev.major is not None and _map.dev.minor is not None and \
             _map.dev.major == long(major) and _map.dev.minor == long(minor))):
            map = _map
            break

    return map
Example #4
0
    def get_map(self):
        if not self._MultiPath__map is None:
            return self._MultiPath__map

        table = self.get_table()

        import dm as _dm

        for map in _dm.maps():
            if compare_tables(map.table, table):
                if self.name != map.name:
                    self.name = nameCache.rename(self.name, map.name)

                self._MultiPath__map = map
                self.buildParts()
                self.active = True
                del _dm
                return self._MultiPath__map

        # all else has failed, make a new map...
        self._MultiPath__map = _dm.map(name=self.name, table=table)
        self.buildParts()
        self.active = True
        del _dm
        return self._MultiPath__map
Example #5
0
    def get_map(self):
        if not self._RaidSet__map is None:
            return self._RaidSet__map

        # we get "/dev/hda" from one and "3:0" from the other, so we have to
        # fix up the device name
        def map_dev(path):
            if path[0] != '/':
                return path.strip()

            try:
                newpath = path.split('/')[-1]
                pos = 0
                dev = None
                num = None
                while pos < len(newpath):
                    if newpath[pos].isdigit():
                        dev = newpath[:pos]
                        num = newpath[pos:]
                        break
                    pos += 1
                if dev is None:
                    dev = newpath
                sysnewpath = None
                if num is None:
                    sysnewpath = '/sys/block/%s/dev' % (dev, )
                else:
                    sysnewpath = '/sys/block/%s/%s%s/dev' % (dev, dev, num)
                f = open(sysnewpath, 'r')
                l = f.readline()
                return l.strip()
            except:
                return path.strip()

        parts = str(self.rs.dmTable).split(' ')

        table = []
        for part in parts:
            table += [
                map_dev(part),
            ]
        table = _string.join(table, ' ')

        import dm as _dm

        for map in _dm.maps():
            # XXX wtf?  why's it have a space at the end sometimes?
            if str(map.table).strip() == table:
                self._RaidSet__map = map
                self.active = True
                del _dm
                return self._RaidSet__map

        # all else has failed, make a new map...
        self._RaidSet__map = _dm.map(name=self.name, table=self.rs.dmTable)
        self.active = True
        del _dm
        return self._RaidSet__map
Example #6
0
def getDevice(name):
    """Retrieve a major and minor number for a specific path/name.

    The last word after the '/' in name is used to search for the device.
    """
    name = name.split('/')[-1]
    maps = dm.maps()
    for map in maps:
        if name == map.name:
            return (map.dev.major, map.dev.minor)
Example #7
0
def getDmNodeFromName(name):
    """ Return the related node for the specified name.

    Expects a string representing the name.

    Returns dm-MINOR if the map list contains the specified name.
    None if name was not found.
    """
    for map in dm.maps():
        if map.name == name:
            return "dm-%s" % map.dev.minor

    return None
Example #8
0
def DeviceMaps():
    import dm as _dm
    mlist = _dm.maps()
    del _dm
    devs = {}
    for m in mlist:
        devs[m.dev] = m

    maps = {}
    for d, m in devs.items():
        mdeps = filter(lambda x: devs.has_key(x), m.deps)
        for mdep in mdeps:
            key = devs[mdep]
            maps.setdefault(key, []).append(m)
    return maps
Example #9
0
def DeviceMaps():
    import dm as _dm
    mlist = _dm.maps()
    del _dm
    devs = {}
    for m in mlist:
        devs[m.dev] = m
        
    maps = {}
    for d,m in devs.items():
        mdeps = filter(lambda x: devs.has_key(x), m.deps)
        for mdep in mdeps:
            key = devs[mdep]
            maps.setdefault(key, []).append(m)
    return maps
Example #10
0
    def create(self, name=None):

        import dm as _dm
        for map in _dm.maps():
            # XXX wtf?  why's it have a space at the end sometimes?
            if str(map.table).strip() == str(self.table).strip():
                self.map = map
                self.name = map.name
                break
        else:
            if name is None:
                name = self.name
            if self.name is None:
                raise ValueError, "DeviceMap name is not set"
            self.map = _dm.map(name=self.name, table=self.table)
        del _dm
Example #11
0
    def create(self, name=None):

        import dm as _dm
        import device as _device
        for map in _dm.maps():
            if _device.compare_tables(map.table, self.table):
                self.map = map
                self.name = map.name
                break
        else:
            if name is None:
                name = self.name
            if self.name is None:
                raise ValueError, "DeviceMap name is not set"
            self.map = _dm.map(name = self.name, table = self.table)
        del _dm
        del _device
Example #12
0
    def get_map(self):
        if not self._RaidSet__map is None:
            return self._RaidSet__map

        import dm as _dm

        for map in _dm.maps():
            if compare_tables(map.table, self.rs.dmTable):
                self._RaidSet__map = map
                self.active = True
                del _dm
                return self._RaidSet__map

        # all else has failed, make a new map...
        self._RaidSet__map = _dm.map(name=self.name, table=self.rs.dmTable)
        self.active = True
        del _dm
        return self._RaidSet__map
Example #13
0
class MPNameCache(_IUD):
    import dm as _dm
    # we'll get other maps here which will never be dereffed, but we
    # also won't wind up using them for .new(), so that's good.
    data = {}
    for map in _dm.maps():
        data.setdefault(map.name, 1)
    del _dm

    def __init__(self):
        _IUD.__init__(self)
        self.data = MPNameCache.data

    def new(self):
        n = 0
        while True:
            name = 'mpath%s' % (n, )
            if self.try_get(name):
                return name
            n += 1

    def get(self, name):
        self.setdefault(name, 0)
        self[name] += 1
        return name

    def try_get(self, name):
        if not self.has_key(name):
            self.get(name)
            return True
        return False

    def put(self, name):
        self[name] -= 1
        if self[name] == 0:
            del self[name]

    def rename(self, old_name, new_name):
        self[new_name] = self[old_name]
        del self[old_name]
        return new_name
Example #14
0
def getDevice(name):
    name = name.split('/')[-1]
    maps = dm.maps()
    for map in maps:
        if name == map.name:
            return (map.dev.major, map.dev.minor)