def snapshot(self, context, instance, image_id, update_task_state):
    'Create snapshot from a running VM instance.\n\n        This command only works with qemu 0.14+\n        '
    try:
        virt_dom = self._lookup_by_name(instance['name'])
    except exception.InstanceNotFound:
        raise exception.InstanceNotRunning(instance_id=instance['uuid'])
    base_image_ref = instance['image_ref']
    base = compute_utils.get_image_metadata(context, self._image_api, base_image_ref, instance)
    snapshot = self._image_api.get(context, image_id)
    disk_path = libvirt_utils.find_disk(virt_dom)
    source_format = libvirt_utils.get_disk_type(disk_path)
    image_format = (CONF.libvirt.snapshot_image_format or source_format)
    if ((image_format == 'lvm') or (image_format == 'rbd')):
        image_format = 'raw'
    metadata = self._create_snapshot_metadata(base, instance, image_format, snapshot['name'])
    snapshot_name = uuid.uuid4().hex
    state = LIBVIRT_POWER_STATE[virt_dom.info()[0]]
    if (self._has_min_version(MIN_LIBVIRT_LIVESNAPSHOT_VERSION, MIN_QEMU_LIVESNAPSHOT_VERSION, REQ_HYPERVISOR_LIVESNAPSHOT) and (not (source_format == 'lvm')) and (not (source_format == 'rbd'))):
        live_snapshot = True
        try:
            virt_dom.blockJobAbort(disk_path, 0)
        except libvirt.libvirtError as ex:
            error_code = ex.get_error_code()
            if (error_code == libvirt.VIR_ERR_CONFIG_UNSUPPORTED):
                live_snapshot = False
            else:
                pass
    else:
        live_snapshot = False
    if (state == power_state.SHUTDOWN):
        live_snapshot = False
    if ((CONF.libvirt.virt_type != 'lxc') and (not live_snapshot)):
        if ((state == power_state.RUNNING) or (state == power_state.PAUSED)):
            self._detach_pci_devices(virt_dom, pci_manager.get_instance_pci_devs(instance))
            virt_dom.managedSave(0)
    snapshot_backend = self.image_backend.snapshot(disk_path, image_type=source_format)
    if live_snapshot:
        LOG.info(_LI('Beginning live snapshot process'), instance=instance)
    else:
        LOG.info(_LI('Beginning cold snapshot process'), instance=instance)
    update_task_state(task_state=task_states.IMAGE_PENDING_UPLOAD)
    snapshot_directory = 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)
            if live_snapshot:
                os.chmod(tmpdir, 449)
                self._live_snapshot(virt_dom, disk_path, out_path, image_format)
            else:
                snapshot_backend.snapshot_extract(out_path, image_format)
        finally:
            new_dom = None
            if ((CONF.libvirt.virt_type != 'lxc') and (not live_snapshot)):
                if (state == power_state.RUNNING):
                    new_dom = self._create_domain(domain=virt_dom)
                elif (state == power_state.PAUSED):
                    new_dom = self._create_domain(domain=virt_dom, launch_flags=libvirt.VIR_DOMAIN_START_PAUSED)
                if (new_dom is not None):
                    self._attach_pci_devices(new_dom, pci_manager.get_instance_pci_devs(instance))
            LOG.info(_LI('Snapshot extracted, beginning image upload'), instance=instance)
        update_task_state(task_state=task_states.IMAGE_UPLOADING, expected_state=task_states.IMAGE_PENDING_UPLOAD)
        with libvirt_utils.file_open(out_path) as image_file:
            self._image_api.update(context, image_id, metadata, image_file)
            LOG.info(_LI('Snapshot image upload complete'), instance=instance)
Create snapshot from a running VM instance.

This command only works with qemu 0.14+
def snapshot(self, context, instance, image_id, update_task_state):
    'Create snapshot from a running VM instance.\n\n        This command only works with qemu 0.14+\n        '
    try:
        virt_dom = self._lookup_by_name(instance['name'])
    except exception.InstanceNotFound:
        raise exception.InstanceNotRunning(instance_id=instance['uuid'])
    base_image_ref = instance['image_ref']
    base = compute_utils.get_image_metadata(context, self._image_api, base_image_ref, instance)
    snapshot = self._image_api.get(context, image_id)
    disk_path = libvirt_utils.find_disk(virt_dom)
    source_format = libvirt_utils.get_disk_type(disk_path)
    image_format = (CONF.libvirt.snapshot_image_format or source_format)
    if ((image_format == 'lvm') or (image_format == 'rbd')):
        image_format = 'raw'
    metadata = self._create_snapshot_metadata(base, instance, image_format, snapshot['name'])
    snapshot_name = uuid.uuid4().hex
    state = LIBVIRT_POWER_STATE[virt_dom.info()[0]]
    if (self._has_min_version(MIN_LIBVIRT_LIVESNAPSHOT_VERSION, MIN_QEMU_LIVESNAPSHOT_VERSION, REQ_HYPERVISOR_LIVESNAPSHOT) and (not (source_format == 'lvm')) and (not (source_format == 'rbd'))):
        live_snapshot = True
        try:
            virt_dom.blockJobAbort(disk_path, 0)
        except libvirt.libvirtError as ex:
            error_code = ex.get_error_code()
            if (error_code == libvirt.VIR_ERR_CONFIG_UNSUPPORTED):
                live_snapshot = False
            else:
                pass
    else:
        live_snapshot = False
    if (state == power_state.SHUTDOWN):
        live_snapshot = False
    if ((CONF.libvirt.virt_type != 'lxc') and (not live_snapshot)):
        if ((state == power_state.RUNNING) or (state == power_state.PAUSED)):
            self._detach_pci_devices(virt_dom, pci_manager.get_instance_pci_devs(instance))
            virt_dom.managedSave(0)
    snapshot_backend = self.image_backend.snapshot(disk_path, image_type=source_format)
    if live_snapshot:
        LOG.info(_LI('Beginning live snapshot process'), instance=instance)
    else:
        LOG.info(_LI('Beginning cold snapshot process'), instance=instance)
    update_task_state(task_state=task_states.IMAGE_PENDING_UPLOAD)
    snapshot_directory = 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)
            if live_snapshot:
                os.chmod(tmpdir, 449)
                self._live_snapshot(virt_dom, disk_path, out_path, image_format)
            else:
                snapshot_backend.snapshot_extract(out_path, image_format)
        finally:
            new_dom = None
            if ((CONF.libvirt.virt_type != 'lxc') and (not live_snapshot)):
                if (state == power_state.RUNNING):
                    new_dom = self._create_domain(domain=virt_dom)
                elif (state == power_state.PAUSED):
                    new_dom = self._create_domain(domain=virt_dom, launch_flags=libvirt.VIR_DOMAIN_START_PAUSED)
                if (new_dom is not None):
                    self._attach_pci_devices(new_dom, pci_manager.get_instance_pci_devs(instance))
            LOG.info(_LI('Snapshot extracted, beginning image upload'), instance=instance)
        update_task_state(task_state=task_states.IMAGE_UPLOADING, expected_state=task_states.IMAGE_PENDING_UPLOAD)
        with libvirt_utils.file_open(out_path) as image_file:
            self._image_api.update(context, image_id, metadata, image_file)
            LOG.info(_LI('Snapshot image upload complete'), instance=instance)