def test_get_root_disk_path(self, mock_vm_info): mock_vm_info.return_value = { constants.DEFAULT_ROOT_ATTACH_POINT: mock.sentinel.path } self.assertEqual(mock.sentinel.path, pathutils.get_root_disk_path(self._instance))
def _export_disk(self, instance): LOG.debug("Trying to get the root virtual hard driver.") current_disk = pathutils.get_root_disk_path(instance) if not current_disk: raise exception.VBoxException("Cannot get the root disk.") disk_info = vhdutils.disk_info(current_disk) root_disk_uuid = disk_info[constants.VHD_PARENT_UUID] root_disk = vhdutils.disk_info(root_disk_uuid) # The root virtual disk is a base disk root_disk_path = root_disk[constants.VHD_PATH] export_path = os.path.join( pathutils.export_dir(instance, constants.PATH_CREATE), os.path.basename(root_disk_path)) LOG.debug("Trying to export the virtual disk to %(export_path)s.", {"export_path": export_path}) existing = constants.VHD_PARENT_UUID in root_disk if existing: # The root virtual disk is linked to another disk base_info = vhdutils.disk_info( root_disk[constants.VHD_PARENT_UUID]) base_disk_path = base_info[constants.VHD_PATH] try: LOG.debug("Cloning base disk for the root disk.") self._vbox_manage.clone_hd( vhd_path=base_disk_path, new_vdh_path=export_path, disk_format=root_disk[constants.VHD_IMAGE_TYPE]) except exception.VBoxException: with excutils.save_and_reraise_exception(): self._clenup_disk(export_path) try: LOG.debug("Cloning VHD image %(base)s to target: %(target)s", { 'base': root_disk_path, 'target': export_path }) # Note(alexandrucoman): If the target disk already exists # only the portion of the source medium which fits into the # destination medium is copied. # This means if the destination medium is smaller than the # source only a part of it is copied, and if the destination # medium is larger than the source the remaining part of the # destination medium is unchanged. self._vbox_manage.clone_hd(root_disk_path, export_path, existing=existing) except exception.VBoxException: with excutils.save_and_reraise_exception(): self._clenup_disk(export_path) return export_path
def _export_disk(self, instance): LOG.debug("Trying to get the root virtual hard driver.") current_disk = pathutils.get_root_disk_path(instance) if not current_disk: raise exception.VBoxException("Cannot get the root disk.") disk_info = vhdutils.disk_info(current_disk) root_disk_uuid = disk_info[constants.VHD_PARENT_UUID] root_disk = vhdutils.disk_info(root_disk_uuid) # The root virtual disk is a base disk root_disk_path = root_disk[constants.VHD_PATH] export_path = os.path.join( pathutils.export_dir(instance, constants.PATH_CREATE), os.path.basename(root_disk_path)) LOG.debug("Trying to export the virtual disk to %(export_path)s.", {"export_path": export_path}) existing = constants.VHD_PARENT_UUID in root_disk if existing: # The root virtual disk is linked to another disk base_info = vhdutils.disk_info( root_disk[constants.VHD_PARENT_UUID]) base_disk_path = base_info[constants.VHD_PATH] try: LOG.debug("Cloning base disk for the root disk.") self._vbox_manage.clone_hd( vhd_path=base_disk_path, new_vdh_path=export_path, disk_format=root_disk[constants.VHD_IMAGE_TYPE]) except exception.VBoxException: with excutils.save_and_reraise_exception(): self._clenup_disk(export_path) try: LOG.debug( "Cloning VHD image %(base)s to target: %(target)s", {'base': root_disk_path, 'target': export_path}) # Note(alexandrucoman): If the target disk already exists # only the portion of the source medium which fits into the # destination medium is copied. # This means if the destination medium is smaller than the # source only a part of it is copied, and if the destination # medium is larger than the source the remaining part of the # destination medium is unchanged. self._vbox_manage.clone_hd(root_disk_path, export_path, existing=existing) except exception.VBoxException: with excutils.save_and_reraise_exception(): self._clenup_disk(export_path) return export_path
def test_get_root_disk_path_fail(self, mock_vm_info): mock_vm_info.side_effect = [exception.VBoxManageError("err"), {}] for _ in range(2): self.assertIsNone(pathutils.get_root_disk_path(self._instance))
def test_get_root_disk_path(self, mock_vm_info): mock_vm_info.return_value = {constants.DEFAULT_ROOT_ATTACH_POINT: mock.sentinel.path} self.assertEqual(mock.sentinel.path, pathutils.get_root_disk_path(self._instance))