def detach_volume(self, connection_info, instance, mountpoint, encryption=None):
    instance_name = instance['name']
    disk_dev = mountpoint.rpartition('/')[2]
    try:
        virt_dom = self._lookup_by_name(instance_name)
        xml = self._get_disk_xml(virt_dom.XMLDesc(0), disk_dev)
        if (not xml):
            raise exception.DiskNotFound(location=disk_dev)
        else:
            flags = libvirt.VIR_DOMAIN_AFFECT_CONFIG
            state = LIBVIRT_POWER_STATE[virt_dom.info()[0]]
            if (state in (power_state.RUNNING, power_state.PAUSED)):
                flags |= libvirt.VIR_DOMAIN_AFFECT_LIVE
            virt_dom.detachDeviceFlags(xml, flags)
            if encryption:
                encryptor = self._get_volume_encryptor(connection_info, encryption)
                encryptor.detach_volume(**encryption)
    except exception.InstanceNotFound:
        LOG.warn(_LW('During detach_volume, instance disappeared.'))
    except libvirt.libvirtError as ex:
        error_code = ex.get_error_code()
        if (error_code == libvirt.VIR_ERR_NO_DOMAIN):
            LOG.warn(_LW('During detach_volume, instance disappeared.'))
        else:
            raise
    self._disconnect_volume(connection_info, disk_dev)
def detach_volume(self, connection_info, instance, mountpoint, encryption=None):
    instance_name = instance['name']
    disk_dev = mountpoint.rpartition('/')[2]
    try:
        virt_dom = self._lookup_by_name(instance_name)
        xml = self._get_disk_xml(virt_dom.XMLDesc(0), disk_dev)
        if (not xml):
            raise exception.DiskNotFound(location=disk_dev)
        else:
            flags = libvirt.VIR_DOMAIN_AFFECT_CONFIG
            state = LIBVIRT_POWER_STATE[virt_dom.info()[0]]
            if (state in (power_state.RUNNING, power_state.PAUSED)):
                flags |= libvirt.VIR_DOMAIN_AFFECT_LIVE
            virt_dom.detachDeviceFlags(xml, flags)
            if encryption:
                encryptor = self._get_volume_encryptor(connection_info, encryption)
                encryptor.detach_volume(**encryption)
    except exception.InstanceNotFound:
        LOG.warn(_LW('During detach_volume, instance disappeared.'))
    except libvirt.libvirtError as ex:
        error_code = ex.get_error_code()
        if (error_code == libvirt.VIR_ERR_NO_DOMAIN):
            LOG.warn(_LW('During detach_volume, instance disappeared.'))
        else:
            raise
    self._disconnect_volume(connection_info, disk_dev)