Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / test / test-utils.sh
blobfa1f2c5fb06f0ca16572fcd9cd044443c98156b8
1 # Copyright (C) 2008 Red Hat, Inc. All rights reserved.
3 # This copyrighted material is made available to anyone wishing to use,
4 # modify, copy, or redistribute it subject to the terms and conditions
5 # of the GNU General Public License v.2.
7 # You should have received a copy of the GNU General Public License
8 # along with this program; if not, write to the Free Software Foundation,
9 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
11 test_description="foo" # silence test-lib for now
12 . ./test-lib.sh
14 aux() {
15 # use just "$@" for verbose operation
16 "$@" > /dev/null 2> /dev/null
17 #"$@"
20 STACKTRACE() {
21 trap - ERR;
22 i=0;
23 while FUNC=${FUNCNAME[$i]}; test "$FUNC" != "main"; do
24 echo "$i ${FUNC}() called from ${BASH_SOURCE[$i]}:${BASH_LINENO[$i]}"
25 i=$(($i + 1));
26 done
29 teardown() {
30 echo $LOOP
31 echo $PREFIX
33 test -n "$PREFIX" && {
34 rm -rf $G_root_/dev/$PREFIX*
35 while dmsetup table | grep -q ^$PREFIX; do
36 for s in `dmsetup table | grep ^$PREFIX| awk '{ print substr($1,1,length($1)-1) }'`; do
37 dmsetup resume $s 2>/dev/null > /dev/null || true
38 dmsetup remove $s 2>/dev/null > /dev/null || true
39 done
40 done
43 test -n "$LOOP" && losetup -d $LOOP
44 test -n "$LOOPFILE" && rm -f $LOOPFILE
47 teardown_() {
48 teardown
49 cleanup_ # user-overridable cleanup
50 testlib_cleanup_ # call test-lib cleanup routine, too
53 make_ioerror() {
54 echo 0 10000000 error | dmsetup create ioerror
55 dmsetup resume ioerror
56 ln -s $G_dev_/mapper/ioerror $G_dev_/ioerror
59 prepare_loop() {
60 size=$1
61 test -n "$size" || size=32
63 test -n "$LOOP" && return 0
64 trap 'aux teardown_' EXIT # don't forget to clean up
65 trap 'set +vex; STACKTRACE; set -vex' ERR
66 #trap - ERR
68 LOOPFILE="$PWD/test.img"
69 dd if=/dev/zero of="$LOOPFILE" bs=$((1024*1024)) count=1 seek=$(($size-1))
70 if LOOP=`losetup -s -f "$LOOPFILE" 2>/dev/null`; then
71 return 0
72 elif LOOP=`losetup -f` && losetup $LOOP "$LOOPFILE"; then
73 # no -s support
74 return 0
75 else
76 # no -f support
77 # Iterate through $G_dev_/loop{,/}{0,1,2,3,4,5,6,7}
78 for slash in '' /; do
79 for i in 0 1 2 3 4 5 6 7; do
80 local dev=$G_dev_/loop$slash$i
81 ! losetup $dev >/dev/null 2>&1 || continue
82 # got a free
83 losetup "$dev" "$LOOPFILE"
84 LOOP=$dev
85 break
86 done
87 if [ -n "$LOOP" ]; then
88 break
90 done
91 test -n "$LOOP" # confirm or fail
92 return 0
94 exit 1 # should not happen
97 prepare_devs() {
98 local n="$1"
99 test -z "$n" && n=3
100 local devsize="$2"
101 test -z "$devsize" && devsize=33
102 local pvname="$3"
103 test -z "$pvname" && pvname="pv"
105 prepare_loop $(($n*$devsize))
107 PREFIX="LVMTEST$$"
109 if ! loopsz=`blockdev --getsz $LOOP 2>/dev/null`; then
110 loopsz=`blockdev --getsize $LOOP 2>/dev/null`
113 local size=$(($loopsz/$n))
115 for i in `seq 1 $n`; do
116 local name="${PREFIX}$pvname$i"
117 local dev="$G_dev_/mapper/$name"
118 eval "dev$i=$dev"
119 devs="$devs $dev"
120 echo 0 $size linear $LOOP $((($i-1)*$size)) > $name.table
121 dmsetup create $name $name.table
122 dmsetup resume $name
123 done
125 # set up some default names
126 vg=${PREFIX}vg
127 vg1=${PREFIX}vg1
128 vg2=${PREFIX}vg2
129 lv=LV
130 lv1=LV1
131 lv2=LV2
132 lv3=LV3
133 lv4=LV4
136 disable_dev() {
137 for dev in "$@"; do
138 # first we make the device inaccessible
139 echo 0 10000000 error | dmsetup load $dev
140 dmsetup resume $dev
141 # now let's try to get rid of it if it's unused
142 #dmsetup remove $dev
143 done
146 enable_dev() {
147 for dev in "$@"; do
148 local name=`echo "$dev" | sed -e 's,.*/,,'`
149 dmsetup create $name $name.table || dmsetup load $name $name.table
150 dmsetup resume $dev
151 done
154 backup_dev() {
155 for dev in "$@"; do
156 dd if=$dev of=$dev.backup bs=1024
157 done
160 restore_dev() {
161 for dev in "$@"; do
162 test -e $dev.backup || {
163 echo "Internal error: $dev not backed up, can't restore!"
164 exit 1
166 dd of=$dev if=$dev.backup bs=1024
167 done
170 prepare_pvs() {
171 prepare_devs "$@"
172 pvcreate $devs
175 prepare_vg() {
176 prepare_pvs "$@"
177 vgcreate $vg $devs
180 prepare_lvmconf() {
181 local filter="$1"
182 test -z "$filter" && \
183 filter='[ "a/dev\/mirror/", "a/dev\/mapper\/.*pv[0-9_]*$/", "r/.*/" ]'
184 cat > $G_root_/etc/lvm.conf <<-EOF
185 devices {
186 dir = "$G_dev_"
187 scan = "$G_dev_"
188 filter = $filter
189 cache_dir = "$G_root_/etc"
190 sysfs_scan = 0
192 log {
193 verbose = $verboselevel
194 syslog = 0
195 indent = 1
197 backup {
198 backup = 0
199 archive = 0
201 global {
202 library_dir = "$G_root_/lib"
203 locking_dir = "$G_root_/var/lock/lvm"
208 set -vexE
209 aux prepare_lvmconf