#!/bin/bash

pool=scratch

set -x

# Demonstrate that things work
rbd ls -l -p $pool > /dev/null

# Create origin image
dd if=/dev/zero bs=1M count=10000 | rbd import - $pool/origin --image-format 2

# Clone origin
rbd snap create $pool/origin@snap
rbd snap protect $pool/origin@snap
rbd clone $pool/origin@snap $pool/clone

# Flatten clone; create snapshot while flattening
( sleep 5; rbd snap create $pool/clone@broken ) &
rbd flatten $pool/clone

# Tear down origin
rbd snap unprotect $pool/origin@snap
rbd snap rm $pool/origin@snap
rbd rm $pool/origin

# Now things are broken!
rbd snap ls $pool/clone
rbd snap rm $pool/clone@broken
rbd ls -l -p $pool > /dev/null
