PATCH: Streamline mkinitrd storage device dedection

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

We've had this weird code for ages which calls findstoragedriver() twice
once with a /dev/xxxx path and once with a /sys/block/xxx path.

Also our findstoragedriverinsys() method iterates over slaves, but does not
check if the slaves are dm or md, and thus does not call handledm / handleraid
when needed for slaves.

We got away with this sofar atleast for lvm on mdraid, as we handled lvm
seperately in handlevordev, calling findstorage driver there for each
lvm pv, and thus not depending on findstoragedriverinsys() to properly
iterate over the slaves. As one of my latest patches removed handlelvordev,
it has also broken lvm on mdraid. Likewise in the mdraid code we were
iterating over the mdraid slaves instead of letting the slave iteration in
findstoragedriverinsys() handle it.

This patch streamlines the whole storagehandling, it makes the following
changes:
1) Only call findstoragedriver() with a /dev/xxx path, and let it resolve
   this to a /sys/block/xxx path itself, iow do not call it twice
2) Always check if a sysfs dir we are checking for storage devices represent
   perhaps a mdraid or dm block device, also when iterating over slaves
3) No longer call findstoragedriver in handleraid, the slave iteration takes
   care of this
4) Now that we no longer have 3 different code paths to detect the same
   storage device we also no longer need to have 3 different lists to
   keep track of if we already checked a sysfs dir / a device.
---
 mkinitrd |  189 ++++++++++++++++++++++++--------------------------------------
 1 files changed, 72 insertions(+), 117 deletions(-)

diff --git a/mkinitrd b/mkinitrd
index fb1cf3f..6f4847f 100755
--- a/mkinitrd
+++ b/mkinitrd
@@ -283,7 +283,7 @@ access() {
 finddevnoinsys() {
     majmin="$1"
     if [ -n "$majmin" ]; then
- dev=$(for x in /sys/block/* ; do findall $x/ -name dev ; done | while read device ; do \ + dev=$(for x in /sys/block/* ; do findall $x -name dev ; done | while read device ; do \
               echo "$majmin" | cmp -s $device && echo $device ; done)
         if [ -n "$dev" ]; then
             dev=${dev%%/dev}
@@ -295,60 +295,7 @@ finddevnoinsys() {
     return 1
 }

-findblockdevinsys() {
-    devname=$(resolve_device_name "$1")
-    if [[ "$devname" =~ ^/sys/block/ ]]; then
-        echo "$devname"
-    fi
-    # check if it's a dm-crypt device. if so, just return the /dev/mapper path
-    if [[ "$devname" =~ ^/dev/mapper/ ]]; then
-        type=$(/sbin/dmsetup table $(basename $devname) | awk '{print $3}')
-        if [ "$type" == "crypt" ]; then
-            echo "$devname"
-            return 0
-        fi
-    fi
-    majmin=$(get_numeric_dev dec $devname)
-    finddevnoinsys "$majmin"
-}
-
-slavestried=""
-handledsysfspaths=""
-
-findstoragedriverinsys () {
-    while [ ! -L device ]; do
-        for slave in $(ls -d slaves/* 2>/dev/null) ; do
-            [ -e $slave ] || continue
-            slavename=${slave##*/}
-            case " $slavestried " in
-                *" $slavename "*)
-                    continue
-                    ;;
-                *)
-                    slavestried="$slavestried $slavename"
-                    if [ -L $slave ]; then
-                        slave=$(readlink $slave)
-                    fi
-                    qpushd $slave
-                    findstoragedriverinsys
-                    qpopd
-                    ;;
-            esac
-        done
-        [ "$PWD" = "/sys" ] && return
-        cd ..
-    done
-
-    cd $(readlink ./device)
-    case " $handledsysfspaths " in
-        *" $PWD "*)
-            return
-            ;;
-        *)
-            handledsysfspaths="$handledsysfspaths $PWD"
-            ;;
-    esac
-
+finddevicedriverinsys () {
     if is_iscsi $PWD; then
         handleiscsi "$PWD"
         return
@@ -376,40 +323,69 @@ findstoragedriverinsys () {
     done
 }

-findstoragedriver () {
-    for device in $@ ; do
-        case " $handleddevices " in
-            *" $device "*)
-                continue ;;
-            *) handleddevices="$handleddevices $device" ;;
-        esac
-        if [[ "$device" =~ "^md[0-9]+" ]]; then
-            vecho "Found RAID component $device"
-            handleraid "$device"
-            continue
-        fi
-        vecho "Looking for driver for device $device"
-        if [[ "$device" =~ ^/sys ]]; then
-            device=${device##*/}
-        fi
-        if [[ "$device" =~ ^(dm-|mapper/) ]]; then
-            majmin=$(get_numeric_dev dec "/dev/$device")
-            sysfs=$(finddevnoinsys $majmin)
- handledm $(echo "$majmin" |cut -d : -f 1) $(echo "$majmin" |cut -d : -f 2)
-        else
-            sysfs=""
-            device=$(echo "$device" | sed 's,/,!,g')
-            if [ -d /sys/block/$device/ ]; then
-                sysfs="/sys/block/$device"
-            else
- sysfs=$(for x in /sys/block/* ; do findone -type d $x/ -name $device; done)
-            fi
-        fi
-        [ -z "$sysfs" -o ! -e "$sysfs" ] && continue
-        qpushd $sysfs
-        findstoragedriverinsys
-        qpopd
+findstoragedriverinsys () {
+    local sysfs=$(readlink "$1")
+
+    # if its a partition look at the device holding the partition
+    if [ -f "$sysfs/partition" ]; then
+        sysfs=$(readlink ${sysfs%/*})
+    fi
+
+    if [[ ! "$sysfs" =~ '^/sys/devices/.*/block/.*$' ]]; then
+        error "WARNING: $sysfs is a not a block sysfs path, skipping"
+        return
+    fi
+
+    if [ ! -e "$sysfs" ]; then
+        error "WARNING: sysfspath $sysfs does not exist, skipping"
+        return
+    fi
+
+    case " $handleddevices " in
+        *" $sysfs "*)
+            return ;;
+        *) handleddevices="$handleddevices $sysfs" ;;
+    esac
+
+    if [[ "$sysfs" =~ '^/sys/devices/virtual/block/md[0-9]+$' ]]; then
+        local raid=${sysfs##*/}
+        vecho "Found MDRAID component $raid"
+        handleraid $raid
+    fi
+    if [[ "$sysfs" =~ '^/sys/devices/virtual/block/dm-[0-9]+$' ]]; then
+        vecho "Found DeviceMapper component ${sysfs##*/}"
+        handledm $(cat $sysfs/dev |cut -d : -f 1) $(cat $sysfs/dev |cut -d : -f 2)
+    fi
+
+    for slave in $(ls -d "$sysfs"/slaves/* 2>/dev/null) ; do
+        findstoragedriverinsys "$slave"
     done
+
+    if [ -L "$sysfs/device" ]; then
+        qpushd $(readlink "$sysfs/device")
+        finddevicedriverinsys
+        qpopd
+    fi
+}
+
+findstoragedriver () {
+    local device="$1"
+
+    if [ ! -b "$device" ]; then
+        error "WARNING: $device is a not a block device, skipping"
+        return
+    fi
+
+    local majmin=$(get_numeric_dev dec "$device")
+    local sysfs=$(finddevnoinsys "$majmin")
+
+    if [ -z "$sysfs" ]; then
+        error "WARNING: $device major:minor $majmin not found, skipping"
+        return
+    fi
+
+    vecho "Looking for driver for $device in $sysfs"
+    findstoragedriverinsys "$sysfs"
 }

 findnetdriver() {
@@ -581,7 +557,7 @@ handledm() {
                 fi

                 let ncryptodevs++
-                findstoragedriver "$slavedev"
+                findstoragedriver "/dev/$slavedev"
                 ;;
         esac
     done << EOF
@@ -623,7 +599,6 @@ handleraid() {
     fi

     levels=$(awk "/^$1[	 ]*:/ { print\$4 }" /proc/mdstat)
- devs=$(gawk "/^$1[ ]*:/ { print gensub(\"\\\\[[0-9]*\\\\](\\\\([FSW]\\\\))*\",\"\",\"g\",gensub(\"^md.*raid[0-9]*\",\"\",\"1\")) }" /proc/mdstat)

     for level in $levels ; do
         case $level in
@@ -645,7 +620,6 @@ handleraid() {
             ;;
         esac
     done
-    findstoragedriver $devs
     if [ "$start" = 1 ]; then
         raiddevices="$raiddevices $1"
     fi
@@ -1070,8 +1044,10 @@ if [ "x$PROBE" == "xyes" ]; then
[ -z "$rootdev" ] && rootdev=$(awk '/^[ \t]*[^#]/ { if ($2 == "/") { print $1; }}' $fstab)
     # check if it's nfsroot
     physdev=""
-    if [ "$rootfs" == "nfs" -a "x$net_list" == "x" ]; then
-        handlenfs $rootdev
+    if [ "$rootfs" == "nfs" ]; then
+        if [ "x$net_list" == "x" ]; then
+            handlenfs $rootdev
+        fi
     else
         # check if it's root by label
         rdev=$rootdev
@@ -1079,19 +1055,7 @@ if [ "x$PROBE" == "xyes" ]; then
             rdev=$(resolve_device_name "$rdev")
         fi
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/')
-        physdev=$(findblockdevinsys "$rdev")
-        physdev=${physdev##*/dev/}
-        if [ -n "$physdev" ]; then
-            vecho "Found root device $physdev for $rdev"
-        else
-            physdev="$rdev"
-        fi
-    fi
-    if [ "$rootfs" != "nfs" ]; then
-        if [ -n "$physdev" -a "$physdev" != "$rdev" ]; then
-            findstoragedriver "$physdev"
-        fi
-        findstoragedriver ${rdev##/dev/}
+        findstoragedriver "$rdev"
     fi

     # find the first swap dev which would get used for swsusp
@@ -1101,16 +1065,7 @@ if [ "x$PROBE" == "xyes" ]; then
         if [[ "$swsuspdev" =~ ^(UUID=|LABEL=) ]]; then
             swsuspdev=$(resolve_device_name "$swsuspdev")
         fi
-
-        suspdev=$(findblockdevinsys "$swsuspdev")
-        suspdev=${suspdev##*/dev/}
-        if [ -n "$suspdev" ]; then
-	    vecho "Found swsusp device $suspdev for $swsuspdev"
-        fi
-        if [ -n "$suspdev" -a "$suspdev" != "$swsuspdev" ]; then
-            findstoragedriver "$suspdev"
-        fi
-	findstoragedriver "${swsuspdev##/dev/}"
+	findstoragedriver "$swsuspdev"
     fi
 fi

@@ -1161,7 +1116,7 @@ if [ -n "${loopfs}" ] || [[ "$rootopts" =~ "loop" ]]; then
 	# FIXME: label support
 	
 	if [ "$loopfs" != "nfs" ]; then
-            findstoragedriver ${loopdev##/dev/}
+            findstoragedriver "$loopdev"
         fi
 fi

--
1.6.1.2

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux