1# Put lvm-related utilities here. 2# This file is sourced from test-lib.sh. 3 4# Copyright (C) 2007, 2008 Red Hat, Inc. All rights reserved. 5# 6# This copyrighted material is made available to anyone wishing to use, 7# modify, copy, or redistribute it subject to the terms and conditions 8# of the GNU General Public License v.2. 9# 10# You should have received a copy of the GNU General Public License 11# along with this program; if not, write to the Free Software Foundation, 12# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 13 14export LVM_SUPPRESS_FD_WARNINGS=1 15 16ME=$(basename "$0") 17warn() { echo >&2 "$ME: $@"; } 18 19unsafe_losetup_() 20{ 21 f=$1 22 23 test -n "$G_dev_" \ 24 || error "Internal error: unsafe_losetup_ called before init_root_dir_" 25 26 # Iterate through $G_dev_/loop{,/}{0,1,2,3,4,5,6,7,8,9} 27 for slash in '' /; do 28 for i in 0 1 2 3 4 5 6 7 8 9; do 29 dev=$G_dev_/loop$slash$i 30 losetup $dev > /dev/null 2>&1 && continue; 31 losetup "$dev" "$f" > /dev/null && { echo "$dev"; return 0; } 32 break 33 done 34 done 35 36 return 1 37} 38 39loop_setup_() 40{ 41 file=$1 42 dd if=/dev/zero of="$file" bs=1M count=1 seek=1000 > /dev/null 2>&1 \ 43 || { warn "loop_setup_ failed: Unable to create tmp file $file"; return 1; } 44 45 # NOTE: this requires a new enough version of losetup 46 dev=$(unsafe_losetup_ "$file" 2>/dev/null) \ 47 || { warn "loop_setup_ failed: Unable to create loopback device"; return 1; } 48 49 echo "$dev" 50 return 0; 51} 52 53compare_two_fields_() 54{ 55 local cmd1=$1; 56 local obj1=$2; 57 local field1=$3; 58 local cmd2=$4; 59 local obj2=$5; 60 local field2=$6; 61 local val1; 62 local val2; 63 64 val1=$($cmd1 --noheadings -o $field1 $obj1) 65 val2=$($cmd2 --noheadings -o $field2 $obj2) 66if test "$verbose" = "t" 67then 68 echo "compare_two_fields_ $obj1($field1): $val1 $obj2($field2): $val2" 69fi 70 test $val1 = $val2 71} 72 73compare_vg_field_() 74{ 75 local vg1=$1; 76 local vg2=$2; 77 local field=$3; 78 local val1; 79 local val2; 80 81 val1=$(vgs --noheadings -o $field $vg1) 82 val2=$(vgs --noheadings -o $field $vg2) 83if test "$verbose" = "t" 84then 85 echo "compare_vg_field_ VG1: $val1 VG2: $val2" 86fi 87 test $val1 = $val2 88} 89 90check_vg_field_() 91{ 92 local vg=$1; 93 local field=$2; 94 local expected=$3; 95 local actual; 96 97 actual=$(vgs --noheadings -o $field $vg) 98if test "$verbose" = "t" 99then 100 echo "check_vg_field_ VG=$vg, field=$field, actual=$actual, expected=$expected" 101fi 102 test $actual = $expected 103} 104 105check_pv_field_() 106{ 107 local pv=$1; 108 local field=$2; 109 local expected=$3; 110 local actual; 111 112 actual=$(pvs --noheadings -o $field $pv) 113if test "$verbose" = "t" 114then 115 echo "check_pv_field_ PV=$pv, field=$field, actual=$actual, expected=$expected" 116fi 117 test $actual = $expected 118} 119 120check_lv_field_() 121{ 122 local lv=$1; 123 local field=$2; 124 local expected=$3; 125 local actual; 126 127 actual=$(lvs --noheadings -o $field $lv) 128if test "$verbose" = "t" 129then 130 echo "check_lv_field_ LV=$lv, field=$field, actual=$actual, expected=$expected" 131fi 132 test $actual = $expected 133} 134 135vg_validate_pvlv_counts_() 136{ 137 local local_vg=$1 138 local num_pvs=$2 139 local num_lvs=$3 140 local num_snaps=$4 141 142 check_vg_field_ $local_vg pv_count $num_pvs && 143 check_vg_field_ $local_vg lv_count $num_lvs && 144 check_vg_field_ $local_vg snap_count $num_snaps 145} 146 147dmsetup_has_dm_devdir_support_() 148{ 149 # Detect support for the envvar. If it's supported, the 150 # following command will fail with the expected diagnostic. 151 out=$(DM_DEV_DIR=j dmsetup version 2>&1) 152 test "$?:$out" = "1:Invalid DM_DEV_DIR envvar value." -o \ 153 "$?:$out" = "1:Invalid DM_DEV_DIR environment variable value." 154} 155 156# set up private /dev and /etc 157init_root_dir_() 158{ 159 test -n "$test_dir_rand_" \ 160 || error "Internal error: called init_root_dir_ before" \ 161 "defining \$test_dir_rand_" 162 163 # Define these two globals. 164 G_root_=$test_dir_rand_/root 165 G_dev_=$G_root_/dev 166 167 export LVM_SYSTEM_DIR=$G_root_/etc 168 export DM_DEV_DIR=$G_dev_ 169 170 # Only the first caller does anything. 171 mkdir -p $G_root_/etc $G_dev_ $G_dev_/mapper $G_root_/lib 172 for i in 0 1 2 3 4 5 6 7; do 173 mknod $G_root_/dev/loop$i b 7 $i 174 done 175 for i in $abs_top_builddir/dmeventd/mirror/*.so $abs_top_builddir/dmeventd/snapshot/*.so 176 do 177 # NOTE: This check is necessary because the loop above will give us the value 178 # "$abs_top_builddir/dmeventd/mirror/*.so" if no files ending in 'so' exist. 179 # This is the best way I could quickly determine to skip over this bogus value. 180 if [ -f $i ]; then 181 echo Setting up symlink from $i to $G_root_/lib 182 ln -s $i $G_root_/lib 183 fi 184 done 185 cat > $G_root_/etc/lvm.conf <<-EOF 186 devices { 187 dir = "$G_dev_" 188 scan = "$G_dev_" 189 filter = [ "a/loop/", "a/mirror/", "a/mapper/", "r/.*/" ] 190 cache_dir = "$G_root_/etc" 191 sysfs_scan = 0 192 } 193 log { 194 verbose = $verboselevel 195 syslog = 0 196 indent = 1 197 } 198 backup { 199 backup = 0 200 archive = 0 201 } 202 global { 203 library_dir = "$G_root_/lib" 204 } 205EOF 206} 207 208init_root_dir_ 209