Exemple #1
0
def fetch_to_raw(context, image_href, path, user_id, project_id):
    path_tmp = "%s.part" % path
    fetch(context, image_href, path_tmp, user_id, project_id)

    with utils.remove_path_on_error(path_tmp):
        data = qemu_img_info(path_tmp)

        fmt = data.file_format
        if fmt is None:
            raise exception.ImageUnacceptable(
                reason=_("'qemu-img info' parsing failed."),
                image_id=image_href)

        backing_file = data.backing_file
        if backing_file is not None:
            raise exception.ImageUnacceptable(image_id=image_href,
                reason=_("fmt=%(fmt)s backed by: %(backing_file)s") % locals())

        if fmt != "raw" and CONF.force_raw_images:
            staged = "%s.converted" % path
            LOG.debug("%s was %s, converting to raw" % (image_href, fmt))
            with utils.remove_path_on_error(staged):
                convert_image(path_tmp, staged, 'raw')

                data = qemu_img_info(staged)
                if data.file_format != "raw":
                    raise exception.ImageUnacceptable(image_id=image_href,
                        reason=_("Converted to raw, but format is now %s") %
                        data.file_format)

                os.rename(staged, path)

        else:
            os.rename(path_tmp, path)
Exemple #2
0
def fetch_to_raw(context, image_href, path, user_id, project_id):
    path_tmp = "%s.part" % path
    fetch(context, image_href, path_tmp, user_id, project_id)

    with utils.remove_path_on_error(path_tmp):
        data = qemu_img_info(path_tmp)

        fmt = data.get('file format')
        if fmt is None:
            raise exception.ImageUnacceptable(
                reason=_("'qemu-img info' parsing failed."),
                image_id=image_href)

        backing_file = data.get('backing file')
        if backing_file is not None:
            raise exception.ImageUnacceptable(image_id=image_href,
                reason=_("fmt=%(fmt)s backed by: %(backing_file)s") % locals())

        if fmt != "raw" and FLAGS.force_raw_images:
            staged = "%s.converted" % path
            LOG.debug("%s was %s, converting to raw" % (image_href, fmt))
            with utils.remove_path_on_error(staged):
                convert_image(path_tmp, staged, 'raw')

                data = qemu_img_info(staged)
                if data.get('file format') != "raw":
                    raise exception.ImageUnacceptable(image_id=image_href,
                        reason=_("Converted to raw, but format is now %s") %
                        data.get('file format'))

                os.rename(staged, path)

        else:
            os.rename(path_tmp, path)
Exemple #3
0
def fetch_to_raw(context, image_href, path, user_id, project_id):
    path_tmp = "%s.part" % path
    metadata = fetch(context, image_href, path_tmp, user_id, project_id)

    def _qemu_img_info(path):

        out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img',
                                 'info', path)

        # output of qemu-img is 'field: value'
        # the fields of interest are 'file format' and 'backing file'
        data = {}
        for line in out.splitlines():
            (field, val) = line.split(':', 1)
            if val[0] == " ":
                val = val[1:]
            data[field] = val

        return (data)

    with utils.remove_path_on_error(path_tmp):
        data = _qemu_img_info(path_tmp)

        fmt = data.get("file format")
        if fmt is None:
            raise exception.ImageUnacceptable(
                reason=_("'qemu-img info' parsing failed."),
                image_id=image_href)

        if "backing file" in data:
            backing_file = data['backing file']
            raise exception.ImageUnacceptable(
                image_id=image_href,
                reason=_("fmt=%(fmt)s backed by: %(backing_file)s") % locals())

        if fmt != "raw" and FLAGS.force_raw_images:
            staged = "%s.converted" % path
            LOG.debug("%s was %s, converting to raw" % (image_href, fmt))
            with utils.remove_path_on_error(staged):
                out, err = utils.execute('qemu-img', 'convert', '-O', 'raw',
                                         path_tmp, staged)

                data = _qemu_img_info(staged)
                if data.get('file format', None) != "raw":
                    raise exception.ImageUnacceptable(
                        image_id=image_href,
                        reason=_("Converted to raw, but format is now %s") %
                        data.get('file format', None))

                os.rename(staged, path)

        else:
            os.rename(path_tmp, path)

    return metadata
Exemple #4
0
def fetch_to_raw(context, image_href, path, user_id, project_id):
    path_tmp = "%s.part" % path
    metadata = fetch(context, image_href, path_tmp, user_id, project_id)

    def _qemu_img_info(path):

        out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C',
            'qemu-img', 'info', path)

        # output of qemu-img is 'field: value'
        # the fields of interest are 'file format' and 'backing file'
        data = {}
        for line in out.splitlines():
            (field, val) = line.split(':', 1)
            if val[0] == " ":
                val = val[1:]
            data[field] = val

        return(data)

    with utils.remove_path_on_error(path_tmp):
        data = _qemu_img_info(path_tmp)

        fmt = data.get("file format")
        if fmt is None:
            raise exception.ImageUnacceptable(
                reason=_("'qemu-img info' parsing failed."),
                image_id=image_href)

        if "backing file" in data:
            backing_file = data['backing file']
            raise exception.ImageUnacceptable(image_id=image_href,
                reason=_("fmt=%(fmt)s backed by: %(backing_file)s") % locals())

        if fmt != "raw" and FLAGS.force_raw_images:
            staged = "%s.converted" % path
            LOG.debug("%s was %s, converting to raw" % (image_href, fmt))
            with utils.remove_path_on_error(staged):
                out, err = utils.execute('qemu-img', 'convert', '-O', 'raw',
                                         path_tmp, staged)

                data = _qemu_img_info(staged)
                if data.get('file format', None) != "raw":
                    raise exception.ImageUnacceptable(image_id=image_href,
                        reason=_("Converted to raw, but format is now %s") %
                        data.get('file format', None))

                os.rename(staged, path)

        else:
            os.rename(path_tmp, path)

    return metadata
Exemple #5
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @utils.synchronized(base, external=True, lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            # TODO(pbrady): Consider copying the cow image here
            # with preallocation=metadata set for performance reasons.
            # This would be keyed on a 'preallocate_images' setting.
            libvirt_utils.create_cow_image(base, target)
            if size:
                disk.extend(target, size)

        # Download the unmodified base image unless we already have a copy.
        if not os.path.exists(base):
            prepare_template(target=base, *args, **kwargs)

        legacy_backing_size = None
        legacy_base = base

        # Determine whether an existing qcow2 disk uses a legacy backing by
        # actually looking at the image itself and parsing the output of the
        # backing file it expects to be using.
        if os.path.exists(self.path):
            backing_path = libvirt_utils.get_disk_backing_file(self.path)
            backing_file = os.path.basename(backing_path)
            backing_parts = backing_file.rpartition('_')
            if backing_file != backing_parts[-1] and \
                    backing_parts[-1].isdigit():
                legacy_backing_size = int(backing_parts[-1])
                legacy_base += '_%d' % legacy_backing_size
                legacy_backing_size *= 1024 * 1024 * 1024

        # Create the legacy backing file if necessary.
        if legacy_backing_size:
            if not os.path.exists(legacy_base):
                with utils.remove_path_on_error(legacy_base):
                    libvirt_utils.copy_image(base, legacy_base)
                    disk.extend(legacy_base, legacy_backing_size)

        # NOTE(cfb): Having a flavor that sets the root size to 0 and having
        #            nova effectively ignore that size and use the size of the
        #            image is considered a feature at this time, not a bug.
        if size and size < disk.get_disk_size(base):
            LOG.error('%s virtual size larger than flavor root disk size %s' %
                      (base, size))
            raise exception.InstanceTypeDiskTooSmall()
        if not os.path.exists(self.path):
            with utils.remove_path_on_error(self.path):
                copy_qcow2_image(base, self.path, size)
Exemple #6
0
 def copy_qcow2_image(base, target, size):
     qcow2_base = base
     if size:
         size_gb = size / (1024 * 1024 * 1024)
         qcow2_base += '_%d' % size_gb
         if not os.path.exists(qcow2_base):
             with utils.remove_path_on_error(qcow2_base):
                 libvirt_utils.copy_image(base, qcow2_base)
                 disk.extend(qcow2_base, size)
     libvirt_utils.create_cow_image(qcow2_base, target)
Exemple #7
0
 def copy_qcow2_image(base, target, size):
     qcow2_base = base
     if size:
         size_gb = size / (1024 * 1024 * 1024)
         qcow2_base += '_%d' % size_gb
         if not os.path.exists(qcow2_base):
             with utils.remove_path_on_error(qcow2_base):
                 libvirt_utils.copy_image(base, qcow2_base)
                 disk.extend(qcow2_base, size)
     libvirt_utils.create_cow_image(qcow2_base, target)
Exemple #8
0
def fetch(context, image_href, path, _user_id, _project_id):
    # TODO(vish): Improve context handling and add owner and auth data
    #             when it is added to glance.  Right now there is no
    #             auth checking in glance, so we assume that access was
    #             checked before we got here.
    (image_service, image_id) = glance.get_remote_image_service(context,
                                                                image_href)
    with utils.remove_path_on_error(path):
        with open(path, "wb") as image_file:
            image_service.download(context, image_id, image_file)
Exemple #9
0
def fetch(context, image_href, path, _user_id, _project_id):
    # TODO(vish): Improve context handling and add owner and auth data
    #             when it is added to glance.  Right now there is no
    #             auth checking in glance, so we assume that access was
    #             checked before we got here.
    (image_service, image_id) = glance.get_remote_image_service(context,
                                                                image_href)
    with utils.remove_path_on_error(path):
        with open(path, "wb") as image_file:
            image_service.download(context, image_id, image_file)
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @lockutils.synchronized(base, 'nova-', external=True,
                                lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            libvirt_utils.create_cow_image(base, target)
            if size:
                disk.extend(target, size)

        prepare_template(target=base, *args, **kwargs)
        with utils.remove_path_on_error(self.path):
            copy_qcow2_image(base, self.path, size)
Exemple #11
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @lockutils.synchronized(base, 'nova-', external=True,
                                lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            libvirt_utils.create_cow_image(base, target)
            if size:
                disk.extend(target, size)

        if not os.path.exists(base):
            prepare_template(target=base, *args, **kwargs)
        if not os.path.exists(self.path):
            with utils.remove_path_on_error(self.path):
                copy_qcow2_image(base, self.path, size)
Exemple #12
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @utils.synchronized(base, external=True, lock_path=self.lock_path)
        def copy_raw_image(base, target, size):
            libvirt_utils.copy_image(base, target)
            if size:
                disk.extend(target, size)

        generating = 'image_id' not in kwargs
        if generating:
            #Generating image in place
            prepare_template(target=self.path, *args, **kwargs)
        else:
            prepare_template(target=base, *args, **kwargs)
            with utils.remove_path_on_error(self.path):
                copy_raw_image(base, self.path, size)
Exemple #13
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @utils.synchronized(base, external=True, lock_path=self.lock_path)
        def copy_raw_image(base, target, size):
            libvirt_utils.copy_image(base, target)
            if size:
                disk.extend(target, size)

        generating = 'image_id' not in kwargs
        if generating:
            #Generating image in place
            prepare_template(target=self.path, *args, **kwargs)
        else:
            prepare_template(target=base, *args, **kwargs)
            with utils.remove_path_on_error(self.path):
                copy_raw_image(base, self.path, size)
Exemple #14
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @utils.synchronized(base, external=True, lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            qcow2_base = base
            if size:
                size_gb = size / (1024 * 1024 * 1024)
                qcow2_base += '_%d' % size_gb
                if not os.path.exists(qcow2_base):
                    with utils.remove_path_on_error(qcow2_base):
                        libvirt_utils.copy_image(base, qcow2_base)
                        disk.extend(qcow2_base, size)
            libvirt_utils.create_cow_image(qcow2_base, target)

        prepare_template(target=base, *args, **kwargs)
        with utils.remove_path_on_error(self.path):
            copy_qcow2_image(base, self.path, size)
Exemple #15
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @utils.synchronized(base, external=True, lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            qcow2_base = base
            if size:
                size_gb = size / (1024 * 1024 * 1024)
                qcow2_base += '_%d' % size_gb
                if not os.path.exists(qcow2_base):
                    with utils.remove_path_on_error(qcow2_base):
                        libvirt_utils.copy_image(base, qcow2_base)
                        disk.extend(qcow2_base, size)
            libvirt_utils.create_cow_image(qcow2_base, target)

        prepare_template(target=base, *args, **kwargs)
        with utils.remove_path_on_error(self.path):
            copy_qcow2_image(base, self.path, size)
Exemple #16
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @lockutils.synchronized(base, 'nova-', external=True,
                                lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            # TODO(pbrady): Consider copying the cow image here
            # with preallocation=metadata set for performance reasons.
            # This would be keyed on a 'preallocate_images' setting.
            libvirt_utils.create_cow_image(base, target)
            if size:
                disk.extend(target, size)

        if not os.path.exists(base):
            prepare_template(target=base, *args, **kwargs)
        if not os.path.exists(self.path):
            with utils.remove_path_on_error(self.path):
                copy_qcow2_image(base, self.path, size)
Exemple #17
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @lockutils.synchronized(base,
                                'nova-',
                                external=True,
                                lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            # TODO(pbrady): Consider copying the cow image here
            # with preallocation=metadata set for performance reasons.
            # This would be keyed on a 'preallocate_images' setting.
            libvirt_utils.create_cow_image(base, target)
            if size:
                disk.extend(target, size)

        if not os.path.exists(base):
            prepare_template(target=base, *args, **kwargs)
        if not os.path.exists(self.path):
            with utils.remove_path_on_error(self.path):
                copy_qcow2_image(base, self.path, size)
Exemple #18
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @utils.synchronized(base, external=True, lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            # TODO(pbrady): Consider copying the cow image here
            # with preallocation=metadata set for performance reasons.
            # This would be keyed on a 'preallocate_images' setting.
            libvirt_utils.create_cow_image(base, target)
            if size:
                disk.extend(target, size)

        if not os.path.exists(base):
            prepare_template(target=base, *args, **kwargs)
        # NOTE(cfb): Having a flavor that sets the root size to 0 and having
        #            nova effectively ignore that size and use the size of the
        #            image is considered a feature at this time, not a bug.
        if size and size < disk.get_disk_size(base):
            LOG.error("%s virtual size larger than flavor root disk size %s" % (base, size))
            raise exception.InstanceTypeDiskTooSmall()
        if not os.path.exists(self.path):
            with utils.remove_path_on_error(self.path):
                copy_qcow2_image(base, self.path, size)
Exemple #19
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @utils.synchronized(base, external=True, lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            # TODO(pbrady): Consider copying the cow image here
            # with preallocation=metadata set for performance reasons.
            # This would be keyed on a 'preallocate_images' setting.
            libvirt_utils.create_cow_image(base, target)
            if size:
                disk.extend(target, size)

        if not os.path.exists(base):
            prepare_template(target=base, *args, **kwargs)
        # NOTE(cfb): Having a flavor that sets the root size to 0 and having
        #            nova effectively ignore that size and use the size of the
        #            image is considered a feature at this time, not a bug.
        if size and size < disk.get_disk_size(base):
            LOG.error('%s virtual size larger than flavor root disk size %s' %
                      (base, size))
            raise exception.InstanceTypeDiskTooSmall()
        if not os.path.exists(self.path):
            with utils.remove_path_on_error(self.path):
                copy_qcow2_image(base, self.path, size)
Exemple #20
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        @utils.synchronized(base, external=True, lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            qcow2_base = base
            if size:
                size_gb = size / (1024 * 1024 * 1024)
                qcow2_base += '_%d' % size_gb
                if not os.path.exists(qcow2_base):
                    with utils.remove_path_on_error(qcow2_base):
                        libvirt_utils.copy_image(base, qcow2_base)
                        disk.extend(qcow2_base, size)
            libvirt_utils.create_cow_image(qcow2_base, target)

        prepare_template(target=base, *args, **kwargs)
        # NOTE(cfb): Having a flavor that sets the root size to 0 and having
        #            nova effectively ignore that size and use the size of the
        #            image is considered a feature at this time, not a bug.
        if size and size < disk.get_disk_size(base):
            LOG.error('%s virtual size larger than flavor root disk size %s' %
                      (base, size))
            raise exception.ImageTooLarge()
        with utils.remove_path_on_error(self.path):
            copy_qcow2_image(base, self.path, size)