def instance_for_format(image, mountdir, partition): """Get a Mount instance for the image type :param image: instance of nova.virt.image.model.Image :param mountdir: path to mount the image at :param partition: partition number to mount """ LOG.debug( "Instance for format image=%(image)s " "mountdir=%(mountdir)s partition=%(partition)s", { 'image': image, 'mountdir': mountdir, 'partition': partition }) if isinstance(image, imgmodel.LocalFileImage): if image.format == imgmodel.FORMAT_RAW: LOG.debug("Using LoopMount") return importutils.import_object( "nova.virt.disk.mount.loop.LoopMount", image, mountdir, partition) else: LOG.debug("Using NbdMount") return importutils.import_object( "nova.virt.disk.mount.nbd.NbdMount", image, mountdir, partition) elif isinstance(image, imgmodel.LocalBlockImage): LOG.debug("Using BlockMount") return importutils.import_object( "nova.virt.disk.mount.block.BlockMount", image, mountdir, partition) else: # TODO(berrange) We could mount RBDImage directly # using kernel RBD block dev support. # # This is left as an enhancement for future # motivated developers todo, since raising # an exception is on par with what this # code did historically raise exception.UnsupportedImageModel(image.__class__.__name__)
def setup(self, mount=True): LOG.debug("Setting up appliance for %(image)s", {'image': self.image}) try: self.handle = tpool.Proxy( guestfs.GuestFS(python_return_dict=False, close_on_exit=False)) except TypeError as e: if 'close_on_exit' in str(e) or 'python_return_dict' in str(e): # NOTE(russellb) In case we're not using a version of # libguestfs new enough to support parameters close_on_exit # and python_return_dict which were added in libguestfs 1.20. self.handle = tpool.Proxy(guestfs.GuestFS()) else: raise if CONF.guestfs.debug: self.configure_debug() try: if forceTCG: # TODO(mriedem): Should we be using set_backend_setting # instead to just set the single force_tcg setting? Because # according to the guestfs docs, set_backend_settings will # overwrite all backend settings. The question is, what would # the value be? True? "set_backend_setting" is available # starting in 1.27.2 which should be new enough at this point # on modern distributions. ret = self.handle.set_backend_settings(["force_tcg"]) if ret != 0: LOG.warning('Failed to force guestfs TCG mode. ' 'guestfs_set_backend_settings returned: %s', ret) except AttributeError as ex: # set_backend_settings method doesn't exist in older # libguestfs versions, so nothing we can do but ignore LOG.warning("Unable to force TCG mode, " "libguestfs too old? %s", ex) pass try: if isinstance(self.image, imgmodel.LocalImage): self.handle.add_drive_opts(self.image.path, format=self.image.format) elif isinstance(self.image, imgmodel.RBDImage): self.handle.add_drive_opts("%s/%s" % (self.image.pool, self.image.name), protocol="rbd", format=imgmodel.FORMAT_RAW, server=self.image.servers, username=self.image.user, secret=self.image.password) else: raise exception.UnsupportedImageModel( self.image.__class__.__name__) self.handle.launch() if mount: self.setup_os() self.handle.aug_init("/", 0) self.mount = True except RuntimeError as e: # explicitly teardown instead of implicit close() # to prevent orphaned VMs in cases when an implicit # close() is not enough self.teardown() raise exception.NovaException( _("Error mounting %(image)s with libguestfs (%(e)s)") % {'image': self.image, 'e': e}) except Exception: # explicitly teardown instead of implicit close() # to prevent orphaned VMs in cases when an implicit # close() is not enough self.teardown() raise
def setup(self, mount=True): LOG.debug("Setting up appliance for %(image)s", {'image': self.image}) try: self.handle = tpool.Proxy( guestfs.GuestFS(python_return_dict=False, close_on_exit=False)) except TypeError as e: if ('close_on_exit' in six.text_type(e) or 'python_return_dict' in six.text_type(e)): # NOTE(russellb) In case we're not using a version of # libguestfs new enough to support parameters close_on_exit # and python_return_dict which were added in libguestfs 1.20. self.handle = tpool.Proxy(guestfs.GuestFS()) else: raise if CONF.guestfs.debug: self.configure_debug() try: if forceTCG: self.handle.set_backend_settings("force_tcg") except AttributeError as ex: # set_backend_settings method doesn't exist in older # libguestfs versions, so nothing we can do but ignore LOG.warning("Unable to force TCG mode, " "libguestfs too old? %s", ex) pass try: if isinstance(self.image, imgmodel.LocalImage): self.handle.add_drive_opts(self.image.path, format=self.image.format) elif isinstance(self.image, imgmodel.RBDImage): self.handle.add_drive_opts("%s/%s" % (self.image.pool, self.image.name), protocol="rbd", format=imgmodel.FORMAT_RAW, server=self.image.servers, username=self.image.user, secret=self.image.password) else: raise exception.UnsupportedImageModel( self.image.__class__.__name__) self.handle.launch() if mount: self.setup_os() self.handle.aug_init("/", 0) self.mount = True except RuntimeError as e: # explicitly teardown instead of implicit close() # to prevent orphaned VMs in cases when an implicit # close() is not enough self.teardown() raise exception.NovaException( _("Error mounting %(image)s with libguestfs (%(e)s)") % { 'image': self.image, 'e': e }) except Exception: # explicitly teardown instead of implicit close() # to prevent orphaned VMs in cases when an implicit # close() is not enough self.teardown() raise