uevent=add
LVNAME=lvtest
MNT=/mnt
IMG1=dm-device-1.img
VG1=testvg1
IMG2=dm-device-2.img
VG2=testvg2
dd if=/dev/zero of=$IMG1 bs=1M count=1 seek=10240 status=none
dd if=/dev/zero of=$IMG2 bs=1M count=1 seek=10240 status=none

DEV1=$(losetup --show -f $IMG1) 
DEV2=$(losetup --show -f $IMG2) 

vgcreate $VG1 $DEV1 > /dev/null 2>&1
vgcreate $VG2 $DEV2> /dev/null 2>&1
lvcreate -l 100%VG $VG1 -n $LVNAME> /dev/null 2>&1
lvcreate -l 100%VG $VG2 -n $LVNAME> /dev/null 2>&1

# Create fs and add second device
mkfs.btrfs /dev/$VG1/$LVNAME> /dev/null 2>&1
mount /dev/$VG1/$LVNAME $MNT> /dev/null 2>&1
btrfs dev add /dev/$VG2/$LVNAME $MNT> /dev/null 2>&1

echo "Before"
btrfs fi usage -T $MNT | grep /dev|awk '{print $1}'

if [ "$uevent" = "add" ]; then

umount $MNT> /dev/null 2>&1
vgchange -a n $VG1> /dev/null 2>&1
vgchange -a n $VG2> /dev/null 2>&1

# Reactivate and see what devices btrfs knows about for this fs.
# The names *should* be /dev/mapper/$VG1-$LVNAME and /dev/mapper/$VG2-$LVNAME
vgchange -a y $VG1> /dev/null 2>&1
vgchange -a y $VG2> /dev/null 2>&1
mount /dev/$VG1/$LVNAME $MNT> /dev/null 2>&1
else
	partprobe $DEV1 $DEV2
fi

echo "After"
btrfs fi usage -T $MNT | grep /dev|awk '{print $1}'

umount $MNT

vgchange -a n $VG1 > /dev/null 2>&1
vgchange -a n $VG2 > /dev/null 2>&1

vgremove -f $VG1 > /dev/null 2>&1
vgremove -f $VG2 > /dev/null 2>&1
losetup -d $DEV1
losetup -d $DEV2
rm -f $IMG1
rm -f $IMG2
