2008-09-29 19:59:19 +04:00
# Copyright (C) 2008 Red Hat, Inc. All rights reserved.
#
# 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
aux( ) {
2008-10-14 23:48:01 +04:00
# use just "$@" for verbose operation
" $@ " > /dev/null 2> /dev/null
2008-10-06 20:55:30 +04:00
#"$@"
2008-09-29 19:59:19 +04:00
}
2008-10-06 20:55:30 +04:00
STACKTRACE( ) {
trap - ERR;
i = 0;
while FUNC = ${ FUNCNAME [ $i ] } ; test " $FUNC " != "main" ; do
echo " $i ${ FUNC } () called from ${ BASH_SOURCE [ $i ] } : ${ BASH_LINENO [ $i ] } "
i = $(( $i + 1 )) ;
done
2010-03-17 17:55:28 +03:00
# Attempt to get a stacktrace if a core file exists
# and the lvm binary was built with debugging
TEST_LVM_BINARY = $( dirname $( which lvm) ) /../../tools/lvm
TEST_LVM_CORE = ` ls core* | head -1`
GDB_BINARY = ` which gdb`
READELF_BINARY = ` which readelf`
if [ -n " $TEST_LVM_CORE " -a -n " $GDB_BINARY " -a -n " $READELF_BINARY " ] ; then
if $READELF_BINARY -S $TEST_LVM_BINARY 2>& 1 | grep -q .debug_info; then
echo bt full > gdb_commands.txt
echo l >> gdb_commands.txt
echo quit >> gdb_commands.txt
$GDB_BINARY -batch -c $TEST_LVM_CORE -x gdb_commands.txt $TEST_LVM_BINARY
fi
fi
2010-02-15 19:30:13 +03:00
}
init_udev_transaction( ) {
if test " $DM_UDEV_SYNCHRONISATION " = 1; then
2010-03-23 17:47:35 +03:00
COOKIE = $( dmsetup udevcreatecookie)
# Cookie is not generated if udev is not running!
if test -n " $COOKIE " ; then
export DM_UDEV_COOKIE = $COOKIE
fi
2010-02-15 19:30:13 +03:00
fi
}
finish_udev_transaction( ) {
2010-03-23 17:47:35 +03:00
if test " $DM_UDEV_SYNCHRONISATION " = 1 -a -n " $DM_UDEV_COOKIE " ; then
2010-02-15 19:30:13 +03:00
dmsetup udevreleasecookie
unset DM_UDEV_COOKIE
fi
}
2008-10-06 20:55:30 +04:00
2010-03-18 12:19:30 +03:00
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. "
2010-03-18 12:27:39 +03:00
exit 200
2010-03-18 12:19:30 +03:00
fi
# skip if we don't have our own clvmd...
( which clvmd | grep $abs_builddir ) || exit 200
2010-04-07 18:46:26 +04:00
trap_teardown
2010-03-18 12:19:30 +03:00
clvmd -Isinglenode -d 1 &
LOCAL_CLVMD = " $! "
}
2010-04-07 18:46:26 +04:00
prepare_testroot( ) {
2010-04-08 01:19:20 +04:00
OLDPWD = "`pwd`"
2010-04-07 18:46:26 +04:00
PREFIX = " LVMTEST $$ "
trap_teardown
TESTDIR = $( $abs_srcdir /mkdtemp ${ LVM_TEST_DIR - $( pwd ) } $PREFIX .XXXXXXXXXX) \
|| { echo " failed to create temporary directory in $test_dir_ " ; exit 1; }
export LVM_SYSTEM_DIR = $TESTDIR /etc
export DM_DEV_DIR = $TESTDIR /dev
mkdir $LVM_SYSTEM_DIR $DM_DEV_DIR $DM_DEV_DIR /mapper $TESTDIR /lib
2010-04-08 01:38:01 +04:00
cd $TESTDIR
2010-04-07 18:46:26 +04:00
for i in ` find $abs_top_builddir /daemons/dmeventd/plugins/ -name \* .so` ; do
echo Setting up symlink from $i to $TESTDIR /lib
ln -s $i $TESTDIR /lib
done
}
2008-09-29 19:59:19 +04:00
teardown( ) {
2008-10-14 23:48:01 +04:00
echo $LOOP
echo $PREFIX
2010-03-18 12:19:30 +03:00
test -n " $LOCAL_CLVMD " && kill -9 " $LOCAL_CLVMD "
2008-10-14 23:48:01 +04:00
test -n " $PREFIX " && {
2010-04-07 18:46:26 +04:00
rm -rf $TESTDIR /dev/$PREFIX *
2010-02-15 19:30:13 +03:00
init_udev_transaction
2008-10-14 23:48:01 +04:00
while dmsetup table | grep -q ^$PREFIX ; do
2010-04-07 18:46:26 +04:00
for s in ` dmsetup info -c -o name --noheading | grep ^$PREFIX ` ; do
dmsetup remove $s >& /dev/null || true
2008-10-14 23:48:01 +04:00
done
done
2010-02-15 19:30:13 +03:00
finish_udev_transaction
2008-10-14 23:48:01 +04:00
}
2008-09-29 19:59:19 +04:00
2010-01-19 19:44:57 +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 [ -n " $SCSI_DEBUG_DEV " ] ; then
modprobe -r scsi_debug
else
test -n " $LOOP " && losetup -d $LOOP
test -n " $LOOPFILE " && rm -f $LOOPFILE
fi
unset devs # devs is set in prepare_devs()
2010-04-07 18:46:26 +04:00
test -n " $TESTDIR " && {
2010-04-08 01:19:20 +04:00
cd $OLDPWD
2010-04-07 18:46:26 +04:00
rm -rf $TESTDIR || echo BLA
}
2009-01-09 13:16:57 +03:00
}
2008-09-29 19:59:19 +04:00
2010-04-07 18:46:26 +04:00
trap_teardown( ) {
trap 'set +vex; STACKTRACE; set -vex' ERR
trap 'aux teardown' EXIT # don't forget to clean up
2008-09-29 19:59:19 +04:00
}
make_ioerror( ) {
2008-10-14 23:48:01 +04:00
echo 0 10000000 error | dmsetup create ioerror
2010-04-07 18:46:26 +04:00
ln -s $DM_DEV_DIR /mapper/ioerror $DM_DEV_DIR /ioerror
2008-09-29 19:59:19 +04:00
}
prepare_loop( ) {
2008-10-14 23:48:01 +04:00
size = $1
test -n " $size " || size = 32
2008-09-29 19:59:19 +04:00
2010-04-07 18:46:26 +04:00
test -z " $LOOP "
test -n " $DM_DEV_DIR "
trap_teardown
for i in 0 1 2 3 4 5 6 7; do
mknod $DM_DEV_DIR /loop$i b 7 $i
done
2008-10-06 20:55:30 +04:00
LOOPFILE = " $PWD /test.img "
dd if = /dev/zero of = " $LOOPFILE " bs = $(( 1024 * 1024 )) count = 1 seek = $(( $size - 1 ))
if LOOP = ` losetup -s -f " $LOOPFILE " 2>/dev/null` ; then
return 0
elif LOOP = ` losetup -f` && losetup $LOOP " $LOOPFILE " ; then
# no -s support
return 0
else
# no -f support
2010-04-07 18:46:26 +04:00
# Iterate through $DM_DEV_DIR/loop{,/}{0,1,2,3,4,5,6,7}
2008-10-06 20:55:30 +04:00
for slash in '' /; do
2008-12-05 08:03:23 +03:00
for i in 0 1 2 3 4 5 6 7; do
2010-04-07 18:46:26 +04:00
local dev = $DM_DEV_DIR /loop$slash $i
2008-10-06 20:55:30 +04:00
! losetup $dev >/dev/null 2>& 1 || continue
# got a free
losetup " $dev " " $LOOPFILE "
LOOP = $dev
2008-12-05 08:03:23 +03:00
break
2008-10-06 20:55:30 +04:00
done
2008-12-05 08:03:23 +03:00
if [ -n " $LOOP " ] ; then
break
fi
2008-10-06 20:55:30 +04:00
done
2008-11-04 17:38:53 +03:00
test -n " $LOOP " # confirm or fail
return 0
2008-10-06 20:55:30 +04:00
fi
2008-11-04 17:38:53 +03:00
exit 1 # should not happen
2008-09-29 19:59:19 +04:00
}
2010-01-19 19:44:57 +03:00
get_sd_devs_( )
{
# prepare_scsi_debug_dev() requires the ability to lookup
# the scsi_debug created SCSI device in /dev/
local _devs = $( lvmdiskscan --config 'devices { filter = [ "a|/dev/sd.*|", "r|.*|" ] scan = "/dev/" }' | grep /dev/sd | awk '{ print $1 }' )
echo $_devs
}
# 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 -n " $SCSI_DEBUG_DEV " && return 0
2010-04-07 18:46:26 +04:00
trap_teardown
2010-01-19 19:44:57 +03:00
# Skip test if awk isn't available (required for get_sd_devs_)
which awk || exit 200
# Skip test if scsi_debug module is unavailable or is already in use
modinfo scsi_debug || exit 200
lsmod | grep -q scsi_debug && exit 200
# Create the scsi_debug device and determine the new scsi device's name
local devs_before = ` get_sd_devs_`
# NOTE: it will _never_ make sense to pass num_tgts param;
# last param wins.. so num_tgts=1 is imposed
2010-03-28 19:52:04 +04:00
modprobe scsi_debug dev_size_mb = $DEV_SIZE $SCSI_DEBUG_PARAMS num_tgts = 1 || exit 200
2010-01-19 19:44:57 +03:00
sleep 2 # allow for async Linux SCSI device registration
local devs_after = ` get_sd_devs_`
for dev1 in $devs_after ; do
FOUND = 0
for dev2 in $devs_before ; do
if [ " $dev1 " = " $dev2 " ] ; then
FOUND = 1
break
fi
done
if [ $FOUND -eq 0 ] ; then
2010-04-07 18:46:26 +04:00
# Create symlink to scsi_debug device in $DM_DEV_DIR
SCSI_DEBUG_DEV = $DM_DEV_DIR /$( basename $dev1 )
2010-01-19 19:44:57 +03:00
# Setting $LOOP provides means for prepare_devs() override
LOOP = $SCSI_DEBUG_DEV
ln -snf $dev1 $SCSI_DEBUG_DEV
return 0
fi
done
exit 1 # should not happen
}
cleanup_scsi_debug_dev( )
{
aux teardown
unset SCSI_DEBUG_DEV
}
2008-09-29 19:59:19 +04:00
prepare_devs( ) {
2008-10-14 23:48:01 +04:00
local n = " $1 "
test -z " $n " && n = 3
local devsize = " $2 "
test -z " $devsize " && devsize = 33
2009-05-13 23:18:47 +04:00
local pvname = " $3 "
test -z " $pvname " && pvname = "pv"
2008-10-14 23:48:01 +04:00
prepare_loop $(( $n * $devsize ))
2008-12-05 08:03:23 +03:00
if ! loopsz = ` blockdev --getsz $LOOP 2>/dev/null` ; then
loopsz = ` blockdev --getsize $LOOP 2>/dev/null`
fi
2008-10-14 23:48:01 +04:00
local size = $(( $loopsz / $n ))
2010-02-15 19:30:13 +03:00
init_udev_transaction
2008-10-14 23:48:01 +04:00
for i in ` seq 1 $n ` ; do
2009-05-13 23:18:47 +04:00
local name = " ${ PREFIX } $pvname $i "
2010-04-07 18:46:26 +04:00
local dev = " $DM_DEV_DIR /mapper/ $name "
2008-10-14 23:48:01 +04:00
eval " dev $i = $dev "
devs = " $devs $dev "
echo 0 $size linear $LOOP $(( ( $i - 1 ) * $size )) > $name .table
dmsetup create $name $name .table
done
2010-02-15 19:30:13 +03:00
finish_udev_transaction
2008-09-29 19:59:19 +04:00
2010-04-06 21:36:41 +04:00
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
2008-09-29 19:59:19 +04:00
# set up some default names
2008-10-14 23:48:01 +04:00
vg = ${ PREFIX } vg
vg1 = ${ PREFIX } vg1
vg2 = ${ PREFIX } vg2
lv = LV
lv1 = LV1
lv2 = LV2
lv3 = LV3
2009-05-14 01:29:10 +04:00
lv4 = LV4
2008-09-29 19:59:19 +04:00
}
disable_dev( ) {
2010-02-15 19:30:13 +03:00
init_udev_transaction
2008-10-14 23:48:01 +04:00
for dev in " $@ " ; do
2008-09-29 19:59:19 +04:00
# first we make the device inaccessible
2008-10-14 23:48:01 +04:00
echo 0 10000000 error | dmsetup load $dev
dmsetup resume $dev
2008-09-29 19:59:19 +04:00
# now let's try to get rid of it if it's unused
#dmsetup remove $dev
2008-10-14 23:48:01 +04:00
done
2010-02-15 19:30:13 +03:00
finish_udev_transaction
2008-09-29 19:59:19 +04:00
}
enable_dev( ) {
2010-02-15 19:30:13 +03:00
init_udev_transaction
2008-10-14 23:48:01 +04:00
for dev in " $@ " ; do
local name = ` echo " $dev " | sed -e 's,.*/,,' `
dmsetup create $name $name .table || dmsetup load $name $name .table
dmsetup resume $dev
done
2010-02-15 19:30:13 +03:00
finish_udev_transaction
2008-09-29 19:59:19 +04:00
}
2009-01-09 13:16:57 +03:00
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
}
2008-09-29 19:59:19 +04:00
prepare_pvs( ) {
2008-10-14 23:48:01 +04:00
prepare_devs " $@ "
pvcreate $devs
2008-09-29 19:59:19 +04:00
}
prepare_vg( ) {
2008-10-14 23:48:01 +04:00
prepare_pvs " $@ "
2009-12-28 21:33:04 +03:00
vgcreate -c n $vg $devs
2010-04-06 18:25:07 +04:00
pvs -v
2008-09-29 19:59:19 +04:00
}
prepare_lvmconf( ) {
2009-08-03 01:45:45 +04:00
local filter = " $1 "
test -z " $filter " && \
filter = '[ "a/dev\/mirror/", "a/dev\/mapper\/.*pv[0-9_]*$/", "r/.*/" ]'
2010-03-18 12:19:30 +03:00
locktype =
if test -n " $LVM_TEST_LOCKING " ; then locktype = " locking_type = $LVM_TEST_LOCKING " ; fi
2010-04-07 18:46:26 +04:00
cat > $TESTDIR /etc/lvm.conf <<-EOF
2010-04-01 02:18:17 +04:00
$LVM_TEST_CONFIG
2008-09-29 19:59:19 +04:00
devices {
2010-04-07 18:46:26 +04:00
dir = " $DM_DEV_DIR "
scan = " $DM_DEV_DIR "
2009-08-03 01:45:45 +04:00
filter = $filter
2010-04-07 18:46:26 +04:00
cache_dir = " $TESTDIR /etc "
2008-09-29 19:59:19 +04:00
sysfs_scan = 0
}
log {
syslog = 0
indent = 1
}
backup {
backup = 0
archive = 0
}
global {
2009-11-30 20:17:11 +03:00
abort_on_internal_errors = 1
2010-04-07 18:46:26 +04:00
library_dir = " $TESTDIR /lib "
locking_dir = " $TESTDIR /var/lock/lvm "
2010-03-18 12:19:30 +03:00
$locktype
2008-09-29 19:59:19 +04:00
}
2010-01-11 18:43:19 +03:00
activation {
udev_sync = 1
udev_rules = 1
}
2008-09-29 19:59:19 +04:00
EOF
2010-04-07 18:46:26 +04:00
cat $TESTDIR /etc/lvm.conf
2008-09-29 19:59:19 +04:00
}
2010-04-07 18:46:26 +04:00
prepare( ) {
2010-04-07 20:04:22 +04:00
ulimit -c unlimited
2010-04-07 18:46:26 +04:00
prepare_testroot
prepare_lvmconf
prepare_clvmd
}
LANG = C
LC_ALL = C
TZ = UTC
unset CDPATH
2008-09-29 19:59:19 +04:00
2010-04-07 18:46:26 +04:00
. ./init.sh || { echo >& 2 you must run make first; exit 1; }
2010-04-08 01:38:01 +04:00
. ./lvm-utils.sh
2010-04-07 18:46:26 +04:00
set -vexE -o pipefail
aux prepare