: Save boot record before writing to the dest_drive
PATCH 1/1 : Save boot record before writing to the dest_drive
Greetings!
I have added the following new features:
1) when grub-setup runs, it automatically stores the areas of (mbr + embed) region, which are overwritten, into the file (in the following specified format) in root directory.
2) grub-setup can restore the stored mbr in the following format back to destination drive:
Format:
-------------------------------------------------------------------------------------------------
1st 8 bytes - for embed_region.start [ this is of type grub_uint64_t - 8bytes for sure]
next 8 bytes for the core_size, which is of size_t format. I put 8 bytes for this because if we want to restore the backup file
from i386, it will not cause a problem.
next core_size bytes for core.img
next one byte contains 0xff - used as a signature bit, seperating the embed and mbr regions
next GRUB_DISK_SECTOR_SIZE bytes for mbr
-------------------------------------------------------------------------------------------------
Typical operations:
1) grub-setup --help:
Usage: grub-setup [OPTION]... DEVICE
Set up images to boot from DEVICE.
DEVICE must be a GRUB device (e.g. ``(hd0,1)'').
-b, --boot-image=FILE use FILE as the boot image [default=boot.img] -c, --core-image=FILE use FILE as the core image [default=core.img] -Z, --backup-file=FILE use FILE as the backup/restore file
[default=bootsector.bak]
-d, --directory=DIR use GRUB files in the directory DIR
[default=/boot/grub]
-m, --device-map=FILE use FILE as the device map
[default=/boot/grub/device.map]
-r, --root-device=DEV use DEV as the root device [default=guessed] -f, --force install even if problems are detected -h, --help display this message and exit -z, --restore restore the boot sectors -V, --version print version information and exit -v, --verbose print verbose messages
Therefore when we execute grub-setup -d <dir> /dev/sda (optionally -Z backup-file name), it will create a file called bootsector.bak in <dir> and write to it.
RESTORATION: (grub-setup --restore ) V ( grub-setup -z ) V (grub-setup -z -Z backup_file) .... [ according to the modified help ]
PROBLEMS:
A warning:util/i386/pc/grub-setup.c:93: warning: ‘backup_img’ may be used uninitialized in this function
When I remove the line (496 in the patched one): free(backup_img);
the warning goes off.
I could not understand how to resolve it. I need some help
PATCH DETAILS:
Patch obtained by
svn di util/i386/pc/grub-setup.c
applyihng patch:
patch util/i386/pc/grub-setup.c patch_save_mbr
I tried my best to follow the coding standards and the style. I am very enthusiastic in developing grub. Please suggest better format for storing the boot sectors.
I hope I am not introducing more bugs.
Thank you,
Kashyap Garimella Jagannadh (irc: garimella )
Undergraduate student,
Indian Instition of Technology (IIT), Madras.
Quoted 10 lines · kashyap garimella
Greetings!
I have added the following new features:
1) when grub-setup runs, it automatically stores the areas of (mbr + embed) region, which are overwritten, into the file (in the following specified format) in root directory.
2) grub-setup can restore the stored mbr in the following format back to destination drive:
Thanks for your interest, but I really think this is overkill. Users would still need dd(1) to restore those images, so if they know dd(1), why can't they just use that to back them up in first place?
Signature · 5 lines
Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."
Quoted 10 lines · Robert Millan
Quoted 5 lines · kashyap garimella
Greetings!
I have added the following new features:
1) when grub-setup runs, it automatically stores the areas of (mbr +
embed)
Quoted 3 lines
region, which are overwritten, into the file (in the following specified format) in root directory.
Quoted 7 lines
Quoted 2 lines
2) grub-setup can restore the stored mbr in the following format back to destination drive:
Thanks for your interest, but I really think this is overkill. Users would still need dd(1) to restore those images, so if they know dd(1), why can't they just use that to back them up in first place?
Sir,
I also added a restore option (grub-setup --restore dest_drive). A dd needs to be done explicitly. But with this patch, it will (by default) store the mbr and the areas in embed region ( which are written by grub ). And with the backup file, you can restore
from a live cd also. The backup file contains the start sector and the length of the core image.
There is also a request for this feature http://savannah.gnu.org/bugs/?27058
Thank you,
garimella kashyap
--
Quoted 12 lines
Robert Millan
The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."
_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 19 lines · kashyap garimella
Quoted 10 lines · Robert Millan
Quoted 5 lines · kashyap garimella
Greetings!
I have added the following new features:
1) when grub-setup runs, it automatically stores the areas of (mbr +
embed)
Quoted 3 lines
region, which are overwritten, into the file (in the following specified format) in root directory.
Quoted 7 lines
Quoted 2 lines
2) grub-setup can restore the stored mbr in the following format back to destination drive:
Thanks for your interest, but I really think this is overkill. Users would still need dd(1) to restore those images, so if they know dd(1), why can't they just use that to back them up in first place?
Quoted 8 lines
Sir,
I also added a restore option (grub-setup --restore
dest_drive). A dd needs to be done explicitly. But with this patch, it will (by default) store the mbr and the areas in embed region ( which are written by grub ). And with the backup file, you can restore
from a live cd also. The backup file contains the start sector and the length of the core image.
I'm sorry, but I really see very limited usefulness in this. It's only potentially useful to expert users, but those should know how to backup sectors in their disk already.
But if others reading this think differently, please let it be known...
Signature · 5 lines
Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."
Quoted 3 lines · Robert Millan
I'm sorry, but I really see very limited usefulness in this. It's only potentially useful to expert users, but those should know how to backup sectors in their disk already.
Of course, like many restore facilities, it's only useful if you remembered or realised that you needed to do the backup before the data was overwritten ...
It would be nice to have a straightforward answer to the sort of bug report that goes "argh, I installed Debian/Ubuntu and it ate the special magic boot loader I carefully installed there five years ago", I must admin.
Signature · 1 line
Colin Watson [[email protected]]
Quoted 8 lines · Colin Watson
Quoted 3 lines · Robert Millan
I'm sorry, but I really see very limited usefulness in this. It's only potentially useful to expert users, but those should know how to backup sectors in their disk already.
Of course, like many restore facilities, it's only useful if you remembered or realised that you needed to do the backup before the data was overwritten ...
As a user of grub, I think that automatically making a backup is nice, as long as:
(1) I can specify not to make the backup
(2) It doesn't overwrite any existing file unless I explicitly specify the filename
Quoted 15 lines
It would be nice to have a straightforward answer to the sort of bug report that goes "argh, I installed Debian/Ubuntu and it ate the special magic boot loader I carefully installed there five years ago", I must admin.
--
Colin Watson [[email protected]]_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 5 lines · Colin Watson
Quoted 3 lines · Robert Millan
I'm sorry, but I really see very limited usefulness in this. It's only potentially useful to expert users, but those should know how to backup sectors in their disk already.
Quoted 8 lines
Of course, like many restore facilities, it's only useful if you remembered or realised that you needed to do the backup before the data was overwritten ...
It would be nice to have a straightforward answer to the sort of bug report that goes "argh, I installed Debian/Ubuntu and it ate the special magic boot loader I carefully installed there five years ago", I must admin.
So your point is that if backup is done automatically, this would be useful when dealing with users who didn't plan ahead?
Seems reasonable.
Signature · 5 lines
Robert Millan The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."
kashyap garimella wrote at 2009-09-12:
Quoted 12 lines
Greetings!
I have added the following new features:
1) when grub-setup runs, it automatically stores the areas of (mbr + embed) region, which are overwritten, into the file (in the following specified format) in root directory.
2) grub-setup can restore the stored mbr in the following format back to destination drive:
[...]
Quoted 7 lines
PROBLEMS:
A warning:util/i386/pc/grub-setup.c:93: warning: ‘backup_img’ may be used uninitialized in this function
When I remove the line (496 in the patched one):free(backup_img);
the warning goes off.
I could not understand how to resolve it. I need some help
I looked at the patch and it seems that the problem could be caused by following code:
Quoted 9 lines
(quoted diff) +9
-0+ if (!backup_fp) + { + strcpy(grub_errmsg,"Unable to create the backup file"); + goto unable_to_save; + } + + /* 8 bytes for embed_region.start, another 8 for core_size */ + int backup_img_size = core_size + GRUB_DISK_SECTOR_SIZE + 8 + 8 + 1 /* seperation byte 0xff */ ; + backup_img = (char *) xmalloc (backup_img_size);
...
Review
Quoted 4 lines
(quoted diff) +4
-0+save_finish: + fclose (backup_fp); + free(backup_path); + free(backup_img);
Notice that you're using "goto" to skip initialization of backup_img and you blindly free()ing it in "save_finish".
I think a nice way to fix this (if you want to use "goto") is to use multiple clean up labels. Like this:
+save_cleanup_backup_img:
+ free(backup_img);
+save_cleanup_backup_fp:
+ fclose (backup_fp);
+save_cleanup_backup_path:
+ free(backup_path);
Note that you should release the resources in reverse order compared to the order of acquiring the resources. If you need to bail out after you have acquired the backup_fp but have not yet acquired the backup_img, use "goto save_cleanup_backup_fp".
Signature · 1 line
Mikko
Quoted 32 lines · Mikko Rantalainen
I looked at the patch and it seems that the problem could be caused by following code:
Review
Quoted 8 lines
(quoted diff) +8
-0+ if (!backup_fp) + { + strcpy(grub_errmsg,"Unable to create the backup file"); + goto unable_to_save; + } + + /* 8 bytes for embed_region.start, another 8 for core_size */ + int backup_img_size = core_size + GRUB_DISK_SECTOR_SIZE + 8 + 8 + 1/* seperation byte
0xff*/ ;Quoted 1 line
+
backup_img= (char *) xmalloc (backup_img_size);...
Review
Quoted 4 lines
(quoted diff) +4
-0+save_finish: + fclose (backup_fp); + free(backup_path); + free(backup_img);Notice that you're using "goto" to skip initialization of
backup_imgand you blindly free()ing it in "save_finish".I think a nice way to fix this (if you want to use "goto") is to use multiple clean up labels. Like this:
(quoted diff) +6
-0+save_cleanup_backup_img: + free(backup_img); +save_cleanup_backup_fp: + fclose (backup_fp); +save_cleanup_backup_path: + free(backup_path);
Quoted 5 lines
Note that you should release the resources in reverse order compared to the order of acquiring the resources. If you need to bail out after you have acquired the
backup_fpbut have not yet acquired thebackup_img, use "gotosave_cleanup_backup_fp".
Thank you for the help. I followed your suggestion. I thought of putting a check before freeing, but that did not remove the warning.
ChangeLog entry:
* util/i386/pc/grub-setup.c: Added sections for saving the boot + embed
sector before writing to them. Also added another
section for restoring the boot+embed sectors, given the backup file. The backup file is relative to the root directory.
* grub-setup takes more options ( -Z to specify the backup file, -z to
restore the backup file )
I did not use indent(1), because it indented many other parts of the code, which I did not write. I indented only my code.
Thank you,
Kashyap Garimella Jagannadh (irc: garimella )
Undergraduate student,
Department of Computer Science & Eng
Indian Instition of Technology (IIT), Madras.