Tizen 2.1 base
[external/device-mapper.git] / test / lvm-utils.sh
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
14 export LVM_SUPPRESS_FD_WARNINGS=1
15
16 ME=$(basename "$0")
17 warn() { echo >&2 "$ME: $@"; }
18
19 trim()
20 {
21     trimmed=${1%% }
22     trimmed=${trimmed## }
23
24     echo "$trimmed"
25 }
26
27 compare_two_fields_()
28 {
29     local cmd1=$1;
30     local obj1=$2;
31     local field1=$3;
32     local cmd2=$4;
33     local obj2=$5;
34     local field2=$6;
35     local val1;
36     local val2;
37
38     val1=$($cmd1 --noheadings -o $field1 $obj1)
39     val2=$($cmd2 --noheadings -o $field2 $obj2)
40 if test "$verbose" = "t"
41 then
42   echo "compare_two_fields_ $obj1($field1): $val1 $obj2($field2): $val2"
43 fi
44   test "$val1" = "$val2"
45 }
46
47 compare_vg_field_()
48 {
49     local vg1=$1;
50     local vg2=$2;
51     local field=$3;
52     local val1;
53     local val2;
54
55     val1=$(vgs --noheadings -o $field $vg1)
56     val2=$(vgs --noheadings -o $field $vg2)
57 if test "$verbose" = "t"
58 then
59   echo "compare_vg_field_ VG1: $val1 VG2: $val2"
60 fi
61   test "$val1" = "$val2"
62 }
63
64
65 get_pv_field() {
66         local pv=$1
67         local field=$2
68         local value
69         pvs --noheading -o $field $pv | sed 's/^ *//'
70 }
71
72 get_vg_field() {
73         local vg=$1
74         local field=$2
75         local value
76         vgs --noheading -o $field $vg | sed 's/^ *//'
77 }
78
79 get_lv_field() {
80         local lv=$1
81         local field=$2
82         local value
83         lvs --noheading -o $field $lv | sed 's/^ *//'
84 }
85
86 check_vg_field_()
87 {
88     local vg=$1;
89     local field=$2;
90     local expected=$3;
91     local actual;
92
93     actual=$(trim $(vgs --noheadings -o $field $vg))
94 if test "$verbose" = "t"
95 then
96   echo "check_vg_field_ VG=$vg, field=$field, actual=$actual, expected=$expected"
97 fi
98   test "$actual" = "$expected"
99 }
100
101 check_pv_field_()
102 {
103     local pv=$1;
104     local field=$2;
105     local expected=$3;
106     local pvs_args=$4; # optional
107     local actual;
108
109     actual=$(trim $(pvs --noheadings $pvs_args -o $field $pv))
110 if test "$verbose" = "t"
111 then
112   echo "check_pv_field_ PV=$pv, field=$field, actual=$actual, expected=$expected"
113 fi
114     test "$actual" = "$expected"
115 }
116
117 check_lv_field_()
118 {
119     local lv=$1;
120     local field=$2;
121     local expected=$3;
122     local actual;
123
124     actual=$(trim $(lvs --noheadings -o $field $lv))
125 if test "$verbose" = "t"
126 then
127   echo "check_lv_field_ LV=$lv, field=$field, actual=$actual, expected=$expected"
128 fi
129   test "$actual" = "$expected"
130 }
131
132 vg_validate_pvlv_counts_()
133 {
134         local local_vg=$1
135         local num_pvs=$2
136         local num_lvs=$3
137         local num_snaps=$4
138
139         lvs -a -o+devices $local_vg
140
141         check_vg_field_ $local_vg pv_count $num_pvs && \
142           check_vg_field_ $local_vg lv_count $num_lvs && \
143           check_vg_field_ $local_vg snap_count $num_snaps
144 }
145
146 dmsetup_has_dm_devdir_support_()
147 {
148   # Detect support for the envvar.  If it's supported, the
149   # following command will fail with the expected diagnostic.
150   out=$(DM_DEV_DIR=j dmsetup version 2>&1)
151   test "$?:$out" = "1:Invalid DM_DEV_DIR envvar value." -o \
152        "$?:$out" = "1:Invalid DM_DEV_DIR environment variable value."
153 }