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, use_cow=True)
def _cache_image(fn, target, fname, cow=False, *args, **kwargs): """Wrapper for a method that creates an image that caches the image. This wrapper will save the image into a common store and create a copy for use by the hypervisor. The underlying method should specify a kwarg of target representing where the image will be saved. fname is used as the filename of the base image. The filename needs to be unique to a given image. If cow is True, it will make a CoW image instead of a copy. """ if not os.path.exists(target): base_dir = os.path.join(FLAGS.instances_path, "_base") if not os.path.exists(base_dir): libvirt_utils.ensure_tree(base_dir) base = os.path.join(base_dir, fname) @utils.synchronized(fname) def call_if_not_exists(base, fn, *args, **kwargs): if not os.path.exists(base): fn(target=base, *args, **kwargs) call_if_not_exists(base, fn, *args, **kwargs) if cow: libvirt_utils.create_cow_image(base, target) else: libvirt_utils.copy_image(base, target)
def _cache_image(fn, target, fname, cow=False, *args, **kwargs): """Wrapper for a method that creates an image that caches the image. This wrapper will save the image into a common store and create a copy for use by the hypervisor. The underlying method should specify a kwarg of target representing where the image will be saved. fname is used as the filename of the base image. The filename needs to be unique to a given image. If cow is True, it will make a CoW image instead of a copy. """ if not os.path.exists(target): base_dir = os.path.join(FLAGS.instances_path, '_base') if not os.path.exists(base_dir): libvirt_utils.ensure_tree(base_dir) base = os.path.join(base_dir, fname) @utils.synchronized(fname) def call_if_not_exists(base, fn, *args, **kwargs): if not os.path.exists(base): fn(target=base, *args, **kwargs) call_if_not_exists(base, fn, *args, **kwargs) if cow: libvirt_utils.create_cow_image(base, target) else: libvirt_utils.copy_image(base, target)
def test_create_cow_image(self, mock_execute, mock_exists): mock_execute.return_value = ('stdout', None) libvirt_utils.create_cow_image('/some/path', '/the/new/cow') expected_args = [(('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', '/some/path'), ), (('qemu-img', 'create', '-f', 'qcow2', '-o', 'backing_file=/some/path', '/the/new/cow'), )] self.assertEqual(expected_args, mock_execute.call_args_list)
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: image = imgmodel.LocalFileImage(target, imgmodel.FORMAT_QCOW2) disk.extend(image, size)
def test_create_cow_image(self, mock_execute, mock_exists): mock_execute.return_value = ("stdout", None) libvirt_utils.create_cow_image("/some/path", "/the/new/cow") expected_args = [ (("env", "LC_ALL=C", "LANG=C", "qemu-img", "info", "/some/path"),), (("qemu-img", "create", "-f", "qcow2", "-o", "backing_file=/some/path", "/the/new/cow"),), ] self.assertEqual(expected_args, mock_execute.call_args_list)
def test_create_cow_image(self, mock_execute, mock_exists): mock_execute.return_value = ('stdout', None) libvirt_utils.create_cow_image('/some/path', '/the/new/cow') expected_args = [(('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', '/some/path'),), (('qemu-img', 'create', '-f', 'qcow2', '-o', 'backing_file=/some/path', '/the/new/cow'),)] self.assertEqual(expected_args, mock_execute.call_args_list)
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)
def test_create_cow_image(self): self.mox.StubOutWithMock(os.path, "exists") self.mox.StubOutWithMock(utils, "execute") rval = ("stdout", None) os.path.exists("/some/path").AndReturn(True) utils.execute("env", "LC_ALL=C", "LANG=C", "qemu-img", "info", "/some/path").AndReturn(rval) utils.execute("qemu-img", "create", "-f", "qcow2", "-o", "backing_file=/some/path", "/the/new/cow") # Start test self.mox.ReplayAll() libvirt_utils.create_cow_image("/some/path", "/the/new/cow")
def test_create_cow_image(self): self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') rval = ('stdout', None) os.path.exists('/some/path').AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', '/some/path').AndReturn(rval) utils.execute('qemu-img', 'create', '-f', 'qcow2', '-o', 'backing_file=/some/path', '/the/new/cow') # Start test self.mox.ReplayAll() libvirt_utils.create_cow_image('/some/path', '/the/new/cow')
def test_create_cow_image(self, mock_info, mock_execute, mock_exists): mock_execute.return_value = ('stdout', None) mock_info.return_value = mock.Mock( file_format=mock.sentinel.backing_fmt, cluster_size=mock.sentinel.cluster_size) libvirt_utils.create_cow_image(mock.sentinel.backing_path, mock.sentinel.new_path) mock_info.assert_called_once_with(mock.sentinel.backing_path) mock_execute.assert_has_calls([mock.call( 'qemu-img', 'create', '-f', 'qcow2', '-o', 'backing_file=%s,backing_fmt=%s,cluster_size=%s' % ( mock.sentinel.backing_path, mock.sentinel.backing_fmt, mock.sentinel.cluster_size), mock.sentinel.new_path)])
def copy_qcow2_image(base, target, size): libvirt_utils.create_cow_image(base, target) if size: disk.extend(target, size)