def _fetch(context, image_href, path, force_raw=False): """Fetch image and convert to raw format if needed.""" path_tmp = "%s.part" % path images.fetch(context, image_href, path_tmp, force_raw=False) # Notes(yjiang5): If glance can provide the virtual size information, # then we can firstly clean cache and then invoke images.fetch(). if force_raw: if images.force_raw_will_convert(image_href, path_tmp): required_space = images.converted_size(path_tmp, estimate=False) directory = os.path.dirname(path_tmp) try: _clean_up_caches(directory, required_space) except exception.InsufficientDiskSpace: # try again with an estimated raw size instead of the full size required_space = images.converted_size(path_tmp, estimate=True) try: _clean_up_caches(directory, required_space) except exception.InsufficientDiskSpace: LOG.warning( 'Not enough space for estimated image size. ' 'Consider lowering ' '[DEFAULT]raw_image_growth_factor=%s', CONF.raw_image_growth_factor) raise images.image_to_raw(image_href, path, path_tmp) else: os.rename(path_tmp, path)
def test_converted_size(self, qemu_img_info_mock): info = self.FakeImgInfo() info.virtual_size = 1 qemu_img_info_mock.return_value = info size = images.converted_size('path') qemu_img_info_mock.assert_called_once_with('path') self.assertEqual(1, size)
def _fetch_to_raw(context, image_href, path, image_service=None): """Fetch image and convert to raw format if needed.""" path_tmp = "%s.part" % path images.fetch(context, image_href, path_tmp, image_service) required_space = images.converted_size(path_tmp) directory = os.path.dirname(path_tmp) _clean_up_caches(directory, required_space) images.image_to_raw(image_href, path, path_tmp)
def test_converted_size_estimate_default(self, qemu_img_info_mock): info = self.FakeImgInfo() info.disk_size = 2 info.virtual_size = 10 ** 10 qemu_img_info_mock.return_value = info size = images.converted_size('path', estimate=True) qemu_img_info_mock.assert_called_once_with('path') self.assertEqual(4, size)
def test_converted_size_estimate_raw_smaller(self, qemu_img_info_mock): CONF.set_override('raw_image_growth_factor', 3) info = self.FakeImgInfo() info.disk_size = 2 info.virtual_size = 5 qemu_img_info_mock.return_value = info size = images.converted_size('path', estimate=True) qemu_img_info_mock.assert_called_once_with('path') self.assertEqual(5, size)
def get_image_mb(image_path, virtual_size=True): """Get size of an image in Megabyte.""" mb = 1024 * 1024 if not virtual_size: image_byte = os.path.getsize(image_path) else: image_byte = images.converted_size(image_path) # round up size to MB image_mb = int((image_byte + mb - 1) / mb) return image_mb
def _fetch(context, image_href, path, force_raw=False): """Fetch image and convert to raw format if needed.""" path_tmp = "%s.part" % path images.fetch(context, image_href, path_tmp, force_raw=False) # Notes(yjiang5): If glance can provide the virtual size information, # then we can firstly clean cache and then invoke images.fetch(). if force_raw: required_space = images.converted_size(path_tmp) directory = os.path.dirname(path_tmp) _clean_up_caches(directory, required_space) images.image_to_raw(image_href, path, path_tmp) else: os.rename(path_tmp, path)