All of lore.kernel.org
 help / color / mirror / Atom feed
* [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

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.