* [PATCH 1/2] Backup old boot sectors before installation
@ 2010-01-06 9:10 Zhu Yi
2010-01-06 9:10 ` [PATCH 2/2] Add grub-install --restore option Zhu Yi
0 siblings, 1 reply; 5+ messages in thread
From: Zhu Yi @ 2010-01-06 9:10 UTC (permalink / raw)
To: rmh; +Cc: grub-devel, Zhu Yi, Zhu Yi
From: Zhu Yi <chuyee@octavia.sh.intel.com>
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.
Signed-off-by: Zhu Yi <yi.zhu@intel.com>
---
util/i386/pc/grub-setup.c | 33 +++++++++++++++++++++++++++++++++
1 files changed, 33 insertions(+), 0 deletions(-)
diff --git a/util/i386/pc/grub-setup.c b/util/i386/pc/grub-setup.c
index c536be0..b045e23 100644
--- a/util/i386/pc/grub-setup.c
+++ b/util/i386/pc/grub-setup.c
@@ -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);
--
1.5.3.6
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/2] Add grub-install --restore option
2010-01-06 9:10 [PATCH 1/2] Backup old boot sectors before installation Zhu Yi
@ 2010-01-06 9:10 ` Zhu Yi
2010-01-06 13:50 ` Robert Millan
0 siblings, 1 reply; 5+ messages in thread
From: Zhu Yi @ 2010-01-06 9:10 UTC (permalink / raw)
To: rmh; +Cc: grub-devel, Zhu Yi, Zhu Yi
From: Zhu Yi <chuyee@octavia.sh.intel.com>
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 <yi.zhu@intel.com>
---
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] Add grub-install --restore option
2010-01-06 9:10 ` [PATCH 2/2] Add grub-install --restore option Zhu Yi
@ 2010-01-06 13:50 ` Robert Millan
2010-01-07 1:10 ` Zhu Yi
0 siblings, 1 reply; 5+ messages in thread
From: Robert Millan @ 2010-01-06 13:50 UTC (permalink / raw)
To: Zhu Yi; +Cc: grub-devel, Zhu Yi
Hi,
On Wed, Jan 06, 2010 at 05:10:04PM +0800, Zhu Yi wrote:
> 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
Please don't add this to grub-install. This kind of highly BIOS-specific
logic would suit much better in grub-setup.
--
Robert Millan
"Be the change you want to see in the world" -- Gandhi
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] Add grub-install --restore option
2010-01-06 13:50 ` Robert Millan
@ 2010-01-07 1:10 ` Zhu Yi
2010-01-07 1:36 ` Robert Millan
0 siblings, 1 reply; 5+ messages in thread
From: Zhu Yi @ 2010-01-07 1:10 UTC (permalink / raw)
To: Robert Millan; +Cc: grub-devel@gnu.org, Zhu Yi
On Wed, 2010-01-06 at 21:50 +0800, Robert Millan wrote:
> Hi,
>
> On Wed, Jan 06, 2010 at 05:10:04PM +0800, Zhu Yi wrote:
> > 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
>
> 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?
Thanks,
-yi
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] Add grub-install --restore option
2010-01-07 1:10 ` Zhu Yi
@ 2010-01-07 1:36 ` Robert Millan
0 siblings, 0 replies; 5+ messages in thread
From: Robert Millan @ 2010-01-07 1:36 UTC (permalink / raw)
To: Zhu Yi; +Cc: grub-devel@gnu.org, Zhu Yi
On Thu, Jan 07, 2010 at 09:10:05AM +0800, Zhu Yi wrote:
> >
> > 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?
--
Robert Millan
"Be the change you want to see in the world" -- Gandhi
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-01-07 1:37 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-06 9:10 [PATCH 1/2] Backup old boot sectors before installation Zhu Yi
2010-01-06 9:10 ` [PATCH 2/2] Add grub-install --restore option Zhu Yi
2010-01-06 13:50 ` Robert Millan
2010-01-07 1:10 ` Zhu Yi
2010-01-07 1:36 ` Robert Millan
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.