Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1kmTVH-0005Fu-OK for mharc-grub-devel@gnu.org; Mon, 07 Dec 2020 22:17:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:39664) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kmTVG-0005FX-7W for grub-devel@gnu.org; Mon, 07 Dec 2020 22:17:26 -0500 Received: from de-smtp-delivery-102.mimecast.com ([62.140.7.102]:40317) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kmTVC-0002LB-9L for grub-devel@gnu.org; Mon, 07 Dec 2020 22:17:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1607397439; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=IRkMomn+POg3tdFq+wOcPbdStx2tNvzm+pqa3XFnXEs=; b=DL8RcWDzEjLvE2tGkZFRIuTF+8rZyRtqgHI6YKY0US4BKcYrvz3M/MN0hfuTsCpkoWza7I RqYoE/XukdTd5aN04MpK/eQh1d3NRkACH/spwdLBowD+M+li4WqfNLHLFSGexTSIeLTPG4 jHmpA8G5vqT0KXplj9uvfipeiZMzTzA= Received: from EUR01-HE1-obe.outbound.protection.outlook.com (mail-he1eur01lp2055.outbound.protection.outlook.com [104.47.0.55]) (Using TLS) by relay.mimecast.com with ESMTP id de-mta-10-ilpSXyRdM56lFjumc3uzOQ-1; Tue, 08 Dec 2020 04:17:13 +0100 X-MC-Unique: ilpSXyRdM56lFjumc3uzOQ-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kr3G9Gc6bFdUHH1TjRWptjETD4heJygYUnxLVhZ4Fxhfty6bHnt3nKsZWgq5Uco2VQUYnDRXq/htmX3Wvjtea2ECruQlsp2OtQN8YefL48MkYpxludCrsNVEmQIWl+Ovs96hG2sUKYbdzAGMZoTmx+EXtsowOefaAyOAonZoRadLw+YwA2gt/P9289mfAHbiN0CjfIwUas2NZsfCW76d07MkWr/+OokLJAkSnXLLDRgmDnQUs+E+v8t5MyFYSWc2yBMrkEN+lgOUc5QWm2Bv83z+XGCQvfVQ+Lo4AcxOP0fPZb/xPCfxGG1i8lzgBsbqcnVmT91sNs4XPKK8lt6BiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=IRkMomn+POg3tdFq+wOcPbdStx2tNvzm+pqa3XFnXEs=; b=ROlImlSlyxod42dY9Ig8liMGGx9ZuD4n6qnI84BTNvYjl+uebQrd/mWMnb5dpLhFLvkxjWiEHM5irDbcbILoI1C652m9N0UVgzU/8Bid4uZtC+A8SvtO2qKD9LuNTZJNLKI64QiVIjloaqoBSsTR6syKxmjPfbz6jE48mi8GEgDMjkSEttOHzZpXzRh1SRUprqIIdaJ78GiAtht9h/+UM1vH0ORf44IgTwmpbtoSluejOk8lQWGV1SMuulYgcHPs/ztdVw1ZxKPkOsVw0QfLMmBcuQhYRbmr8Yzi9+dfTmZPrz35EmMurcbNsKAXb71NOiNqVyZGUMteNA2a5BDeOA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: gnu.org; dkim=none (message not signed) header.d=none;gnu.org; dmarc=none action=none header.from=suse.com; Received: from VI1PR04MB4991.eurprd04.prod.outlook.com (2603:10a6:803:57::28) by VI1PR04MB5789.eurprd04.prod.outlook.com (2603:10a6:803:e2::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.23; Tue, 8 Dec 2020 03:17:10 +0000 Received: from VI1PR04MB4991.eurprd04.prod.outlook.com ([fe80::3103:76b1:ed7f:8994]) by VI1PR04MB4991.eurprd04.prod.outlook.com ([fe80::3103:76b1:ed7f:8994%7]) with mapi id 15.20.3632.023; Tue, 8 Dec 2020 03:17:10 +0000 Date: Tue, 8 Dec 2020 11:17:00 +0800 From: Michael Chang To: The development of GNU GRUB Cc: Dimitri John Ledkov Subject: Re: [PATCHv2] grub-install: Add backup and restore Message-ID: <20201208031700.GA10094@mercury> References: <20200825233820.315098-1-xnox@ubuntu.com> <20201207123728.100393-1-xnox@ubuntu.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201207123728.100393-1-xnox@ubuntu.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Originating-IP: [2001:b011:30d0:3a16:e73d:ae23:13d6:2215] X-ClientProxiedBy: HK2PR04CA0090.apcprd04.prod.outlook.com (2603:1096:202:15::34) To VI1PR04MB4991.eurprd04.prod.outlook.com (2603:10a6:803:57::28) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from mercury (2001:b011:30d0:3a16:e73d:ae23:13d6:2215) by HK2PR04CA0090.apcprd04.prod.outlook.com (2603:1096:202:15::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.17 via Frontend Transport; Tue, 8 Dec 2020 03:17:09 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1d5e58d6-f646-4f59-bf25-08d89b27c035 X-MS-TrafficTypeDiagnostic: VI1PR04MB5789: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gNr16UJC7MWrDGZBi/iCrGll9Vicx7ey+IOwH7T4kwjJiix6gtwA8HmyeJNVMtW8zzFa8bJxgeWvAojssbiEa4ZoRLR4GovrjurR5bx/eOpWPvHPCuJ3tDQ69WDqupwPE1FvYptDGqA9qQpVxf92D0Y897C/Qho7Kn8N1BdvhPauTFZ5UCZ8Q/m/Ky6O7jM42tSdV9f5Xrnq2mFjHExrx6BEJs78wJBF1F68JybuV+9Mh8s74EilzMgkoQPaZ9lQkzols0cw1m57fFfZ4iPq+ax3s4Xfb94eJ7JLS3I0iThc3CPwiT4W8NzYl8i9QxrdkTIJcO91dBeoGY3Ti3/pNqT7zFRd81uhEoN1j+FBrU/c2UfvMa6M90g3f1pdHyJz2J/njhArDqQSpLlnWULVYA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:VI1PR04MB4991.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(366004)(346002)(376002)(136003)(6916009)(8676002)(186003)(33716001)(4326008)(66556008)(9576002)(66946007)(6666004)(966005)(1076003)(33656002)(55016002)(66476007)(52116002)(5660300002)(8936002)(83380400001)(16526019)(86362001)(508600001)(2906002)(6496006)(9686003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?Ypg7OPf16nLjueSX6ohzg+tGX5eh5a83q0NbsY191dgw6Z0cIZ5oLod1OrIn?= =?us-ascii?Q?vK3JJYoe+lYrZbNfagRby1c4RzU4bj5y0Ylk2exyNKWcLDotMGpw9iHA/R81?= =?us-ascii?Q?T8W8z2gXPuROnjK9HJcEnkQUoSLWXG6awE6DDh+3ZraUVFiRXtAw+1Y3g33w?= =?us-ascii?Q?0zVNyFYI0WkZl/Pt2R0vAU20kolaF2w2Pw8K5uaCo+d4qVYeVqBj1HNDAw7R?= =?us-ascii?Q?va1cdCVUpSlDJs+pzRWISgSars2Eh4/yB/kaXelLF7w8ZFLdqNQWLPeLpspy?= =?us-ascii?Q?x4YrjHRo0JoGEKy8UrRpbwWZLuLQHJQ7bmgYSw7jUK/zCJzhYaLPz5oR1vcd?= =?us-ascii?Q?w6Pq63EG4jOdArXfcnk7Na0EzJadU7ESl3tDu+tmiM7P9j7rVimrIYWIuIi1?= =?us-ascii?Q?YpiGDFSz5pvOr/4v86oA0j3APL2tnucfidTmGdF1Lm2UdcMLpTDp+ZEZgI1Z?= =?us-ascii?Q?X4TzUXycJdlYzHBNPOCW8srpVu7fLUK8rapKQCU0ORA6ZDL40YpMLPhdwsZi?= =?us-ascii?Q?Ngcu7JhDcXoSzL0AtDzdL42GJhdGjJEk1zOxjMkCXKP3Okjl+mjRQu8mCxQd?= =?us-ascii?Q?E3RaNreZjPYCNuBRh9dsOHZ0ZUfOWTSynmbQ3Dhv1vQrJkcIOQ+BPmWBb1N6?= =?us-ascii?Q?RzTum7LDFX3X/7LvFK5uZmmS5jhkRZndDXjP07ZsL3pmjLh+J5Fs/M8llL/z?= =?us-ascii?Q?tUDt0SRmIAW/UN3WEXXIrOkYaW7h/cD8FIER138xCXHYRzwMedMpFa3hic9k?= =?us-ascii?Q?avZhEbd0haKRiApZYF7iktb/jreXu8gfFHFVKJbNapevbgfPmI7n32hXsG/j?= =?us-ascii?Q?Jj5jeq1Q1GBkMMlSNFVHBxiz+HdhAMxaV6tH4pBPh+GmRK3BMVj1EKs77S/V?= =?us-ascii?Q?ad/ii7QbgDtLrmNDK4xhWcESKTTO10KKuWe7bePh5p4o3r+ULNzenOG+IOfw?= =?us-ascii?Q?2I5UF/EkU/dSh2A6Qw6ilprKlaAI5Nz+0bIVwj1qxGm4MtZZFCTpPHiwURWC?= =?us-ascii?Q?/+wqLk6oqycKe0qiYDftKqsnNHPm0gq1fS4flaaFe1T+lImnZVCoTYP7IuF4?= =?us-ascii?Q?PB9e1gch?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1d5e58d6-f646-4f59-bf25-08d89b27c035 X-MS-Exchange-CrossTenant-AuthSource: VI1PR04MB4991.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Dec 2020 03:17:10.5921 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: YVpwu9dJcXTF9mfr7o5Sa8LQENi3za/UynXPL3KWgxeG/x2imfLOU3p943gg+44V X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB5789 Received-SPF: pass client-ip=62.140.7.102; envelope-from=mchang@suse.com; helo=de-smtp-delivery-102.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 Dec 2020 03:17:26 -0000 On Mon, Dec 07, 2020 at 12:37:28PM +0000, Dimitri John Ledkov wrote: > 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 on_exit handle to restore the backup if any errors occur, or > remove the backup if everything was successful. If on_exit is not > available, the backup remains on disk for manual recovery. > > 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 increases peak disk-usage slightly, by > requiring temporarily twice the disk space to complete grub-install. I'd love to have the described problem fixed too, as it helps a lot in the update path to survive grub-install error which can be contributed by many different reasons, even though grub has to stay with old version but still much better than unbootable system. But here I have a concern, as to what will happen if the error comes after image installation ? For example, the efibootmgr may fail to register boot entry after copying the images to efi partition. It looks to me that the restoring backup would be triggerd incorrectly for the new image to load restored old backups. Would you please help to clarify that ? Thanks, Michael > > Also add modinfo.sh to the cleanup/backup/restore codepath, to ensure > it is also cleaned / backed up / restored. > > Signed-off-by: Dimitri John Ledkov > --- > > Changes since v1: as reported on linux-ext4 mailing list and debugged > by Colin Watson, the grub_util_path_concat_ext call was incorrect in > the restore backup case as there was no extention needed. Instead the > call is corrected to just grub_util_path_concat. > > This patch is now shipped in Ubuntu & Debian in multiple series. It > would be nice to have this merged upstream, as it greatly improves > grub upgrades and prevents missmatches of MBR & modules. > > configure.ac | 2 +- > util/grub-install-common.c | 105 +++++++++++++++++++++++++++++++------ > 2 files changed, 91 insertions(+), 16 deletions(-) > > diff --git a/configure.ac b/configure.ac > index 7c10a4db7..71cd414c3 100644 > --- a/configure.ac > +++ b/configure.ac > @@ -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 on_exit) > 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 > diff --git a/util/grub-install-common.c b/util/grub-install-common.c > index 277eaf4e2..74584b92b 100644 > --- a/util/grub-install-common.c > +++ b/util/grub-install-common.c > @@ -185,38 +185,113 @@ 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; > +} > + > +enum clean_grub_dir_mode > +{ > + CLEAN = 0, > + CLEAN_BACKUP = 1, > + CREATE_BACKUP = 2, > + RESTORE_BACKUP = 3, > +}; > + > static void > -clean_grub_dir (const char *di) > +clean_grub_dir_real (const char *di, enum clean_grub_dir_mode mode) > { > grub_util_fd_dir_t d; > grub_util_fd_dirent_t de; > + char suffix[2] = ""; > + > + if ((mode == CLEAN_BACKUP) || (mode == RESTORE_BACKUP)) > + { > + strcpy (suffix, "-"); > + } > > d = grub_util_fd_opendir (di); > if (!d) > - grub_util_error (_("cannot open directory `%s': %s"), > - di, grub_util_fd_strerror ()); > + { > + if (mode == CLEAN_BACKUP) > + return; > + grub_util_error (_("cannot open directory `%s': %s"), > + di, grub_util_fd_strerror ()); > + } > > while ((de = grub_util_fd_readdir (d))) > { > const char *ext = strrchr (de->d_name, '.'); > - if ((ext && (strcmp (ext, ".mod") == 0 > - || strcmp (ext, ".lst") == 0 > - || strcmp (ext, ".img") == 0 > - || strcmp (ext, ".mo") == 0) > - && strcmp (de->d_name, "menu.lst") != 0) > - || strcmp (de->d_name, "efiemu32.o") == 0 > - || strcmp (de->d_name, "efiemu64.o") == 0) > + if ((ext && (strcmp_ext (ext, ".mod", suffix) == 0 > + || strcmp_ext (ext, ".lst", suffix) == 0 > + || strcmp_ext (ext, ".img", suffix) == 0 > + || strcmp_ext (ext, ".mo", suffix) == 0) > + && strcmp_ext (de->d_name, "menu.lst", suffix) != 0) > + || strcmp_ext (de->d_name, "modinfo.sh", suffix) == 0 > + || strcmp_ext (de->d_name, "efiemu32.o", suffix) == 0 > + || strcmp_ext (de->d_name, "efiemu64.o", suffix) == 0) > { > - char *x = grub_util_path_concat (2, di, de->d_name); > - if (grub_util_unlink (x) < 0) > - grub_util_error (_("cannot delete `%s': %s"), x, > - grub_util_fd_strerror ()); > - free (x); > + char *srcf = grub_util_path_concat (2, di, de->d_name); > + > + if (mode == CREATE_BACKUP) > + { > + char *dstf = grub_util_path_concat_ext (2, di, de->d_name, "-"); > + if (grub_util_rename (srcf, dstf) < 0) > + grub_util_error (_("cannot backup `%s': %s"), srcf, > + grub_util_fd_strerror ()); > + free (dstf); > + } > + else if (mode == RESTORE_BACKUP) > + { > + char *dstf = grub_util_path_concat (2, di, de->d_name); > + dstf[strlen (dstf) - 1] = 0; > + if (grub_util_rename (srcf, dstf) < 0) > + grub_util_error (_("cannot restore `%s': %s"), dstf, > + grub_util_fd_strerror ()); > + free (dstf); > + } > + else > + { > + if (grub_util_unlink (srcf) < 0) > + grub_util_error (_("cannot delete `%s': %s"), srcf, > + grub_util_fd_strerror ()); > + } > + free (srcf); > } > } > grub_util_fd_closedir (d); > } > > +static void > +restore_backup_on_exit (int status, void *arg) > +{ > + if (status == 0) > + { > + clean_grub_dir_real ((char *) arg, CLEAN_BACKUP); > + } > + else > + { > + clean_grub_dir_real ((char *) arg, CLEAN); > + clean_grub_dir_real ((char *) arg, RESTORE_BACKUP); > + } > + free (arg); > + arg = NULL; > +} > + > +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_ON_EXIT) > + on_exit (restore_backup_on_exit, strdup (di)); > +#endif > +} > + > struct install_list > { > int is_default; > -- > 2.27.0 > > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel