Пример #1
0
def _detectLogicalVolumes(hierarchy):
    """
    Detects the existing logical volumes. Updates the passed LVM hierarchy
    dictionary.

    @type  hierarchy: dict
    @param hierarchy: LVM hierarchy dictionary

    @rtype: None
    @returns: nothing
    """
    # get volume groups dictionary
    vgs = hierarchy["vgs"]

    # make a query for logical volumes
    stdout = os.popen(_CMD_LVS)
    lines = stdout.read().splitlines()
    stdout.close()

    # parse logical group info lines - vg_name:lv_name,lv_size
    for line in lines:
        parts = line.strip().split(":")

        # invalid line: ignore it
        if len(parts) < 3:
            # print 'ERROR', line
            continue

        # expand the parsed fields
        vgName, lvName, lvSize = parts[0:3]

        # volume group does not exist: ignore
        if vgName not in vgs:
            # print 'ERROR', line
            continue

        # get the device for the logical volume
        device = os.path.join("/dev/mapper", vgName + "-" + lvName)

        # add logical group info to its volume group
        vgs[vgName]["lvs"][lvName] = {
            "end": -1,
            "name": lvName,
            "format": None,
            "free": _convertToKB(fs.getFreeSpace(device, -1)),
            "fs": fs.getFileSystem(device),
            "size": _convertToKB(lvSize),
            "start": -1,
            "type": None,
        }
Пример #2
0
def getHierarchy():
    """
    Detects and returns the hierarchy of RAID. It'll look something like:

        {
            'md0': {
                'name': 'md0',
                'level': 5,
                'devices': ['sda2', 'sdb4', 'sdc7', ...],
                'spares': 2,
                'chunkSize': 1024,
                'fileSystem': 'ext3',
                'size' : 4194304,  # 4 GB in KB
                'free' : 2097152,  # 50% free
            }
            ...
        }

    @rtype: dict
    @returns: RAID hierarchy
    """

    # initialize hierarchy dictionary
    hierarchy = {}

    # make a query to detect all devices
    stdout = os.popen(_CMD_AUTO_DETECT)
    stdout.read()
    stdout.close()

    # check every md device:
    for md in glob.glob('/dev/md*'):

        # make a query to detail this md device
        stdout = os.popen(_CMD_DETAIL % md)
        text = stdout.read()
        status = stdout.close() or 0

        # command failed : not a valid device
        if status != 0:
            continue

        # short name for md (e.g: '/dev/md0' -> 'md0')
        shortMd = short(md)

        # apply the regex to get the device's data
        detailsMatch = _DETAILS.search(text.replace('\n', ' '))

        # no match object : something went wrong when parsing, skip it
        if not detailsMatch:
            continue

        # get data of this md device
        detailsDict = detailsMatch.groupdict()

        # there's no spare device : assign 0 to it
        spares = int(detailsDict.get('spares', 0) or 0)

        # convert level to internal int number
        level = _LEVEL_TO_INTERNAL.get(detailsDict['level'], -1)

        # separate partitions, tray spaces and get only the shortname
        devices = [short(x.strip()) for x in detailsDict['devices'].split(',')]

        # get total size
        stdout = os.popen(_CMD_ALL % md)
        text = stdout.read()
        stdout.close()

        # apply regex to get total size
        totalMatch = _DETAIL_TOTAL.search(text.replace('\n', ' '))

        # no match object : something went wrong when parsing, skip it
        if not totalMatch:
            continue

        # get total size (in KB)
        totalDict = totalMatch.groupdict()
        size = int(totalDict.get('size', 0))

        # arrange data
        mdData = {
            'name' : shortMd,
            'level' : level,
            'devices' : devices,
            'spares' : spares,
            # TODO : not implemented
            'chunkSize' : -1,
            'filesystem' : fs.getFileSystem(md),
            'size' : size,  # in KB
            'free' : fs.getFreeSpace(md, -1) / 1024,  # in KB
        }

        # fill this device data on the hierarchy
        hierarchy[shortMd] = mdData

    # return the built hierarchy dictionary
    return hierarchy