import source from 3.0
[external/parted.git] / tests / t-lvm.sh
1 # Put lvm-related utilities here.
2 # This file is sourced from test infrastructure.
3
4 # Copyright (C) 2007, 2008, 2010 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 unsafe_losetup_()
20 {
21   f=$1
22
23   test -n "$G_dev_" \
24     || error "Internal error: unsafe_losetup_ called before init_root_dir_"
25
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
35
36   return 1
37 }
38
39 loop_setup_()
40 {
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; }
44
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; }
48
49   echo "$dev"
50   return 0;
51 }
52
53 # set up private /dev and /etc
54 lvm_init_root_dir_()
55 {
56   test -z "$test_dir_" \
57     && skip_ "Internal error: called lvm_init_root_dir_ before" \
58       "defining \$test_dir_"
59
60   # Define these two globals.
61   G_root_=$test_dir_/root
62   G_dev_=$G_root_/dev
63
64   export LVM_SYSTEM_DIR=$G_root_/etc
65   export DM_DEV_DIR=$G_dev_
66
67   # Only the first caller does anything.
68   mkdir -p $G_root_/etc $G_dev_ $G_dev_/mapper $G_root_/lib
69   for i in 0 1 2 3 4 5 6 7; do
70     mknod $G_root_/dev/loop$i b 7 $i
71   done
72   for i in $abs_top_builddir/dmeventd/mirror/*.so \
73            $abs_top_builddir/dmeventd/snapshot/*.so
74   do
75     # NOTE: This check is necessary because the loop above will give us the
76     # value "$abs_top_builddir/dmeventd/mirror/*.so" if no files ending in
77     # 'so' exist.  This is the best way I could quickly determine to skip
78     # over this bogus value.
79     if [ -f $i ]; then
80       echo Setting up symlink from $i to $G_root_/lib
81       ln -s $i $G_root_/lib
82     fi
83   done
84   cat > $G_root_/etc/lvm.conf <<-EOF
85   devices {
86     dir = "$G_dev_"
87     scan = "$G_dev_"
88     filter = [ "a/loop/", "a/mirror/", "a/mapper/", "r/.*/" ]
89     cache_dir = "$G_root_/etc"
90     sysfs_scan = 0
91   }
92   log {
93     verbose = $verboselevel
94     syslog = 0
95     indent = 1
96   }
97   backup {
98     backup = 0
99     archive = 0
100   }
101   global {
102     library_dir = "$G_root_/lib"
103   }
104 EOF
105 }