grub-install: Add backup and restore
PATCH v5 1/1 grub-install: Add backup and restore
From: Dimitri John Ledkov <[email protected]>
Refactor clean_grub_dir to create a backup of all the files, instead of just irrevocably removing them as the first action. If available, register atexit handle to restore the backup if errors occur before point of no return, or remove the backup if everything was successful. If atexit is not available, the backup remains on disk for manual recovery.
Some platforms defined a point of no return, i.e. after modules & core images were updated. Failures from any commands after that stage are ignored, and backup is cleanedup. For example, on EFI platforms update is not reverted when efibootmgr fails.
Extra care is taken to ensure atexit handler is only invoked by the parent process and not any children forks. Some older grub codebases can invoke parent atexit hooks from forks, which can mess up the backup.
This allows safer upgrades of MBR & modules, such that modules/images/fonts/translations are consistent with MBR in case of errors. For example accidental grub-install /dev/non-existent-disk currently clobbers and upgrades modules in /boot/grub, despite not actually updating any MBR.
This patch only handles backup and restore of files copied to /boot/grub. This patch does not perform backup (or restoration) of MBR itself or blocklists. Thus when installing i386-pc platform, corruption may still occur with MBR and blocklists which will not be attempted to be automatically recovered.
Also add modinfo.sh and *.efi to the cleanup/backup/restore codepath, to ensure it is also cleaned / backed up / restored.
---
changes since v4:
+ change ponr from size_t to int (there is no bool in grub yet)
+ declare loop variable at the start of the function
+ format one more comment correctly
+ add comment about significance to set ponr
configure.ac | 2 +- include/grub/util/install.h | 13 ++++ util/grub-install-common.c | 143 ++++++++++++++++++++++++++++++++---- util/grub-install.c | 40 ++++++++-- util/grub-mknetdir.c | 3 + util/grub-mkrescue.c | 3 + util/grub-mkstandalone.c | 2 + 7 files changed, 182 insertions(+), 24 deletions(-)
configure.ac +1 -1
@@ -419,7 +419,7 @@ else fi # Check for functions and headers. -AC_CHECK_FUNCS(posix_memalign memalign getextmntent) +AC_CHECK_FUNCS(posix_memalign memalign getextmntent atexit) AC_CHECK_HEADERS(sys/param.h sys/mount.h sys/mnttab.h limits.h) # glibc 2.25 still includes sys/sysmacros.h in sys/types.h but emits deprecation
include/grub/util/install.h +13 -0
@@ -275,4 +275,17 @@ extern char *grub_install_copy_buffer;int grub_install_is_short_mbrgap_supported (void ); +/* +* grub-install-common tries to make backups of modules & auxilary +* files, and restore the backup upon failure to install core.img. There +* are platforms with additional actions after modules & core got +* installed in place. It is a point of no return, as core.img cannot be +* reverted from this point onwards, and new modules should be kept +* installed. Before performing these additional actions raise +* grub_install_backup_ponr flag, this way failure to perform additional +* actions will not result in reverting new modules to the old +* ones. (i.e. in case efivars updates fails) +*/ +extern int grub_install_backup_ponr; +#endif
util/grub-install-common.c +128 -15
166 lines not on this page · raw message
util/grub-install.c +32 -8
@@ -1719,10 +1719,14 @@ main (int argc, char *argv[])/* Now perform the installation. */ if (install_bootsector) -grub_util_bios_setup (platdir,"boot.img" ,"core.img" , - install_drive, force, - fs_probe, allow_floppy, add_rs_codes, - !grub_install_is_short_mbrgap_supported ()); + { +grub_util_bios_setup (platdir,"boot.img" ,"core.img" , + install_drive, force, + fs_probe, allow_floppy, add_rs_codes, + !grub_install_is_short_mbrgap_supported ()); + + grub_install_backup_ponr =1 ; + }break ; }case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275 :
@@ -1746,10 +1750,14 @@ main (int argc, char *argv[])/* Now perform the installation. */ if (install_bootsector) -grub_util_sparc_setup (platdir,"boot.img" ,"core.img" , - install_drive, force, - fs_probe, allow_floppy, -0 /* unused */ ,0 /* unused */ ); + { +grub_util_sparc_setup (platdir,"boot.img" ,"core.img" , + install_drive, force, + fs_probe, allow_floppy, +0 /* unused */ ,0 /* unused */ ); + + grub_install_backup_ponr =1 ; + }break ; }
@@ -1776,6 +1784,8 @@ main (int argc, char *argv[]) grub_elf =grub_util_path_concat (2 , core_services,"grub.elf" );grub_install_copy_file (imgfile, grub_elf,1 ); + grub_install_backup_ponr =1 ; + f =grub_util_fopen (mach_kernel,"a+" );if (!f)grub_util_error (_ ("Can't create file: %s" ),strerror (errno));
@@ -1878,6 +1888,8 @@ main (int argc, char *argv[]) boot_efi =grub_util_path_concat (2 , core_services,"boot.efi" );grub_install_copy_file (imgfile, boot_efi,1 ); + grub_install_backup_ponr =1 ; + f =grub_util_fopen (mach_kernel,"r+" );if (!f)grub_util_error (_ ("Can't create file: %s" ),strerror (errno));
@@ -1916,6 +1928,9 @@ main (int argc, char *argv[]) {char *dst =grub_util_path_concat (2 , efidir, efi_file);grub_install_copy_file (imgfile, dst,1 ); + + grub_install_backup_ponr =1 ; +free (dst); }if (!removable && update_nvram)
@@ -1967,6 +1982,15 @@ main (int argc, char *argv[])break ; } +/* +* Either there are no platform specific code, or it didn't raise +* ponr. Raise it here, because usually this is already past point +* of no return. If we leave this flag false, atexit all the modules +* will be removed from the prefix which would be very confusing. +* +*/ + grub_install_backup_ponr =1 ; +fprintf (stderr,"%s\n" ,_ ("Installation finished. No error reported." ));/* Free resources. */
util/grub-mknetdir.c +3 -0
@@ -159,6 +159,9 @@ process_input_dir (const char *input_dir, enum grub_install_plat platform)grub_install_make_image_wrap (input_dir, prefix, output,0 , load_cfg, targets[platform].mkimage_target,0 ); + + grub_install_backup_ponr =1 ; +grub_install_pop_module ();/* TRANSLATORS: First %s is replaced by platform name. Second one by filename. */
util/grub-mkrescue.c +3 -0
@@ -530,6 +530,9 @@ main (int argc, char *argv[]) boot_grub, plat); source_dirs[plat] =xstrdup (grub_install_source_directory); } + + grub_install_backup_ponr =1 ; +if (system_area ==SYS_AREA_AUTO || grub_install_source_directory) { if (source_dirs[GRUB_INSTALL_PLATFORM_I386_PC]
util/grub-mkstandalone.c +2 -0
@@ -318,6 +318,8 @@ main (int argc, char *argv[])grub_install_copy_files (grub_install_source_directory, boot_grub, plat); + grub_install_backup_ponr =1 ; +char *memdisk_img =grub_util_make_temporary_file (); memdisk =grub_util_fopen (memdisk_img,"wb" );
Signature · 1 line
2.27.0
Quoted 31 lines · Dimitri John Ledkov
From: Dimitri John Ledkov <[email protected]>
Refactor
clean_grub_dirto create a backup of all the files, instead of just irrevocably removing them as the first action. If available, register atexit handle to restore the backup if errors occur before point of no return, or remove the backup if everything was successful. If atexit is not available, the backup remains on disk for manual recovery.Some platforms defined a point of no return, i.e. after modules & core images were updated. Failures from any commands after that stage are ignored, and backup is cleanedup. For example, on EFI platforms update is not reverted when efibootmgr fails.
Extra care is taken to ensure atexit handler is only invoked by the parent process and not any children forks. Some older grub codebases can invoke parent atexit hooks from forks, which can mess up the backup.
This allows safer upgrades of MBR & modules, such that modules/images/fonts/translations are consistent with MBR in case of errors. For example accidental grub-install
/dev/non-existent-diskcurrently clobbers and upgrades modules in/boot/grub, despite not actually updating any MBR.This patch only handles backup and restore of files copied to
/boot/grub. This patch does not perform backup (or restoration) of MBR itself or blocklists. Thus when installing i386-pc platform, corruption may still occur with MBR and blocklists which will not be attempted to be automatically recovered.
Quoted 4 lines
Also add
modinfo.shand *.efi to the cleanup/backup/restore codepath, to ensure it is also cleaned / backed up / restored.Signed-off-by Dimitri John Ledkov <[email protected]>
But...
util/grub-install-common.c grub_install_mkdir_p (const char *dst) · line 314
Quoted 5 lines
util/grub-install-common.c +5
-0+static void +append_to_backup_dirs (const char *dir) +{ + backup_dirs =xrealloc (backup_dirs,sizeof (char *) * (backup_dirs_size +1 )); + backup_dirs[backup_dirs_size] =strdup (dir);
s/strdup/xstrdup/
I will fix it before committing...
util/grub-install-common.c grub_install_mkdir_p (const char *dst) · line 321
Quoted 7 lines
util/grub-install-common.c +7
-0+ backup_dirs_size++; +if (!backup_process) + { +atexit (restore_backup_atexit); + backup_process =getpid (); + } +}
Thank you for doing the work!
Daniel
Ugh... I have just realized that this will not build properly if HAVE_ATEXIT is not defined. Please look below...
Quoted 75 lines · Dimitri John Ledkov
From: Dimitri John Ledkov <[email protected]>
Refactor
clean_grub_dirto create a backup of all the files, instead of just irrevocably removing them as the first action. If available, register atexit handle to restore the backup if errors occur before point of no return, or remove the backup if everything was successful. If atexit is not available, the backup remains on disk for manual recovery.Some platforms defined a point of no return, i.e. after modules & core images were updated. Failures from any commands after that stage are ignored, and backup is cleanedup. For example, on EFI platforms update is not reverted when efibootmgr fails.
Extra care is taken to ensure atexit handler is only invoked by the parent process and not any children forks. Some older grub codebases can invoke parent atexit hooks from forks, which can mess up the backup.
This allows safer upgrades of MBR & modules, such that modules/images/fonts/translations are consistent with MBR in case of errors. For example accidental grub-install
/dev/non-existent-diskcurrently clobbers and upgrades modules in/boot/grub, despite not actually updating any MBR.This patch only handles backup and restore of files copied to
/boot/grub. This patch does not perform backup (or restoration) of MBR itself or blocklists. Thus when installing i386-pc platform, corruption may still occur with MBR and blocklists which will not be attempted to be automatically recovered.Also add
modinfo.shand *.efi to the cleanup/backup/restore codepath, to ensure it is also cleaned / backed up / restored.Signed-off-by Dimitri John Ledkov <[email protected]>---
changes since v4:
+ change ponr fromsize_tto int (there is no bool in grub yet)
+ declare loop variable at the start of the function
+ format one more comment correctly
+ add comment about significance to set ponrconfigure.ac | 2 +- include/grub/util/install.h | 13 ++++ util/grub-install-common.c | 143 ++++++++++++++++++++++++++++++++---- util/grub-install.c | 40 ++++++++-- util/grub-mknetdir.c | 3 + util/grub-mkrescue.c | 3 + util/grub-mkstandalone.c | 2 + 7 files changed, 182 insertions(+), 24 deletions(-)configure.ac +1
-1@@ -419,7 +419,7 @@ else fi # Check for functions and headers. -AC_CHECK_FUNCS(posix_memalign memalign getextmntent) +AC_CHECK_FUNCS(posix_memalign memalign getextmntent atexit) AC_CHECK_HEADERS(sys/param.h sys/mount.h sys/mnttab.h limits.h) # glibc 2.25 still includes sys/sysmacros.h in sys/types.h but emits deprecationinclude/grub/util/install.h +2
-0@@ -275,4 +275,17 @@ extern char *grub_install_copy_buffer;int grub_install_is_short_mbrgap_supported (void ); +/* +* grub-install-common tries to make backups of modules & auxilary
include/grub/util/install.h extern char *grub_install_copy_buffer; · line 289
Quoted 10 lines
include/grub/util/install.h +10
-0+* files, and restore the backup upon failure to install core.img. There +* are platforms with additional actions after modules & core got +* installed in place. It is a point of no return, as core.img cannot be +* reverted from this point onwards, and new modules should be kept +* installed. Before performing these additional actions raise +* grub_install_backup_ponr flag, this way failure to perform additional +* actions will not result in reverting new modules to the old +* ones. (i.e. in case efivars updates fails) +*/ +extern int grub_install_backup_ponr;
I would use a function which sets this variable and then
#ifdef HAVE_ATEXIT
extern voidgrub_set_install_backup_ponr (void);
#else
static inline voidgrub_set_install_backup_ponr (void)
{
}
#endif
Quoted 18 lines
include/grub/util/install.h +1
-0+#endif util/grub-install-common.c +8
-0@@ -185,38 +185,151 @@ grub_install_mkdir_p (const char *dst)free (t); } +static int +strcmp_ext (const char *a,const char *b,const char *ext) +{ +char *bsuffix =grub_util_path_concat_ext (1 , b, ext); +int r =strcmp (a, bsuffix); + +free (bsuffix); +return r;
util/grub-install-common.c grub_install_mkdir_p (const char *dst) · line 205
Quoted 10 lines
util/grub-install-common.c +10
-0+} + +enum clean_grub_dir_mode +{ +CLEAN_NEW , +CLEAN_BACKUP , +CREATE_BACKUP , +RESTORE_BACKUP +}; +
#ifdef HAVE_ATEXIT
util/grub-install-common.c grub_install_mkdir_p (const char *dst) · line 209
Quoted 4 lines
util/grub-install-common.c +4
-0+static size_t backup_dirs_size =0 ; +static char **backup_dirs =NULL ; +static pid_t backup_process =0 ; +int grub_install_backup_ponr =0 ;
#endif
[...]
#ifdef HAVE_ATEXIT
voidgrub_set_install_backup_ponr (void)
{
grub_install_backup_ponr = 1;
}
Quoted 35 lines
util/grub-install-common.c +35
-0+static void +restore_backup_atexit (void ) +{ +size_t i; +/* +* some child inherited atexit handler, did not clear it, and called +* it; thus skip clean or restore logic. +*/ +if (backup_process !=getpid ()) +return ; + +for (i =0 ; i < backup_dirs_size; i++) + { +/* +* if past point of no return simply clean the backups. +* Otherwise cleanup newly installed files, and restore the +* backups +*/ +if (grub_install_backup_ponr) +clean_grub_dir_real (backup_dirs[i],CLEAN_BACKUP ); +else + { +clean_grub_dir_real (backup_dirs[i],CLEAN_NEW ); +clean_grub_dir_real (backup_dirs[i],RESTORE_BACKUP ); + } +free (backup_dirs[i]); + } + + backup_dirs_size =0 ; + +free (backup_dirs); +} + +static void +append_to_backup_dirs (const char *dir)
util/grub-install-common.c grub_install_mkdir_p (const char *dst) · line 321
Quoted 10 lines
util/grub-install-common.c +10
-0+{ + backup_dirs =xrealloc (backup_dirs,sizeof (char *) * (backup_dirs_size +1 )); + backup_dirs[backup_dirs_size] =strdup (dir); + backup_dirs_size++; +if (!backup_process) + { +atexit (restore_backup_atexit); + backup_process =getpid (); + } +}
#else
static voidappend_to_backup_dirs (const char *dir __attribute__ ((unused))) {
}
#endif
util/grub-install-common.c grub_install_mkdir_p (const char *dst) · line 328
Quoted 6 lines
util/grub-install-common.c +6
-0+static void +clean_grub_dir (const char *di) +{ +clean_grub_dir_real (di,CLEAN_BACKUP ); +clean_grub_dir_real (di,CREATE_BACKUP ); +#if defined(HAVE_ATEXIT )
Please drop this #if...
util/grub-install-common.c grub_install_mkdir_p (const char *dst) · line 331
Quoted 3 lines
util/grub-install-common.c +3
-0+append_to_backup_dirs (di); +#endif +}
I think we need at least these changes. Otherwise if HAVE_ATEXIT is not defined we will have unused code which will compiler complain about... Please try to build the GRUB with and without HAVE_ATEXIT defined after fixing this issue.
Daniel