Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1og0cR-0002Fh-P8 for mharc-grub-devel@gnu.org; Wed, 05 Oct 2022 05:23:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55532) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1og0cC-0002Cz-4R for grub-devel@gnu.org; Wed, 05 Oct 2022 05:23:04 -0400 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]:39669) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1og0c9-0003q7-36 for grub-devel@gnu.org; Wed, 05 Oct 2022 05:22:55 -0400 Received: by mail-pl1-x633.google.com with SMTP id f23so14912841plr.6 for ; Wed, 05 Oct 2022 02:22:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=Ztv7rCk0I8Ba6jGZTJPdZJvT7WlcjThBI3Xn9ZTxHcU=; b=XNFx1XdWslIO9fF8Gc2a3DEoZ6R3YwfDFnMJ8clbfZ2EK2Q6Fr2toQDLJW8wW7cme1 Zd6lYugJ6EOTopdMVX/jxE7iCqDbGbYPeYIIYl5dfjjruN4ykuL2HMZeb6zk+kqcdhFv ptaqRX3rwt8qATR4wKanPOrcIncfP64uv91ZPYZxukrikGG/J6BgwY0Hb2k+f7x+dkTO KTnyNVrJRHJPAyf/PWv6VSXyeVOv2sjJ9qjIqz6QHRlePRyWnM761t8riqj7tARASmIT Jhu0nO7DbSoMmeuL0El7OYnbi6rjUhotDFBqdLv0pTTwkxoEnKGpYHgSTxAxK43MD0Ow jdLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=Ztv7rCk0I8Ba6jGZTJPdZJvT7WlcjThBI3Xn9ZTxHcU=; b=L1F4eFYBTaRxQl1+a9nbUSkri1MYadLdnjEt2McRXeRWeomWIT0+QgLRC75QHBmkW8 TZTJtNTLZiUSXU151WUwtaWf4LxSdQMiiv1uGevPLuGKaIxMJvbWq2+gn4/bhtEL7TmL Hxx99YXG8uUA000hYEHRtJxGl783W1h4j4PiqtJFHnF/50hkfhDDFn2PMbXySHgDOzdO QBQ6lJc4w4tOsrU7YxCiPVf/946/QCWI2CyYTChWOtSjMvlnwc3HWU0yoH7OIxP+OW9R R/ZclgejA340WeAH5Krk8BS+JrjdBdAjgxshUfh49RbGUJCLmCBlUKlYmUuiNWeBBh2Q fBYw== X-Gm-Message-State: ACrzQf2vb/Jlo+A9Hnqvkp7GIGc4ss9NLv2UENZ9c2WLyUTAnlA45Kb4 9zxAn+eiU6+ynRWSd3MYnY1e+lJUtTs= X-Google-Smtp-Source: AMsMyM6SU1AmLGlzkcKtn4VAB0YCBENL6f4ft4YcJGmtDSXYjcSPsdG8qtM54xZA1n3zG8AY+TQMlw== X-Received: by 2002:a17:902:e414:b0:17f:5c82:ac01 with SMTP id m20-20020a170902e41400b0017f5c82ac01mr13899283ple.123.1664961769329; Wed, 05 Oct 2022 02:22:49 -0700 (PDT) Received: from archbookpro.localdomain (136-25-1-99.cab.webpass.net. [136.25.1.99]) by smtp.gmail.com with ESMTPSA id u10-20020a17090341ca00b0017e93c158d7sm6188712ple.214.2022.10.05.02.22.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 02:22:48 -0700 (PDT) From: Denton Liu To: grub-devel@gnu.org Cc: Daniel Kiper , Oskari Pirhonen Subject: [PATCH v3] templates: introduce GRUB_TOP_LEVEL_* vars Date: Wed, 5 Oct 2022 02:22:38 -0700 Message-Id: <20221005092238.213506-1-liu.denton@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220930111111.67328-1-liu.denton@gmail.com> References: <20220930111111.67328-1-liu.denton@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::633; envelope-from=liu.denton@gmail.com; helo=mail-pl1-x633.google.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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, 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.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 Oct 2022 09:23:06 -0000 A user may wish to use an image that is not sorted as the "latest" version as the top-level entry. For example, in Arch Linux, if a user has the LTS and regular kernels installed, `/boot/vmlinuz-linux-lts` gets sorted as the "latest" compared to `/boot/vmlinuz-linux`. However, a user may wish to use the regular kernel as the default with the LTS only existing as a backup. Introduce the GRUB_TOP_LEVEL, GRUB_TOP_LEVEL_XEN and GRUB_TOP_LEVEL_OS_PROBER variables to allow users to specify the top-level entry. Create grub_move_to_front() as a helper function which moves entries to the front of a list. This function does the heavy lifting of moving the menu entry to the front in each script. In 10_netbsd, since there isn't an explicit list variable, extract the items that are being iterated through into a list so that we can optionally apply grub_move_to_front() to the list before the loop. Signed-off-by: Denton Liu --- Notes: The only file that was tested is 10_linux. I do not have access to any of the other images or systems so they remain untested. Changes since v2: * Added more detail to GRUB_TOP_LEVEL docs * Moved GRUB_TOP_LEVEL_OS_PROBER to separate section in docs * Renamed grub_move_entry_to_front() to grub_move_to_front() and added code comment * Give 10_netbsd an intermediate list of images to interact with Interdiff against v2: diff --git a/docs/grub.texi b/docs/grub.texi index 16a445178..db6b20531 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -1446,9 +1446,13 @@ and @samp{GRUB_CMDLINE_LINUX_DEFAULT} for Linux and Xen menu entries. @item GRUB_TOP_LEVEL @item GRUB_TOP_LEVEL_XEN +This option should be a path to a kernel image. If provided, the image +specified will be made the top-level entry if it is found in the scan. + @item GRUB_TOP_LEVEL_OS_PROBER -If this option is provided, the given image file will be made the top-level -entry if the image file is found in the scan. +This option should be a line of output from @command{os-prober}. As +@samp{GRUB_TOP_LEVEL}, if provided, the image specified will be made the +top-level entry if it is found in the scan. @item GRUB_EARLY_INITRD_LINUX_CUSTOM @itemx GRUB_EARLY_INITRD_LINUX_STOCK diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in index 5be49a07c..f4ae41f86 100644 --- a/util/grub-mkconfig_lib.in +++ b/util/grub-mkconfig_lib.in @@ -218,23 +218,26 @@ version_sort () esac } -grub_move_entry_to_front () +# Given an item as the first argument and a list as the subsequent arguments, +# returns the list with the first argument moved to the front if it exists in +# the list. +grub_move_to_front () { - entry="$1" + item="$1" shift - entry_found=false + item_found=false for i in "$@"; do - if [ "x$i" = "x$entry" ]; then - entry_found=true + if [ "x$i" = "x$item" ]; then + item_found=true fi done - if [ "x$entry_found" = xtrue ]; then - echo "$entry" + if [ "x$item_found" = xtrue ]; then + echo "$item" fi for i in "$@"; do - if [ "x$i" = "x$entry" ]; then continue; fi + if [ "x$i" = "x$item" ]; then continue; fi echo "$i" done } diff --git a/util/grub.d/10_hurd.in b/util/grub.d/10_hurd.in index 0ae5a3927..b7be7041c 100644 --- a/util/grub.d/10_hurd.in +++ b/util/grub.d/10_hurd.in @@ -206,7 +206,7 @@ submenu_indentation="" reverse_sorted_kernels=$(echo ${kernels} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//') if [ "x$GRUB_TOP_LEVEL" != x ]; then - reverse_sorted_kernels=$(grub_move_entry_to_front "$GRUB_TOP_LEVEL" ${reverse_sorted_kernels}) + reverse_sorted_kernels=$(grub_move_to_front "$GRUB_TOP_LEVEL" ${reverse_sorted_kernels}) fi is_top_level=true diff --git a/util/grub.d/10_kfreebsd.in b/util/grub.d/10_kfreebsd.in index 506b2df66..83e9636e8 100644 --- a/util/grub.d/10_kfreebsd.in +++ b/util/grub.d/10_kfreebsd.in @@ -165,7 +165,7 @@ submenu_indentation="" reverse_sorted_list=$(echo ${list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//') if [ "x$GRUB_TOP_LEVEL" != x ]; then - reverse_sorted_list=$(grub_move_entry_to_front "$GRUB_TOP_LEVEL" ${reverse_sorted_list}) + reverse_sorted_list=$(grub_move_to_front "$GRUB_TOP_LEVEL" ${reverse_sorted_list}) fi is_top_level=true diff --git a/util/grub.d/10_linux.in b/util/grub.d/10_linux.in index a479c16af..7263f2983 100644 --- a/util/grub.d/10_linux.in +++ b/util/grub.d/10_linux.in @@ -203,7 +203,7 @@ submenu_indentation="" reverse_sorted_list=$(echo $list | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//') if [ "x$GRUB_TOP_LEVEL" != x ]; then - reverse_sorted_list=$(grub_move_entry_to_front "$GRUB_TOP_LEVEL" ${reverse_sorted_list}) + reverse_sorted_list=$(grub_move_to_front "$GRUB_TOP_LEVEL" ${reverse_sorted_list}) fi is_top_level=true diff --git a/util/grub.d/10_netbsd.in b/util/grub.d/10_netbsd.in index 4eae58e82..3154e9e15 100644 --- a/util/grub.d/10_netbsd.in +++ b/util/grub.d/10_netbsd.in @@ -146,19 +146,17 @@ pattern="^ELF[^,]*executable.*statically linked" # yet, so it's empty. In a submenu it will be equal to '\t' (one tab). submenu_indentation="" +list="/netbsd $(ls -t /netbsd?* 2>/dev/null)" + +if [ "x$GRUB_TOP_LEVEL" != x ]; then + list=$(grub_move_to_front "$GRUB_TOP_LEVEL" ${list}) +fi + is_top_level=true -grub_top_level_found=false -for k in "$GRUB_TOP_LEVEL" /netbsd $(ls -t /netbsd?* 2>/dev/null) ; do +for k in ${list}; do if ! grub_file_is_not_garbage "$k" ; then continue fi - if [ "x$GRUB_TOP_LEVEL" = "x$k" ] ; then - if [ "x$grub_top_level_found" = xfalse ] ; then - grub_top_level_found=true - else - continue - fi - fi if ! (zcat -f "$k" | file -bL - | grep -q "${pattern}") 2>/dev/null ; then continue fi diff --git a/util/grub.d/20_linux_xen.in b/util/grub.d/20_linux_xen.in index c32bb979f..386bfb9be 100644 --- a/util/grub.d/20_linux_xen.in +++ b/util/grub.d/20_linux_xen.in @@ -246,10 +246,10 @@ reverse_sorted_xen_list=$(echo ${xen_list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; reverse_sorted_linux_list=$(echo ${linux_list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//') if [ "x$GRUB_TOP_LEVEL_XEN" != x ]; then - reverse_sorted_xen_list=$(grub_move_entry_to_front "$GRUB_TOP_LEVEL_XEN" ${reverse_sorted_xen_list}) + reverse_sorted_xen_list=$(grub_move_to_front "$GRUB_TOP_LEVEL_XEN" ${reverse_sorted_xen_list}) fi -if [ "x$GRUB_TOP_LEVEL_LINUX" != x ]; then - reverse_sorted_linux_list=$(grub_move_entry_to_front "$GRUB_TOP_LEVEL_LINUX" ${reverse_sorted_linux_list}) +if [ "x$GRUB_TOP_LEVEL" != x ]; then + reverse_sorted_linux_list=$(grub_move_to_front "$GRUB_TOP_LEVEL" ${reverse_sorted_linux_list}) fi is_top_level=true diff --git a/util/grub.d/30_os-prober.in b/util/grub.d/30_os-prober.in index 9b2a96bd0..656301eaf 100644 --- a/util/grub.d/30_os-prober.in +++ b/util/grub.d/30_os-prober.in @@ -114,7 +114,7 @@ EOF used_osprober_linux_ids= if [ "x$GRUB_TOP_LEVEL_OS_PROBER" != x ]; then - OSPROBED=$(grub_move_entry_to_front "$GRUB_TOP_LEVEL_OS_PROBER" ${OSPROBED}) + OSPROBED=$(grub_move_to_front "$GRUB_TOP_LEVEL_OS_PROBER" ${OSPROBED}) fi for OS in ${OSPROBED} ; do docs/grub.texi | 10 ++++++++++ util/grub-mkconfig.in | 3 +++ util/grub-mkconfig_lib.in | 24 ++++++++++++++++++++++++ util/grub.d/10_hurd.in | 4 ++++ util/grub.d/10_kfreebsd.in | 4 ++++ util/grub.d/10_linux.in | 4 ++++ util/grub.d/10_netbsd.in | 8 +++++++- util/grub.d/20_linux_xen.in | 7 +++++++ util/grub.d/30_os-prober.in | 4 ++++ 9 files changed, 67 insertions(+), 1 deletion(-) diff --git a/docs/grub.texi b/docs/grub.texi index 107f66ebc..db6b20531 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -1444,6 +1444,16 @@ for all respectively normal entries. The values of these options replace the values of @samp{GRUB_CMDLINE_LINUX} and @samp{GRUB_CMDLINE_LINUX_DEFAULT} for Linux and Xen menu entries. +@item GRUB_TOP_LEVEL +@item GRUB_TOP_LEVEL_XEN +This option should be a path to a kernel image. If provided, the image +specified will be made the top-level entry if it is found in the scan. + +@item GRUB_TOP_LEVEL_OS_PROBER +This option should be a line of output from @command{os-prober}. As +@samp{GRUB_TOP_LEVEL}, if provided, the image specified will be made the +top-level entry if it is found in the scan. + @item GRUB_EARLY_INITRD_LINUX_CUSTOM @itemx GRUB_EARLY_INITRD_LINUX_STOCK List of space-separated early initrd images to be loaded from @samp{/boot}. diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in index 62335d027..32c480dae 100644 --- a/util/grub-mkconfig.in +++ b/util/grub-mkconfig.in @@ -233,6 +233,9 @@ export GRUB_DEFAULT \ GRUB_CMDLINE_NETBSD \ GRUB_CMDLINE_NETBSD_DEFAULT \ GRUB_CMDLINE_GNUMACH \ + GRUB_TOP_LEVEL \ + GRUB_TOP_LEVEL_XEN \ + GRUB_TOP_LEVEL_OS_PROBER \ GRUB_EARLY_INITRD_LINUX_CUSTOM \ GRUB_EARLY_INITRD_LINUX_STOCK \ GRUB_TERMINAL_INPUT \ diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in index 634bc8a50..f4ae41f86 100644 --- a/util/grub-mkconfig_lib.in +++ b/util/grub-mkconfig_lib.in @@ -218,6 +218,30 @@ version_sort () esac } +# Given an item as the first argument and a list as the subsequent arguments, +# returns the list with the first argument moved to the front if it exists in +# the list. +grub_move_to_front () +{ + item="$1" + shift + + item_found=false + for i in "$@"; do + if [ "x$i" = "x$item" ]; then + item_found=true + fi + done + + if [ "x$item_found" = xtrue ]; then + echo "$item" + fi + for i in "$@"; do + if [ "x$i" = "x$item" ]; then continue; fi + echo "$i" + done +} + # One layer of quotation is eaten by "" and the second by sed; so this turns # ' into \'. grub_quote () { diff --git a/util/grub.d/10_hurd.in b/util/grub.d/10_hurd.in index 4294bbe4c..b7be7041c 100644 --- a/util/grub.d/10_hurd.in +++ b/util/grub.d/10_hurd.in @@ -205,6 +205,10 @@ submenu_indentation="" reverse_sorted_kernels=$(echo ${kernels} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//') +if [ "x$GRUB_TOP_LEVEL" != x ]; then + reverse_sorted_kernels=$(grub_move_to_front "$GRUB_TOP_LEVEL" ${reverse_sorted_kernels}) +fi + is_top_level=true for kernel in ${reverse_sorted_kernels}; do diff --git a/util/grub.d/10_kfreebsd.in b/util/grub.d/10_kfreebsd.in index 0a67decaa..83e9636e8 100644 --- a/util/grub.d/10_kfreebsd.in +++ b/util/grub.d/10_kfreebsd.in @@ -164,6 +164,10 @@ submenu_indentation="" reverse_sorted_list=$(echo ${list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//') +if [ "x$GRUB_TOP_LEVEL" != x ]; then + reverse_sorted_list=$(grub_move_to_front "$GRUB_TOP_LEVEL" ${reverse_sorted_list}) +fi + is_top_level=true for kfreebsd in ${reverse_sorted_list}; do diff --git a/util/grub.d/10_linux.in b/util/grub.d/10_linux.in index c6a1ec935..7263f2983 100644 --- a/util/grub.d/10_linux.in +++ b/util/grub.d/10_linux.in @@ -202,6 +202,10 @@ submenu_indentation="" reverse_sorted_list=$(echo $list | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//') +if [ "x$GRUB_TOP_LEVEL" != x ]; then + reverse_sorted_list=$(grub_move_to_front "$GRUB_TOP_LEVEL" ${reverse_sorted_list}) +fi + is_top_level=true for linux in ${reverse_sorted_list}; do gettext_printf "Found linux image: %s\n" "$linux" >&2 diff --git a/util/grub.d/10_netbsd.in b/util/grub.d/10_netbsd.in index dc0cd1b17..3154e9e15 100644 --- a/util/grub.d/10_netbsd.in +++ b/util/grub.d/10_netbsd.in @@ -146,8 +146,14 @@ pattern="^ELF[^,]*executable.*statically linked" # yet, so it's empty. In a submenu it will be equal to '\t' (one tab). submenu_indentation="" +list="/netbsd $(ls -t /netbsd?* 2>/dev/null)" + +if [ "x$GRUB_TOP_LEVEL" != x ]; then + list=$(grub_move_to_front "$GRUB_TOP_LEVEL" ${list}) +fi + is_top_level=true -for k in /netbsd $(ls -t /netbsd?* 2>/dev/null) ; do +for k in ${list}; do if ! grub_file_is_not_garbage "$k" ; then continue fi diff --git a/util/grub.d/20_linux_xen.in b/util/grub.d/20_linux_xen.in index 626aed40c..386bfb9be 100644 --- a/util/grub.d/20_linux_xen.in +++ b/util/grub.d/20_linux_xen.in @@ -245,6 +245,13 @@ submenu_indentation="" reverse_sorted_xen_list=$(echo ${xen_list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//') reverse_sorted_linux_list=$(echo ${linux_list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//') +if [ "x$GRUB_TOP_LEVEL_XEN" != x ]; then + reverse_sorted_xen_list=$(grub_move_to_front "$GRUB_TOP_LEVEL_XEN" ${reverse_sorted_xen_list}) +fi +if [ "x$GRUB_TOP_LEVEL" != x ]; then + reverse_sorted_linux_list=$(grub_move_to_front "$GRUB_TOP_LEVEL" ${reverse_sorted_linux_list}) +fi + is_top_level=true for current_xen in ${reverse_sorted_xen_list}; do diff --git a/util/grub.d/30_os-prober.in b/util/grub.d/30_os-prober.in index daa603778..656301eaf 100644 --- a/util/grub.d/30_os-prober.in +++ b/util/grub.d/30_os-prober.in @@ -113,6 +113,10 @@ EOF used_osprober_linux_ids= +if [ "x$GRUB_TOP_LEVEL_OS_PROBER" != x ]; then + OSPROBED=$(grub_move_to_front "$GRUB_TOP_LEVEL_OS_PROBER" ${OSPROBED}) +fi + for OS in ${OSPROBED} ; do DEVICE="`echo ${OS} | cut -d ':' -f 1`" LONGNAME="`echo ${OS} | cut -d ':' -f 2 | tr '^' ' '`" -- 2.37.3