Blob Blame History Raw
From 9d36d4fb1d0774d9ef0597abb79390e88771bf1e Mon Sep 17 00:00:00 2001
From: Harald Hoyer <harald@redhat.com>
Date: Tue, 9 Apr 2013 10:47:39 +0200
Subject: [PATCH] dracut-functions.sh: make use of findmnt

---
 dracut-functions.sh | 163 +++++++++++++++++++++-------------------------------
 1 file changed, 64 insertions(+), 99 deletions(-)

diff --git a/dracut-functions.sh b/dracut-functions.sh
index eba7412..96d3c6e 100755
--- a/dracut-functions.sh
+++ b/dracut-functions.sh
@@ -319,131 +319,96 @@ get_maj_min() {
 # $ find_block_device /usr
 # 8:4
 find_block_device() {
-    local _x _mpt _majmin _dev _fs _maj _min _find_mpt
+    local _majmin _dev _majmin _find_mpt
     _find_mpt="$1"
     if [[ $use_fstab != yes ]]; then
-        while read _x; do
-            set -- $_x
-            _majmin="$3"
-            _mpt="$5"
-            [[ $8 = "-" ]] && shift
-            _fs="$8"
-            _dev="$9"
-            [[ $_mpt = $_find_mpt ]] || continue
-            [[ $_fs = nfs ]] && { echo $_dev; return 0;}
-            [[ $_fs = nfs3 ]] && { echo $_dev; return 0;}
-            [[ $_fs = nfs4 ]] && { echo $_dev; return 0;}
-            [[ $_fs = btrfs ]] && {
-                get_maj_min $_dev
-                return 0;
-            }
-            if [[ ${_majmin#0:} = $_majmin ]]; then
-                echo $_majmin
-                return 0 # we have a winner!
+        [[ -d $_find_mpt/. ]]
+        while read _majmin _dev; do
+            if [[ -b $_dev ]]; then
+                if ! [[ $_majmin ]] || [[ $_majmin == 0:* ]]; then
+                    read _majmin < <(get_maj_min $_dev)
+                fi
+                if [[ $_majmin ]]; then
+                    echo $_majmin
+                else
+                    echo $_dev
+                fi
+                return 0
+            fi
+            if [[ $_dev = *:* ]]; then
+                echo $_dev
+                return 0
             fi
-        done < /proc/self/mountinfo
+        done < <(findmnt -e -v -n -o 'MAJ:MIN,SOURCE' "$_find_mpt")
     fi
     # fall back to /etc/fstab
-    while read _dev _mpt _fs _x; do
-        [ "${_dev%%#*}" != "$_dev" ] && continue
-
-        if [[ $_mpt = $_find_mpt ]]; then
-            [[ $_fs = nfs ]] && { echo $_dev; return 0;}
-            [[ $_fs = nfs3 ]] && { echo $_dev; return 0;}
-            [[ $_fs = nfs4 ]] && { echo $_dev; return 0;}
-            [[ $_dev != ${_dev#UUID=} ]] && _dev=/dev/disk/by-uuid/${_dev#UUID=}
-            [[ $_dev != ${_dev#LABEL=} ]] && _dev=/dev/disk/by-label/${_dev#LABEL=}
-            [[ -b $_dev ]] || return 1 # oops, not a block device.
-            get_maj_min "$_dev" && return 0
+
+    while read _majmin _dev; do
+        if ! [[ $_dev ]]; then
+            _dev="$_majmin"
+            unset _majmin
+        fi
+        if [[ -b $_dev ]]; then
+            [[ $_majmin ]] || read _majmin < <(get_maj_min $_dev)
+            if [[ $_majmin ]]; then
+                echo $_majmin
+            else
+                echo $_dev
+            fi
+            return 0
+        fi
+        if [[ $_dev = *:* ]]; then
+            echo $_dev
+            return 0
         fi
-    done < /etc/fstab
+    done < <(findmnt -e --fstab -v -n -o 'MAJ:MIN,SOURCE' "$_find_mpt")
 
     return 1
 }
 
-# find_dev_fstype <device>
-# Echo the filesystem type for a given device.
+# find_mp_fstype <mountpoint>
+# Echo the filesystem type for a given mountpoint.
 # /proc/self/mountinfo is taken as the primary source of information
 # and /etc/fstab is used as a fallback.
 # No newline is appended!
 # Example:
-# $ find_dev_fstype /dev/sda2;echo
+# $ find_mp_fstype /;echo
 # ext4
-find_dev_fstype() {
-    local _x _mpt _majmin _dev _fs _maj _min _find_dev
-    _find_dev="$1"
-    strstr "$_find_dev" "/dev" || _find_dev="/dev/block/$_find_dev"
-    while read _x; do
-        set -- $_x
-        _majmin="$3"
-        _mpt="$5"
-        [[ $8 = "-" ]] && shift
-        _fs="$8"
-        _dev="$9"
-        strstr "$_dev" "/dev" || continue
-        [[ $_dev -ef $_find_dev ]] || continue
-        [[ $_fs = "autofs" ]] && continue
-        echo -n $_fs;
-        return 0;
-    done < /proc/self/mountinfo
+find_mp_fstype() {
+    local _fs
 
-    # fall back to /etc/fstab
-    while read _dev _mpt _fs _x; do
-        [ "${_dev%%#*}" != "$_dev" ] && continue
-        case "$_dev" in
-            LABEL=*)
-                _dev="$(echo $_dev | sed 's,/,\\x2f,g')"
-                _dev="/dev/disk/by-label/${_dev#LABEL=}"
-                ;;
-            UUID=*)
-                _dev="/dev/disk/by-uuid/${_dev#UUID=}"
-                ;;
-            PARTUUID=*)
-                _dev="/dev/disk/by-partuuid/${_dev#PARTUUID=}"
-                ;;
-        esac
+    if [[ $use_fstab != yes ]]; then
+        while read _fs; do
+            [[ $_fs ]] || continue
+            [[ $_fs = "autofs" ]] && continue
+            echo -n $_fs
+            return 0
+        done < <(findmnt -e -v -n -o 'FSTYPE' "$1")
+    fi
 
-        [[ $_dev -ef $_find_dev ]] || continue
-        echo -n $_fs;
-        return 0;
-    done < /etc/fstab
+    while read _fs; do
+        [[ $_fs ]] || continue
+        [[ $_fs = "autofs" ]] && continue
+        echo -n $_fs
+        return 0
+    done < <(findmnt --fstab -e -v -n -o 'FSTYPE' "$1")
 
     return 1
 }
 
-# find_mp_fstype <mountpoint>
-# Echo the filesystem type for a given mountpoint.
+# find_dev_fstype <device>
+# Echo the filesystem type for a given device.
 # /proc/self/mountinfo is taken as the primary source of information
 # and /etc/fstab is used as a fallback.
 # No newline is appended!
 # Example:
-# $ find_mp_fstype /;echo
+# $ find_dev_fstype /dev/sda2;echo
 # ext4
-find_mp_fstype() {
-    local _x _mpt _majmin _dev _fs _maj _min _find_mpt
-    _find_mpt="$1"
-    while read _x; do
-        set -- $_x
-        _majmin="$3"
-        _mpt="$5"
-        [[ $8 = "-" ]] && shift
-        _fs="$8"
-        _dev="$9"
-        [[ $_mpt = $_find_mpt ]] || continue
-        [[ $_fs = "autofs" ]] && continue
-        echo -n $_fs;
-        return 0;
-    done < /proc/self/mountinfo
-
-    # fall back to /etc/fstab
-    while read _dev _mpt _fs _x; do
-        [ "${_dev%%#*}" != "$_dev" ] && continue
-        [[ $_mpt = $_find_mpt ]] || continue
-        echo -n $_fs;
-        return 0;
-    done < /etc/fstab
-
-    return 1
+find_dev_fstype() {
+    local _x _mpt _majmin _dev _fs _maj _min _find_dev
+    _find_dev="$1"
+    [[ "$_find_dev" = /dev* ]] || _find_dev="/dev/block/$_find_dev"
+    find_mp_fstype "$_find_dev"
 }
 
 # finds the major:minor of the block device backing the root filesystem.