Fix leaked file descriptors
[grml-rescueboot.git] / 42_grml
diff --git a/42_grml b/42_grml
index c7b2016..30f1c07 100755 (executable)
--- a/42_grml
+++ b/42_grml
@@ -1,8 +1,7 @@
 #!/bin/sh
 # Filename:      42_grml
 # Purpose:       grub-mkconfig helper script for Grml rescue systems
 #!/bin/sh
 # Filename:      42_grml
 # Purpose:       grub-mkconfig helper script for Grml rescue systems
-# Authors:       grml-team (grml.org), (c) Andreas Gredler <jimmy@grml.org>, Michael Prokop <mika@grml.org>
-# Bug-Reports:   see http://grml.org/bugs/
+# Authors:       Grml team (grml.org), (c) Andreas Gredler <jimmy@grml.org>, Michael Prokop <mika@grml.org>
 # License:       This file is licensed under the GPL v2+.
 ################################################################################
 
 # License:       This file is licensed under the GPL v2+.
 ################################################################################
 
@@ -21,6 +20,58 @@ if [ -r /etc/default/grml-rescueboot ] ; then
   . /etc/default/grml-rescueboot
 fi
 
   . /etc/default/grml-rescueboot
 fi
 
+resolve_dm_name() {
+  retval="$1"
+  base=${1##*/}
+  for block in /sys/block /sys/class/block ; do
+    [ ! -d ${block}/${base}/dm ] && continue
+    retval="/dev/mapper/$(cat ${block}/${base}/dm/name)"
+    break
+  done
+  case "$retval" in
+    /dev/dm-*)
+    minor=${retval##*-}
+   retval="/dev/mapper/$(dmsetup info -C --noheadings -o name -j 253 -m $minor)"
+    ;;
+  esac
+
+  echo "$retval"
+}
+
+get_dependencies() {
+  device=${1}
+  if [ -z ${device} ] ; then
+    return
+  fi
+  device=$(readlink -f ${device})
+  case "$device" in
+    /dev/mapper/*)
+    device="/dev/dm-$(dmsetup info -C --noheadings -o minor "$device")"
+    ;;
+  esac
+
+  base=${device##*/}
+  dependencies=""
+  additional_dependencies=""
+  for block in /sys/block /sys/class/block ; do
+    [ ! -d ${block}/${base}/slaves ] && continue
+    for file in  ${block}/${base}/slaves/* ; do
+      dep_name="/dev/${file##*/}"
+      dep_name=$(resolve_dm_name ${dep_name})
+
+      # resolve recursively all dependencies
+      additional_dependencies=$(get_dependencies ${dep_name})
+
+      dependencies="$dependencies $additional_dependencies $dep_name"
+    done
+    break
+  done
+  echo $dependencies
+
+}
+
+
+
 iso_list=""
 for file in "${ISO_LOCATION}"/*.iso ; do
      if grub_file_is_not_garbage "$file" ; then
 iso_list=""
 for file in "${ISO_LOCATION}"/*.iso ; do
      if grub_file_is_not_garbage "$file" ; then
@@ -36,20 +87,27 @@ for grmliso in $iso_list ; do
   additional_param=""
 
   case "$device" in
   additional_param=""
 
   case "$device" in
-      /dev/mapper*|/dev/md*)
-      additional_param="live-media=$device"
-      ;;
+    /dev/mapper*|/dev/md*)
+      dependencies=$(get_dependencies ${device})
+      dep_string=""
+      for dep in $dependencies $device ; do
+        dep_string="$dep_string,$dep"
+      done
+      dep_string=${dep_string#,}
+      additional_param="live-media=$dep_string"
+    ;;
   esac
 
   echo "Found Grml ISO image: $grmliso" >&2
   title="Grml Rescue System ($grml)"
   esac
 
   echo "Found Grml ISO image: $grmliso" >&2
   title="Grml Rescue System ($grml)"
+  grub_prep=$(prepare_grub_to_access_device "$device" | sed -e "s/^/        /")
 
   cat << EOF
 menuentry "${title}" {
 
   cat << EOF
 menuentry "${title}" {
-$(prepare_grub_to_access_device "$device" | sed -e "s/^/        /")
+${grub_prep}
         iso_path="${rel_dirname}/${grml}"
         export iso_path
         iso_path="${rel_dirname}/${grml}"
         export iso_path
-        kernelopts="$CUSTOM_BOOTOPTIONS $additional_param"
+        kernelopts=" $CUSTOM_BOOTOPTIONS $additional_param "
         export kernelopts
         loopback loop "${rel_dirname}/$grml"
         set root=(loop)
         export kernelopts
         loopback loop "${rel_dirname}/$grml"
         set root=(loop)