'Shelve an instance.
Shuts down an instance and frees it up to be removed from the
hypervisor.'

@check_instance_lock
@check_instance_state(vm_state=[vm_states.ACTIVE, vm_states.STOPPED, vm_states.PAUSED, vm_states.SUSPENDED])
def shelve(self, context, instance, clean_shutdown=True):

    instance.task_state = task_states.SHELVING
    instance.save(expected_task_state=[None])
    self._record_action_start(context, instance, instance_actions.SHELVE)
    if (not compute_utils.is_volume_backed_instance(context, instance)):
        name = ('%s-shelved' % instance.display_name)
        image_meta = self._create_image(context, instance, name, 'snapshot')
        image_id = image_meta['id']
        self.compute_rpcapi.shelve_instance(context, instance=instance, image_id=image_id, clean_shutdown=clean_shutdown)
    else:
        self.compute_rpcapi.shelve_offload_instance(context, instance=instance, clean_shutdown=clean_shutdown)
