2011-01-28 19:10:21 +03:00
#!/bin/bash
2012-01-26 22:25:46 +04:00
# Copyright (C) 2011-2012 Red Hat, Inc. All rights reserved.
2011-01-05 03:16:18 +03:00
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
. lib/utils
prepare_clvmd( ) {
if test -z " $LVM_TEST_LOCKING " || test " $LVM_TEST_LOCKING " -ne 3 ; then
return 0 # not needed
fi
if pgrep clvmd ; then
echo " Cannot use fake cluster locking with real clvmd ( $( pgrep clvmd) ) running. "
2011-01-07 16:03:04 +03:00
skip
2011-01-05 03:16:18 +03:00
fi
# skip if we don't have our own clvmd...
2011-01-07 16:03:04 +03:00
( which clvmd | grep $abs_builddir ) || skip
2011-01-05 03:16:18 +03:00
# skip if we singlenode is not compiled in
2011-01-07 16:03:04 +03:00
( clvmd --help 2>& 1 | grep "Available cluster managers" | grep singlenode) || skip
2011-01-05 03:16:18 +03:00
2011-01-13 17:57:18 +03:00
lvmconf "activation/monitoring = 1"
2011-01-05 03:16:18 +03:00
clvmd -Isinglenode -d 1 &
LOCAL_CLVMD = " $! "
# check that it is really running now
sleep .1
2012-01-26 22:25:46 +04:00
ps $LOCAL_CLVMD || die
echo " $LOCAL_CLVMD " > LOCAL_CLVMD
2011-01-05 03:16:18 +03:00
}
prepare_dmeventd( ) {
if pgrep dmeventd ; then
echo " Cannot test dmeventd with real dmeventd ( $( pgrep dmeventd) ) running. "
touch SKIP_THIS_TEST
exit 1
fi
# skip if we don't have our own dmeventd...
2011-01-05 03:25:07 +03:00
( which dmeventd | grep $abs_builddir ) || {
touch SKIP_THIS_TEST
exit 1
}
2011-01-05 03:16:18 +03:00
2011-01-07 16:04:17 +03:00
lvmconf "activation/monitoring = 1"
dmeventd -f " $@ " &
2011-01-05 03:16:18 +03:00
echo " $! " > LOCAL_DMEVENTD
2011-03-04 17:19:18 +03:00
# FIXME wait for pipe in /var/run instead
sleep 1
2011-01-05 03:16:18 +03:00
}
2011-08-31 15:31:57 +04:00
prepare_lvmetad( ) {
2012-02-15 18:24:31 +04:00
echo preparing lvmetad...
2011-08-31 15:31:57 +04:00
# skip if we don't have our own lvmetad...
( which lvmetad | grep $abs_builddir ) || {
touch SKIP_THIS_TEST
exit 1
}
2012-02-15 18:24:31 +04:00
lvmconf "global/use_lvmetad = 1"
2012-02-23 17:11:07 +04:00
lvmconf "devices/md_component_detection = 0"
2011-08-31 15:31:57 +04:00
2012-02-15 18:24:31 +04:00
lvmetad -f " $@ " -s $TESTDIR /lvmetad.socket &
2011-08-31 15:31:57 +04:00
echo " $! " > LOCAL_LVMETAD
sleep 1
}
2012-02-24 03:58:42 +04:00
notify_lvmetad( ) {
if test -e LOCAL_LVMETAD; then
pvscan --lvmetad " $@ " || true
fi
}
2011-01-05 03:16:18 +03:00
teardown_devs( ) {
2011-06-29 22:14:08 +04:00
# Delete any remaining dm/udev semaphores
2011-06-29 22:18:18 +04:00
teardown_udev_cookies
2011-06-29 22:14:08 +04:00
2011-01-05 03:16:18 +03:00
test -n " $PREFIX " && {
rm -rf $TESTDIR /dev/$PREFIX *
init_udev_transaction
while dmsetup table | grep -q ^$PREFIX ; do
for s in ` dmsetup info -c -o name --noheading | grep ^$PREFIX ` ; do
umount -fl $DM_DEV_DIR /mapper/$s >& /dev/null || true
2011-06-30 13:08:22 +04:00
dmsetup remove -f $s >& /dev/null || true
2011-01-05 03:16:18 +03:00
done
done
finish_udev_transaction
}
2011-01-28 19:10:21 +03:00
udev_wait
2011-01-05 03:16:18 +03:00
# NOTE: SCSI_DEBUG_DEV test must come before the LOOP test because
# prepare_scsi_debug_dev() also sets LOOP to short-circuit prepare_loop()
if test -f SCSI_DEBUG_DEV; then
modprobe -r scsi_debug
else
test -f LOOP && losetup -d $( cat LOOP)
test -f LOOPFILE && rm -f $( cat LOOPFILE)
fi
rm -f DEVICES # devs is set in prepare_devs()
rm -f LOOP
2011-06-29 21:33:39 +04:00
2011-06-30 13:15:53 +04:00
# Attempt to remove any loop devices that failed to get torn down if earlier tests aborted
test -n " $COMMON_PREFIX " && {
# Resume any linears to be sure we do not deadlock
2011-06-30 12:50:10 +04:00
STRAY_DEVS = $( dmsetup table | sed 's/:.*//' | grep $COMMON_PREFIX | cut -d' ' -f 1)
for dm in $STRAY_DEVS ; do
2011-06-30 13:15:53 +04:00
# FIXME: only those really suspended
echo dmsetup resume $dm
dmsetup resume $dm || true
2011-06-30 12:50:10 +04:00
done
2011-06-30 13:15:53 +04:00
STRAY_MOUNTS = ` mount | grep $COMMON_PREFIX | cut -d\ -f1`
if test -n " $STRAY_MOUNTS " ; then
echo " Removing stray mounted devices containing $COMMON_PREFIX : "
mount | grep $COMMON_PREFIX
umount -fl $STRAY_MOUNTS || true
sleep 2
fi
init_udev_transaction
NUM_REMAINING_DEVS = 999
while NUM_DEVS = ` dmsetup table | grep ^$COMMON_PREFIX | wc -l` && \
test $NUM_DEVS -lt $NUM_REMAINING_DEVS -a $NUM_DEVS -ne 0; do
echo " Removing $NUM_DEVS stray mapped devices with names beginning with $COMMON_PREFIX : "
STRAY_DEVS = $( dmsetup table | sed 's/:.*//' | grep $COMMON_PREFIX | cut -d' ' -f 1)
dmsetup info -c | grep ^$COMMON_PREFIX
for dm in $STRAY_DEVS ; do
echo dmsetup remove $dm
2011-06-30 13:17:49 +04:00
dmsetup remove -f $dm || true
2011-06-30 13:15:53 +04:00
done
NUM_REMAINING_DEVS = $NUM_DEVS
done
finish_udev_transaction
udev_wait
STRAY_LOOPS = ` losetup -a | grep $COMMON_PREFIX | cut -d: -f1`
if test -n " $STRAY_LOOPS " ; then
echo " Removing stray loop devices containing $COMMON_PREFIX : "
losetup -a | grep $COMMON_PREFIX
losetup -d $STRAY_LOOPS || true
fi
}
2011-01-05 03:16:18 +03:00
}
teardown( ) {
echo -n "## teardown..."
2011-01-12 19:07:55 +03:00
test -f LOCAL_CLVMD && {
kill " $( cat LOCAL_CLVMD) "
2011-01-05 03:16:18 +03:00
sleep .1
2011-01-12 19:07:55 +03:00
kill -9 " $( cat LOCAL_CLVMD) " || true
2011-01-05 03:16:18 +03:00
}
echo -n .
test -f LOCAL_DMEVENTD && kill -9 " $( cat LOCAL_DMEVENTD) "
2011-08-31 15:31:57 +04:00
test -f LOCAL_LVMETAD && kill -9 " $( cat LOCAL_LVMETAD) "
2011-01-05 03:16:18 +03:00
echo -n .
2011-09-25 00:54:35 +04:00
test -d $DM_DEV_DIR /mapper && teardown_devs
2011-01-05 03:16:18 +03:00
echo -n .
test -n " $TESTDIR " && {
cd $OLDPWD
rm -rf $TESTDIR || echo BLA
}
echo "ok"
}
make_ioerror( ) {
2011-06-29 16:37:51 +04:00
echo 0 10000000 error | dmsetup create -u TEST-ioerror ioerror
2011-01-05 03:16:18 +03:00
ln -s $DM_DEV_DIR /mapper/ioerror $DM_DEV_DIR /ioerror
}
prepare_loop( ) {
size = $1
test -n " $size " || size = 32
echo -n "## preparing loop device..."
# skip if prepare_scsi_debug_dev() was used
if [ -f "SCSI_DEBUG_DEV" -a -f "LOOP" ] ; then
echo "(skipped)"
return 0
fi
test ! -e LOOP
test -n " $DM_DEV_DIR "
for i in 0 1 2 3 4 5 6 7; do
test -e $DM_DEV_DIR /loop$i || mknod $DM_DEV_DIR /loop$i b 7 $i
done
echo -n .
LOOPFILE = " $PWD /test.img "
dd if = /dev/zero of = " $LOOPFILE " bs = $(( 1024 * 1024 )) count = 0 seek = $(( $size - 1 )) 2> /dev/null
if LOOP = ` losetup -s -f " $LOOPFILE " 2>/dev/null` ; then
:
elif LOOP = ` losetup -f` && losetup $LOOP " $LOOPFILE " ; then
# no -s support
:
else
# no -f support
# Iterate through $DM_DEV_DIR/loop{,/}{0,1,2,3,4,5,6,7}
for slash in '' /; do
for i in 0 1 2 3 4 5 6 7; do
local dev = $DM_DEV_DIR /loop$slash $i
! losetup $dev >/dev/null 2>& 1 || continue
# got a free
losetup " $dev " " $LOOPFILE "
LOOP = $dev
break
done
if [ -n " $LOOP " ] ; then
break
fi
done
fi
test -n " $LOOP " # confirm or fail
echo " $LOOP " > LOOP
echo " ok ( $LOOP ) "
}
# A drop-in replacement for prepare_loop() that uses scsi_debug to create
# a ramdisk-based SCSI device upon which all LVM devices will be created
# - scripts must take care not to use a DEV_SIZE that will enduce OOM-killer
prepare_scsi_debug_dev( )
{
local DEV_SIZE = " $1 "
shift
local SCSI_DEBUG_PARAMS = " $@ "
test -f "SCSI_DEBUG_DEV" && return 0
test -z " $LOOP "
test -n " $DM_DEV_DIR "
# Skip test if awk isn't available (required for get_sd_devs_)
2011-01-07 16:03:04 +03:00
which awk || skip
2011-01-05 03:16:18 +03:00
# Skip test if scsi_debug module is unavailable or is already in use
2011-01-07 16:03:04 +03:00
modprobe --dry-run scsi_debug || skip
lsmod | grep -q scsi_debug && skip
2011-01-05 03:16:18 +03:00
# Create the scsi_debug device and determine the new scsi device's name
# NOTE: it will _never_ make sense to pass num_tgts param;
# last param wins.. so num_tgts=1 is imposed
2011-01-07 16:03:04 +03:00
modprobe scsi_debug dev_size_mb = $DEV_SIZE $SCSI_DEBUG_PARAMS num_tgts = 1 || skip
2011-01-05 03:16:18 +03:00
sleep 2 # allow for async Linux SCSI device registration
local DEBUG_DEV = /dev/$( grep -H scsi_debug /sys/block/*/device/model | cut -f4 -d /)
[ -b $DEBUG_DEV ] || exit 1 # should not happen
# Create symlink to scsi_debug device in $DM_DEV_DIR
SCSI_DEBUG_DEV = " $DM_DEV_DIR / $( basename $DEBUG_DEV ) "
echo " $SCSI_DEBUG_DEV " > SCSI_DEBUG_DEV
echo " $SCSI_DEBUG_DEV " > LOOP
# Setting $LOOP provides means for prepare_devs() override
2011-10-23 19:40:15 +04:00
test " $LVM_TEST_DEVDIR " != "/dev" && ln -snf $DEBUG_DEV $SCSI_DEBUG_DEV
2011-01-05 03:16:18 +03:00
return 0
}
cleanup_scsi_debug_dev( )
{
aux teardown_devs
rm -f SCSI_DEBUG_DEV
rm -f LOOP
}
prepare_devs( ) {
local n = " $1 "
test -z " $n " && n = 3
local devsize = " $2 "
test -z " $devsize " && devsize = 34
local pvname = " $3 "
test -z " $pvname " && pvname = "pv"
prepare_loop $(( $n * $devsize ))
echo -n " ## preparing $n devices... "
if ! loopsz = ` blockdev --getsz $LOOP 2>/dev/null` ; then
loopsz = ` blockdev --getsize $LOOP 2>/dev/null`
fi
local size = $(( $loopsz / $n ))
2011-01-05 03:25:07 +03:00
devs =
2011-01-05 03:16:18 +03:00
init_udev_transaction
for i in ` seq 1 $n ` ; do
local name = " ${ PREFIX } $pvname $i "
local dev = " $DM_DEV_DIR /mapper/ $name "
devs = " $devs $dev "
echo 0 $size linear $LOOP $(( ( $i - 1 ) * $size )) > $name .table
2011-06-29 16:37:51 +04:00
dmsetup create -u TEST-$name $name $name .table
2011-01-05 03:16:18 +03:00
done
finish_udev_transaction
#for i in `seq 1 $n`; do
# local name="${PREFIX}$pvname$i"
# dmsetup info -c $name
#done
#for i in `seq 1 $n`; do
# local name="${PREFIX}$pvname$i"
# dmsetup table $name
#done
echo $devs > DEVICES
echo "ok"
}
disable_dev( ) {
init_udev_transaction
for dev in " $@ " ; do
2012-02-23 17:11:07 +04:00
maj = $(( $( stat --printf= 0x%t $dev ) ))
min = $(( $( stat --printf= 0x%T $dev ) ))
echo " disabling device $dev ( $maj : $min ) "
dmsetup remove -f $dev || true
2012-02-24 03:58:42 +04:00
notify_lvmetad $maj :$min
2011-01-05 03:16:18 +03:00
done
finish_udev_transaction
}
enable_dev( ) {
init_udev_transaction
for dev in " $@ " ; do
local name = ` echo " $dev " | sed -e 's,.*/,,' `
2011-06-29 16:37:51 +04:00
dmsetup create -u TEST-$name $name $name .table || dmsetup load $name $name .table
2011-10-23 19:43:10 +04:00
# using device name (since device path does not exists yes with udev)
dmsetup resume $name
2012-02-24 03:58:42 +04:00
notify_lvmetad $dev
2011-01-05 03:16:18 +03:00
done
finish_udev_transaction
}
backup_dev( ) {
for dev in " $@ " ; do
dd if = $dev of = $dev .backup bs = 1024
done
}
restore_dev( ) {
for dev in " $@ " ; do
test -e $dev .backup || {
echo " Internal error: $dev not backed up, can't restore! "
exit 1
}
dd of = $dev if = $dev .backup bs = 1024
done
}
prepare_pvs( ) {
prepare_devs " $@ "
pvcreate -ff $devs
}
prepare_vg( ) {
vgremove -ff $vg >& /dev/null || true
teardown_devs
prepare_pvs " $@ "
vgcreate -c n $vg $devs
#pvs -v
}
lvmconf( ) {
if test -z " $LVM_TEST_LOCKING " ; then LVM_TEST_LOCKING = 1; fi
2011-06-28 04:11:46 +04:00
if test " $DM_DEV_DIR " = "/dev" ; then
2011-09-25 01:15:13 +04:00
LVM_VERIFY_UDEV = ${ LVM_VERIFY_UDEV :- 0 } ;
2011-06-28 04:11:46 +04:00
else
2011-09-25 01:15:13 +04:00
LVM_VERIFY_UDEV = ${ LVM_VERIFY_UDEV :- 1 } ;
2011-06-28 04:11:46 +04:00
fi
2011-01-05 03:16:18 +03:00
test -f CONFIG_VALUES || {
cat > CONFIG_VALUES <<-EOF
devices/dir = " $DM_DEV_DIR "
devices/scan = " $DM_DEV_DIR "
2011-12-12 04:01:12 +04:00
devices/filter = [ "a/dev\\\\/mirror/" , " a/dev\\\\/mapper\\\\/.*pv[0-9_]* $/ " , "r/.*/" ]
2011-01-05 03:16:18 +03:00
devices/cache_dir = " $TESTDIR /etc "
devices/sysfs_scan = 0
devices/default_data_alignment = 1
log/syslog = 0
log/indent = 1
log/level = 9
log/file = " $TESTDIR /debug.log "
log/overwrite = 1
log/activation = 1
backup/backup = 0
backup/archive = 0
global/abort_on_internal_errors = 1
2011-08-11 21:46:13 +04:00
global/detect_internal_vg_cache_corruption = 1
2011-01-05 03:16:18 +03:00
global/library_dir = " $TESTDIR /lib "
global/locking_dir = " $TESTDIR /var/lock/lvm "
global/locking_type= $LVM_TEST_LOCKING
global/si_unit_consistency = 1
global/fallback_to_local_locking = 0
2011-07-01 18:09:19 +04:00
activation/checks = 1
2011-01-05 03:16:18 +03:00
activation/udev_sync = 1
2011-06-28 04:38:26 +04:00
activation/udev_rules = 1
2011-09-25 01:15:13 +04:00
activation/verify_udev_operations = $LVM_VERIFY_UDEV
2011-01-05 03:16:18 +03:00
activation/polling_interval = 0
activation/snapshot_autoextend_percent = 50
activation/snapshot_autoextend_threshold = 50
2011-01-07 16:04:17 +03:00
activation/monitoring = 0
2011-01-05 03:16:18 +03:00
EOF
}
for v in " $@ " ; do
echo " $v " >> CONFIG_VALUES
done
rm -f CONFIG
for s in ` cat CONFIG_VALUES | cut -f1 -d/ | sort | uniq` ; do
echo " $s { " >> CONFIG
for k in ` grep ^$s / CONFIG_VALUES | cut -f1 -d= | sed -e 's, *$,,' | sort | uniq` ; do
grep " ^ $k " CONFIG_VALUES | tail -n 1 | sed -e " s,^ $s /, , " >> CONFIG
done
echo "}" >> CONFIG
echo >> CONFIG
done
mv -f CONFIG $TESTDIR /etc/lvm.conf
}
apitest( ) {
t = $1
shift
2011-01-07 16:03:04 +03:00
test -x $abs_top_builddir /test/api/$t .t || skip
2011-01-05 03:16:18 +03:00
$abs_top_builddir /test/api/$t .t " $@ "
}
api( ) {
2011-01-07 16:03:04 +03:00
test -x $abs_top_builddir /test/api/wrapper || skip
2011-01-05 03:16:18 +03:00
$abs_top_builddir /test/api/wrapper " $@ "
}
2011-01-28 19:10:21 +03:00
udev_wait( ) {
2011-03-02 02:44:07 +03:00
pgrep udev >/dev/null || return 0
which udevadm >/dev/null || return 0
2011-01-28 19:10:21 +03:00
if test -n " $1 " ; then
udevadm settle --exit-if-exists= $1
else
2011-07-08 14:05:43 +04:00
udevadm settle --timeout= 15
2011-01-28 19:10:21 +03:00
fi
}
2011-11-10 16:44:00 +04:00
#
# Check wheter kernel [dm module] target exist
# at least in expected version
#
# [dm-]target-name major minor revision
#
# i.e. dm_target_at_least dm-thin-pool 1 0
target_at_least( )
{
case " $1 " in
dm-*) modprobe " $1 " ; ;
esac
version = $( dmsetup targets 2>/dev/null | grep " ${ 1 ##dm- } " 2>/dev/null)
version = ${ version ##* v }
shift
major = $( echo $version | cut -d. -f1)
minor = $( echo $version | cut -d. -f2)
revision = $( echo $version | cut -d. -f3)
test -z " $1 " && return 0
test -z " $major " && return 1
test " $major " -gt " $1 " && return 0
test " $major " -lt " $1 " && return 1
test -z " $2 " && return 0
test -z " $minor " && return 1
test " $minor " -gt " $2 " && return 0
test " $minor " -lt " $2 " && return 1
test -z " $4 " && return 0
test -z " $revision " && return 1
test " $revision " -lt " $3 " && return 1
}
2011-01-05 03:16:18 +03:00
test -f DEVICES && devs = $( cat DEVICES)
test -f LOOP && LOOP = $( cat LOOP)
" $@ "