Exemple #1
0
    def MountedMoblabDiskContext(self):
        """A contextmanager to mount the already prepared moblab disk.

    This can be used to modify the external disk mounted by the moblab VM, while
    the VMs are not running. It is often much more performance to modify the
    disk on the host than SCP'ing large files into the VM.

    vms = moblab_vm.MoblabVm(workspace_dir)
    vms.Create(...)
    with vms.MountedMoblabDiskContext() as moblab_disk_dir:
      # Copy stuff into the directory at moblab_disk_dir
    vms.Start()
    ...
    """
        if not self.initialized:
            raise MoblabVmError(
                'Uninitialized workspace %s. Can not mount disk.' %
                self.workspace)
        if self.running:
            raise MoblabVmError(
                'VM at %s is already running. Stop() before mounting disk.' %
                self.workspace)

        with osutils.TempDir() as tempdir:
            osutils.MountDir(self._config[_CONFIG_MOBLAB_DISK],
                             tempdir,
                             'ext4',
                             skip_mtab=True)
            try:
                yield tempdir
            finally:
                osutils.UmountDir(tempdir)
        def _GetImageParams(image):
            """Returns the parameters of a single DLC image.

      Args:
        image: The input image.

      Returns:
        Same values as _GetDlcImageParams()
      """
            mount_point = os.path.join(self.work_dir, 'mount-point')
            osutils.MountDir(image, mount_point, mount_opts=('ro', ))
            try:
                lsb_release = utils.ReadLsbRelease(mount_point)
            finally:
                osutils.UmountDir(mount_point)

            dlc_id = lsb_release[build_dlc.DLC_ID_KEY]
            dlc_package = lsb_release[build_dlc.DLC_PACKAGE_KEY]
            appid = lsb_release[build_dlc.DLC_APPID_KEY]

            if gspaths.IsDLCImage(image):
                if dlc_id != image.dlc_id:
                    raise Error(
                        'The DLC ID (%s) inferred from the file path does not '
                        'match the one (%s) from the lsb-release.' %
                        (image.dlc_id, dlc_id))
                if dlc_package != image.dlc_package:
                    raise Error(
                        'The DLC package (%s) inferred from the file path '
                        'does not match the one (%s) from the lsb-release.' %
                        (image.dlc_package, dlc_package))

            return dlc_id, dlc_package, appid
Exemple #3
0
  def CreateExt4Image(self):
    """Create an ext4 image."""
    with osutils.TempDir(prefix='dlc_') as temp_dir:
      mount_point = os.path.join(temp_dir, 'mount_point')
      # Create the directory where the image is located if it doesn't exist.
      osutils.SafeMakedirs(os.path.split(self.dest_image)[0])
      # Create a raw image file.
      with open(self.dest_image, 'w') as f:
        f.truncate(self._BLOCKS * self._BLOCK_SIZE)
      # Create an ext4 file system on the raw image.
      cros_build_lib.run([
          '/sbin/mkfs.ext4', '-b',
          str(self._BLOCK_SIZE), '-O', '^has_journal', self.dest_image
      ],
                         capture_output=True)
      # Create the mount_point directory.
      osutils.SafeMakedirs(mount_point)
      # Mount the ext4 image.
      osutils.MountDir(self.dest_image, mount_point, mount_opts=('loop', 'rw'))

      try:
        self.SetupDlcImageFiles(mount_point)
      finally:
        # Unmount the ext4 image.
        osutils.UmountDir(mount_point)
      # Shrink to minimum size.
      cros_build_lib.run(['/sbin/e2fsck', '-y', '-f', self.dest_image],
                         capture_output=True)
      cros_build_lib.run(['/sbin/resize2fs', '-M', self.dest_image],
                         capture_output=True)
Exemple #4
0
 def Mount(self):
     """Mounts /proc in chroot. Remounts it if already mounted."""
     self.Unmount()
     osutils.MountDir(self.PROC_PATH,
                      self.proc_path_chroot,
                      'proc',
                      debug_level=logging.DEBUG)
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 _Mount(self, part, mount_opts):
    if not self.destination:
      self.destination = osutils.TempDir().tempdir
      self._destination_created = True

    dest_number, dest_label = self._GetMountPointAndSymlink(part)
    if part in self._mounted and 'remount' not in mount_opts:
      return dest_number

    osutils.MountDir(self.GetPartitionDevName(part.number), dest_number,
                     makedirs=True, skip_mtab=False, sudo=True,
                     mount_opts=mount_opts)
    self._mounted.add(part)

    osutils.SafeSymlink(os.path.basename(dest_number), dest_label)
    self._symlinks.add(dest_label)

    return dest_number
Exemple #7
0
 def setUp(self):
     osutils.MountDir(
         os.path.join(image_test_lib.STATEFUL, 'var_overlay'),
         os.path.join(image_test_lib.ROOT_A, 'var'),
         mount_opts=('bind', ),
     )