def find_cloudstack_volume(mcs, vm_id, device_id=None, volume_id=None):
    """
    Find a CloudStack volume that is mounted to 'vm_id' or safe to re-mount.
    You need to provide either 'device_id' or 'volume_id'.
    :param mcs: MiniCloudStack instance with valid connection
    :param vm_id: id of virtual machine
    :param device_id: id of device
    :param volume_id: id of volume
    :return: volume object or 'None'
    """
    volume = None
    for v in mcs.list('volumes'):
        attached_vm = getattr(v, 'virtualmachineid', vm_id)
        if attached_vm == vm_id or getattr(v, 'vmstate', '') == 'Stopped':
            if volume_id and v.id == volume_id:
                volume = v
                break
            if device_id and getattr(v, 'deviceid', -1) == device_id:
                volume = v
                break
    return volume
