Exemple #1
0
def getDiskInfo(diskDevice):
  """
  Returns a dictionary with the following disk device's info:
    - model: model name
    - size: size in bytes
    - sizeHuman: human readable size
    - removable: whether it is removable or not
    - type: either 'msdos' or 'gpt'.
  diskDevice should no be prefixed with '/dev/'
  """
  if S_ISBLK(os.stat('/dev/{0}'.format(diskDevice)).st_mode) and os.path.exists('/sys/block/{0}'.format(diskDevice)):
    if os.path.exists('/sys/block/{0}/device/model'.format(diskDevice)):
      modelName = open('/sys/block/{0}/device/model'.format(diskDevice), 'r').read().strip()
    else:
      modelName = None
    blockSize = int(open('/sys/block/{0}/queue/logical_block_size'.format(diskDevice), 'r').read().strip())
    size = int(open('/sys/block/{0}/size'.format(diskDevice), 'r').read().strip()) * blockSize
    sizeHuman = getHumanSize(size)
    try:
      removable = int(open('/sys/block/{0}/removable'.format(diskDevice), 'r').read().strip()) == 1
    except:
      removable = False
    with open('/dev/{0}'.format(diskDevice), 'rb') as f:
      parts = pyrp.get_disk_partitions_info(f)
      if parts.mbr is not None:
        partType = 'msdos'
      elif parts.gpt is not None:
        partType = 'gpt'
      else:
        partType = None
    return {'model': modelName, 'size': size, 'sizeHuman': sizeHuman, 'removable': removable, 'type': partType}
  else:
    return None
Exemple #2
0
def getFsType(partitionDevice):
  """
  Returns the file system type for that partition.
  'partitionDevice' should no be prefixed with '/dev/' if it's a block device.
  It can be a full path if the partition is contained in a file.
  Returns 'Extended' if the partition is an extended partition and has no filesystem.
  """
  if os.path.exists('/dev/{0}'.format(partitionDevice)) and S_ISBLK(os.stat('/dev/{0}'.format(partitionDevice)).st_mode):
    path = '/dev/{0}'.format(partitionDevice)
  elif os.path.isfile(partitionDevice):
    path = partitionDevice
  else:
    fstype = False
    path = False
  if path:
    try:
      fstype = execGetOutput(['/sbin/blkid', '-s', 'TYPE', '-o', 'value', path], shell=False)
      if fstype:
        fstype = fstype[0]
      else:
        fstype = False
    except subprocess.CalledProcessError:
      fstype = False
    if not fstype and not os.path.isfile(path):
      # is it a real error or is it an extended partition?
      # only check if block device rather than partition in file
      m = re.match(r'^(.*/[^/]+?)([0-9]+)$', path)
      # split into block device and partition
      devpath = m.group(1)
      partNum = m.group(2)
      if re.match(r'^.+[0-9]p$', devpath):
        devpath = devpath[:-1]  # case when using loop device with partitions
      with open(devpath, 'rb') as device:
        parts = pyrp.get_disk_partitions_info(device)
        if parts.mbr is not None:
          for part in parts.mbr.partitions:
            if 'Extended' in part[-1]:
              if str(part.index) == partNum:
                fstype = 'Extended'
  return fstype