def inject_data(image, key=None, net=None, metadata=None, admin_password=None, files=None, partition=None, mandatory=()): """Inject the specified items into a disk image. :param image: instance of nova.virt.image.model.Image :param key: the SSH public key to inject :param net: the network configuration to inject :param metadata: the user metadata to inject :param admin_password: the root password to set :param files: the files to copy into the image :param partition: the partition number to access :param mandatory: the list of parameters which must not fail to inject If an item name is not specified in the MANDATORY iterable, then a warning is logged on failure to inject that item, rather than raising an exception. it will mount the image as a fully partitioned disk and attempt to inject into the specified partition number. If PARTITION is not specified the image is mounted as a single partition. Returns True if all requested operations completed without issue. Raises an exception if a mandatory item can't be injected. """ items = {'image': image, 'key': key, 'net': net, 'metadata': metadata, 'files': files, 'partition': partition} LOG.debug("Inject data image=%(image)s key=%(key)s net=%(net)s " "metadata=%(metadata)s admin_password=<SANITIZED> " "files=%(files)s partition=%(partition)s", items) try: fs = vfs.VFS.instance_for_image(image, partition) fs.setup() except Exception as e: # If a mandatory item is passed to this function, # then reraise the exception to indicate the error. for inject in mandatory: inject_val = items[inject] if inject_val: raise LOG.warning('Ignoring error injecting data into image %(image)s ' '(%(e)s)', {'image': image, 'e': e}) return False try: return inject_data_into_fs(fs, key, net, metadata, admin_password, files, mandatory) finally: fs.teardown()
def is_image_extendable(image): """Check whether we can extend the image.""" LOG.debug('Checking if we can extend filesystem inside %(image)s.', {'image': image}) # For anything except a local raw file we must # go via the VFS layer if (not isinstance(image, imgmodel.LocalImage) or image.format != imgmodel.FORMAT_RAW): fs = None try: fs = vfs.VFS.instance_for_image(image, None) fs.setup(mount=False) if fs.get_image_fs() in SUPPORTED_FS_TO_EXTEND: return True except exception.NovaException as e: # FIXME(sahid): At this step we probably want to break the # process if something wrong happens however our CI # provides a bad configuration for libguestfs reported in # the bug lp#1413142. When resolved we should remove this # except to let the error to be propagated. LOG.warning( 'Unable to mount image %(image)s with ' 'error %(error)s. Cannot resize.', { 'image': image, 'error': e }) finally: if fs is not None: fs.teardown() return False else: # For raw, we can directly inspect the file system try: utils.execute('e2label', image.path) except processutils.ProcessExecutionError as e: LOG.debug( 'Unable to determine label for image %(image)s with ' 'error %(error)s. Cannot resize.', { 'image': image, 'error': e }) return False return True
def is_image_extendable(image): """Check whether we can extend the image.""" LOG.debug('Checking if we can extend filesystem inside %(image)s.', {'image': image}) # For anything except a local raw file we must # go via the VFS layer if (not isinstance(image, imgmodel.LocalImage) or image.format != imgmodel.FORMAT_RAW): fs = None try: fs = vfs.VFS.instance_for_image(image, None) fs.setup(mount=False) if fs.get_image_fs() in SUPPORTED_FS_TO_EXTEND: return True except exception.NovaException as e: # FIXME(sahid): At this step we probably want to break the # process if something wrong happens however our CI # provides a bad configuration for libguestfs reported in # the bug lp#1413142. When resolved we should remove this # except to let the error to be propagated. LOG.warning('Unable to mount image %(image)s with ' 'error %(error)s. Cannot resize.', {'image': image, 'error': e}) finally: if fs is not None: fs.teardown() return False else: # For raw, we can directly inspect the file system try: processutils.execute('e2label', image.path) except processutils.ProcessExecutionError as e: LOG.debug('Unable to determine label for image %(image)s with ' 'error %(error)s. Cannot resize.', {'image': image, 'error': e}) return False return True