trap "lvremove -f $VG/__lvmsynctest_src || true;
      lvremove -f $VG/__lvmsynctest_dest || true;
      lvremove -f $VG/__lvmsynctest_snap || true;
      rm -f $HERE/,,snapshot_data_file || true" EXIT

lvcreate -l 5 -n __lvmsynctest_src $VG >/dev/null
lvcreate -l 5 -n __lvmsynctest_dest $VG >/dev/null

# Fill src with gibberish
dd if=/dev/urandom of=/dev/$VG/__lvmsynctest_src bs=1M >/dev/null 2>&1 || true

# Snapshot
lvcreate --snapshot $VG/__lvmsynctest_src -l 5 -n __lvmsynctest_snap >/dev/null

# Initial copy
dd if=/dev/$VG/__lvmsynctest_src of=/dev/$VG/__lvmsynctest_dest bs=1M >/dev/null 2>&1

# Write in some gibberish at intervals across the LV
i=0
while dd if=/dev/urandom of=/dev/$VG/__lvmsynctest_src bs=512 count=1 seek=$(($i*2048)) >/dev/null 2>&1; do
	i=$(($i+1))
done

# Now lvmsync -- dump to a file
$LVMSYNC /dev/$VG/__lvmsynctest_snap /dev/$VG/__lvmsynctest_dest --stdout >$HERE/,,snapshot_data_file 2>/dev/null || true

# Apply from the file
$LVMSYNC --apply $HERE/,,snapshot_data_file /dev/$VG/__lvmsynctest_dest 2>/dev/null || true

# Verify that our source and dest are now equal
SRCSUM="$(md5sum </dev/$VG/__lvmsynctest_src)"
DESTSUM="$(md5sum </dev/$VG/__lvmsynctest_dest)"

# Cleanup
lvremove -f $VG/__lvmsynctest_snap >/dev/null >/dev/null
lvremove -f $VG/__lvmsynctest_dest >/dev/null >/dev/null
lvremove -f $VG/__lvmsynctest_src >/dev/null >/dev/null
rm -f $HERE/,,snapshot_data_file

trap "" EXIT

# Make sure test succeeded
if [ "$SRCSUM" != "$DESTSUM" ]; then
	echo "FAIL: 05sync_via_snapshot_file"
	exit 1
else
	echo "OK: 05sync_via_snapshot_file"
fi
