Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / test / lvm-utils.sh
blob5ad8872a4aab0cefebe3789adfa2e851f85508e9
1 # Put lvm-related utilities here.
2 # This file is sourced from test-lib.sh.
4 # Copyright (C) 2007, 2008 Red Hat, Inc. All rights reserved.
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.
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
14 export LVM_SUPPRESS_FD_WARNINGS=1
16 ME=$(basename "$0")
17 warn() { echo >&2 "$ME: $@"; }
19 unsafe_losetup_()
21 f=$1
23 test -n "$G_dev_" \
24 || error "Internal error: unsafe_losetup_ called before init_root_dir_"
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
36 return 1
39 loop_setup_()
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; }
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; }
49 echo "$dev"
50 return 0;
53 compare_two_fields_()
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;
64 val1=$($cmd1 --noheadings -o $field1 $obj1)
65 val2=$($cmd2 --noheadings -o $field2 $obj2)
66 if test "$verbose" = "t"
67 then
68 echo "compare_two_fields_ $obj1($field1): $val1 $obj2($field2): $val2"
70 test $val1 = $val2
73 compare_vg_field_()
75 local vg1=$1;
76 local vg2=$2;
77 local field=$3;
78 local val1;
79 local val2;
81 val1=$(vgs --noheadings -o $field $vg1)
82 val2=$(vgs --noheadings -o $field $vg2)
83 if test "$verbose" = "t"
84 then
85 echo "compare_vg_field_ VG1: $val1 VG2: $val2"
87 test $val1 = $val2
90 check_vg_field_()
92 local vg=$1;
93 local field=$2;
94 local expected=$3;
95 local actual;
97 actual=$(vgs --noheadings -o $field $vg)
98 if test "$verbose" = "t"
99 then
100 echo "check_vg_field_ VG=$vg, field=$field, actual=$actual, expected=$expected"
102 test $actual = $expected
105 check_pv_field_()
107 local pv=$1;
108 local field=$2;
109 local expected=$3;
110 local actual;
112 actual=$(pvs --noheadings -o $field $pv)
113 if test "$verbose" = "t"
114 then
115 echo "check_pv_field_ PV=$pv, field=$field, actual=$actual, expected=$expected"
117 test $actual = $expected
120 check_lv_field_()
122 local lv=$1;
123 local field=$2;
124 local expected=$3;
125 local actual;
127 actual=$(lvs --noheadings -o $field $lv)
128 if test "$verbose" = "t"
129 then
130 echo "check_lv_field_ LV=$lv, field=$field, actual=$actual, expected=$expected"
132 test $actual = $expected
135 vg_validate_pvlv_counts_()
137 local local_vg=$1
138 local num_pvs=$2
139 local num_lvs=$3
140 local num_snaps=$4
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
147 dmsetup_has_dm_devdir_support_()
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."
156 # set up private /dev and /etc
157 init_root_dir_()
159 test -n "$test_dir_rand_" \
160 || error "Internal error: called init_root_dir_ before" \
161 "defining \$test_dir_rand_"
163 # Define these two globals.
164 G_root_=$test_dir_rand_/root
165 G_dev_=$G_root_/dev
167 export LVM_SYSTEM_DIR=$G_root_/etc
168 export DM_DEV_DIR=$G_dev_
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
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
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
193 log {
194 verbose = $verboselevel
195 syslog = 0
196 indent = 1
198 backup {
199 backup = 0
200 archive = 0
202 global {
203 library_dir = "$G_root_/lib"
208 init_root_dir_