Backup old boot sectors before installation
PATCH 1/1 Backup old boot sectors before installation
Add a feature to backup the old boot sectors before overwritting them with grub2 boot and core images. Users can later recover the previous boot sectors with grub-install --recover option.
P.S. I found this might be a useful feature after I overwrote my second PGP encrypted hard disk (Windows XP installed) boot sectors by grub2 by mistake.
---
util/i386/pc/grub-install.in +37 -0
@@ -51,6 +51,7 @@ no_floppy= force_lba= recheck=no debug=no +recover= 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 + --recover 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" ;; + --recover) + recover="$grub_prefix/bootsectors.bak" ;; + --recover=*) + recover=`echo "$option" | sed 's/--recover=//'` ;; -*) echo "Unrecognized option \`$option'" 1>&2 usage
@@ -203,6 +209,37 @@ else exit 1 fi +if test -f "$recover"; then + if test `stat -c%s $recover` -eq 512; then + echo "The backup file $recover contains MBR only. Did you install" + echo "grub2 using UNRELIABLE blocklist with --force? If so, try to" + echo "recover with `dd if=$recover of=$1 bs=512 count=1`. But you" + echo "take your own risk!" + exit 1 + fi + start=`od -j512 -N8 -An -td8 $recover` + start2=`od -j92 -N8 -An -td8 $grubdir/boot.img` + + # Synaty check for the recovery file + if test $start -ne $start2; then + echo "Error: start position of $recover doesn't match with boot.img" + exit 1 + fi + if test $((`stat -c%s $recover` - 520)) -ne \ + `stat -c%s $grubdir/core.img`; then + echo "Error: $recover doesn't match current core.img" + exit 1 + fi + + # Recovery + dd if=$recover of=$install_device bs=512 count=1 > /dev/null 2>&1 + dd if=$recover of=$install_device skip=520 seek=`expr $start \* 512` \ + bs=1 > /dev/null 2>&1 + rm -f $recover + echo "Recover boot sectors from $recover 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
util/i386/pc/grub-setup.c +80 -3
162 lines not on this page · raw message
Am Freitag, den 11.12.2009, 17:26 +0800 schrieb Zhu Yi:
Quoted 9 lines
Add a feature to backup the old boot sectors before overwritting them with grub2 boot and core images. Users can later recover the previous boot sectors with grub-install --recover option.
P.S. I found this might be a useful feature after I overwrote my second PGP encrypted hard disk (Windows XP installed) boot sectors by grub2 by mistake.Signed-off-by Zhu Yi <[email protected]>
Someone already made a patch for this inside grub-setup See here and also for the discussion of it:
http://lists.gnu.org/archive/html/grub-devel/2009-09/msg00242.html
Signature · 2 lines
Felix Zielcke Proud Debian Maintainer and GNU GRUB developer
Hi,
Quoted 9 lines · Felix Zielcke
Am Freitag, den 11.12.2009, 17:26 +0800 schrieb Zhu Yi:
Quoted 8 lines
Add a feature to backup the old boot sectors before overwritting them with grub2 boot and core images. Users can later recover the previous boot sectors with grub-install --recover option.
P.S. I found this might be a useful feature after I overwrote my second PGP encrypted hard disk (Windows XP installed) boot sectors by grub2 by mistake.
Quoted 5 lines
Quoted 1 line
Signed-off-by Zhu Yi <[email protected]>Someone already made a patch for this inside grub-setup See here and also for the discussion of it:
http://lists.gnu.org/archive/html/grub-devel/2009-09/msg00242.html
I like the idea, and I think that as Colin pointed it out it can be useful.
I haven't watched to any of the implementations.
Signature · 2 lines
Carles Pina i Estany http://pinux.info
Quoted 4 lines · Felix Zielcke
Someone already made a patch for this inside grub-setup See here and also for the discussion of it:
http://lists.gnu.org/archive/html/grub-devel/2009-09/msg00242.html
Thanks. If I knew this patch, I won't try to write one my own. I did do some basic search and asked in #grub2 IRC before writing it though.
Anyway, I did a quick review for Garimella's patch. Both the ideas of the two patches are the same: backup the mbr and boot sectors will be overwritten by core.img to a file before grub2 overwrites them. The implementations slightly differ in:
1. The format of the backup file.
The old boot sectors and the start of embed_region position is required for both patches. Garimella also added some redundant fields (i.e. image size and 0xff) for integrity checking. I think it's a good idea. But something like a md5/sha1 checksum should be even better.
2. The image backup and recovery.
Both of the patches choose to backup the old boot sectors in grub-setup.c. On the recovery, Garimella selected to do it in grub-setup.c and I used grub-install script with dd(1). I don't think this is a very big deal and either way is OK. The reason I choose grub-install is I want to keep the grub-setup.c as clean as possible. Because given the backup file format, one can recover it with dd(1) even without any help with grub-setup.
3. The backup policy.
Garimella's patch backups every time before grub-setup overwrote the boot sectors. There might be a problem when someone run grub-setup/grub-install twice. So the backup image ends up with the grub2 boot sectors. This makes the recovery for the "real" boot sectors impossible. My patch won't overwrite if a previous backup image already existed. grub-install will remove the backup image after a successful recovery.
4. Blocklists installation.
This is not very important because blocklists is not recommended. To be completeness, my patch backups the mbr if blocklists are used due to embedding is not possible. This will be detected by grub-install as well.
In conclusion, I believe this backup feature is useful and either implementation should do the work. If we can merge the good parts of them, we can get a better one. I just curious why the thread is stuck since September. Any blocking issues with it? Can we keep the topic moving forward? Your comments are highly welcome.
Thanks,
-yi
Quoted 9 lines · Zhu Yi
3. The backup policy.
Garimella's patch backups every time before grub-setup overwrote the boot sectors. There might be a problem when someone run grub-setup/grub-install twice. So the backup image ends up with the grub2 boot sectors. This makes the recovery for the "real" boot sectors impossible. My patch won't overwrite if a previous backup image already existed. grub-install will remove the backup image after a successful recovery.
maybe the boot sector is small enough that we could just keep separate additional backup files each time grub-setup/grub-install is used? e.g. backup-1
backup-2
backup-3
or maybe using date/time instead of sequential numbering would be nicer.
-Isaac
Quoted 10 lines · Isaac Dupree
maybe the boot sector is small enough that we could just keep separate
additional backup files each time grub-setup/grub-install is used? e.g.
backup-1
backup-2
backup-3or maybe using date/time instead of sequential numbering would be nicer.
Yup, I thought this before. This is certainly an option, but it also requires the user to remember what is 1, 2, 3 or what did I do on date1, date2, date3 on the recovery.
Thanks,
-yi
Quoted 36 lines · Zhu Yi
Quoted 4 lines · Felix Zielcke
Someone already made a patch for this inside grub-setup See here and also for the discussion of it:
http://lists.gnu.org/archive/html/grub-devel/2009-09/msg00242.htmlThanks. If I knew this patch, I won't try to write one my own. I did do some basic search and asked in #grub2 IRC before writing it though.
Anyway, I did a quick review for Garimella's patch. Both the ideas of the two patches are the same: backup the mbr and boot sectors will be overwritten by core.img to a file before grub2 overwrites them. The implementations slightly differ in:
1. The format of the backup file.
The old boot sectors and the start of
embed_regionposition is required for both patches. Garimella also added some redundant fields (i.e. image size and0xff) for integrity checking. I think it's a good idea. But something like a md5/sha1 checksum should be even better.2. The image backup and recovery.
Both of the patches choose to backup the old boot sectors in
grub-setup.c. On the recovery, Garimella selected to do it ingrub-setup.cand I used grub-install script with dd(1). I don't think this is a very big deal and either way is OK. The reason I choose grub-install is I want to keep thegrub-setup.cas clean as possible. Because given the backup file format, one can recover it with dd(1) even without any help with grub-setup.md5/sha sum is a good idea. If you want, I can implement it.
3. The backup policy.
Quoted 8 lines
Garimella's patch backups every time before grub-setup overwrote the boot sectors. There might be a problem when someone run grub-setup/grub-install twice. So the backup image ends up with the grub2 boot sectors. This makes the recovery for the "real" boot sectors impossible. My patch won't overwrite if a previous backup image already existed. grub-install will remove the backup image after a successful recovery.
I think this is questionable. Some prefer to backup only the previous copy of mbr. Some prefer to save the oldest copy.
We can take the users' opinion.
Quoted 8 lines
4. Blocklists installation.
This is not very important because blocklists is not recommended. To be completeness, my patch backups the mbr if blocklists are used due to embedding is not possible. This will be detected by grub-install as well.
Quoted 7 lines
In conclusion, I believe this backup feature is useful and either implementation should do the work. If we can merge the good parts of them, we can get a better one. I just curious why the thread is stuck since September. Any blocking issues with it? Can we keep the topic moving forward? Your comments are highly welcome.
The reason is very simple. If you are good user, you should have already
backed up your copy on your own. And this feature is not necessary.
-Garimella Kashyap
Quoted 8 lines
Quoted 6 lines
In conclusion, I believe this backup feature is useful and either implementation should do the work. If we can merge the good parts of them, we can get a better one. I just curious why the thread is stuck since September. Any blocking issues with it? Can we keep the topic moving forward? Your comments are highly welcome.
The reason is very simple. If you are good user, you should have already backed up your copy on your own. And this feature is not necessary.
That opinion was voiced, then countered and I think even the author of that opinion eventually retracted it.
An automatic backup is valuable both for savvy users who know how but forgot, and for those who will find the backup and restore procedures online only after their computer is bricked.
Quoted 11 lines
-Garimella Kashyap
_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 3 lines · [email protected] wrote:
An automatic backup is valuable both for savvy users who know how but forgot, and for those who will find the backup and restore procedures online only after their computer is bricked.
hmm....
bootloader installation is often done by distro install-CDs.
Will the distro be smart enough to save these backups to the disk somewhere sensible, rather than to the temporary in-memory filesystem?
I'm... Just being paranoid enough to ask this question -- I never quite trust by install CDs not to install a bootloader, because I have to find the right place in the GUI install sequence to disable doing so (and hope that disabling works -- as a supposedly "advanced" option, sometimes it's not been tested enough). Anything GRUB2 upstream can do to make this less risky will make me happy!
Quoted 6 lines · Isaac Dupree
hmm....
bootloader installation is often done by distro install-CDs.Will the distro be smart enough to save these backups to the disk somewhere sensible, rather than to the temporary in-memory filesystem?
The backup files will (normally) be stored in /boot/grub/ together with grub.cfg and core.img. I assume it is not in memory. No?
Thanks,
-yi
Quoted 9 lines · Zhu Yi
Quoted 5 lines · Isaac Dupree
hmm....
bootloader installation is often done by distro install-CDs.Will the distro be smart enough to save these backups to the disk somewhere sensible, rather than to the temporary in-memory filesystem?
The backup files will (normally) be stored in
/boot/grub/together with grub.cfg and core.img.
if it's there, then excellent, you are right. After all, /boot/grub needs to be permanently on the disk in order for the installed bootloader to work, also :-)
Quoted 10 lines · kashyap garimella
In conclusion, I believe this backup feature is useful and either
implementation should do the work. If we can merge the good parts of
them, we can get a better one. I just curious why the thread is stuck
since September. Any blocking issues with it? Can we keep the topic
moving forward? Your comments are highly welcome.
Quoted 3 lines
The reason is very simple. If you are good user, you should have already backed up your copy on your own. And this feature is not necessary.
Is grub2 only designed for "good users"? I'm not. Or at least, Ubuntu update-grub2 script is not. It messes up with the two disks (with drivemap? not sure) in my laptop and overwrote PGP encrypted hard disk. That's the motivation for me to write this patch. Boot sectors are important. You know how to backup is one story. But when to backup is another and even important in this case.
Thanks,
-yi
Quoted 7 lines · Zhu Yi
Add a feature to backup the old boot sectors before overwritting them with grub2 boot and core images. Users can later recover the previous boot sectors with grub-install --recover option.
P.S. I found this might be a useful feature after I overwrote my second PGP encrypted hard disk (Windows XP installed) boot sectors by grub2 by mistake.
I think making a backup is a fine idea, but I'd rather avoid the "option creep". It doesn't hurt to simply dump the files in /boot/grub/. If user later discovers that valuable data was overwritten, she can figure out how to call dd just as she can figure out the right parameters for grub-install.
Signature · 3 lines
Robert Millan "Be the change you want to see in the world" -- Gandhi
Quoted 5 lines · Robert Millan
I think making a backup is a fine idea, but I'd rather avoid the "option creep". It doesn't hurt to simply dump the files in
/boot/grub/. If user later discovers that valuable data was overwritten, she can figure out how to call dd just as she can figure out the right parameters for grub-install.
OK. My only concern is the start position of grub core.img (as in disk sectors) is a variable (see find_usable_region_xxx). This ends up the backup file is not a simple dump. I used below format in my patch.
<512 bytes MBR> + <U64 start position> + <sizeof(core.img) old sectors>
Option 2 is to put the start position in a separated file.
Option 3 is to use the information in boot.img (as I used in my patch as a verification method) to get the start position, i.e. od -j92 -N8 -An -td8 $grubdir/boot.img.
Option 4 is to backup everything from MBR to the end of core.img (including the holes in the middle). This ends up with a bigger image.
Option 1~3 requires the recover has the knowledge of the backup file format somehow, but a simple dd is enough for option 4. What do you think?
Thanks,
-yi
Quoted 19 lines · Zhu Yi
Quoted 5 lines · Robert Millan
I think making a backup is a fine idea, but I'd rather avoid the "option creep". It doesn't hurt to simply dump the files in
/boot/grub/. If user later discovers that valuable data was overwritten, she can figure out how to call dd just as she can figure out the right parameters for grub-install.OK. My only concern is the start position of grub core.img (as in disk sectors) is a variable (see
find_usable_region_xxx). This ends up the backup file is not a simple dump. I used below format in my patch.<512 bytes MBR> + <U64 start position> + <sizeof(core.img) old sectors>
Option 2 is to put the start position in a separated file.
Option 3 is to use the information in boot.img (as I used in my patch as a verification method) to get the start position, i.e.
od -j92 -N8 -An -td8 $grubdir/boot.img.
Quoted 6 lines
Option 4 is to backup everything from MBR to the end of core.img (including the holes in the middle). This ends up with a bigger image.
Option 1~3 requires the recover has the knowledge of the backup file format somehow, but a simple dd is enough for option 4. What do you think?
If the partitioning had been changed in the mean time, 4 would overwrite it with the older values. Similar problem for a weird setup with partitions in between (I dunno, might be possible with GPT or other weirdness).
I think option 2 or 3 is soundest. Option 2 would have three separate files of information, (right?), and there must be a pretty straightforwards dd option to skip to the correct start position. Option 3 would omit the "start position" file... but that file is really cheap, maybe we'd better keep things simple for restorers and stick with three separate files.(Or, we could do something weird like putting start-position somewhere in the filename of the backup.)
-Isaac
Quoted 8 lines · Isaac Dupree
Quoted 3 lines
Option 1~3 requires the recover has the knowledge of the backup file format somehow, but a simple dd is enough for option 4. What do you think?
If the partitioning had been changed in the mean time, 4 would overwrite it with the older values. Similar problem for a weird setup with partitions in between (I dunno, might be possible with GPT or other weirdness).
Good point.
Quoted 7 lines
I think option 2 or 3 is soundest. Option 2 would have three separate files of information, (right?), and there must be a pretty straightforwards
ddoption to skip to the correct start position. Option 3 would omit the "start position" file... but that file is really cheap, maybe we'd better keep things simple for restorers and stick with three separate files.(Or, we could do something weird like putting start-position somewhere in the filename of the backup.)
This requires the restorer to understand the role of each of the 3 (or
2) files and use `dd' twice with correct options. So an important thing is, we need to document this somewhere. And I think the best place is to "document" it in the grub-install script. Because this will enable the not-so-advanced user to recover her boot sectors easily as well.
If we decide so, we go back to option 1 (my original implementation). Because the backup file format is not important any more as it is totally transparent to the restorers. Am I right?
Thanks,
-yi
Quoted 26 lines · Zhu Yi
Quoted 8 lines · Isaac Dupree
Quoted 3 lines
Option 1~3 requires the recover has the knowledge of the backup file format somehow, but a simple dd is enough for option 4. What do you think?
If the partitioning had been changed in the mean time, 4 would overwrite it with the older values. Similar problem for a weird setup with partitions in between (I dunno, might be possible with GPT or other weirdness).
Good point.
Quoted 7 lines
I think option 2 or 3 is soundest. Option 2 would have three separate files of information, (right?), and there must be a pretty straightforwards
ddoption to skip to the correct start position. Option 3 would omit the "start position" file... but that file is really cheap, maybe we'd better keep things simple for restorers and stick with three separate files.(Or, we could do something weird like putting start-position somewhere in the filename of the backup.)This requires the restorer to understand the role of each of the 3 (or
2) files and use `dd' twice with correct options. So an important thing is, we need to document this somewhere. And I think the best place is to "document" it in the grub-install script. Because this will enable the not-so-advanced user to recover her boot sectors easily as well.
Quoted 3 lines
If we decide so, we go back to option 1 (my original implementation). Because the backup file format is not important any more as it is totally transparent to the restorers. Am I right?
Another option would be to start the backup file with "#!/bin/sh" ...
Quoted 11 lines
Thanks,
-yi_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 6 lines · On Tue, 2009-12-29 at 11:59 +0800, [email protected] wrote:
Quoted 1 line
If we decide so, we go back to option 1 (my original
implementation).
Quoted 2 lines
Because the backup file format is not important any more as it is totally transparent to the restorers. Am I right?
Another option would be to start the backup file with "#!/bin/sh" ...
Create this shell script in grub-setup.c? I don't see how this is less invasive than changing grub-install script. But if everyone agree to add the sharutils package dependency (for uuencode/uudecode), I can implement this in V2.
Thanks,
-yi
Quoted 6 lines · Zhu Yi
Quoted 5 lines · On Tue, 2009-12-29 at 11:59 +0800, [email protected] wrote:
Quoted 1 line
If we decide so, we go back to option 1 (my original
implementation).
Quoted 2 lines
Because the backup file format is not important any more as it is totally transparent to the restorers. Am I right?
Quoted 6 lines
Quoted 1 line
Another option would be to start the backup file with "#!/bin/sh" ...
Create this shell script in
grub-setup.c? I don't see how this is less invasive than changing grub-install script. But if everyone agree to add the sharutils package dependency (for uuencode/uudecode), I can implement this in V2.
Erm, no, please don't do such a thing. C code is fine :-)
Signature · 3 lines
Robert Millan "Be the change you want to see in the world" -- Gandhi