def image_to_raw(image_href, path, path_tmp): with fileutils.remove_path_on_error(path_tmp): data = disk_utils.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") % {'fmt': fmt, 'backing_file': backing_file}) if fmt != "raw": staged = "%s.converted" % path LOG.debug("%(image)s was %(format)s, converting to raw", {'image': image_href, 'format': fmt}) with fileutils.remove_path_on_error(staged): disk_utils.convert_image(path_tmp, staged, 'raw') os.unlink(path_tmp) data = disk_utils.qemu_img_info(staged) if data.file_format != "raw": raise exception.ImageConvertFailed( 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)
def image_to_raw(image_href, path, path_tmp): with fileutils.remove_path_on_error(path_tmp): data = disk_utils.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") % {'fmt': fmt, 'backing_file': backing_file}) if fmt != "raw": staged = "%s.converted" % path LOG.debug("%(image)s was %(format)s, converting to raw" % {'image': image_href, 'format': fmt}) with fileutils.remove_path_on_error(staged): disk_utils.convert_image(path_tmp, staged, 'raw') os.unlink(path_tmp) data = disk_utils.qemu_img_info(staged) if data.file_format != "raw": raise exception.ImageConvertFailed( 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)
def test_qemu_img_info_path_exists(self, path_exists_mock, qemu_img_info_mock, execute_mock): disk_utils.qemu_img_info('img') path_exists_mock.assert_called_once_with('img') execute_mock.assert_called_once_with('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', 'img') qemu_img_info_mock.assert_called_once_with('out')
def image_to_raw(image_href, path, path_tmp): with fileutils.remove_path_on_error(path_tmp): fmt = get_source_format(image_href, path_tmp) if fmt != "raw": staged = "%s.converted" % path utils.is_memory_insufficent(raise_if_fail=True) LOG.debug("%(image)s was %(format)s, converting to raw", { 'image': image_href, 'format': fmt }) with fileutils.remove_path_on_error(staged): disk_utils.convert_image(path_tmp, staged, 'raw') os.unlink(path_tmp) data = disk_utils.qemu_img_info(staged) if data.file_format != "raw": raise exception.ImageConvertFailed( 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)
def converted_size(path): """Get size of converted raw image. The size of image converted to raw format can be growing up to the virtual size of the image. :param path: path to the image file. :returns: virtual size of the image or 0 if conversion not needed. """ data = disk_utils.qemu_img_info(path) return data.virtual_size
def get_source_format(image_href, path): data = disk_utils.qemu_img_info(path) 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") % {'fmt': fmt, 'backing_file': backing_file}) return fmt
def converted_size(path, estimate=False): """Get size of converted raw image. The size of image converted to raw format can be growing up to the virtual size of the image. :param path: path to the image file. :param estimate: Whether to estimate the size by scaling the original size :returns: For `estimate=False`, return the size of the raw image file. For `estimate=True`, return the size of the original image scaled by the configuration value `raw_image_growth_factor`. """ data = disk_utils.qemu_img_info(path) if not estimate: return data.virtual_size growth_factor = CONF.raw_image_growth_factor return int(min(data.disk_size * growth_factor, data.virtual_size))
def test_qemu_img_info_path_doesnt_exist(self, path_exists_mock, qemu_img_info_mock): disk_utils.qemu_img_info('noimg') path_exists_mock.assert_called_once_with('noimg') qemu_img_info_mock.assert_called_once_with()