Received: from list by lists.gnu.org with archive (Exim 4.71) id 1ghvGx-00038Q-91 for mharc-grub-devel@gnu.org; Fri, 11 Jan 2019 06:46:47 -0500 Received: from eggs.gnu.org ([209.51.188.92]:38676) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ghvGv-000380-01 for grub-devel@gnu.org; Fri, 11 Jan 2019 06:46:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ghvGt-0004kq-VD for grub-devel@gnu.org; Fri, 11 Jan 2019 06:46:44 -0500 Received: from mx1.redhat.com ([209.132.183.28]:37272) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ghvGt-0004iw-Mt for grub-devel@gnu.org; Fri, 11 Jan 2019 06:46:43 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C5F247970A for ; Fri, 11 Jan 2019 11:41:08 +0000 (UTC) Received: from prarit.bos.redhat.com (prarit-guest.khw1.lab.eng.bos.redhat.com [10.16.200.63]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9CC3917DC7; Fri, 11 Jan 2019 11:40:59 +0000 (UTC) Subject: Re: [PATCH] grub: add grub variable update functionality To: grub-devel@gnu.org Cc: mleitner@redhat.com, pjones@redhat.com, javierm@redhat.com, aruiz@redhat.com References: <20190104125342.8987-1-prarit@redhat.com> From: Prarit Bhargava Message-ID: Date: Fri, 11 Jan 2019 06:40:59 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.1 MIME-Version: 1.0 In-Reply-To: <20190104125342.8987-1-prarit@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 11 Jan 2019 11:41:08 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Jan 2019 11:46:46 -0000 Just re-pinging on this ... P. On 1/4/19 7:53 AM, Prarit Bhargava wrote: > Please be aware I am NOT subscribed to grub-devel. > > P. > > ---8<--- > > Customers and users of the kernel are commenting that there is no way to update > a grub variable without copy and pasting the existing data. > > For example, > > [10:57 AM root@intel-wildcatpass-07 grub-2.02]# ./grub-editenv list > saved_entry=0 > next_entry= > kernelopts=root=/dev/mapper/rhel_intel--wildcatpass--07-root ro crashkernel=auto resume=/dev/mapper/rhel_intel--wildcatpass--07-swap rd.lvm.lv=rhel_intel-wildcatpass-07/root rd.lvm.lv=rhel_intel-wildcatpass-07/swap console=ttyS0,115200n81 ignore_loglevel > [10:57 AM root@intel-wildcatpass-07 grub-2.02]# ./grub-editenv - set kernelopts="root=/dev/mapper/rhel_intel--wildcatpass--07-root ro crashkernel=auto resume=/dev/mapper/rhel_intel--wildcatpass--07-swap rd.lvm.lv=rhel_intel-wildcatpass-07/root rd.lvm.lv=rhel_intel-wildcatpass-07/swap console=ttyS0,115200n81 ignore_loglevel newarg" > [10:57 AM root@intel-wildcatpass-07 grub-2.02]# ./grub-editenv list > saved_entry=0 > next_entry= > kernelopts=root=/dev/mapper/rhel_intel--wildcatpass--07-root ro crashkernel=auto resume=/dev/mapper/rhel_intel--wildcatpass--07-swap rd.lvm.lv=rhel_intel-wildcatpass-07/root rd.lvm.lv=rhel_intel-wildcatpass-07/swap console=ttyS0,115200n81 ignore_loglevel newarg > > which is cumbersome. > > Add functionality to add to an existing variable. For example, > > [10:58 AM root@intel-wildcatpass-07 grub-2.02]# ./grub-editenv list > saved_entry=0 > next_entry= > kernelopts=root=/dev/mapper/rhel_intel--wildcatpass--07-root ro crashkernel=auto resume=/dev/mapper/rhel_intel--wildcatpass--07-swap rd.lvm.lv=rhel_intel-wildcatpass-07/root rd.lvm.lv=rhel_intel-wildcatpass-07/swap console=ttyS0,115200n81 ignore_loglevel > [10:58 AM root@intel-wildcatpass-07 grub-2.02]# ./grub-editenv - set kernelopts+="newarg" > [10:59 AM root@intel-wildcatpass-07 grub-2.02]# ./grub-editenv list > saved_entry=0 > next_entry= > kernelopts=root=/dev/mapper/rhel_intel--wildcatpass--07-root ro crashkernel=auto resume=/dev/mapper/rhel_intel--wildcatpass--07-swap rd.lvm.lv=rhel_intel-wildcatpass-07/root rd.lvm.lv=rhel_intel-wildcatpass-07/swap console=ttyS0,115200n81 ignore_loglevel newarg > > Signed-off-by: Prarit Bhargava > Cc: mleitner@redhat.com > Cc: pjones@redhat.com > Cc: javierm@redhat.com > Cc: aruiz@redhat.com > --- > grub-core/lib/envblk.c | 61 +++++++++++++++++++++++++++++++++++++++ > include/grub/lib/envblk.h | 1 + > util/grub-editenv.c | 25 +++++++++++++++- > 3 files changed, 86 insertions(+), 1 deletion(-) > > diff --git a/grub-core/lib/envblk.c b/grub-core/lib/envblk.c > index 230e0e9d9abe..8ddbe2e8267e 100644 > --- a/grub-core/lib/envblk.c > +++ b/grub-core/lib/envblk.c > @@ -295,3 +295,64 @@ grub_envblk_iterate (grub_envblk_t envblk, > p = find_next_line (p, pend); > } > } > + > +int > +grub_envblk_add (grub_envblk_t envblk, const char *name, const char *add) > +{ > + char *current, *new, *ostart, *pstart, *pend; > + int newsize, ret = 1; > + > + /* get a copy of the existing entry */ > + pstart = envblk->buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1; > + pstart = grub_strstr (pstart, name); > + if (!pstart) > + { > + ret = -1; /* existing entry not found */ > + goto out; > + } > + pend = grub_strchr (pstart, '\n'); > + if (!pend || pend > (envblk->buf + envblk->size)) > + { > + ret = -1; /* existing entry not found */ > + goto out; > + } > + > + current = grub_zalloc (pend - pstart + 1); > + if (!current) > + { > + ret = -2; /* out of memory */ > + goto out; > + } > + grub_strncpy (current, pstart, (int)(pend - pstart)); > + > + ostart = grub_strchr (current, '='); > + ostart++; > + > + /* create a buffer for the updated entry. */ > + newsize = grub_strlen (ostart) + grub_strlen (add) + 2; > + new = grub_zalloc (newsize); > + if (!new) > + { > + return -2; /* out of memory */ > + goto out; > + } > + > + grub_strcpy (new, ostart); > + grub_memcpy (new + grub_strlen (new), " ", 1); > + grub_strcpy (new + grub_strlen (new), add); > + > + /* erase the current entry */ > + grub_envblk_delete (envblk, name); > + /* add the updated entry */ > + if (!grub_envblk_set (envblk, name, new)) > + { > + /* restore the original entry. This should always work */ > + grub_envblk_set (envblk, name, ostart); > + ret = 0; > + } > + > +out: > + grub_free(new); > + grub_free(current); > + return ret; > +} > diff --git a/include/grub/lib/envblk.h b/include/grub/lib/envblk.h > index c3e655921709..2a0f09e3435b 100644 > --- a/include/grub/lib/envblk.h > +++ b/include/grub/lib/envblk.h > @@ -37,6 +37,7 @@ void grub_envblk_delete (grub_envblk_t envblk, const char *name); > void grub_envblk_iterate (grub_envblk_t envblk, > void *hook_data, > int hook (const char *name, const char *value, void *hook_data)); > +int grub_envblk_add(grub_envblk_t envblk, const char *name, const char *add); > void grub_envblk_close (grub_envblk_t envblk); > > static inline char * > diff --git a/util/grub-editenv.c b/util/grub-editenv.c > index 118e89fe57fe..2df81a20b6bc 100644 > --- a/util/grub-editenv.c > +++ b/util/grub-editenv.c > @@ -210,15 +210,38 @@ set_variables (const char *name, int argc, char *argv[]) > while (argc) > { > char *p; > + int add = 0; > > p = strchr (argv[0], '='); > if (! p) > grub_util_error (_("invalid parameter %s"), argv[0]); > > + if ( *(p - 1) == '+') > + { > + add = 1; > + *(p - 1) = 0; > + } > *(p++) = 0; > > - if (! grub_envblk_set (envblk, argv[0], p)) > + if (!add && ! grub_envblk_set (envblk, argv[0], p)) > grub_util_error ("%s", _("environment block too small")); > + else if (add) { > + int ret; > + ret = grub_envblk_add (envblk, argv[0], p); > + switch (ret) { > + case (0): > + grub_util_error ("%s", _("environment block too small")); > + break; > + case (-1): > + grub_util_error("%s", _("existing entry not found")); > + break; > + case (-2): > + grub_util_error("%s", _("out of memory error")); > + break; > + default: > + break; > + } > + } > > argc--; > argv++; >