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