grub-devel
[Top][All Lists]
Advanced

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

[PATCH 2/2] Add grub-install --restore option


From: Zhu Yi
Subject: [PATCH 2/2] Add grub-install --restore option
Date: Wed, 6 Jan 2010 17:10:04 +0800

From: Zhu Yi <address@hidden>

Add the --restore option to restore the previous boot sectors from a
backup image (created by grub-setup). A simple `dd` won't work when
partitioning has been changed after the backup. In this case, a script
is useful as it only restores the MBR and the boot sectors overwritten
by core.img (embed region is excluded) to the install device. So this
method is recommended even for advanced users.

Signed-off-by: Zhu Yi <address@hidden>
---
 util/i386/pc/grub-install.in |   29 +++++++++++++++++++++++++++++
 1 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/util/i386/pc/grub-install.in b/util/i386/pc/grub-install.in
index 8a06213..4d0043e 100644
--- a/util/i386/pc/grub-install.in
+++ b/util/i386/pc/grub-install.in
@@ -51,6 +51,7 @@ no_floppy=
 force_lba=
 recheck=no
 debug=no
+restore=
 
 if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
     disk_module=biosdisk
@@ -77,6 +78,7 @@ Install GRUB on your drive.
   --no-floppy             do not probe any floppy drive
   --recheck               probe a device map even if it already exists
   --force                 install even if problems are detected
+  --restore               restore the previous boot sectors
 EOF
 if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
     cat <<EOF
@@ -129,6 +131,10 @@ for option in "$@"; do
        debug=yes ;;
     -f | --force)
         setup_force="--force" ;;
+    --restore)
+        restore="$grub_prefix/bootsectors.bak" ;;
+    --restore=*)
+        restore=`echo "$option" | sed 's/--restore=//'` ;;
     -*)
        echo "Unrecognized option \`$option'" 1>&2
        usage
@@ -203,6 +209,29 @@ else
     exit 1
 fi
 
+if test -f "$restore"; then
+    if test `stat -c%s $restore` -eq 512; then
+       dd if=$restore of=$install_device bs=512 count=1
+        exit 0
+    fi
+    start=`od -j92 -N8 -An -td8 $grubdir/boot.img`
+
+    # Synaty check
+    if test $((`stat -c%s $restore` - $start * 512)) -ne \
+       `stat -c%s $grubdir/core.img`; then
+        echo "Error: $restore doesn't match core.img, restore aborted."
+        exit 1
+    fi
+
+    # Restore
+    dd if=$restore of=$install_device bs=512 count=1 > /dev/null 2>&1
+    dd if=$restore of=$install_device skip=512 seek=`expr $start \* 512` \
+       bs=1 > /dev/null 2>&1
+    rm -f $restore
+    echo "Restore boot sectors from $restore successfully"
+    exit 0
+fi
+
 # Create the GRUB directory if it is not present.
 test -d "$bootdir" || mkdir "$bootdir" || exit 1
 test -d "$grubdir" || mkdir "$grubdir" || exit 1
-- 
1.5.3.6





reply via email to

[Prev in Thread] Current Thread [Next in Thread]