Blob Blame History Raw
From 7ae5d9d11d1a0ccd31dced528e2792f1c1d5aeca Mon Sep 17 00:00:00 2001
From: Harald Hoyer <harald@redhat.com>
Date: Thu, 8 Dec 2011 10:25:58 +0100
Subject: [PATCH] dracut: export host_fs_types host_devs

Determine devices and filesystems to be included in the host-only
initramfs image.

To get a minimal initramfs, which can mount
 /
 /etc
 /usr
 /usr/bin
 /usr/sbin
 /usr/lib
 /usr/lib64
 /boot
we look in fstab for the corresponding devices and determine their and
their slaves' filesystem type and put all that in $host_fs_types
and $host_devs.
---
 dracut           |   42 +++++++++++++++++++++++++++++++++++++++++-
 dracut-functions |   30 ++++++++++++++++++++++++++----
 2 files changed, 67 insertions(+), 5 deletions(-)

diff --git a/dracut b/dracut
index 45ee759..3d08680 100755
--- a/dracut
+++ b/dracut
@@ -507,12 +507,52 @@ trap 'exit 1;' SIGINT
 # Need to be able to have non-root users read stuff (rpcbind etc)
 chmod 755 "$initdir"
 
+if [[ $hostonly ]]; then
+
+    _get_fs_type() (
+        [[ $1 ]] || return
+        if [[ -b /dev/block/$1 ]] && get_fs_env /dev/block/$1; then
+            echo -n "$ID_FS_TYPE "
+            return 1
+        fi
+        if find_dev_fstype $1; then
+            echo -n " "
+            return 1
+        fi
+        return 1
+    )
+
+    push host_mp \
+        "/" \
+        "/etc" \
+        "/usr" \
+        "/usr/bin" \
+        "/usr/sbin" \
+        "/usr/lib" \
+        "/usr/lib64" \
+        "/boot"
+
+    host_fs_types=""
+    for mp in "${host_mp[@]}"; do
+        mountpoint "$mp" >/dev/null 2>&1 || continue
+        push host_devs $(find_block_device "$mp")
+    done
+    for dev in "${host_devs[@]}"; do
+        unset fs_type
+        for fstype in $(_get_fs_type $dev) \
+            $(check_block_and_slaves _get_fs_type $dev); do
+            strstr " $host_fs_types " "$fstype" || host_fs_types+="$fstype "
+        done
+    done
+fi
+echo "host_fs_types=$host_fs_types"
+
 export initdir dracutbasedir dracutmodules drivers \
     fw_dir drivers_dir debug no_kernel kernel_only \
     add_drivers mdadmconf lvmconf filesystems \
     use_fstab libdir usrlibdir fscks nofscks \
     stdloglvl sysloglvl fileloglvl kmsgloglvl logfile \
-    debug
+    debug host_fs_types host_devs
 
 # Create some directory structure first
 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
diff --git a/dracut-functions b/dracut-functions
index c54cd7c..258d376 100755
--- a/dracut-functions
+++ b/dracut-functions
@@ -165,8 +165,11 @@ get_fs_type() (
         echo "nfs"
         return
     fi
-    get_fs_env $1 || return
-    echo $ID_FS_TYPE
+    if get_fs_env $1; then
+        echo $ID_FS_TYPE
+        return
+    fi
+    find_dev_fstype $1
 )
 
 get_fs_uuid() (
@@ -174,11 +177,11 @@ get_fs_uuid() (
     echo $ID_FS_UUID
 )
 
-# finds the major:minor of the block device backing the root filesystem.
 find_block_device() {
     local _x _mpt _majmin _dev _fs _maj _min
     if [[ $use_fstab != yes ]]; then
         while read _x _x _majmin _x _mpt _x _x _fs _dev _x; do
+            [[ $_mpt = $1 ]] || continue
             [[ $_fs = nfs ]] && { echo $_dev; return 0;}
             [[ $_fs = nfs3 ]] && { echo $_dev; return 0;}
             [[ $_fs = nfs4 ]] && { echo $_dev; return 0;}
@@ -189,7 +192,7 @@ find_block_device() {
                     echo $_maj:$_min
                 } && return 0
             }
-            if [[ $_mpt = $1 ]] && [[ ${_majmin#0:} = $_majmin ]]; then
+            if [[ ${_majmin#0:} = $_majmin ]]; then
                 echo $_majmin
                 return 0 # we have a winner!
             fi
@@ -215,6 +218,25 @@ find_block_device() {
     return 1
 }
 
+find_dev_fstype() {
+    local _x _mpt _majmin _dev _fs _maj _min
+    while read _x _x _majmin _x _mpt _x _x _fs _dev _x; do
+        [[ $_dev = $1 ]] || continue
+        echo -n $_fs;
+        return 0;
+    done < /proc/self/mountinfo
+
+    # fall back to /etc/fstab
+    while read _dev _mpt _fs _x; do
+        [[ $_dev = $1 ]] || continue
+        echo -n $_fs;
+        return 0;
+    done < /etc/fstab
+
+    return 1
+}
+
+# finds the major:minor of the block device backing the root filesystem.
 find_root_block_device() { find_block_device /; }
 
 # Walk all the slave relationships for a given block device.