xref: /netbsd-src/external/gpl2/lvm2/dist/test/lvm-utils.sh (revision 7c604eea85b4f330dc75ffe65e947f4d73758aa0)
156a34939Shaad# Put lvm-related utilities here.
256a34939Shaad# This file is sourced from test-lib.sh.
356a34939Shaad
456a34939Shaad# Copyright (C) 2007, 2008 Red Hat, Inc. All rights reserved.
556a34939Shaad#
656a34939Shaad# This copyrighted material is made available to anyone wishing to use,
756a34939Shaad# modify, copy, or redistribute it subject to the terms and conditions
856a34939Shaad# of the GNU General Public License v.2.
956a34939Shaad#
1056a34939Shaad# You should have received a copy of the GNU General Public License
1156a34939Shaad# along with this program; if not, write to the Free Software Foundation,
1256a34939Shaad# Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1356a34939Shaad
1456a34939Shaadexport LVM_SUPPRESS_FD_WARNINGS=1
1556a34939Shaad
1656a34939ShaadME=$(basename "$0")
1756a34939Shaadwarn() { echo >&2 "$ME: $@"; }
1856a34939Shaad
1956a34939Shaadunsafe_losetup_()
2056a34939Shaad{
2156a34939Shaad  f=$1
2256a34939Shaad
2356a34939Shaad  test -n "$G_dev_" \
2456a34939Shaad    || error "Internal error: unsafe_losetup_ called before init_root_dir_"
2556a34939Shaad
2656a34939Shaad  # Iterate through $G_dev_/loop{,/}{0,1,2,3,4,5,6,7,8,9}
2756a34939Shaad  for slash in '' /; do
2856a34939Shaad    for i in 0 1 2 3 4 5 6 7 8 9; do
2956a34939Shaad      dev=$G_dev_/loop$slash$i
3056a34939Shaad      losetup $dev > /dev/null 2>&1 && continue;
3156a34939Shaad      losetup "$dev" "$f" > /dev/null && { echo "$dev"; return 0; }
3256a34939Shaad      break
3356a34939Shaad    done
3456a34939Shaad  done
3556a34939Shaad
3656a34939Shaad  return 1
3756a34939Shaad}
3856a34939Shaad
3956a34939Shaadloop_setup_()
4056a34939Shaad{
4156a34939Shaad  file=$1
4256a34939Shaad  dd if=/dev/zero of="$file" bs=1M count=1 seek=1000 > /dev/null 2>&1 \
4356a34939Shaad    || { warn "loop_setup_ failed: Unable to create tmp file $file"; return 1; }
4456a34939Shaad
4556a34939Shaad  # NOTE: this requires a new enough version of losetup
4656a34939Shaad  dev=$(unsafe_losetup_ "$file" 2>/dev/null) \
4756a34939Shaad    || { warn "loop_setup_ failed: Unable to create loopback device"; return 1; }
4856a34939Shaad
4956a34939Shaad  echo "$dev"
5056a34939Shaad  return 0;
5156a34939Shaad}
5256a34939Shaad
53*7c604eeaShaadcompare_two_fields_()
54*7c604eeaShaad{
55*7c604eeaShaad    local cmd1=$1;
56*7c604eeaShaad    local obj1=$2;
57*7c604eeaShaad    local field1=$3;
58*7c604eeaShaad    local cmd2=$4;
59*7c604eeaShaad    local obj2=$5;
60*7c604eeaShaad    local field2=$6;
61*7c604eeaShaad    local val1;
62*7c604eeaShaad    local val2;
63*7c604eeaShaad
64*7c604eeaShaad    val1=$($cmd1 --noheadings -o $field1 $obj1)
65*7c604eeaShaad    val2=$($cmd2 --noheadings -o $field2 $obj2)
66*7c604eeaShaadif test "$verbose" = "t"
67*7c604eeaShaadthen
68*7c604eeaShaad  echo "compare_two_fields_ $obj1($field1): $val1 $obj2($field2): $val2"
69*7c604eeaShaadfi
70*7c604eeaShaad  test $val1 = $val2
71*7c604eeaShaad}
72*7c604eeaShaad
7356a34939Shaadcompare_vg_field_()
7456a34939Shaad{
7556a34939Shaad    local vg1=$1;
7656a34939Shaad    local vg2=$2;
7756a34939Shaad    local field=$3;
7856a34939Shaad    local val1;
7956a34939Shaad    local val2;
8056a34939Shaad
8156a34939Shaad    val1=$(vgs --noheadings -o $field $vg1)
8256a34939Shaad    val2=$(vgs --noheadings -o $field $vg2)
8356a34939Shaadif test "$verbose" = "t"
8456a34939Shaadthen
8556a34939Shaad  echo "compare_vg_field_ VG1: $val1 VG2: $val2"
8656a34939Shaadfi
87bec4d750Shaad  test $val1 = $val2
8856a34939Shaad}
8956a34939Shaad
9056a34939Shaadcheck_vg_field_()
9156a34939Shaad{
9256a34939Shaad    local vg=$1;
9356a34939Shaad    local field=$2;
9456a34939Shaad    local expected=$3;
9556a34939Shaad    local actual;
9656a34939Shaad
9756a34939Shaad    actual=$(vgs --noheadings -o $field $vg)
9856a34939Shaadif test "$verbose" = "t"
9956a34939Shaadthen
10056a34939Shaad  echo "check_vg_field_ VG=$vg, field=$field, actual=$actual, expected=$expected"
10156a34939Shaadfi
102bec4d750Shaad  test $actual = $expected
10356a34939Shaad}
10456a34939Shaad
10556a34939Shaadcheck_pv_field_()
10656a34939Shaad{
10756a34939Shaad    local pv=$1;
10856a34939Shaad    local field=$2;
10956a34939Shaad    local expected=$3;
11056a34939Shaad    local actual;
11156a34939Shaad
11256a34939Shaad    actual=$(pvs --noheadings -o $field $pv)
11356a34939Shaadif test "$verbose" = "t"
11456a34939Shaadthen
11556a34939Shaad  echo "check_pv_field_ PV=$pv, field=$field, actual=$actual, expected=$expected"
11656a34939Shaadfi
117bec4d750Shaad    test $actual = $expected
11856a34939Shaad}
11956a34939Shaad
12056a34939Shaadcheck_lv_field_()
12156a34939Shaad{
12256a34939Shaad    local lv=$1;
12356a34939Shaad    local field=$2;
12456a34939Shaad    local expected=$3;
12556a34939Shaad    local actual;
12656a34939Shaad
12756a34939Shaad    actual=$(lvs --noheadings -o $field $lv)
12856a34939Shaadif test "$verbose" = "t"
12956a34939Shaadthen
13056a34939Shaad  echo "check_lv_field_ LV=$lv, field=$field, actual=$actual, expected=$expected"
13156a34939Shaadfi
132bec4d750Shaad  test $actual = $expected
13356a34939Shaad}
13456a34939Shaad
13556a34939Shaadvg_validate_pvlv_counts_()
13656a34939Shaad{
13756a34939Shaad	local local_vg=$1
13856a34939Shaad	local num_pvs=$2
13956a34939Shaad	local num_lvs=$3
14056a34939Shaad	local num_snaps=$4
14156a34939Shaad
14256a34939Shaad	check_vg_field_ $local_vg pv_count $num_pvs &&
14356a34939Shaad	check_vg_field_ $local_vg lv_count $num_lvs &&
14456a34939Shaad	check_vg_field_ $local_vg snap_count $num_snaps
14556a34939Shaad}
14656a34939Shaad
14756a34939Shaaddmsetup_has_dm_devdir_support_()
14856a34939Shaad{
14956a34939Shaad  # Detect support for the envvar.  If it's supported, the
15056a34939Shaad  # following command will fail with the expected diagnostic.
15156a34939Shaad  out=$(DM_DEV_DIR=j dmsetup version 2>&1)
15256a34939Shaad  test "$?:$out" = "1:Invalid DM_DEV_DIR envvar value." -o \
15356a34939Shaad       "$?:$out" = "1:Invalid DM_DEV_DIR environment variable value."
15456a34939Shaad}
15556a34939Shaad
15656a34939Shaad# set up private /dev and /etc
15756a34939Shaadinit_root_dir_()
15856a34939Shaad{
15956a34939Shaad  test -n "$test_dir_rand_" \
16056a34939Shaad    || error "Internal error: called init_root_dir_ before" \
16156a34939Shaad      "defining \$test_dir_rand_"
16256a34939Shaad
16356a34939Shaad  # Define these two globals.
16456a34939Shaad  G_root_=$test_dir_rand_/root
16556a34939Shaad  G_dev_=$G_root_/dev
16656a34939Shaad
16756a34939Shaad  export LVM_SYSTEM_DIR=$G_root_/etc
16856a34939Shaad  export DM_DEV_DIR=$G_dev_
16956a34939Shaad
17056a34939Shaad  # Only the first caller does anything.
17156a34939Shaad  mkdir -p $G_root_/etc $G_dev_ $G_dev_/mapper $G_root_/lib
17256a34939Shaad  for i in 0 1 2 3 4 5 6 7; do
17356a34939Shaad    mknod $G_root_/dev/loop$i b 7 $i
17456a34939Shaad  done
17556a34939Shaad  for i in $abs_top_builddir/dmeventd/mirror/*.so $abs_top_builddir/dmeventd/snapshot/*.so
17656a34939Shaad  do
17756a34939Shaad    # NOTE: This check is necessary because the loop above will give us the value
17856a34939Shaad    # "$abs_top_builddir/dmeventd/mirror/*.so" if no files ending in 'so' exist.
17956a34939Shaad    # This is the best way I could quickly determine to skip over this bogus value.
18056a34939Shaad    if [ -f $i ]; then
18156a34939Shaad      echo Setting up symlink from $i to $G_root_/lib
18256a34939Shaad      ln -s $i $G_root_/lib
18356a34939Shaad    fi
18456a34939Shaad  done
18556a34939Shaad  cat > $G_root_/etc/lvm.conf <<-EOF
18656a34939Shaad  devices {
18756a34939Shaad    dir = "$G_dev_"
18856a34939Shaad    scan = "$G_dev_"
18956a34939Shaad    filter = [ "a/loop/", "a/mirror/", "a/mapper/", "r/.*/" ]
19056a34939Shaad    cache_dir = "$G_root_/etc"
19156a34939Shaad    sysfs_scan = 0
19256a34939Shaad  }
19356a34939Shaad  log {
19456a34939Shaad    verbose = $verboselevel
19556a34939Shaad    syslog = 0
19656a34939Shaad    indent = 1
19756a34939Shaad  }
19856a34939Shaad  backup {
19956a34939Shaad    backup = 0
20056a34939Shaad    archive = 0
20156a34939Shaad  }
20256a34939Shaad  global {
20356a34939Shaad    library_dir = "$G_root_/lib"
20456a34939Shaad  }
20556a34939ShaadEOF
20656a34939Shaad}
20756a34939Shaad
20856a34939Shaadinit_root_dir_
209