Backup old boot sectors before installation
PATCH 1/2 Backup old boot sectors before installation
From: Zhu Yi <[email protected]>
Make grub-setup backup the old boot sectors into a file (bootsectors.bak) before overwriting it. The backup image starts from the MBR to the end of core.img position (including embed regions). This makes it possible for a user to later restore it with dd or other methods.
--- util/i386/pc/grub-setup.c | 33 +++++++++++++++++++++++++++++++++ 1 files changed, 33 insertions(+), 0 deletions(-)
util/i386/pc/grub-setup.c +33 -0
@@ -53,6 +53,7 @@ static const grub_gpt_part_type_t grub_gpt_partition_type_bios_boot = GRUB_GPT_P#define DEFAULT_BOOT_FILE "boot.img"#define DEFAULT_CORE_FILE "core.img" +#define DEFAULT_BACKUP_FILE "bootsectors.bak"/* This is the blocklist used in the diskboot image. */ struct boot_blocklist
@@ -396,6 +397,33 @@ setup (const char *dir, block->len =0 ; block->segment =0 ; +int grub_disk_backup (grub_disk_t disk,grub_size_t size,const char *path) + { +char *tmp_buf; + +grub_util_info ("opening the backup file `%s'" , path); + fp =fopen (path,"wb" ); +if (! fp) +return -1 ; + + tmp_buf =xmalloc (size); +if (grub_disk_read (disk,0 ,0 , size, tmp_buf) !=GRUB_ERR_NONE ) { +fclose (fp); +return -1 ; + } + +grub_util_write_image (tmp_buf, size, fp); + +fclose (fp); +return 0 ; + } + +/* Backup old boot sectors */ +if (grub_disk_backup (dest_dev->disk, +GRUB_DISK_SECTOR_SIZE * embed_region.start + core_size, +grub_util_get_path (dir,DEFAULT_BACKUP_FILE ))) +grub_util_error ("failed to backup previous boot sectors" ); +/* Write the core image onto the disk. */ if (grub_disk_write (dest_dev->disk, embed_region.start,0 , core_size, core_img))grub_util_error ("%s" , grub_errmsg);
@@ -548,6 +576,11 @@ unable_to_embed:grub_util_write_image (core_img,GRUB_DISK_SECTOR_SIZE *2 , fp);fclose (fp); +/* Backup MBR only */ +if (grub_disk_backup (dest_dev->disk,GRUB_DISK_SECTOR_SIZE , +grub_util_get_path (dir,DEFAULT_BACKUP_FILE ))) +grub_util_error ("failed to backup previous boot sectors" ); +/* Write the boot image onto the disk. */ if (grub_disk_write (dest_dev->disk,0 ,0 ,GRUB_DISK_SECTOR_SIZE , boot_img))grub_util_error ("%s" , grub_errmsg);
Signature · 1 line
1.5.3.6
PATCH 2/2 Add grub-install --restore option
From: Zhu Yi <[email protected]>
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.
--- util/i386/pc/grub-install.in | 29 +++++++++++++++++++++++++++++ 1 files changed, 29 insertions(+), 0 deletions(-)
util/i386/pc/grub-install.in +29 -0
@@ -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
Signature · 1 line
1.5.3.6
Re: [PATCH 2/2] Add grub-install --restore option
Hi,
Quoted 47 lines · Zhu Yi
util/i386/pc/grub-install.in +18
-0@@ -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
util/i386/pc/grub-install.in else · line 233
Quoted 10 lines
util/i386/pc/grub-install.in +10
-0+ 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
Please don't add this to grub-install. This kind of highly BIOS-specific logic would suit much better in grub-setup.
Signature · 3 lines
Robert Millan "Be the change you want to see in the world" -- Gandhi
Re: [PATCH 2/2] Add grub-install --restore option
Quoted 55 lines · Robert Millan
Hi,
Quoted 52 lines · Zhu Yi
util/i386/pc/grub-install.in +23
-0@@ -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` \
Quoted 8 lines
util/i386/pc/grub-install.in else · line 233
Quoted 5 lines
util/i386/pc/grub-install.in +5
-0+ bs=1 > /dev/null 2>&1 + rm -f $restore + echo "Restore boot sectors from $restore successfully" + exit 0 +fiPlease don't add this to grub-install. This kind of highly BIOS-specific logic would suit much better in grub-setup.
Do you prefer to implement above in C code and add a "--restore" option to grub-setup or create a separate script for doing this?
Thanks,
-yi
Re: [PATCH 2/2] Add grub-install --restore option
Quoted 6 lines · Zhu Yi
Quoted 3 lines
Please don't add this to grub-install. This kind of highly BIOS-specific logic would suit much better in grub-setup.
Do you prefer to implement above in C code and add a "--restore" option to grub-setup or create a separate script for doing this?
I think a --restore option to grub-setup would be more appropiate, since the save routine is also there (also, C suits better for this kind of byte mangling IMO).
What do others think?
Signature · 3 lines
Robert Millan "Be the change you want to see in the world" -- Gandhi