]> git.pld-linux.org Git - projects/geninitrd.git/blob - mod-md.sh
no need to mess with magic values, let the "magic" be value
[projects/geninitrd.git] / mod-md.sh
1 #!/bin/sh
2 MD_RCSID='$Revision$ $Date::                            $'
3
4 # geninitrd mod: mdadm
5 USE_MD=${USE_MD:-yes}
6
7 # if we should init md (softraid) at boot
8 have_md=no
9
10 # setup geninitrd module
11 # @access       public
12 setup_mod_md() {
13         mdassemble=$(find_tool $initrd_dir/mdassemble /sbin/initrd-mdassemble)
14         if [ ! -x "$mdassemble" ]; then
15                 USE_MD=no
16         fi
17         mdadm=$(find_tool /sbin/mdadm $initrd_dir/mdadm /sbin/initrd-mdadm)
18         if [ ! -x "$mdadm" ]; then
19                 USE_MD=no
20         fi
21 }
22
23 # return true if mdadm is set on $devpath
24 # @param        string $devpath device node to be examined
25 # @access       public
26 is_md() {
27         local devpath="$1"
28
29         # mdadm disabled
30         if ! is_yes "$USE_MD"; then
31                 return 1
32         fi
33
34         if [[ "$devpath" == /dev/md* ]]; then
35                 return 0
36         fi
37
38         return 1
39 }
40
41 # find md modules for $devpath
42 # @param        $devpath        device to be examined
43 # @return       false if $devpath is not on md
44 # @access       public
45 find_modules_md() {
46         local found raidlevel
47
48         if [ -f /etc/mdadm.conf ]; then
49                 debug "Finding RAID details using mdadm for rootdev=$1"
50                 eval `($mdadm -v --examine --scan --config=/etc/mdadm.conf 2> /dev/null;$mdadm -v --detail --scan --config=/etc/mdadm.conf 2> /dev/null) | awk -v rootdev="$1" '
51                 BEGIN {
52                         found = "no";
53                         dev_list = "";
54                         raidlevel = ""
55                         rootdev_new = rootdev
56                         rootdev_alias = rootdev;
57                         # alternative name: normalize from /dev/md/X to /dev/mdX
58                         if (rootdev_alias ~ /\/dev\/md\/[0-9]+/) {
59                                 gsub(/\/dev\/md\//,"/dev/md",rootdev_alias);
60                         }
61                         # alternative name: normalize from /dev/mdXpY to /dev/mdX
62                         if (rootdev_alias ~/\/dev\/md[0-9]+p[0-9]+/) {
63                                 gsub(/p[0-9]+/,"",rootdev_alias);
64                         }
65                 }
66
67                 /^ARRAY/ {
68                         arr_device = $2
69                         # normalize to /dev/mdX form
70                         if (arr_device ~ /\/dev\/md\/[0-9]+/) {
71                                 gsub(/\/dev\/md\//,"/dev/md",arr_device);
72                         }
73                         if ((arr_device == rootdev) || (arr_device == rootdev_alias)) {
74                                 raidlevel=$3;
75                                 rootdev_new=arr_device
76                                 gsub(/level=/,NUL,raidlevel);
77                                 if (raidlevel ~ /^raid([0-6]|10)/) {
78                                         gsub(/raid/,NUL,raidlevel);
79                                 };
80                                 found="yes";
81                                 getline x;
82                                 if (x ~ /devices=/) {
83                                         dev_list = x;
84                                         gsub(".*devices=", NUL, dev_list);
85                                         gsub(",", " ", dev_list);
86                                 }
87                         }
88                 }
89
90                 END {
91                         print "rootdev_new=" rootdev_new;
92                         print "have_md=" found;
93                         print "raidlevel=" raidlevel;
94                         print "dev_list=\"" dev_list "\"";
95                 }'`
96         fi
97
98         if [ "$have_md" != "yes" -a -f /etc/raidtab ]; then
99                 die "raidtools are not longer supported. Please migrate to mdadm setup!"
100         fi
101
102         if is_yes "$have_md"; then
103                 case "$raidlevel" in
104                 [01]|10)
105                         find_module "raid$raidlevel"
106                         ;;
107                 [456])
108                         find_module "-raid$raidlevel"
109                         find_module "-raid456"
110                         ;;
111                 linear)
112                         find_module "linear"
113                         ;;
114                 *)
115                         warn "raid level $number (in mdadm config) not recognized"
116                         ;;
117                 esac
118         else
119                 die "RAID devices not found for \"$1\", check your configuration!"
120         fi
121
122         debug "md: found rootdev=$1 on device $rootdev_new with devices list ${dev_list}"
123
124         rootdev_nr=$(( $rootdev_nr + 1 ))
125         eval "rootdev${rootdev_nr}=\"$rootdev_new\""
126         eval "dev_list${rootdev_nr}=\"${dev_list}\""
127
128         for device in $dev_list; do
129                 find_modules_for_devpath $device
130         done
131 }
132
133
134 # generate initrd fragment for md
135 # @access       public
136 initrd_gen_md() {
137         if ! is_yes "$have_md"; then
138                 return
139         fi
140         debug "Setting up mdadm..."
141
142         inst_exec $mdassemble /bin/mdassemble
143
144         # LVM on RAID case
145         local dev_list_extra ex_dev
146         dev_list_extra=$(awk '/^DEVICE / { for (i=2; i<=NF; i++) { printf "%s ", $i; }; } ' /etc/mdadm.conf | xargs)
147         new_dev_list_extra=""
148         for ex_dev in $dev_list_extra; do
149                 if [ "$ex_dev" = "partitions" ]; then
150                         echo "DEVICE partitions" >> "$DESTDIR/etc/mdadm.conf"
151                         # FIXME: find and copy partition devices from /proc/partitions
152                         #        - best if done at runtime, now initrd gen time
153                         continue
154                 fi
155                 echo "DEVICE $ex_dev" >> "$DESTDIR/etc/mdadm.conf"
156                 new_dev_list_extra="$new_dev_list_extra $ex_dev"
157         done
158         dev_list_extra=$new_dev_list_extra
159
160         local cr_rootdev cr_dev_list do_md0=1 nr cr_dev_list_md f cr_md_conf
161         for nr in `seq 1 $rootdev_nr`; do
162                 eval cr_rootdev="\$rootdev${nr}"
163                 eval cr_dev_list="\$dev_list${nr}"
164                 debug echo "Setting up array ($cr_rootdev = $cr_dev_list)"
165
166                 [ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
167
168                 echo "DEVICE $cr_dev_list" >> "$DESTDIR/etc/mdadm.conf"
169                 cr_dev_list_md="$(echo "$cr_dev_list" | xargs | awk ' { gsub(/ +/,",",$0); print $0; }')"
170                 cr_md_conf=$($mdadm --detail --brief --config=/etc/mdadm.conf $cr_rootdev | awk ' { gsub(/spares=[0-9]+/, "", $0); print $0; }')
171                 if [ -n "$cr_md_conf" ]; then
172                         echo "$cr_md_conf" >> "$DESTDIR/etc/mdadm.conf"
173                 else
174                         echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$DESTDIR/etc/mdadm.conf"
175                 fi
176
177                 for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
178                         # mkdir in case of devfs name
179                         inst_d $(dirname $f)
180                         [ -e "$DESTDIR/$f" ] && continue
181                         # this works fine with and without devfs
182                         inst $f $f
183                 done
184         done
185
186         add_linuxrc <<-EOF
187         mdassemble
188
189         if [ "$DEBUGINITRD" ]; then
190                 [ -e /proc/mdstat ] && echo "/proc/mdstat contents:" && cat /proc/mdstat
191         fi
192         EOF
193
194         # needed to determine md-version
195         if [ "$do_md0" -eq 1 ]; then
196                 mknod $DESTDIR/dev/md0 b 9 0
197         fi
198 }
This page took 0.235903 seconds and 3 git commands to generate.