2007-09-18 18:00:42 +04:00
# Put lvm-related utilities here.
# This file is sourced from test-lib.sh.
2008-06-06 17:48:57 +04:00
# Copyright (C) 2007, 2008 Red Hat, Inc. All rights reserved.
2007-09-18 18:02:22 +04: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
2007-09-18 18:00:42 +04:00
export LVM_SUPPRESS_FD_WARNINGS = 1
ME = $( basename " $0 " )
warn( ) { echo >& 2 " $ME : $@ " ; }
unsafe_losetup_( )
{
f = $1
2007-10-09 17:13:06 +04:00
test -n " $G_dev_ " \
|| error "Internal error: unsafe_losetup_ called before init_root_dir_"
2007-09-18 18:00:42 +04:00
2007-10-09 17:13:06 +04:00
# Iterate through $G_dev_/loop{,/}{0,1,2,3,4,5,6,7,8,9}
2007-09-18 18:00:42 +04:00
for slash in '' /; do
for i in 0 1 2 3 4 5 6 7 8 9; do
2007-10-09 17:13:06 +04:00
dev = $G_dev_ /loop$slash $i
2007-09-18 18:00:42 +04:00
losetup $dev > /dev/null 2>& 1 && continue ;
losetup " $dev " " $f " > /dev/null && { echo " $dev " ; return 0; }
break
done
done
return 1
}
loop_setup_( )
{
file = $1
dd if = /dev/zero of = " $file " bs = 1M count = 1 seek = 1000 > /dev/null 2>& 1 \
|| { warn " loop_setup_ failed: Unable to create tmp file $file " ; return 1; }
# NOTE: this requires a new enough version of losetup
dev = $( unsafe_losetup_ " $file " 2>/dev/null) \
|| { warn "loop_setup_ failed: Unable to create loopback device" ; return 1; }
echo " $dev "
return 0;
}
2007-09-22 01:14:25 +04:00
2009-05-08 10:10:45 +04:00
compare_two_fields_( )
{
local cmd1 = $1 ;
local obj1 = $2 ;
local field1 = $3 ;
local cmd2 = $4 ;
local obj2 = $5 ;
local field2 = $6 ;
local val1;
local val2;
val1 = $( $cmd1 --noheadings -o $field1 $obj1 )
val2 = $( $cmd2 --noheadings -o $field2 $obj2 )
if test " $verbose " = "t"
then
echo " compare_two_fields_ $obj1 ( $field1 ): $val1 $obj2 ( $field2 ): $val2 "
fi
test $val1 = $val2
}
2008-03-23 18:40:35 +03:00
compare_vg_field_( )
{
2008-06-25 20:51:26 +04:00
local vg1 = $1 ;
local vg2 = $2 ;
local field = $3 ;
local val1;
local val2;
val1 = $( vgs --noheadings -o $field $vg1 )
val2 = $( vgs --noheadings -o $field $vg2 )
2008-03-23 18:40:35 +03:00
if test " $verbose " = "t"
then
2008-06-25 20:51:26 +04:00
echo " compare_vg_field_ VG1: $val1 VG2: $val2 "
2008-03-23 18:40:35 +03:00
fi
2009-01-10 15:19:41 +03:00
test $val1 = $val2
2008-03-23 18:40:35 +03:00
}
2008-01-15 00:07:58 +03:00
check_vg_field_( )
{
2008-06-25 20:51:26 +04:00
local vg = $1 ;
local field = $2 ;
local expected = $3 ;
local actual;
actual = $( vgs --noheadings -o $field $vg )
2008-01-17 21:05:57 +03:00
if test " $verbose " = "t"
then
2008-06-25 20:51:26 +04:00
echo " check_vg_field_ VG= $vg , field= $field , actual= $actual , expected= $expected "
2008-01-17 21:05:57 +03:00
fi
2009-01-10 15:19:41 +03:00
test $actual = $expected
2008-01-15 00:07:58 +03:00
}
2008-01-17 21:05:57 +03:00
check_pv_field_( )
2007-09-22 01:14:25 +04:00
{
2008-06-25 20:51:26 +04:00
local pv = $1 ;
local field = $2 ;
local expected = $3 ;
local actual;
actual = $( pvs --noheadings -o $field $pv )
2008-01-17 21:05:57 +03:00
if test " $verbose " = "t"
then
2008-06-25 20:51:26 +04:00
echo " check_pv_field_ PV= $pv , field= $field , actual= $actual , expected= $expected "
2008-01-17 21:05:57 +03:00
fi
2009-01-10 15:19:41 +03:00
test $actual = $expected
2007-09-22 01:14:25 +04:00
}
2008-01-17 21:05:57 +03:00
check_lv_field_( )
2007-09-22 01:14:25 +04:00
{
2008-06-25 20:51:26 +04:00
local lv = $1 ;
local field = $2 ;
local expected = $3 ;
local actual;
actual = $( lvs --noheadings -o $field $lv )
2008-01-17 21:05:57 +03:00
if test " $verbose " = "t"
then
2008-06-25 20:51:26 +04:00
echo " check_lv_field_ LV= $lv , field= $field , actual= $actual , expected= $expected "
2008-01-17 21:05:57 +03:00
fi
2009-01-10 15:19:41 +03:00
test $actual = $expected
2007-09-22 01:14:25 +04:00
}
2007-10-09 17:13:06 +04:00
2008-04-11 18:06:16 +04:00
vg_validate_pvlv_counts_( )
{
local local_vg = $1
local num_pvs = $2
local num_lvs = $3
local num_snaps = $4
check_vg_field_ $local_vg pv_count $num_pvs &&
check_vg_field_ $local_vg lv_count $num_lvs &&
check_vg_field_ $local_vg snap_count $num_snaps
}
2007-10-09 17:13:06 +04:00
dmsetup_has_dm_devdir_support_( )
{
# Detect support for the envvar. If it's supported, the
# following command will fail with the expected diagnostic.
out = $( DM_DEV_DIR = j dmsetup version 2>& 1)
2008-01-17 00:21:22 +03:00
test " $? : $out " = "1:Invalid DM_DEV_DIR envvar value." -o \
" $? : $out " = "1:Invalid DM_DEV_DIR environment variable value."
2007-10-09 17:13:06 +04:00
}
# set up private /dev and /etc
init_root_dir_( )
{
test -n " $test_dir_rand_ " \
|| error "Internal error: called init_root_dir_ before" \
2008-06-06 17:48:57 +04:00
"defining \$test_dir_rand_"
2007-10-09 17:13:06 +04:00
# Define these two globals.
G_root_ = $test_dir_rand_ /root
G_dev_ = $G_root_ /dev
export LVM_SYSTEM_DIR = $G_root_ /etc
export DM_DEV_DIR = $G_dev_
# Only the first caller does anything.
2008-08-05 15:39:54 +04:00
mkdir -p $G_root_ /etc $G_dev_ $G_dev_ /mapper $G_root_ /lib
2007-10-09 17:13:06 +04:00
for i in 0 1 2 3 4 5 6 7; do
mknod $G_root_ /dev/loop$i b 7 $i
done
2008-08-21 18:33:48 +04:00
for i in $abs_top_builddir /dmeventd/mirror/*.so $abs_top_builddir /dmeventd/snapshot/*.so
do
# NOTE: This check is necessary because the loop above will give us the value
# "$abs_top_builddir/dmeventd/mirror/*.so" if no files ending in 'so' exist.
# This is the best way I could quickly determine to skip over this bogus value.
if [ -f $i ] ; then
echo Setting up symlink from $i to $G_root_ /lib
ln -s $i $G_root_ /lib
fi
done
2007-10-09 17:13:06 +04:00
cat > $G_root_ /etc/lvm.conf <<-EOF
devices {
dir = " $G_dev_ "
scan = " $G_dev_ "
filter = [ "a/loop/" , "a/mirror/" , "a/mapper/" , "r/.*/" ]
cache_dir = " $G_root_ /etc "
sysfs_scan = 0
}
2008-08-05 15:39:54 +04:00
log {
verbose = $verboselevel
syslog = 0
indent = 1
}
backup {
backup = 0
archive = 0
}
global {
library_dir = " $G_root_ /lib "
}
2007-10-09 17:13:06 +04:00
EOF
}
2008-12-10 19:15:41 +03:00
init_root_dir_