From 8ac8a961503537d2753d9a163834a8108b871880 Mon Sep 17 00:00:00 2001
From: Dinko Korunic <kreator@carnet.hr>
Date: Tue, 26 May 2009 18:11:43 +0200
Subject: [PATCH] - jos fixeva -- u finalnom stageu grub instalacije u devfs
 slucajnu: popravi sve root= opcije kao i finalno vrati
 nazad device.map na originalno stanje... kako bi stroj
 radio normalno nakon reboota

---
 debian/postinst   |   27 +++++++++++++++++++++++++++
 grub-functions.sh |   28 ++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/debian/postinst b/debian/postinst
index 79a5a79..e279cfb 100755
--- a/debian/postinst
+++ b/debian/postinst
@@ -154,6 +154,7 @@ if [ ! -e /etc/udev/rules.d/udev.rules ]; then
     ln -s ../udev.rules /etc/udev/rules.d/udev.rules
 fi
 udevcontrol reload_rules >/dev/null 2>&1 || true
+udevsettle || true
 echo -n " udev"
 
 # update device map if possible
@@ -189,6 +190,7 @@ if [ -h "$install_device" ]; then
     if [ -z "$install_device_resolved" ]; then
         install_device_resolved="$install_device"
     fi
+
     # try to fix device map with symlink resolved device
     if [ "x$install_device_resolved" != "x$install_device" ]; then
         cp_check_and_sed '^\(hd0\)' \
@@ -217,6 +219,31 @@ cp_check_and_sed '^# kopt=' \
 /usr/sbin/update-grub >/dev/null 2>&1 || true
 echo -n " grub7"
 
+# and final stage of devfs/udev brokeness fix
+if [ "x$install_device_resolved" != "x$install_device" ]; then
+    # get original (symlinked device)
+    root_device=$(find_device_nonresolved "/")
+    if [ -z "$root_device" ]; then
+        root_device=$(find_root_device)
+    fi
+
+    # fix (hd0) mapping
+    cp_check_and_sed '^\(hd0\)' \
+        "s;^(hd0).*;(hd0)	$install_device;" "$device_map" \
+        || true
+
+    # fix global root= invocation
+    cp_check_and_sed '^# kopt=' \
+      "s;^# kopt=\(.*\)root=[^[:space:]]*\(.*\);# kopt=\1root=$root_device\2;g" \
+          "$menu_file" || true
+
+    # fix root= invocation for individual kernel profiles
+    cp_check_and_sed '^kernel' \
+      "s;\(^kernel.*\)root=[^[:space:]]*\(.*\);\1root=$root_device\2;g" \
+          "$menu_file" || true
+fi
+echo -n " grub8"
+
 # restore execshield state
 if [ ! -z "$SHIELD" ]; then
     sysctl -w -e "kernel.exec-shield=$SHIELD" >/dev/null 2>&1
diff --git a/grub-functions.sh b/grub-functions.sh
index e68bb42..260c611 100755
--- a/grub-functions.sh
+++ b/grub-functions.sh
@@ -75,6 +75,34 @@ find_device ()
 	echo $device
 }
 
+find_device_nonresolved ()
+{
+	mount_point=$1
+
+	# Autodetect current root device
+	device=
+	if [ -f /etc/fstab ] ; then
+		while read DEV MNT FOO; do
+			if `echo "$DEV" | grep -q "^#"`; then
+				continue
+			fi
+			if [ "$MNT" = "$mount_point" ]; then
+				device="$DEV";
+			fi
+		done < /etc/fstab
+	fi
+
+	if [ -n "$device" ] ; then
+		case "$device" in
+			LABEL=* | UUID=*)
+				device=`findfs $device`
+			;;
+		esac
+	fi
+
+	echo $device
+}
+
 find_root_device ()
 {
 	device=$(find_device "/")
-- 
1.7.10.4