Ejemplo n.º 1
0
  def _GetVolumeInfo(lvm_cmd, fields):
    """Returns LVM Volume infos using lvm_cmd

    @param lvm_cmd: Should be one of "pvs", "vgs" or "lvs"
    @param fields: Fields to return
    @return: A list of dicts each with the parsed fields

    """
    if not fields:
      raise errors.ProgrammerError("No fields specified")

    sep = "|"
    cmd = [lvm_cmd, "--noheadings", "--nosuffix", "--units=m", "--unbuffered",
           "--separator=%s" % sep, "-o%s" % ",".join(fields)]

    result = utils.RunCmd(cmd)
    if result.failed:
      raise errors.CommandError("Can't get the volume information: %s - %s" %
                                (result.fail_reason, result.output))

    data = []
    for line in result.stdout.splitlines():
      splitted_fields = line.strip().split(sep)

      if len(fields) != len(splitted_fields):
        raise errors.CommandError("Can't parse %s output: line '%s'" %
                                  (lvm_cmd, line))

      data.append(splitted_fields)

    return data
Ejemplo n.º 2
0
def ReleaseBdevPartitionMapping(loop_dev_path):
    """Release allocated dm devices and loopback devices.

  @type loop_dev_path: string
  @param loop_dev_path: path of loopback device returned by
  L{CreateBdevPartitionMapping}

  """
    result = utils_process.RunCmd(["kpartx", "-d", loop_dev_path])
    if result.failed:
        raise errors.CommandError(
            "Failed to release partition mapping of %s: %s" %
            (loop_dev_path, result.output))

    # The invocation of udevadm settle was added here because users had issues
    # with the loopback device still being busy after kpartx / earlier commands
    # did their work.
    result = utils_process.RunCmd(["udevadm", "settle"])
    if result.failed:
        raise errors.CommandError("Waiting on udev failed: %s" % result.output)

    result = utils_process.RunCmd(["losetup", "-d", loop_dev_path])
    if result.failed:
        raise errors.CommandError("Failed to detach %s: %s" %
                                  (loop_dev_path, result.output))
Ejemplo n.º 3
0
def CreateBdevPartitionMapping(image_path):
    """Create dm device for each partition of disk image.

  This operation will allocate a loopback and a device-mapper device to map
  partitions.
  You must call L{ReleaseBdevPartitionMapping} to clean up resources allocated
  by this function call.

  @type image_path: string
  @param image_path: path of multi-partition disk image
  @rtype: tuple(string, list(string)) or NoneType
  @return: returns the tuple(loopback_device, list(device_mapper_files)) if
    image_path is a multi-partition disk image. otherwise, returns None.

  """
    # Unfortunately, there are two different losetup commands in this world.
    # One has the '-s' switch and the other has the '--show' switch to provide the
    # same functionality.
    result = utils_process.RunCmd(["losetup", "-f", "-s", image_path])
    if result.failed and "invalid option -- 's'" in result.stderr:
        result = utils_process.RunCmd(["losetup", "-f", "--show", image_path])
    if result.failed:
        raise errors.CommandError("Failed to setup loop device for %s: %s" %
                                  (image_path, result.output))
    loop_dev_path = result.stdout.strip()
    logging.debug("Loop dev %s allocated for %s", loop_dev_path, image_path)

    result = utils_process.RunCmd(["kpartx", "-a", "-v", loop_dev_path])
    if result.failed:
        # Just try to cleanup allocated loop device
        utils_process.RunCmd(["losetup", "-d", loop_dev_path])
        raise errors.CommandError(
            "Failed to add partition mapping for %s: %s" %
            (image_path, result.output))
    dm_devs = [x.split(" ") for x in result.stdout.split("\n") if x]
    if dm_devs:
        dm_dev_paths = [
            utils_io.PathJoin("/dev/mapper", x[2]) for x in dm_devs
        ]
        return (loop_dev_path, dm_dev_paths)
    else:
        # image_path is not a multi partition disk image, no need to use
        # device-mapper.
        logging.debug("Release loop dev %s allocated for %s", loop_dev_path,
                      image_path)
        ReleaseBdevPartitionMapping(loop_dev_path)
        return None
Ejemplo n.º 4
0
def ReleaseBdevPartitionMapping(loop_dev_path):
    """Release allocated dm devices and loopback devices.

  @type loop_dev_path: string
  @param loop_dev_path: path of loopback device returned by
  L{CreateBdevPartitionMapping}

  """
    result = utils_process.RunCmd(["kpartx", "-d", loop_dev_path])
    if result.failed:
        raise errors.CommandError(
            "Failed to release partition mapping of %s: %s" %
            (loop_dev_path, result.output))

    result = utils_process.RunCmd(["losetup", "-d", loop_dev_path])
    if result.failed:
        raise errors.CommandError("Failed to detach %s: %s" %
                                  (loop_dev_path, result.output))
Ejemplo n.º 5
0
def _IsUidUsed(uid):
    """Check if there is any process in the system running with the given user-id

  @type uid: integer
  @param uid: the user-id to be checked.

  """
    pgrep_command = [constants.PGREP, "-u", uid]
    result = utils.RunCmd(pgrep_command)

    if result.exit_code == 0:
        return True
    elif result.exit_code == 1:
        return False
    else:
        raise errors.CommandError("Running pgrep failed. exit code: %s" %
                                  result.exit_code)
Ejemplo n.º 6
0
def GetFileStorageSpaceInfo(path):
  """Retrieves the free and total space of the device where the file is
     located.

     @type path: string
     @param path: Path of the file whose embracing device's capacity is
       reported.
     @return: a dictionary containing 'vg_size' and 'vg_free' given in MebiBytes

  """
  try:
    result = os.statvfs(path)
    free = (result.f_frsize * result.f_bavail) / (1024 * 1024)
    size = (result.f_frsize * result.f_blocks) / (1024 * 1024)
    return {"type": constants.ST_FILE,
            "name": path,
            "storage_size": size,
            "storage_free": free}
  except OSError, e:
    raise errors.CommandError("Failed to retrieve file system information about"
                              " path: %s - %s" % (path, e.strerror))