1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#!/usr/bin/env bash
# nbd client library in userspace
# Copyright Red Hat
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# Test that NBD errors are reported correctly.
. ../tests/functions.sh
set -e
set -x
requires_fuse
requires $DD --version
requires $DD iflag=count_bytes,skip_bytes </dev/null
requires $NBDKIT --version
requires $NBDKIT sh --version
# Difficult to arrange for this test to be run this test under
# valgrind, so don't bother.
if [ "x$LIBNBD_VALGRIND" = "x1" ]; then
echo "$0: test skipped under valgrind"
exit 77
fi
mp=test-errors.d
pidfile=test-errors.pid
cleanup_fn fusermount3 -u $mp
cleanup_fn rm -rf $mp $pidfile
mkdir $mp
export DD mp pidfile prog="$0"
$NBDKIT -U - \
sh - \
--run '
set -e
set -x
error=
# Run nbdfuse and connect to the nbdkit server.
nbdfuse -P $pidfile $mp --unix $unixsocket &
# Wait for the pidfile to appear.
for i in {1..60}; do
if test -f $pidfile; then
break
fi
sleep 1
done
if ! test -f $pidfile; then
echo "$prog: nbdfuse PID file $pidfile was not created"
exit 1
fi
ls -al $mp
# Commands which do not touch byte 100,000 should succeed, but
# watch out for readahead.
$DD if=$mp/nbd of=/dev/null skip=500000 count=1000 iflag=count_bytes,skip_bytes
$DD if=$mp/nbd of=/dev/null skip=700000 count=200 iflag=count_bytes,skip_bytes
# Commands which touch byte 100,000 must fail.
if $DD if=$mp/nbd of=/dev/null skip=99000 count=2000 iflag=count_bytes,skip_bytes; then
echo "$prog: error: expected dd to fail"
error=1
fi
if $DD if=$mp/nbd of=/dev/null skip=90000 count=12000 iflag=count_bytes,skip_bytes; then
echo "$prog: error: expected dd to fail"
error=1
fi
# We have to explicitly unmount it here else nbdfuse will
# never exit and nbdkit will hang.
fusermount3 -u $mp
if [ -z "$error" ]; then exit 0; else exit 1; fi
' <<'EOF'
#!/bin/bash -
# NBD server that has an error at byte 100,000
case "$1" in
get_size) echo 1M ;;
pread)
if [ $4 -le 100000 ] && [ $(( $4 + $3 )) -gt 100000 ]; then
echo EIO Bad block >&2
exit 1
else
$DD if=/dev/zero count=$3 iflag=count_bytes
fi ;;
*) exit 2 ;;
esac
EOF
|