2 MD_RCSID='$Revision$ $Date:: $'
7 # if we should init md (softraid) at boot
10 # setup geninitrd module
13 mdassemble=$(find_tool $initrd_dir/mdassemble /sbin/initrd-mdassemble)
14 if [ ! -x "$mdassemble" ]; then
17 mdadm=$(find_tool /sbin/mdadm $initrd_dir/mdadm /sbin/initrd-mdadm)
18 if [ ! -x "$mdadm" ]; then
23 # return true if mdadm is set on $devpath
24 # @param string $devpath device node to be examined
30 if ! is_yes "$USE_MD"; then
34 if [[ "$devpath" == /dev/md* ]]; then
41 # find md modules for $devpath
42 # @param $devpath device to be examined
43 # @return false if $devpath is not on md
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" '
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);
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);
69 # normalize to /dev/mdX form
70 if (arr_device ~ /\/dev\/md\/[0-9]+/) {
71 gsub(/\/dev\/md\//,"/dev/md",arr_device);
73 if ((arr_device == rootdev) || (arr_device == rootdev_alias)) {
75 rootdev_new=arr_device
76 gsub(/level=/,NUL,raidlevel);
77 if (raidlevel ~ /^raid([0-6]|10)/) {
78 gsub(/raid/,NUL,raidlevel);
84 gsub(".*devices=", NUL, dev_list);
85 gsub(",", " ", dev_list);
91 print "rootdev_new=" rootdev_new;
92 print "have_md=" found;
93 print "raidlevel=" raidlevel;
94 print "dev_list=\"" dev_list "\"";
98 if [ "$have_md" != "yes" -a -f /etc/raidtab ]; then
99 die "raidtools are not longer supported. Please migrate to mdadm setup!"
102 if is_yes "$have_md"; then
105 find_module "raid$raidlevel"
108 find_module "-raid$raidlevel"
109 find_module "-raid456"
115 warn "raid level $number (in mdadm config) not recognized"
119 die "RAID devices not found for \"$1\", check your configuration!"
122 debug "md: found rootdev=$1 on device $rootdev_new with devices list ${dev_list}"
124 rootdev_nr=$(( $rootdev_nr + 1 ))
125 eval "rootdev${rootdev_nr}=\"$rootdev_new\""
126 eval "dev_list${rootdev_nr}=\"${dev_list}\""
128 for device in $dev_list; do
129 find_modules_for_devpath $device
134 # generate initrd fragment for md
137 if ! is_yes "$have_md"; then
140 debug "Setting up mdadm..."
142 inst_exec $mdassemble /bin/mdassemble
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
155 echo "DEVICE $ex_dev" >> "$DESTDIR/etc/mdadm.conf"
156 new_dev_list_extra="$new_dev_list_extra $ex_dev"
158 dev_list_extra=$new_dev_list_extra
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)"
166 [ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
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"
174 echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$DESTDIR/etc/mdadm.conf"
177 for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
178 # mkdir in case of devfs name
180 [ -e "$DESTDIR/$f" ] && continue
181 # this works fine with and without devfs
189 if [ "$DEBUGINITRD" ]; then
190 [ -e /proc/mdstat ] && echo "/proc/mdstat contents:" && cat /proc/mdstat
194 # needed to determine md-version
195 if [ "$do_md0" -eq 1 ]; then
196 mknod $DESTDIR/dev/md0 b 9 0