Received: from list by lists.gnu.org with archive (Exim 4.86_2) id 1hpzhi-00083z-An for mharc-grub-devel@gnu.org; Tue, 23 Jul 2019 14:40:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43541) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hpzhe-00081T-VA for grub-devel@gnu.org; Tue, 23 Jul 2019 14:40:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hpzhd-0007mE-F0 for grub-devel@gnu.org; Tue, 23 Jul 2019 14:39:58 -0400 Received: from a-painless.mh.aa.net.uk ([2001:8b0:0:30::51]:41881) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hpzhd-0007lX-75 for grub-devel@gnu.org; Tue, 23 Jul 2019 14:39:57 -0400 Received: from f.b.1.7.2.1.e.f.f.f.a.c.5.0.a.6.4.1.b.e.2.f.f.b.0.b.8.0.1.0.0.2.ip6.arpa ([2001:8b0:bff2:eb14:6a05:caff:fe12:71bf] helo=riva.pelham.vpn.ucam.org) by a-painless.mh.aa.net.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1hpzhc-0001ds-5q for grub-devel@gnu.org; Tue, 23 Jul 2019 19:39:56 +0100 Received: from ns1.pelham.vpn.ucam.org ([172.20.153.2] helo=riva.ucam.org) by riva.pelham.vpn.ucam.org with esmtp (Exim 4.92) (envelope-from ) id 1hpzhV-0000pv-JO for grub-devel@gnu.org; Tue, 23 Jul 2019 19:39:49 +0100 Date: Tue, 23 Jul 2019 19:39:49 +0100 From: Colin Watson To: grub-devel@gnu.org Subject: Re: grub-mkconfig - pick local defaults from /etc/default/grub.local Message-ID: <20190723183949.GA2857@riva.ucam.org> References: <36de4afe-c9be-03e4-e39f-6a92f15c488d@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="NzB8fVQJ5HfG6fxh" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <36de4afe-c9be-03e4-e39f-6a92f15c488d@gmail.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:8b0:0:30::51 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, 23 Jul 2019 18:40:00 -0000 --NzB8fVQJ5HfG6fxh Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Tue, Jul 16, 2019 at 09:30:40AM +0530, Amish wrote: > But if grub-mkconfig also sources /etc/default/grub.local in addition to > /etc/default/grub then Administrators (like me) can have their own config > without need to edit vendor specific configuration file everytime. > > It is 3 line addition as follows: > > --- grub-mkconfig       2019-07-05 15:44:43.000000000 +0530 > +++ grub-mkconfig   2019-07-16 09:13:31.020769527 +0530 > @@ -158,6 +158,10 @@ >    . ${sysconfdir}/default/grub >  fi > > +if test -f ${sysconfdir}/default/grub.local ; then > +  . ${sysconfdir}/default/grub.local > +fi > + >  # XXX: should this be deprecated at some point? >  if [ "x${GRUB_TERMINAL}" != "x" ] ; then >    GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}" It's a bit more than that, because grub_util_load_config needs to be consistent with it for grub-install. However, just supporting a single extra file isn't as helpful as it could be. I've attached the Debian patch that supports /etc/default/grub.d/*.cfg. -- Colin Watson [cjwatson@debian.org] --NzB8fVQJ5HfG6fxh Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="default-grub-d.patch" From 413121ddac2aa1484b0dc6fd3a32aad0d417aa80 Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Mon, 13 Jan 2014 12:13:10 +0000 Subject: Read /etc/default/grub.d/*.cfg after /etc/default/grub Bug-Ubuntu: https://bugs.launchpad.net/bugs/901600 Forwarded: no Last-Update: 2014-01-28 Patch-Name: default-grub-d.patch --- grub-core/osdep/unix/config.c | 114 +++++++++++++++++++++++++++------- util/grub-mkconfig.in | 5 ++ 2 files changed, 98 insertions(+), 21 deletions(-) diff --git a/grub-core/osdep/unix/config.c b/grub-core/osdep/unix/config.c index 65effa9f3..5478030fd 100644 --- a/grub-core/osdep/unix/config.c +++ b/grub-core/osdep/unix/config.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include #include #include @@ -61,13 +63,27 @@ grub_util_get_localedir (void) return LOCALEDIR; } +struct cfglist +{ + struct cfglist *next; + struct cfglist *prev; + char *path; +}; + void grub_util_load_config (struct grub_util_config *cfg) { pid_t pid; const char *argv[4]; - char *script, *ptr; + char *script = NULL, *ptr; const char *cfgfile, *iptr; + char *cfgdir; + grub_util_fd_dir_t d; + struct cfglist *cfgpaths = NULL, *cfgpath, *next_cfgpath; + int num_cfgpaths = 0; + size_t len_cfgpaths = 0; + char **sorted_cfgpaths = NULL; + int i; FILE *f = NULL; int fd; const char *v; @@ -83,29 +99,75 @@ grub_util_load_config (struct grub_util_config *cfg) cfg->grub_distributor = xstrdup (v); cfgfile = grub_util_get_config_filename (); - if (!grub_util_is_regular (cfgfile)) - return; + if (grub_util_is_regular (cfgfile)) + { + ++num_cfgpaths; + len_cfgpaths += strlen (cfgfile) * 4 + sizeof (". ''; ") - 1; + } + + cfgdir = xasprintf ("%s.d", cfgfile); + d = grub_util_fd_opendir (cfgdir); + if (d) + { + grub_util_fd_dirent_t de; + + while ((de = grub_util_fd_readdir (d))) + { + const char *ext = strrchr (de->d_name, '.'); + + if (!ext || strcmp (ext, ".cfg") != 0) + continue; + + cfgpath = xmalloc (sizeof (*cfgpath)); + cfgpath->path = grub_util_path_concat (2, cfgdir, de->d_name); + grub_list_push (GRUB_AS_LIST_P (&cfgpaths), GRUB_AS_LIST (cfgpath)); + ++num_cfgpaths; + len_cfgpaths += strlen (cfgpath->path) * 4 + sizeof (". ''; ") - 1; + } + grub_util_fd_closedir (d); + } + + if (num_cfgpaths == 0) + goto out; + + sorted_cfgpaths = xmalloc (num_cfgpaths * sizeof (*sorted_cfgpaths)); + i = 0; + if (grub_util_is_regular (cfgfile)) + sorted_cfgpaths[i++] = xstrdup (cfgfile); + FOR_LIST_ELEMENTS_SAFE (cfgpath, next_cfgpath, cfgpaths) + { + sorted_cfgpaths[i++] = cfgpath->path; + free (cfgpath); + } + assert (i == num_cfgpaths); + qsort (sorted_cfgpaths + 1, num_cfgpaths - 1, sizeof (*sorted_cfgpaths), + (int (*) (const void *, const void *)) strcmp); argv[0] = "sh"; argv[1] = "-c"; - script = xmalloc (4 * strlen (cfgfile) + 300); + script = xmalloc (len_cfgpaths + 300); ptr = script; - memcpy (ptr, ". '", 3); - ptr += 3; - for (iptr = cfgfile; *iptr; iptr++) + for (i = 0; i < num_cfgpaths; i++) { - if (*iptr == '\\') + memcpy (ptr, ". '", 3); + ptr += 3; + for (iptr = sorted_cfgpaths[i]; *iptr; iptr++) { - memcpy (ptr, "'\\''", 4); - ptr += 4; - continue; + if (*iptr == '\\') + { + memcpy (ptr, "'\\''", 4); + ptr += 4; + continue; + } + *ptr++ = *iptr; } - *ptr++ = *iptr; + memcpy (ptr, "'; ", 3); + ptr += 3; } - strcpy (ptr, "'; printf \"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\n\" " + strcpy (ptr, "printf \"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\n\" " "\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\""); argv[2] = script; @@ -125,15 +187,25 @@ grub_util_load_config (struct grub_util_config *cfg) waitpid (pid, NULL, 0); } if (f) - return; + goto out; - f = grub_util_fopen (cfgfile, "r"); - if (f) + for (i = 0; i < num_cfgpaths; i++) { - grub_util_parse_config (f, cfg, 0); - fclose (f); + f = grub_util_fopen (sorted_cfgpaths[i], "r"); + if (f) + { + grub_util_parse_config (f, cfg, 0); + fclose (f); + } + else + grub_util_warn (_("cannot open configuration file `%s': %s"), + cfgfile, strerror (errno)); } - else - grub_util_warn (_("cannot open configuration file `%s': %s"), - cfgfile, strerror (errno)); + +out: + free (script); + for (i = 0; i < num_cfgpaths; i++) + free (sorted_cfgpaths[i]); + free (sorted_cfgpaths); + free (cfgdir); } diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in index b506d63bf..d18bf972f 100644 --- a/util/grub-mkconfig.in +++ b/util/grub-mkconfig.in @@ -164,6 +164,11 @@ fi if test -f ${sysconfdir}/default/grub ; then . ${sysconfdir}/default/grub fi +for x in ${sysconfdir}/default/grub.d/*.cfg ; do + if [ -e "${x}" ]; then + . "${x}" + fi +done # XXX: should this be deprecated at some point? if [ "x${GRUB_TERMINAL}" != "x" ] ; then --NzB8fVQJ5HfG6fxh--