#!/bin/bash -

set -e
set -x

nbdkit=/usr/sbin/nbdkit
nbdcopy=/usr/bin/nbdcopy
input=/var/tmp/fedora-35.img
output=/var/tmp/out.raw

$nbdkit --version
$nbdcopy --version

rm -f /tmp/insock /tmp/outsock

$nbdkit -f --exit-with-parent -U /tmp/insock \
        file $input cache=none --filter=cow &

rm -f $output
truncate -r $input $output
$nbdkit -f --exit-with-parent -U /tmp/outsock \
        file $output cache=none &

sleep 1

time $nbdcopy -p --flush --request-size=$((16 * 1024 * 1024)) \
     'nbd+unix:///?socket=/tmp/insock' 'nbd+unix:///?socket=/tmp/outsock'
time sync
