예제 #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)
예제 #2
0
        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
예제 #3
0
 def _Unmount(self, part):
     """Unmount a partition that was mounted by _Mount."""
     dest_number, _ = self._GetMountPointAndSymlink(part)
     # Due to crosbug/358933, the RmDir call might fail. So we skip the cleanup.
     osutils.UmountDir(dest_number, cleanup=False)
     self._mounted.remove(part)
     self._to_be_rmdir.add(dest_number)
예제 #4
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)
예제 #5
0
  def testMountTmpfsDir(self):
    """Verify mounting a tmpfs works"""
    cleaned = False
    with osutils.TempDir(prefix='chromite.test.osutils') as tempdir:
      st_before = os.stat(tempdir)
      try:
        # Mount the dir and verify it worked.
        osutils.MountTmpfsDir(tempdir)
        st_after = os.stat(tempdir)
        self.assertNotEqual(st_before.st_dev, st_after.st_dev)

        # Unmount the dir and verify it worked.
        osutils.UmountDir(tempdir)
        cleaned = True

        # Finally make sure it's cleaned up.
        self.assertFalse(os.path.exists(tempdir))
      finally:
        if not cleaned:
          cros_build_lib.SudoRunCommand(['umount', '-lf', tempdir],
                                        error_code_ok=True)
예제 #6
0
 def tearDown(self):
     osutils.UmountDir(
         os.path.join(image_test_lib.ROOT_A, 'var'),
         cleanup=False,
     )
예제 #7
0
 def Unmount(self):
     """Unmounts /proc in chroot. Undoes Mount."""
     if not self.is_mounted:
         return
     osutils.UmountDir(self.proc_path_chroot, cleanup=False)