Imported Upstream version 2.02.79
[platform/upstream/device-mapper.git] / test / check.sh
1 #!/bin/bash
2
3 # Copyright (C) 2010 Red Hat, Inc. All rights reserved.
4 #
5 # This copyrighted material is made available to anyone wishing to use,
6 # modify, copy, or redistribute it subject to the terms and conditions
7 # of the GNU General Public License v.2.
8 #
9 # You should have received a copy of the GNU General Public License
10 # along with this program; if not, write to the Free Software Foundation,
11 # Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
12
13 # check.sh: assert various things about volumes
14
15 # USAGE
16 #  check linear VG LV
17 #  check lv_on VG LV PV
18
19 #  check mirror VG LV [LOGDEV|core]
20 #  check mirror_nonredundant VG LV
21 #  check mirror_legs VG LV N
22 #  check mirror_images_on VG LV DEV [DEV...]
23
24 # ...
25
26 set -e -o pipefail
27
28 lvl() {
29         lvs -a --noheadings "$@"
30 }
31
32 lvdevices() {
33         lvl -odevices "$@" | sed 's/([^)]*)//g; s/,/ /g'
34 }
35
36 mirror_images_redundant()
37 {
38   vg=$1
39   lv=$vg/$2
40
41   lvs -a $vg -o+devices
42   for i in `lvdevices $lv`; do
43           echo "# $i:"
44           lvdevices $vg/$i | sort | uniq
45   done > check.tmp.all
46
47   (grep -v ^# check.tmp.all || true) | sort | uniq -d > check.tmp
48
49   test "`cat check.tmp | wc -l`" -eq 0 || {
50           echo "mirror images of $lv expected redundant, but are not:"
51           cat check.tmp.all
52           exit 1
53   }
54 }
55
56 mirror_images_on() {
57         vg=$1
58         lv=$2
59
60         shift 2
61
62         for i in `lvdevices $lv`; do
63                 lv_on $vg $lv $1
64                 shift
65         done
66 }
67
68 lv_on()
69 {
70         lv="$1/$2"
71         lvdevices $lv | grep -F "$3" || {
72                 echo "LV $lv expected on $3 but is not:" >&2
73                 lvdevices $lv >&2
74                 exit 1
75         }
76         test `lvdevices $lv | grep -vF "$3" | wc -l` -eq 0 || {
77                 echo "LV $lv contains unexpected devices:" >&2
78                 lvdevices $lv >&2
79                 exit 1
80         }
81 }
82
83 mirror_log_on()
84 {
85         vg="$1"
86         lv="$2"
87         where="$3"
88         if test "$where" = "core"; then
89                 lvl -omirror_log "$vg/$lv" | not grep mlog
90         else
91                 lv_on $vg "${lv}_mlog" "$where"
92         fi
93 }
94
95 lv_is_contiguous()
96 {
97         test `lvl --segments $1 | wc -l` -eq 1 || {
98                 echo "LV $1 expected to be contiguous, but is not:"
99                 lvl --segments $1
100                 exit 1
101         }
102 }
103
104 lv_is_clung()
105 {
106         test `lvdevices $1 | sort | uniq | wc -l` -eq 1 || {
107                 echo "LV $1 expected to be clung, but is not:"
108                 lvdevices $! | sort | uniq
109                 exit 1
110         }
111 }
112
113 mirror_images_contiguous()
114 {
115         for i in `lvdevices $1/$2`; do
116                 lv_is_contiguous $1/$i
117         done
118 }
119
120 mirror_images_clung()
121 {
122         for i in `lvdevices $1/$2`; do
123                 lv_is_clung $1/$i
124         done
125 }
126
127 mirror() {
128         mirror_nonredundant "$@"
129         mirror_images_redundant "$1" "$2"
130 }
131
132 mirror_nonredundant() {
133         lv="$1/$2"
134         lvs -oattr "$lv" | grep -q "^ *m.....$" || {
135                 echo "$lv expected a mirror, but is not:"
136                 lvs -a $lv
137                 exit 1
138         }
139         if test -n "$3"; then mirror_log_on "$1" "$2" "$3"; fi
140 }
141
142 mirror_legs() {
143         lv="$1/$2"
144         expect="$3"
145         lvdevices "$lv"
146         real=`lvdevices "$lv" | wc -w`
147         test "$expect" = "$real"
148 }
149
150 mirror_no_temporaries()
151 {
152         vg=$1
153         lv=$2
154         lvl -oname $vg | grep $lv | not grep "tmp" || {
155                 echo "$lv has temporary mirror images unexpectedly:"
156                 lvl $vg | grep $lv
157                 exit 1
158         }
159 }
160
161 linear() {
162         lv="$1/$2"
163         lvl -ostripes "$lv" | grep -q "1" || {
164                 echo "$lv expected linear, but is not:"
165                 lvl "$lv" -o+devices
166                 exit 1
167         }
168 }
169
170 active() {
171         lv="$1/$2"
172         lvl -oattr "$lv" 2> /dev/null | grep -q "^ *....a.$" || {
173                 echo "$lv expected active, but lvs says it's not:"
174                 lvl "$lv" -o+devices 2>/dev/null
175                 exit 1
176         }
177         dmsetup table | egrep -q "$1-$2: *[^ ]+" || {
178                 echo "$lv expected active, lvs thinks it is but there are no mappings!"
179                 dmsetup table | grep $1-$2:
180                 exit 1
181         }
182 }
183
184 inactive() {
185         lv="$1/$2"
186         lvl -oattr "$lv" 2> /dev/null | grep -q '^ *....[-isd].$' || {
187                 echo "$lv expected inactive, but lvs says it's not:"
188                 lvl "$lv" -o+devices 2>/dev/null
189                 exit 1
190         }
191         dmsetup table | not egrep -q "$1-$2: *[^ ]+" || {
192                 echo "$lv expected inactive, lvs thinks it is but there are mappings!"
193                 dmsetup table | grep $1-$2:
194                 exit 1
195         }
196 }
197
198 "$@"