def qemu_img_info(path, format=None): """Return an object containing the parsed output from qemu-img info.""" # TODO(mikal): this code should not be referring to a libvirt specific # flag. # NOTE(sirp): The config option import must go here to avoid an import # cycle CONF.import_opt('images_type', 'nova.virt.libvirt.imagebackend', group='libvirt') if not os.path.exists(path) and CONF.libvirt.images_type != 'rbd': raise exception.DiskNotFound(location=path) try: cmd = ('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) if format is not None: cmd = cmd + ('-f', format) out, err = utils.execute(*cmd) except processutils.ProcessExecutionError as exp: msg = (_("qemu-img failed to execute on %(path)s : %(exp)s") % { 'path': path, 'exp': exp }) raise exception.InvalidDiskInfo(reason=msg) if not out: msg = (_("Failed to run qemu-img info on %(path)s : %(error)s") % { 'path': path, 'error': err }) raise exception.InvalidDiskInfo(reason=msg) return imageutils.QemuImgInfo(out)
def _check_and_update_bdm(self, slot_map, vm_gen, bdm): disk_bus = bdm.get('disk_bus') if not disk_bus: bdm['disk_bus'] = self._DEFAULT_BUS elif disk_bus not in self._VALID_BUS[vm_gen]: msg = _("Hyper-V does not support bus type %(disk_bus)s " "for generation %(vm_gen)s instances.") % { 'disk_bus': disk_bus, 'vm_gen': vm_gen } raise exception.InvalidDiskInfo(reason=msg) device_type = bdm.get('device_type') if not device_type: bdm['device_type'] = 'disk' elif device_type != 'disk': msg = _("Hyper-V does not support disk type %s for ephemerals " "or volumes.") % device_type raise exception.InvalidDiskInfo(reason=msg) (bdm['drive_addr'], bdm['ctrl_disk_addr']) = self._get_available_controller_slot( bdm['disk_bus'], slot_map) # make sure that boot_index is set. bdm['boot_index'] = bdm.get('boot_index')
def qemu_img_info(path, format=None): """Return an object containing the parsed output from qemu-img info.""" # TODO(mikal): this code should not be referring to a libvirt specific # flag. if not os.path.exists(path) and CONF.libvirt.images_type != 'rbd': raise exception.DiskNotFound(location=path) try: # The following check is about ploop images that reside within # directories and always have DiskDescriptor.xml file beside them if (os.path.isdir(path) and os.path.exists(os.path.join(path, "DiskDescriptor.xml"))): path = os.path.join(path, "root.hds") cmd = ('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) if format is not None: cmd = cmd + ('-f', format) out, err = utils.execute(*cmd, prlimit=QEMU_IMG_LIMITS) except processutils.ProcessExecutionError as exp: # this means we hit prlimits, make the exception more specific if exp.exit_code == -9: msg = (_("qemu-img aborted by prlimits when inspecting " "%(path)s : %(exp)s") % {'path': path, 'exp': exp}) else: msg = (_("qemu-img failed to execute on %(path)s : %(exp)s") % {'path': path, 'exp': exp}) raise exception.InvalidDiskInfo(reason=msg) if not out: msg = (_("Failed to run qemu-img info on %(path)s : %(error)s") % {'path': path, 'error': err}) raise exception.InvalidDiskInfo(reason=msg) return imageutils.QemuImgInfo(out)
def qemu_img_info(path, format=None): """Return an object containing the parsed output from qemu-img info.""" # TODO(mikal): this code should not be referring to a libvirt specific # flag. # NOTE(sirp): The config option import must go here to avoid an import # cycle CONF.import_opt('images_type', 'nova.virt.libvirt.imagebackend', group='libvirt') if not os.path.exists(path) and CONF.libvirt.images_type != 'rbd': msg = (_("Path does not exist %(path)s") % {'path': path}) raise exception.InvalidDiskInfo(reason=msg) cmd = ('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) if format is not None: cmd = cmd + ('-f', format) # PF9: run as root to capture situations where the existing VM disks are not readable by # nova/pf9 users out, err = utils.execute(*cmd, run_as_root=True) if not out: msg = (_("Failed to run qemu-img info on %(path)s : %(error)s") % { 'path': path, 'error': err }) raise exception.InvalidDiskInfo(reason=msg) return imageutils.QemuImgInfo(out)
def qemu_img_info(path, format=None): """Return an object containing the parsed output from qemu-img info.""" # TODO(mikal): this code should not be referring to a libvirt specific # flag. if not os.path.exists(path) and CONF.libvirt.images_type != 'rbd': raise exception.DiskNotFound(location=path) try: # The following check is about ploop images that reside within # directories and always have DiskDescriptor.xml file beside them if (os.path.isdir(path) and os.path.exists(os.path.join(path, "DiskDescriptor.xml"))): path = os.path.join(path, "root.hds") cmd = ('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) if format is not None: cmd = cmd + ('-f', format) # Check to see if the qemu version is >= 2.10 because if so, we need # to add the --force-share flag. if QEMU_VERSION and operator.ge(QEMU_VERSION, QEMU_VERSION_REQ_SHARED): cmd = cmd + ('--force-share', ) out, err = processutils.execute(*cmd, prlimit=QEMU_IMG_LIMITS) except processutils.ProcessExecutionError as exp: if exp.exit_code == -9: # this means we hit prlimits, make the exception more specific msg = (_("qemu-img aborted by prlimits when inspecting " "%(path)s : %(exp)s") % { 'path': path, 'exp': exp }) elif exp.exit_code == 1 and 'No such file or directory' in exp.stderr: # The os.path.exists check above can race so this is a simple # best effort at catching that type of failure and raising a more # specific error. raise exception.DiskNotFound(location=path) else: msg = (_("qemu-img failed to execute on %(path)s : %(exp)s") % { 'path': path, 'exp': exp }) raise exception.InvalidDiskInfo(reason=msg) if not out: msg = (_("Failed to run qemu-img info on %(path)s : %(error)s") % { 'path': path, 'error': err }) raise exception.InvalidDiskInfo(reason=msg) return imageutils.QemuImgInfo(out)
def unprivileged_qemu_img_info(path, format=None, output_format=None): """Return an object containing the parsed output from qemu-img info.""" try: # The following check is about ploop images that reside within # directories and always have DiskDescriptor.xml file beside them if (os.path.isdir(path) and os.path.exists(os.path.join(path, "DiskDescriptor.xml"))): path = os.path.join(path, "root.hds") cmd = ( 'env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path, '--force-share', ) if format is not None: cmd = cmd + ('-f', format) if output_format is not None: cmd = cmd + ("--output=%s" % (output_format), ) out, err = processutils.execute(*cmd, prlimit=QEMU_IMG_LIMITS) except processutils.ProcessExecutionError as exp: if exp.exit_code == -9: # this means we hit prlimits, make the exception more specific msg = (_("qemu-img aborted by prlimits when inspecting " "%(path)s : %(exp)s") % { 'path': path, 'exp': exp }) elif exp.exit_code == 1 and 'No such file or directory' in exp.stderr: # The os.path.exists check above can race so this is a simple # best effort at catching that type of failure and raising a more # specific error. raise exception.DiskNotFound(location=path) else: msg = (_("qemu-img failed to execute on %(path)s : %(exp)s") % { 'path': path, 'exp': exp }) raise exception.InvalidDiskInfo(reason=msg) if not out: msg = (_("Failed to run qemu-img info on %(path)s : %(error)s") % { 'path': path, 'error': err }) raise exception.InvalidDiskInfo(reason=msg) return out
def qemu_img_info(path): """Return an object containing the parsed output from qemu-img info.""" # flag. if not os.path.exists(path): msg = (_("Path does not exist %(path)s") % {'path': path}) raise exception.InvalidDiskInfo(reason=msg) out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) if not out: msg = (_("Failed to run qemu-img info on %(path)s : %(error)s") % {'path': path, 'error': err}) raise exception.InvalidDiskInfo(reason=msg) return imageutils.QemuImgInfo(out)
def qemu_img_info(path): """Return an object containing the parsed output from qemu-img info.""" # TODO(mikal): this code should not be referring to a libvirt specific # flag. if not os.path.exists(path) and CONF.libvirt.images_type != 'rbd': msg = (_("Path does not exist %(path)s") % {'path': path}) raise exception.InvalidDiskInfo(reason=msg) out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) if not out: msg = (_("Failed to run qemu-img info on %(path)s : %(error)s") % {'path': path, 'error': err}) raise exception.InvalidDiskInfo(reason=msg) return imageutils.QemuImgInfo(out)
def create_hd(cls, filename, size=None, disk_format=constants.DISK_FORMAT_VDI, variant=constants.VARIANT_STANDARD, parent=None): """Creates a new virtual hard disk image. :param filename: the file name for the hard disk image :param size: the image capacity, in MiB units :param disk_format: file format for the output file (default: DISK_FORMAT_VDI) :param variant: file format variant for the output file (default: VARIANT_STANDARD) :param parent: :return: UUID for the disk image created """ if disk_format not in constants.ALL_DISK_FORMATS: raise exception.InvalidDiskFormat(disk_format=disk_format) if variant not in constants.ALL_VARIANTS: raise vbox_exc.VBoxValueNotAllowed( argument="variant", value=variant, method=cls.CREATE_HD, allowed_values=constants.ALL_VARIANTS) if size and size < 1: raise exception.InvalidDiskInfo( reason="Disk size should be bigger than 0.") command = [cls.CREATE_HD, "--filename", filename, "--format", disk_format, "--variant", variant] if size: command.extend(["--size", size]) if parent: command.extend(["--diffparent", parent]) output, error = cls._execute(*command) if error and constants.DONE not in error: if constants.VBOX_E_FILE_ERROR in error: raise exception.DestinationDiskExists(path=filename) raise vbox_exc.VBoxManageError(method=cls.CREATE_HD, reason=error) # The ouput should look like: # Disk image created. UUID: 6917a94b-ecb0-4996-8ab8-5e4ef8f9539a for line in output.splitlines(): if "UUID:" not in line: continue hd_uuid = line.split("UUID:")[1].strip() break else: # TODO(alexandrucoman): Fail to get UUID (Something went wrong) return return hd_uuid
def _dict_from_line(line): if not line: return {} try: return jsonutils.loads(line) except (TypeError, ValueError) as e: msg = (_("Could not load line %(line)s, got error " "%(error)s") % {'line': line, 'error': e}) raise exception.InvalidDiskInfo(reason=msg)
def qemu_img_info(path, format=None): """Return an object containing the parsed output from qemu-img info.""" # TODO(mikal): this code should not be referring to a libvirt specific # flag. # NOTE(sirp): The config option import must go here to avoid an import # cycle CONF.import_opt('images_type', 'nova.virt.libvirt.imagebackend', group='libvirt') if not os.path.exists(path) and CONF.libvirt.images_type != 'rbd': msg = (_("Path does not exist %(path)s") % {'path': path}) raise exception.InvalidDiskInfo(reason=msg) cmd = ('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) if format is not None: cmd = cmd + ('-f', format) try: if QEMU_IMG_LIMITS is not None: out, err = utils.execute(*cmd, prlimit=QEMU_IMG_LIMITS) else: out, err = utils.execute(*cmd) except processutils.ProcessExecutionError as exp: if exp.exit_code == -9: # this means we hit prlimits, make the exception more specific msg = (_("qemu-img aborted by prlimits when inspecting " "%(path)s : %(exp)s") % { 'path': path, 'exp': exp }) elif exp.exit_code == 1 and 'No such file or directory' in exp.stderr: # The os.path.exists check above can race so this is a simple # best effort at catching that type of failure and raising a more # specific error. raise exception.DiskNotFound(location=path) if not out: msg = (_("Failed to run qemu-img info on %(path)s : %(error)s") % { 'path': path, 'error': err }) raise exception.InvalidDiskInfo(reason=msg) return imageutils.QemuImgInfo(out)
def qemu_img_info(path): """Return an object containing the parsed output from qemu-img info.""" # TODO(mikal): this code should not be referring to a libvirt specific # flag. # NOTE(sirp): The config option import must go here to avoid an import # cycle CONF.import_opt('images_type', 'nova.virt.libvirt.imagebackend', group='libvirt') if not os.path.exists(path) and CONF.libvirt.images_type != 'rbd': msg = (_("Path does not exist %(path)s") % {'path': path}) raise exception.InvalidDiskInfo(reason=msg) out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) if not out: msg = (_("Failed to run qemu-img info on %(path)s : %(error)s") % {'path': path, 'error': err}) raise exception.InvalidDiskInfo(reason=msg) return imageutils.QemuImgInfo(out)
def write_to_disk_info_file(): # Use os.open to create it without group or world write permission. fd = os.open(self.disk_info_path, os.O_RDWR | os.O_CREAT, 0o644) with os.fdopen(fd, "r+") as disk_info_file: line = disk_info_file.read().rstrip() dct = _dict_from_line(line) if self.path in dct: msg = _("Attempted overwrite of an existing value.") raise exception.InvalidDiskInfo(reason=msg) dct.update({self.path: driver_format}) disk_info_file.seek(0) disk_info_file.truncate() disk_info_file.write('%s\n' % jsonutils.dumps(dct))
def qemu_img_info(path, format=None): """Return an object containing the parsed output from qemu-img info.""" # TODO(mikal): this code should not be referring to a libvirt specific # flag. if not os.path.exists(path) and CONF.libvirt.images_type != 'rbd': raise exception.DiskNotFound(location=path) try: cmd = ('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) if format is not None: cmd = cmd + ('-f', format) out, err = utils.execute(*cmd, prlimit=QEMU_IMG_LIMITS) except processutils.ProcessExecutionError as exp: msg = (_("qemu-img failed to execute on %(path)s : %(exp)s") % {'path': path, 'exp': exp}) raise exception.InvalidDiskInfo(reason=msg) if not out: msg = (_("Failed to run qemu-img info on %(path)s : %(error)s") % {'path': path, 'error': err}) raise exception.InvalidDiskInfo(reason=msg) return imageutils.QemuImgInfo(out)