dmraid/mdraid: use ENV{DEVTYPE} instead of ATTR{partition}
[dracut/plouj.git] / dracut-gencmdline
blob15c3c9406a4f3f5ecd35605de4578e7b0c95f765
1 #!/bin/bash --norc
4 # Copyright 2005-2008 Red Hat, Inc. All rights reserved.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # code taken from mkinitrd
21 #. /usr/libexec/initrd-functions
24 function error() {
25 local NONL=""
26 if [ "$1" == "-n" ]; then
27 NONL="-n"
28 shift
30 echo $NONL "$@" >&2
33 function vecho() {
34 return
35 local NONL=""
36 if [ "$1" == "-n" ]; then
37 NONL="-n"
38 shift
40 is_verbose && echo $NONL "$@"
43 # module dep finding and installation functions
44 moduledep() {
45 MPARGS=""
46 if [ "$1" == "--ignore-install" ]; then
47 MPARGS="$MPARGS --ignore-install"
48 shift
50 vecho -n "Looking for deps of module $1"
51 deps=""
52 deps=$(modprobe $MPARGS --set-version $kernel --show-depends $1 2>/dev/null| awk '/^insmod / { print gensub(".*/","","g",$2) }' | while read foo ; do [ "${foo%%.ko}" != "$1" ] && echo -n "${foo%%.ko} " ; done)
53 [ -n "$deps" ] && vecho ": $deps" || vecho
56 if [ $UID != 0 ]; then
57 error "$0 must be run as root."
58 exit 1
61 export MALLOC_PERTURB_=204
63 PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH
64 export PATH
66 . /etc/rc.d/init.d/functions
68 # Set the umask. For iscsi, the initrd can contain plaintext
69 # password (chap secret), so only allow read by owner.
70 umask 077
72 VERSION=6.0.87
74 PROBE="yes"
75 MODULES=""
76 GRAPHICSMODS=""
77 PREMODS=""
78 DMRAIDS=""
79 ncryptodevs=0
80 ncryptoparts=0
81 ncryptolvs=0
82 ncryptoraids=0
83 root=""
84 scsi_wait_scan="no"
86 NET_LIST=""
87 LD_SO_CONF=/etc/ld.so.conf
88 LD_SO_CONF_D=/etc/ld.so.conf.d/
90 [ -e /etc/sysconfig/mkinitrd ] && . /etc/sysconfig/mkinitrd
92 CONFMODS="$MODULES"
93 MODULES=""
94 ARCH=$(uname -m | sed -e 's/s390x/s390/')
96 compress=1
97 allowmissing=""
98 target=""
99 kernel=""
100 force=""
101 img_vers=""
102 builtins=""
103 modulefile=/etc/modules.conf
104 [ "$ARCH" != "s390" ] && withusb=1
105 rc=0
106 nolvm=""
107 nodmraid=""
109 IMAGESIZE=8000
110 PRESCSIMODS=""
111 fstab="/etc/fstab"
113 vg_list=""
114 net_list="$NET_LIST"
116 usage () {
117 if [ "$1" == "-n" ]; then
118 cmd=echo
119 else
120 cmd=error
123 $cmd "usage: `basename $0` [--version] [--help] [-v] [-f]"
125 if [ "$1" == "-n" ]; then
126 exit 0
127 else
128 exit 1
133 qpushd() {
134 pushd "$1" >/dev/null 2>&1
137 qpopd() {
138 popd >/dev/null 2>&1
141 resolve_device_name() {
142 echo "$1"
143 # echo "resolve_device_name $1" 1>&2
146 readlink() {
147 /usr/bin/readlink -f "$1"
150 finddevnoinsys() {
151 majmin="$1"
152 if [ -n "$majmin" ]; then
153 dev=$(for x in /sys/block/* ; do find $x/ -name dev ; done | while read device ; do \
154 echo "$majmin" | cmp -s $device && echo $device ; done)
155 if [ -n "$dev" ]; then
156 dev=${dev%%/dev}
157 dev=${dev%%/}
158 echo "$dev"
159 return 0
162 return 1
165 finddevicedriverinsys () {
166 if is_iscsi $PWD; then
167 handleiscsi "$PWD"
168 return
170 while [ "$PWD" != "/sys/devices" ]; do
171 deps=
172 if [ -f modalias ]; then
173 MODALIAS=$(cat modalias)
174 if [ "${MODALIAS::7}" == "scsi:t-" ]; then
175 scsi_wait_scan=yes
177 moduledep $MODALIAS
178 unset MODALIAS
181 cd ..
182 done
185 findstoragedriverinsys () {
186 local sysfs=$(readlink "$1")
188 # if its a partition look at the device holding the partition
189 if [ -f "$sysfs/start" ]; then
190 sysfs=$(readlink ${sysfs%/*})
193 if [[ ! "$sysfs" =~ '^/sys/.*block/.*$' ]]; then
194 #error "WARNING: $sysfs is a not a block sysfs path, skipping"
195 return
198 case " $handleddevices " in
199 *" $sysfs "*)
200 return ;;
201 *) handleddevices="$handleddevices $sysfs" ;;
202 esac
204 if [[ "$sysfs" =~ '^/sys/.*block/md[0-9]+$' ]]; then
205 local raid=${sysfs##*/}
206 vecho "Found MDRAID component $raid"
207 handleraid $raid
209 if [[ "$sysfs" =~ '^/sys/.*block/dm-[0-9]+$' ]]; then
210 vecho "Found DeviceMapper component ${sysfs##*/}"
211 handledm $(cat $sysfs/dev |cut -d : -f 1) $(cat $sysfs/dev |cut -d : -f 2)
214 for slave in $(ls -d "$sysfs"/slaves/* 2>/dev/null) ; do
215 findstoragedriverinsys "$slave"
216 done
218 if [ -L "$sysfs/device" ]; then
219 qpushd $(readlink "$sysfs/device")
220 finddevicedriverinsys
221 qpopd
225 findstoragedriver () {
226 local device="$1"
228 if [ ! -b "$device" ]; then
229 #error "WARNING: $device is a not a block device, skipping"
230 return
233 local majmin=$(get_numeric_dev dec "$device")
234 local sysfs=$(finddevnoinsys "$majmin")
236 if [ -z "$sysfs" ]; then
237 #error "WARNING: $device major:minor $majmin not found, skipping"
238 return
241 vecho "Looking for driver for $device in $sysfs"
242 findstoragedriverinsys "$sysfs"
245 iscsi_get_rec_val() {
247 # The open-iscsi 742 release changed to using flat files in
248 # /var/lib/iscsi.
250 result=$(grep "^${2} = " "$1" | sed -e s'/.* = //')
253 iscsi_set_parameters() {
254 path=$1
255 vecho setting iscsi parameters
257 tmpfile=$(mktemp)
259 # Check once before getting explicit values, so we can output a decent
260 # error message.
261 /sbin/iscsiadm --show -m session -r $path > $tmpfile
262 if [ ! -s $tmpfile ]; then
263 echo Unable to find iscsi record for $path
264 exit 1
267 nit_name=$(grep "^InitiatorName=" /etc/iscsi/initiatorname.iscsi | \
268 sed -e "s/^InitiatorName=//")
270 iscsi_get_rec_val $tmpfile "node.name"
271 tgt_name=${result}
272 iscsi_get_rec_val $tmpfile "node.tpgt"
273 tpgt=${result}
274 # iscsistart wants node.conn[0].address / port
275 iscsi_get_rec_val $tmpfile 'node.conn\[0\].address'
276 tgt_ipaddr=${result}
277 iscsi_get_rec_val $tmpfile 'node.conn\[0\].port'
278 tgt_port=${result}
280 # Note: we get chap secrets (passwords) in plaintext, and also store
281 # them in the initrd.
283 iscsi_get_rec_val $tmpfile "node.session.auth.username"
284 chap=${result}
285 if [ -n "${chap}" -a "${chap}" != "<empty>" ]; then
286 chap="-u ${chap}"
287 iscsi_get_rec_val $tmpfile "node.session.auth.password"
288 chap_pw="-w ${result}"
289 else
290 chap=""
293 iscsi_get_rec_val $tmpfile "node.session.auth.username_in"
294 chap_in=${result}
295 if [ -n "${chap_in}" -a "${chap_in}" != "<empty>" ]; then
296 chap_in="-U ${chap_in}"
297 iscsi_get_rec_val $tmpfile "node.session.auth.password_in"
298 chap_in_pw="-W ${result}"
299 else
300 chap_in=""
303 rm $tmpfile
306 emit_iscsi () {
307 if [ -n "${iscsi_devs}" ]; then
308 for dev in ${iscsi_devs}; do
309 iscsi_set_parameters $dev
310 # recid is not really used, just use 0 for it
311 echo "/bin/iscsistart -t ${tgt_name} -i ${nit_name} \
312 -g ${tpgt} -a ${tgt_ipaddr} ${chap} ${chap_pw} \
313 ${chap_in} ${chap_in_pw}"
314 done
318 is_iscsi() {
319 path=$1
320 if echo $path | grep -q "/platform/host[0-9]*/session[0-9]*/target[0-9]*:[0-9]*:[0-9]*/[0-9]*:[0-9]*:[0-9]*:[0-9]*"; then
321 return 0
322 else
323 return 1
327 handledm() {
328 major=$1
329 minor=$2
330 while read dmstart dmend dmtype r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 ; do
331 case "$dmtype" in
332 crypt)
333 # this device is encrypted; find the slave device and see
334 # whether the encryption is LUKS; if not, bail.
335 slavedev=$(finddevnoinsys $r3)
336 # get the basename, then s,!,/, in case it's a cciss device
337 slavedev=$(echo ${slavedev##*/} | tr '!' '/')
338 cryptsetup isLuks "/dev/$slavedev" 2>/dev/null || continue
339 find_base_dm_mods
340 dmname=$(dmsetup info -j $major -m $minor -c --noheadings -o name)
341 # do the device resolution dance to get /dev/mapper/foo
342 # since 'lvm lvs' doesn't like dm-X device nodes
343 if [[ "$slavedev" =~ ^dm- ]]; then
344 majmin=$(get_numeric_dev dec "/dev/$slavedev")
345 for dmdev in /dev/mapper/* ; do
346 dmnum=$(get_numeric_dev dev $dmdev)
347 if [ $dmnum = $majmin ]; then
348 slavedev=${dmdev#/dev/}
349 break
351 done
354 # determine if $slavedev is an LV
355 # if so, add the device to latecryptodevs
356 # if not, add the device to cryptodevs
357 local vg=$(lvshow /dev/$slavedev)
358 if [ -n "$vg" ]; then
359 eval cryptolv${ncryptolvs}='"'/dev/$slavedev $dmname'"'
360 let ncryptolvs++
361 elif grep -q "^$slavedev :" /proc/mdstat ; then
362 eval cryptoraid${ncryptoraids}='"'/dev/$slavedev $dmname'"'
363 let ncryptoraids++
364 else
365 eval cryptoparts${ncryptoparts}='"'/dev/$slavedev $dmname'"'
366 let ncryptoparts++
369 let ncryptodevs++
370 findstoragedriver "/dev/$slavedev"
372 esac
373 done << EOF
374 $(dmsetup table -j $major -m $minor 2>/dev/null)
376 local name=$(dmsetup info --noheadings -c -j $major -m $minor -o name)
377 local vg=$(lvshow "/dev/mapper/$name")
378 local raids=$(/sbin/dmraid -s -craidname 2>/dev/null | grep -vi "no raid disks")
379 if [ -n "$vg" ]; then
380 vg=`echo $vg` # strip whitespace
381 case " $vg_list " in
382 *" $vg "*) ;;
383 *) vg_list="$vg_list $vg"
384 [ -z "$nolvm" ] && find_base_dm_mods
386 esac
388 for raid in $raids ; do
389 if [ "$raid" == "$name" ]; then
390 case " $DMRAIDS " in
391 *" $raid "*) ;;
392 *) DMRAIDS="$DMRAIDS $raid"
393 [ -z "$nodmraid" ] && find_base_dm_mods
395 esac
396 break
398 done
401 handleiscsi() {
402 vecho "Found iscsi component $1"
404 # We call iscsi_set_parameters once here to figure out what network to
405 # use (it sets tgt_ipaddr), and once again to emit iscsi values,
406 # not very efficient.
407 iscsi_set_parameters $1
408 iscsi_devs="$iscsi_devs $1"
409 netdev=$(/sbin/ip route get to $tgt_ipaddr | \
410 sed 's|.*dev \(.*\).*|\1|g' | awk '{ print $1; exit }')
411 addnetdev $netdev
414 handleraid() {
415 local start=0
417 if [ -n "$noraid" -o ! -f /proc/mdstat ]; then
418 return 0
421 levels=$(awk "/^$1[ ]*:/ { print\$4 }" /proc/mdstat)
423 for level in $levels ; do
424 case $level in
425 linear)
426 start=1
428 multipath)
429 start=1
431 raid[01] | raid10)
432 start=1
434 raid[456])
435 start=1
438 error "raid level $level (in /proc/mdstat) not recognized"
440 esac
441 done
442 if [ "$start" = 1 ]; then
443 raiddevices="$raiddevices $1"
445 return $start
448 lvshow() {
449 lvm lvs --ignorelockingfailure --noheadings -o vg_name \
450 $1 2>/dev/null | egrep -v '^ *(WARNING:|Volume Groups with)'
453 vgdisplay() {
454 lvm vgdisplay --ignorelockingfailure -v $1 2>/dev/null |
455 sed -n 's/PV Name//p'
458 dmmods_found="n"
459 find_base_dm_mods()
461 [ "$dmmods_found" == "n" ] || return
462 dmmods_found="y"
465 savedargs=$*
466 while [ $# -gt 0 ]; do
467 case $1 in
468 --fstab*)
469 if [ "$1" != "${1##--fstab=}" ]; then
470 fstab=${1##--fstab=}
471 else
472 fstab=$2
473 shift
478 -v|--verbose)
479 set_verbose true
481 --net-dev*)
482 if [ "$1" != "${1##--net-dev=}" ]; then
483 net_list="$net_list ${1##--net-dev=}"
484 else
485 net_list="$net_list $2"
486 shift
489 --rootdev*)
490 if [ "$1" != "${1##--rootdev=}" ]; then
491 rootdev="${1##--rootdev=}"
492 else
493 rootdev="$2"
494 shift
497 --thawdev*)
498 if [ "$1" != "${1##--thawdev=}" ]; then
499 thawdev="${1##--thawdev=}"
500 else
501 thawdev="$2"
502 shift
505 --rootfs*)
506 if [ "$1" != "${1##--rootfs=}" ]; then
507 rootfs="${1##--rootfs=}"
508 else
509 rootfs="$2"
510 shift
513 --rootopts*)
514 if [ "$1" != "${1##--rootopts=}" ]; then
515 rootopts="${1##--rootopts=}"
516 else
517 rootopts="$2"
518 shift
521 --root*)
522 if [ "$1" != "${1##--root=}" ]; then
523 root="${1##--root=}"
524 else
525 root="$2"
526 shift
529 --loopdev*)
530 if [ "$1" != "${1##--loopdev=}" ]; then
531 loopdev="${1##--loopdev=}"
532 else
533 loopdev="$2"
534 shift
537 --loopfs*)
538 if [ "$1" != "${1##--loopfs=}" ]; then
539 loopfs="${1##--loopfs=}"
540 else
541 loopfs="$2"
542 shift
545 --loopopts*)
546 if [ "$1" != "${1##--loopopts=}" ]; then
547 loopopts="${1##--loopopts=}"
548 else
549 loopopts="$2"
550 shift
553 --looppath*)
554 if [ "$1" != "${1##--looppath=}" ]; then
555 looppath="${1##--looppath=}"
556 else
557 looppath="$2"
558 shift
561 --help)
562 usage -n
565 if [ -z "$target" ]; then
566 target=$1
567 elif [ -z "$kernel" ]; then
568 kernel=$1
569 else
570 usage
573 esac
575 shift
576 done
578 [ -z "$rootfs" ] && rootfs=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' $fstab)
579 [ -z "$rootopts" ] && rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' $fstab)
580 [ -z "$rootopts" ] && rootopts="defaults"
583 [ -z "$rootdev" ] && rootdev=$(awk '/^[ \t]*[^#]/ { if ($2 == "/") { print $1; }}' $fstab)
584 # check if it's nfsroot
585 physdev=""
586 if [ "$rootfs" == "nfs" ]; then
587 if [ "x$net_list" == "x" ]; then
588 handlenfs $rootdev
590 else
591 # check if it's root by label
592 rdev=$rootdev
593 if [[ "$rdev" =~ ^(UUID=|LABEL=) ]]; then
594 rdev=$(resolve_device_name "$rdev")
596 rootopts=$(echo $rootopts | sed -e 's/^r[ow],//' -e 's/,_netdev//' -e 's/_netdev//' -e 's/,r[ow],$//' -e 's/,r[ow],/,/' -e 's/^r[ow]$/defaults/' -e 's/$/,ro/')
597 findstoragedriver "$rdev"
600 # find the first swap dev which would get used for swsusp
601 [ -z "$thawdev" ] && thawdev=$(awk '/^[ \t]*[^#]/ { if ($3 == "swap") { print $1; exit }}' $fstab)
602 swsuspdev="$thawdev"
603 if [ -n "$swsuspdev" ]; then
604 if [[ "$swsuspdev" =~ ^(UUID=|LABEL=) ]]; then
605 swsuspdev=$(resolve_device_name "$swsuspdev")
607 findstoragedriver "$swsuspdev"
611 cemit()
613 cat
616 emit()
618 NONL=""
619 if [ "$1" == "-n" ]; then
620 NONL="-n"
621 shift
623 echo $NONL "$@"
626 emitdmraids()
628 if [ -z "$nodmraid" -a -n "$DMRAIDS" ]; then
629 for raid in $DMRAIDS; do
630 echo -n "rd_DM_UUID=$raid "
631 done
636 # HACK: module loading + device creation isn't necessarily synchronous...
637 # this will make sure that we have all of our devices before trying
638 # things like RAID or LVM
639 emitdmraids
641 emitcrypto()
643 local luksuuid=$(grep "^$2 " /etc/crypttab 2>/dev/null| awk '{ print $2 }')
644 if [ -z "$luksuuid" ]; then
645 luksuuid="$2"
647 echo -n "rd_LUKS_UUID=$luksuuid "
650 for cryptdev in ${!cryptopart@} ; do
651 emitcrypto `eval echo '$'$cryptdev`
652 done
654 if [ -n "$raiddevices" ]; then
655 for dev in $raiddevices; do
656 echo -n "rd_MD_UUID=${dev} "
657 done
660 for cryptdev in ${!cryptoraid@} ; do
661 emitcrypto `eval echo '$'$cryptdev`
662 done
664 if [ -z "$nolvm" -a -n "$vg_list" ]; then
665 for vg in $vg_list; do
666 echo -n "rd_LVM_VG=$vg "
667 done
670 for cryptdev in ${!cryptolv@} ; do
671 emitcrypto `eval echo '$'$cryptdev`
672 done
674 # output local keyboard/18n settings
675 . /etc/sysconfig/keyboard
676 . /etc/sysconfig/i18n
678 for i in KEYBOARDTYPE KEYTABLE SYSFONT SYSFONTACM UNIMAP LANG; do
679 val=$(eval echo \$$i)
680 [[ $val ]] && echo -n "$i=$val "
681 done
683 echo
684 # vim:ts=8:sw=4:sts=4:et