def testChangeUnitInsideChroot(self):
     self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
     self.rc.AddCmdResult(partial_mock.Ignore(), output=self.SAMPLE_CGPT)
     partitions = image_lib.GetImageDiskPartitionInfo('_ignored')
     part_dict = {p.name: p for p in partitions}
     self.assertEqual(part_dict['STATE'].start, 983564288)
     self.assertEqual(part_dict['STATE'].size, 1073741824)
 def testNormalPath(self):
     self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
     self.rc.AddCmdResult(partial_mock.Ignore(), output=self.SAMPLE_PARTED)
     partitions = image_lib.GetImageDiskPartitionInfo('_ignored')
     part_dict = {p.name: p for p in partitions}
     self.assertEqual(12, len(partitions))
     self.assertEqual(1, part_dict['STATE'].number)
     self.assertEqual(2097152000, part_dict['ROOT-A'].size)
 def testKeyedByNumber(self):
     self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=False)
     self.rc.AddCmdResult(partial_mock.Ignore(), output=self.SAMPLE_PARTED)
     partitions = image_lib.GetImageDiskPartitionInfo('_ignored')
     part_dict = {p.number: p for p in partitions}
     self.assertEqual(12, len(part_dict))
     self.assertEqual('STATE', part_dict[1].name)
     self.assertEqual(2097152000, part_dict[3].size)
     self.assertEqual('reserved', part_dict[9].name)
     self.assertEqual('reserved', part_dict[10].name)
Example #4
0
def VMIsUpdatable(path):
  """Check if the existing VM image is updatable.

  Args:
    path: Path to the VM image.

  Returns:
    True if VM is updatable; False otherwise.
  """
  table = {p.name: p for p in image_lib.GetImageDiskPartitionInfo(path)}
  # Assume if size of the two root partitions match, the image
  # is updatable.
  return table['ROOT-B'].size == table['ROOT-A'].size
def _MountImagePartition(image_file,
                         part_id,
                         destination,
                         gpt_table=None,
                         sudo=True,
                         makedirs=True,
                         mount_opts=('ro', ),
                         skip_mtab=False):
    """Mount a |partition| from |image_file| to |destination|.

  If there is a GPT table (GetImageDiskPartitionInfo), it will be used for
  start offset and size of the selected partition. Otherwise, the GPT will
  be read again from |image_file|.

  The mount option will be:

    -o offset=XXX,sizelimit=YYY,(*mount_opts)

  Args:
    image_file: A path to the image file (chromiumos_base_image.bin).
    part_id: A partition name or number.
    destination: A path to the mount point.
    gpt_table: A list of PartitionInfo objects. See
      image_lib.GetImageDiskPartitionInfo.
    sudo: Same as MountDir.
    makedirs: Same as MountDir.
    mount_opts: Same as MountDir.
    skip_mtab: Same as MountDir.
  """

    if gpt_table is None:
        gpt_table = image_lib.GetImageDiskPartitionInfo(image_file)

    for part in gpt_table:
        if part_id == part.name or part_id == part.number:
            break
    else:
        part = None
        raise ValueError('Partition number %s not found in the GPT %r.' %
                         (part_id, gpt_table))

    opts = ['loop', 'offset=%d' % part.start, 'sizelimit=%d' % part.size]
    opts += mount_opts
    osutils.MountDir(image_file,
                     destination,
                     sudo=sudo,
                     makedirs=makedirs,
                     mount_opts=opts,
                     skip_mtab=skip_mtab)
 def testCgpt(self):
     """Tests that we can list all partitions with `cgpt` correctly."""
     self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True)
     self.rc.AddCmdResult(partial_mock.Ignore(), output=self.SAMPLE_CGPT)
     partitions = image_lib.GetImageDiskPartitionInfo('...')
     part_dict = {p.name: p for p in partitions}
     self.assertEqual(part_dict['STATE'].start, 983564288)
     self.assertEqual(part_dict['STATE'].size, 1073741824)
     self.assertEqual(part_dict['STATE'].number, 1)
     self.assertEqual(part_dict['STATE'].name, 'STATE')
     self.assertEqual(part_dict['EFI-SYSTEM'].start, 249856 * 512)
     self.assertEqual(part_dict['EFI-SYSTEM'].size, 32768 * 512)
     self.assertEqual(part_dict['EFI-SYSTEM'].number, 12)
     self.assertEqual(part_dict['EFI-SYSTEM'].name, 'EFI-SYSTEM')
     self.assertEqual(12, len(partitions))
Example #7
0
def ExtractPartition(filename, partition, out_part):
  """Extracts partition from an image file.

  Args:
    filename: The image file.
    partition: The partition name. e.g. ROOT or KERNEL.
    out_part: The output partition file.
  """
  parts = image_lib.GetImageDiskPartitionInfo(filename)
  part_info = [p for p in parts if p.name == partition][0]

  offset = int(part_info.start)
  length = int(part_info.size)

  filelib.CopyFileSegment(filename, 'rb', length, out_part, 'wb',
                          in_seek=offset)
Example #8
0
def IsGptImage(image):
    """Returns true if the image is a GPT image."""
    try:
        return bool(image_lib.GetImageDiskPartitionInfo(image))
    except cros_build_lib.RunCommandError:
        return False