grub-devel
[Top][All Lists]
Advanced

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

[PATCH 1/2] Backup old boot sectors before installation


From: Zhu Yi
Subject: [PATCH 1/2] Backup old boot sectors before installation
Date: Wed, 6 Jan 2010 17:10:03 +0800

From: Zhu Yi <address@hidden>

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 <address@hidden>
---
 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





reply via email to

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