Revert "ci: skip "lib/test-fork-safe-execvpe.sh" on Alpine Linux"
[libnbd.git] / copy / copy-file-to-qcow2.sh
blob6749f321d0eb7fde6cc1bd2143bc461ff994270b
1 #!/usr/bin/env bash
2 # nbd client library in userspace
3 # Copyright Red Hat
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 . ../tests/functions.sh
21 set -e
22 set -x
24 requires $QEMU_NBD --pid-file=test.pid --version
25 requires qemu-img --version
26 requires cmp --version
27 requires dd --version
28 requires dd oflag=seek_bytes </dev/null
29 requires stat --version
30 requires test -r /dev/urandom
31 requires test -r /dev/zero
32 requires truncate --version
34 file=copy-file-to-qcow2.file
35 file2=copy-file-to-qcow2.file2
36 qcow2=copy-file-to-qcow2.qcow2
37 pid=copy-file-to-qcow2.pid
38 sock=$(mktemp -u /tmp/libnbd-test-copy.XXXXXX)
39 rm -f $file $file2 $qcow2 $pid $sock
40 cleanup_fn rm -f $file $file2 $qcow2 $pid $sock
42 # Create a random partially sparse file.
43 touch $file
44 for i in `seq 1 100`; do
45 dd if=/dev/urandom of=$file ibs=512 count=1 \
46 oflag=seek_bytes seek=$((RANDOM * 9973)) conv=notrunc
47 dd if=/dev/zero of=$file ibs=512 count=1 \
48 oflag=seek_bytes seek=$((RANDOM * 9973)) conv=notrunc
49 done
50 size="$( stat -c %s $file )"
52 # Create the empty target qcow2 file.
53 qemu-img create -f qcow2 $qcow2 $size
55 # Run qemu-nbd as a separate process so that we can copy to and from
56 # the single process in two separate operations.
57 $QEMU_NBD -f qcow2 --cache=writeback -t --socket=$sock --pid-file=$pid $qcow2 &
58 cleanup_fn kill $!
60 wait_for_pidfile qemu-nbd $pid
62 $VG nbdcopy $file "nbd+unix:///?socket=$sock"
63 $VG nbdcopy "nbd+unix:///?socket=$sock" $file2
65 ls -l $file $file2
67 # Because qemu/qcow2 only supports whole sectors, we have to truncate
68 # the copied out file to the expected size before comparing.
69 truncate -s $size $file2
71 cmp $file $file2