def test_disk_type(self): # Seems like lvm detection # if its in /dev ?? for p in ['/dev/b', '/dev/blah/blah']: d_type = libvirt_utils.get_disk_type(p) self.assertEqual('lvm', d_type) # Try rbd detection d_type = libvirt_utils.get_disk_type('rbd:pool/instance') self.assertEqual('rbd', d_type) # Try the other types template_output = """image: %(path)s file format: %(format)s virtual size: 64M (67108864 bytes) cluster_size: 65536 disk size: 96K """ path = '/myhome/disk.config' for f in ['raw', 'qcow2']: output = template_output % ({ 'format': f, 'path': path, }) self.mox.StubOutWithMock(os.path, 'exists') self.mox.StubOutWithMock(utils, 'execute') os.path.exists(path).AndReturn(True) utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((output, '')) self.mox.ReplayAll() d_type = libvirt_utils.get_disk_type(path) self.assertEqual(f, d_type) self.mox.UnsetStubs()
def test_disk_type(self, mock_exists): # Seems like lvm detection # if its in /dev ?? for p in ['/dev/b', '/dev/blah/blah']: d_type = libvirt_utils.get_disk_type(p) self.assertEqual('lvm', d_type) # Try rbd detection d_type = libvirt_utils.get_disk_type('rbd:pool/instance') self.assertEqual('rbd', d_type) # Try the other types template_output = """image: %(path)s file format: %(format)s virtual size: 64M (67108864 bytes) cluster_size: 65536 disk size: 96K """ path = '/myhome/disk.config' for f in ['raw', 'qcow2']: output = template_output % ({ 'format': f, 'path': path, }) with mock.patch('nova.utils.execute', return_value=(output, '')) as mock_execute: d_type = libvirt_utils.get_disk_type(path) mock_execute.assert_called_once_with( 'env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) self.assertEqual(f, d_type)
def test_disk_type(self, mock_exists): # Seems like lvm detection # if its in /dev ?? for p in ["/dev/b", "/dev/blah/blah"]: d_type = libvirt_utils.get_disk_type(p) self.assertEqual("lvm", d_type) # Try rbd detection d_type = libvirt_utils.get_disk_type("rbd:pool/instance") self.assertEqual("rbd", d_type) # Try the other types template_output = """image: %(path)s file format: %(format)s virtual size: 64M (67108864 bytes) cluster_size: 65536 disk size: 96K """ path = "/myhome/disk.config" for f in ["raw", "qcow2"]: output = template_output % ({"format": f, "path": path}) with mock.patch("nova.utils.execute", return_value=(output, "")) as mock_execute: d_type = libvirt_utils.get_disk_type(path) mock_execute.assert_called_once_with("env", "LC_ALL=C", "LANG=C", "qemu-img", "info", path) self.assertEqual(f, d_type)
def test_disk_type(self): # Seems like lvm detection # if its in /dev ?? for p in ["/dev/b", "/dev/blah/blah"]: d_type = libvirt_utils.get_disk_type(p) self.assertEqual("lvm", d_type) # Try rbd detection d_type = libvirt_utils.get_disk_type("rbd:pool/instance") self.assertEqual("rbd", d_type) # Try the other types template_output = """image: %(path)s file format: %(format)s virtual size: 64M (67108864 bytes) cluster_size: 65536 disk size: 96K """ path = "/myhome/disk.config" for f in ["raw", "qcow2"]: output = template_output % ({"format": f, "path": path}) self.mox.StubOutWithMock(os.path, "exists") self.mox.StubOutWithMock(utils, "execute") os.path.exists(path).AndReturn(True) utils.execute("env", "LC_ALL=C", "LANG=C", "qemu-img", "info", path).AndReturn((output, "")) self.mox.ReplayAll() d_type = libvirt_utils.get_disk_type(path) self.assertEqual(f, d_type) self.mox.UnsetStubs()
def test_get_disk_type(self, mock_execute, mock_exists): path = "disk.config" example_output = """image: disk.config file format: raw virtual size: 64M (67108864 bytes) cluster_size: 65536 disk size: 96K blah BLAH: bb """ mock_execute.return_value = (example_output, "") disk_type = libvirt_utils.get_disk_type(path) mock_execute.assert_called_once_with("env", "LC_ALL=C", "LANG=C", "qemu-img", "info", path) mock_exists.assert_called_once_with(path) self.assertEqual("raw", disk_type)
def test_get_disk_type(self, mock_execute, mock_exists): path = "disk.config" example_output = """image: disk.config file format: raw virtual size: 64M (67108864 bytes) cluster_size: 65536 disk size: 96K blah BLAH: bb """ mock_execute.return_value = (example_output, '') disk_type = libvirt_utils.get_disk_type(path) mock_execute.assert_called_once_with('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path) mock_exists.assert_called_once_with(path) self.assertEqual('raw', disk_type)
def test_get_disk_type(self): path = "disk.config" example_output = """image: disk.config file format: raw virtual size: 64M (67108864 bytes) cluster_size: 65536 disk size: 96K blah BLAH: bb """ self.mox.StubOutWithMock(utils, 'execute') utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', path).AndReturn((example_output, '')) self.mox.ReplayAll() disk_type = libvirt_utils.get_disk_type(path) self.assertEqual(disk_type, 'raw')
def test_get_disk_type(self): path = "disk.config" example_output = """image: disk.config file format: raw virtual size: 64M (67108864 bytes) cluster_size: 65536 disk size: 96K blah BLAH: bb """ self.mox.StubOutWithMock(os.path, "exists") self.mox.StubOutWithMock(utils, "execute") os.path.exists(path).AndReturn(True) utils.execute("env", "LC_ALL=C", "LANG=C", "qemu-img", "info", path).AndReturn((example_output, "")) self.mox.ReplayAll() disk_type = libvirt_utils.get_disk_type(path) self.assertEqual(disk_type, "raw")
def test_disk_type_ploop(self, mock_isdir, mock_exists): path = '/some/path' d_type = libvirt_utils.get_disk_type(path) mock_isdir.assert_called_once_with(path) mock_exists.assert_called_once_with("%s/DiskDescriptor.xml" % path) self.assertEqual('ploop', d_type)
def cloudlet_base(self, context, instance, vm_name, disk_meta_id, memory_meta_id, diskhash_meta_id, memoryhash_meta_id, update_task_state): """create base vm and save it to glance """ try: if hasattr(self, "_lookup_by_name"): # icehouse virt_dom = self._lookup_by_name(instance['name']) else: # kilo virt_dom = self._host.get_domain(instance) except exception.InstanceNotFound: raise exception.InstanceNotRunning(instance_id=instance['uuid']) # pause VM self.pause(instance) (image_service, image_id) = glance.get_remote_image_service(context, instance['image_ref']) disk_metadata = self._get_snapshot_metadata(virt_dom, context, instance, disk_meta_id) mem_metadata = self._get_snapshot_metadata(virt_dom, context, instance, memory_meta_id) diskhash_metadata = self._get_snapshot_metadata( virt_dom, context, instance, diskhash_meta_id) memhash_metadata = self._get_snapshot_metadata(virt_dom, context, instance, memoryhash_meta_id) disk_path = libvirt_utils.find_disk(virt_dom) source_format = libvirt_utils.get_disk_type(disk_path) snapshot_name = uuid.uuid4().hex (state, _max_mem, _mem, _cpus, _t) = virt_dom.info() state = libvirt_driver.LIBVIRT_POWER_STATE[state] # creating base vm requires cold snapshotting snapshot_backend = self.image_backend.snapshot( disk_path, image_type=source_format) LOG.info(_("Beginning cold snapshot process"), instance=instance) # not available at icehouse # snapshot_backend.snapshot_create() update_task_state(task_state=task_states.IMAGE_PENDING_UPLOAD, expected_state=None) snapshot_directory = libvirt_driver.CONF.libvirt.snapshots_directory fileutils.ensure_tree(snapshot_directory) with utils.tempdir(dir=snapshot_directory) as tmpdir: try: out_path = os.path.join(tmpdir, snapshot_name) # At this point, base vm should be "raw" format snapshot_backend.snapshot_extract(out_path, "raw") finally: # snapshotting logic is changed since icehouse. # : cannot find snapshot_create and snapshot_delete. # snapshot_extract is replacing these two operations. # snapshot_backend.snapshot_delete() LOG.info(_("Snapshot extracted, beginning image upload"), instance=instance) # generate memory snapshop and hashlist basemem_path = os.path.join(tmpdir, snapshot_name + "-mem") diskhash_path = os.path.join(tmpdir, snapshot_name + "-disk_hash") memhash_path = os.path.join(tmpdir, snapshot_name + "-mem_hash") update_task_state(task_state=task_states.IMAGE_UPLOADING, expected_state=task_states.IMAGE_PENDING_UPLOAD) synthesis._create_baseVM(self._conn, virt_dom, out_path, basemem_path, diskhash_path, memhash_path, nova_util=libvirt_utils) self._update_to_glance(context, image_service, out_path, disk_meta_id, disk_metadata) LOG.info(_("Base disk upload complete"), instance=instance) self._update_to_glance(context, image_service, basemem_path, memory_meta_id, mem_metadata) LOG.info(_("Base memory image upload complete"), instance=instance) self._update_to_glance(context, image_service, diskhash_path, diskhash_meta_id, diskhash_metadata) LOG.info(_("Base disk upload complete"), instance=instance) self._update_to_glance(context, image_service, memhash_path, memoryhash_meta_id, memhash_metadata) LOG.info(_("Base memory image upload complete"), instance=instance)
def cloudlet_base( self, context, instance, vm_name, disk_meta_id, memory_meta_id, diskhash_meta_id, memoryhash_meta_id, update_task_state, ): """create base vm and save it to glance """ try: if hasattr(self, "_lookup_by_name"): # icehouse virt_dom = self._lookup_by_name(instance["name"]) else: # kilo virt_dom = self._host.get_domain(instance) except exception.InstanceNotFound: raise exception.InstanceNotRunning(instance_id=instance["uuid"]) # pause VM self.pause(instance) (image_service, image_id) = glance.get_remote_image_service(context, instance["image_ref"]) disk_metadata = self._get_snapshot_metadata(virt_dom, context, instance, disk_meta_id) mem_metadata = self._get_snapshot_metadata(virt_dom, context, instance, memory_meta_id) diskhash_metadata = self._get_snapshot_metadata(virt_dom, context, instance, diskhash_meta_id) memhash_metadata = self._get_snapshot_metadata(virt_dom, context, instance, memoryhash_meta_id) disk_path = libvirt_utils.find_disk(virt_dom) source_format = libvirt_utils.get_disk_type(disk_path) snapshot_name = uuid.uuid4().hex (state, _max_mem, _mem, _cpus, _t) = virt_dom.info() state = libvirt_driver.LIBVIRT_POWER_STATE[state] # creating base vm requires cold snapshotting snapshot_backend = self.image_backend.snapshot(disk_path, image_type=source_format) LOG.info(_("Beginning cold snapshot process"), instance=instance) # not available at icehouse # snapshot_backend.snapshot_create() update_task_state(task_state=task_states.IMAGE_PENDING_UPLOAD, expected_state=None) snapshot_directory = libvirt_driver.CONF.libvirt.snapshots_directory fileutils.ensure_tree(snapshot_directory) with utils.tempdir(dir=snapshot_directory) as tmpdir: try: out_path = os.path.join(tmpdir, snapshot_name) # At this point, base vm should be "raw" format snapshot_backend.snapshot_extract(out_path, "raw") finally: # snapshotting logic is changed since icehouse. # : cannot find snapshot_create and snapshot_delete. # snapshot_extract is replacing these two operations. # snapshot_backend.snapshot_delete() LOG.info(_("Snapshot extracted, beginning image upload"), instance=instance) # generate memory snapshop and hashlist basemem_path = os.path.join(tmpdir, snapshot_name + "-mem") diskhash_path = os.path.join(tmpdir, snapshot_name + "-disk_hash") memhash_path = os.path.join(tmpdir, snapshot_name + "-mem_hash") update_task_state(task_state=task_states.IMAGE_UPLOADING, expected_state=task_states.IMAGE_PENDING_UPLOAD) synthesis._create_baseVM( self._conn, virt_dom, out_path, basemem_path, diskhash_path, memhash_path, nova_util=libvirt_utils ) self._update_to_glance(context, image_service, out_path, disk_meta_id, disk_metadata) LOG.info(_("Base disk upload complete"), instance=instance) self._update_to_glance(context, image_service, basemem_path, memory_meta_id, mem_metadata) LOG.info(_("Base memory image upload complete"), instance=instance) self._update_to_glance(context, image_service, diskhash_path, diskhash_meta_id, diskhash_metadata) LOG.info(_("Base disk upload complete"), instance=instance) self._update_to_glance(context, image_service, memhash_path, memoryhash_meta_id, memhash_metadata) LOG.info(_("Base memory image upload complete"), instance=instance)