grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Updated savedefault patch


From: Colin Watson
Subject: Re: [PATCH] Updated savedefault patch
Date: Thu, 3 Sep 2009 08:41:16 +0100
User-agent: Mutt/1.5.18 (2008-05-17)

On Wed, Sep 02, 2009 at 03:11:32PM +0100, Colin Watson wrote:
> Still to do: update grub-set-default to use this (obvious, I think);
> implement 'savedefault --once' and grub-reboot (useful when you want to
> tell the boot loader to boot something else just once, but then return
> to the previous default; consider unattended scenarios).

Here's an update that includes these utilities too. I also shortened the
"chosen_entry" environment variable to simply "chosen"; might as well
save six bytes of kernel size ...

2009-09-03  Vladimir Serbinenko  <address@hidden>
2009-09-03  Colin Watson  <address@hidden>

        * normal/menu.c (grub_menu_execute_entry): Save selected entry title
        in `chosen' environment variable.
        * normal/menu_text.c (get_entry_number): Check if the variable
        matches the title of a menu entry.
        (run_menu): Pass menu to get_entry_number.

        * util/grub-reboot.in: New file.
        * util/grub-set-default.in: New file.
        * conf/common.rmk (grub-reboot): New utility.
        (grub-set-default): New utility.

        * util/grub-mkconfig_lib.in (save_default_entry): New function.
        * util/grub.d/00_header.in: If GRUB_DEFAULT is `saved', set
        default to `${saved_entry}'.  If `${prev_saved_entry}' is non-empty,
        move it to `saved_entry' for the next boot.  Load environment on
        initialisation.
        * util/grub.d/10_freebsd.in: Call save_default_entry.
        * util/grub.d/10_hurd.in: Likewise.
        * util/grub.d/10_linux.in (linux_entry): Likewise.
        * util/grub.d/10_windows.in: Likewise.
        * util/grub.d/30_os-prober.in: Likewise.

        * util/i386/pc/grub-install.in: Create environment block.
        * util/i386/efi/grub-install.in: Likewise.
        * util/ieee1275/grub-install.in: Likewise.
        * util/sparc64/ieee1275/grub-install.in: Likewise.

Index: normal/menu.c
===================================================================
--- normal/menu.c       (revision 2561)
+++ normal/menu.c       (working copy)
@@ -137,6 +137,8 @@
       return;
     }
 
+  grub_env_set ("chosen", entry->title);
+
   grub_parser_execute ((char *) entry->sourcecode);
 
   if (grub_errno == GRUB_ERR_NONE && grub_loader_is_loaded ())
Index: normal/menu_text.c
===================================================================
--- normal/menu_text.c  (revision 2561)
+++ normal/menu_text.c  (working copy)
@@ -237,7 +237,7 @@
 
 /* Get the entry number from the variable NAME.  */
 static int
-get_entry_number (const char *name)
+get_entry_number (grub_menu_t menu, const char *name)
 {
   char *val;
   int entry;
@@ -250,6 +250,28 @@
 
   entry = (int) grub_strtoul (val, 0, 0);
 
+  if (grub_errno == GRUB_ERR_BAD_NUMBER)
+    {
+      /* See if the variable matches the title of a menu entry.  */
+      grub_menu_entry_t e = menu->entry_list;
+      int i;
+
+      grub_errno = GRUB_ERR_NONE;
+
+      for (i = 0; e; i++)
+       {
+         if (grub_strcmp (e->title, val) == 0)
+           {
+             entry = i;
+             break;
+           }
+         e = e->next;
+       }
+
+      if (! e)
+       entry = -1;
+    }
+
   if (grub_errno != GRUB_ERR_NONE)
     {
       grub_errno = GRUB_ERR_NONE;
@@ -291,7 +313,7 @@
 
   first = 0;
 
-  default_entry = get_entry_number ("default");
+  default_entry = get_entry_number (menu, "default");
 
   /* If DEFAULT_ENTRY is not within the menu entries, fall back to
      the first entry.  */
Index: util/grub-mkconfig_lib.in
===================================================================
--- util/grub-mkconfig_lib.in   (revision 2561)
+++ util/grub-mkconfig_lib.in   (working copy)
@@ -130,6 +130,13 @@
   echo ${drive}${relative_path}
 }
 
+save_default_entry ()
+{
+  if [ "x${GRUB_DEFAULT}" = "xsaved" ] ; then
+    echo 'save_env saved_entry=${chosen}'
+  fi
+}
+
 prepare_grub_to_access_device ()
 {
   device=$1
Index: util/grub-reboot.in
===================================================================
--- util/grub-reboot.in (revision 0)
+++ util/grub-reboot.in (revision 0)
@@ -0,0 +1,104 @@
+#! /bin/sh
+#
+# Set a default boot entry for GRUB, for the next boot only.
+# Copyright (C) 2004,2009  Free Software Foundation, Inc.
+#
+# GRUB is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# GRUB is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+
+# Initialize some variables.
+transform="@program_transform_name@"
+
address@hidden@
address@hidden@
address@hidden@
+
+grub_editenv=${bindir}/`echo grub-editenv | sed ${transform}`
+rootdir=
+
+# Usage: usage
+# Print the usage.
+usage () {
+    cat <<EOF
+Usage: $0 [OPTION] entry
+Set the default boot entry for GRUB, for the next boot only.
+
+  -h, --help              print this message and exit
+  -v, --version           print the version information and exit
+  --root-directory DIR    expect GRUB images under the directory DIR
+                          instead of the root directory
+
+ENTRY is a number or a menu item title.
+
+Report bugs to <address@hidden>.
+EOF
+}
+
+# Check the arguments.
+for option in "$@"; do
+    case "$option" in
+    -h | --help)
+       usage
+       exit 0 ;;
+    -v | --version)
+       echo "grub-install (GNU GRUB ${PACKAGE_VERSION})"
+       exit 0 ;;
+    --root-directory=*)
+       rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
+    -*)
+       echo "Unrecognized option \`$option'" 1>&2
+       usage
+       exit 1
+       ;;
+    *)
+       if test "x$entry" != x; then
+           echo "More than one entry?" 1>&2
+           usage
+           exit 1
+       fi
+       entry="${option}" ;;
+    esac
+done
+
+if test "x$entry" = x; then
+    echo "entry not specified." 1>&2
+    usage
+    exit 1
+fi
+
+# Initialize these directories here, since ROOTDIR was initialized.
+case "$host_os" in
+netbsd* | openbsd*)
+    # Because /boot is used for the boot block in NetBSD and OpenBSD, use /grub
+    # instead of /boot/grub.
+    grub_prefix=`echo /grub | sed ${transform}`
+    bootdir=${rootdir}
+    ;;
+*)
+    # Use /boot/grub by default.
+    bootdir=${rootdir}/boot
+    ;;
+esac
+
+grubdir=${bootdir}/`echo grub | sed ${transform}`
+
+prev_saved_entry=`$grub_editenv ${grubdir}/grubenv list | sed -n 
's/^saved_entry=//p'`
+if [ "$prev_saved_entry" ]; then
+    $grub_editenv ${grubdir}/grubenv set prev_saved_entry="$prev_saved_entry"
+else
+    $grub_editenv ${grubdir}/grubenv unset prev_saved_entry
+fi
+$grub_editenv ${grubdir}/grubenv set saved_entry="$entry"
+
+# Bye.
+exit 0
Index: util/grub-set-default.in
===================================================================
--- util/grub-set-default.in    (revision 0)
+++ util/grub-set-default.in    (revision 0)
@@ -0,0 +1,99 @@
+#! /bin/sh
+#
+# Set a default boot entry for GRUB.
+# Copyright (C) 2004,2009  Free Software Foundation, Inc.
+#
+# GRUB is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# GRUB is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+
+# Initialize some variables.
+transform="@program_transform_name@"
+
address@hidden@
address@hidden@
address@hidden@
+
+grub_editenv=${bindir}/`echo grub-editenv | sed ${transform}`
+rootdir=
+
+# Usage: usage
+# Print the usage.
+usage () {
+    cat <<EOF
+Usage: $0 [OPTION] entry
+Set the default boot entry for GRUB.
+
+  -h, --help              print this message and exit
+  -v, --version           print the version information and exit
+  --root-directory DIR    expect GRUB images under the directory DIR
+                          instead of the root directory
+
+ENTRY is a number or a menu item title.
+
+Report bugs to <address@hidden>.
+EOF
+}
+
+# Check the arguments.
+for option in "$@"; do
+    case "$option" in
+    -h | --help)
+       usage
+       exit 0 ;;
+    -v | --version)
+       echo "grub-install (GNU GRUB ${PACKAGE_VERSION})"
+       exit 0 ;;
+    --root-directory=*)
+       rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
+    -*)
+       echo "Unrecognized option \`$option'" 1>&2
+       usage
+       exit 1
+       ;;
+    *)
+       if test "x$entry" != x; then
+           echo "More than one entry?" 1>&2
+           usage
+           exit 1
+       fi
+       entry="${option}" ;;
+    esac
+done
+
+if test "x$entry" = x; then
+    echo "entry not specified." 1>&2
+    usage
+    exit 1
+fi
+
+# Initialize these directories here, since ROOTDIR was initialized.
+case "$host_os" in
+netbsd* | openbsd*)
+    # Because /boot is used for the boot block in NetBSD and OpenBSD, use /grub
+    # instead of /boot/grub.
+    grub_prefix=`echo /grub | sed ${transform}`
+    bootdir=${rootdir}
+    ;;
+*)
+    # Use /boot/grub by default.
+    bootdir=${rootdir}/boot
+    ;;
+esac
+
+grubdir=${bootdir}/`echo grub | sed ${transform}`
+
+$grub_editenv ${grubdir}/grubenv unset prev_saved_entry
+$grub_editenv ${grubdir}/grubenv set saved_entry="$entry"
+
+# Bye.
+exit 0
Index: util/grub.d/10_freebsd.in
===================================================================
--- util/grub.d/10_freebsd.in   (revision 2561)
+++ util/grub.d/10_freebsd.in   (working copy)
@@ -58,6 +58,7 @@
   cat << EOF
 menuentry "${OS}" {
 EOF
+  save_default_entry | sed -e "s/^/\t/"
   prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | sed -e "s/^/\t/"
   cat << EOF
        freebsd                 ${kfreebsd_rel_dirname}/${kfreebsd_basename}
Index: util/grub.d/10_windows.in
===================================================================
--- util/grub.d/10_windows.in   (revision 2561)
+++ util/grub.d/10_windows.in   (working copy)
@@ -73,6 +73,7 @@
 menuentry "$OS" {
 EOF
 
+  save_default_entry | sed -e 's,^,\t,'
   prepare_grub_to_access_device "$dev" | sed 's,^,\t,'
 
   cat << EOF
Index: util/grub.d/30_os-prober.in
===================================================================
--- util/grub.d/30_os-prober.in (revision 2561)
+++ util/grub.d/30_os-prober.in (working copy)
@@ -55,6 +55,7 @@
       cat << EOF
 menuentry "${LONGNAME} (on ${DEVICE})" {
 EOF
+      save_default_entry | sed -e "s/^/\t/"
       prepare_grub_to_access_device ${DEVICE} | sed -e "s/^/\t/"
 
       if [ "${LONGNAME}" != "Windows Vista (loader)" ] ; then
@@ -85,6 +86,7 @@
         cat << EOF
 menuentry "${LLABEL} (on ${DEVICE})" {
 EOF
+       save_default_entry | sed -e "s/^/\t/"
        prepare_grub_to_access_device ${DEVICE} | sed -e "s/^/\t/"
        cat <<  EOF
        linux ${LKERNEL} ${LPARAMS}
@@ -104,6 +106,7 @@
         cat << EOF
 menuentry "${LONGNAME} (on ${DEVICE})" {
 EOF
+       save_default_entry | sed -e "s/^/\t/"
        prepare_grub_to_access_device ${DEVICE} | sed -e "s/^/\t/"
        cat << EOF
         insmod vbe
Index: util/grub.d/10_linux.in
===================================================================
--- util/grub.d/10_linux.in     (revision 2561)
+++ util/grub.d/10_linux.in     (working copy)
@@ -47,6 +47,7 @@
   cat << EOF
 menuentry "$1" {
 EOF
+  save_default_entry | sed -e "s/^/\t/"
   prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | sed -e "s/^/\t/"
   cat << EOF
        linux   ${rel_dirname}/${basename} 
root=${linux_root_device_thisversion} ro $2
Index: util/grub.d/10_hurd.in
===================================================================
--- util/grub.d/10_hurd.in      (revision 2561)
+++ util/grub.d/10_hurd.in      (working copy)
@@ -71,6 +71,7 @@
 cat << EOF
 menuentry "${OS}" {
 EOF
+save_default_entry | sed -e "s/^/\t/"
 prepare_grub_to_access_device ${GRUB_DEVICE} | sed -e "s/^/\t/"
 cat << EOF
        multiboot ${kernel} root=device:${GRUB_DEVICE}
Index: util/grub.d/00_header.in
===================================================================
--- util/grub.d/00_header.in    (revision 2561)
+++ util/grub.d/00_header.in    (working copy)
@@ -32,11 +32,17 @@
 done
 
 if [ "x${GRUB_DEFAULT}" = "x" ] ; then GRUB_DEFAULT=0 ; fi
+if [ "x${GRUB_DEFAULT}" = "xsaved" ] ; then GRUB_DEFAULT='${saved_entry}' ; fi
 if [ "x${GRUB_TIMEOUT}" = "x" ] ; then GRUB_TIMEOUT=5 ; fi
 if [ "x${GRUB_GFXMODE}" = "x" ] ; then GRUB_GFXMODE=640x480 ; fi
 
 cat << EOF
+load_env
 set default=${GRUB_DEFAULT}
+if [ \${prev_saved_entry} ]; then
+  save_env saved_entry=\${prev_saved_entry}
+  save_env prev_saved_entry=
+fi
 EOF
 
 case ${GRUB_TERMINAL_INPUT}:${GRUB_TERMINAL_OUTPUT} in
Index: util/i386/pc/grub-install.in
===================================================================
--- util/i386/pc/grub-install.in        (revision 2561)
+++ util/i386/pc/grub-install.in        (working copy)
@@ -39,6 +39,7 @@
 fi
 grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed ${transform}`
 grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
+grub_editenv=${bindir}/`echo grub-editenv | sed ${transform}`
 rootdir=
 grub_prefix=`echo /boot/grub | sed ${transform}`
 modules=
@@ -247,6 +248,10 @@
     done
 fi
 
+if ! test -f ${grubdir}/grubenv; then
+    $grub_editenv ${grubdir}/grubenv create
+fi
+
 # Write device to a variable so we don't have to traverse /dev every time.
 grub_device=`$grub_probe --target=device ${grubdir}`
 
Index: util/i386/efi/grub-install.in
===================================================================
--- util/i386/efi/grub-install.in       (revision 2561)
+++ util/i386/efi/grub-install.in       (working copy)
@@ -34,6 +34,7 @@
 grub_mkimage=${bindir}/`echo grub-mkimage | sed ${transform}`
 grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed ${transform}`
 grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
+grub_editenv=${bindir}/`echo grub-editenv | sed ${transform}`
 rootdir=
 grub_prefix=`echo /boot/grub | sed ${transform}`
 modules=
@@ -178,6 +179,10 @@
     cp -f $file ${grubdir} || exit 1
 done
 
+if ! test -f ${grubdir}/grubenv; then
+    $grub_editenv ${grubdir}/grubenv create
+fi
+
 # Create the core image. First, auto-detect the filesystem module.
 fs_module=`$grub_probe --target=fs --device-map=${device_map} ${grubdir}`
 if test "x$fs_module" = xfat; then :; else
Index: util/ieee1275/grub-install.in
===================================================================
--- util/ieee1275/grub-install.in       (revision 2561)
+++ util/ieee1275/grub-install.in       (working copy)
@@ -37,6 +37,7 @@
 grub_mkimage=${bindir}/`echo grub-mkelfimage | sed ${transform}`
 grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed ${transform}`
 grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
+grub_editenv=${bindir}/`echo grub-editenv | sed ${transform}`
 rootdir=
 grub_prefix=`echo /boot/grub | sed ${transform}`
 modules=
@@ -163,6 +164,10 @@
     cp -f $file ${grubdir} || exit 1
 done
 
+if ! test -f ${grubdir}/grubenv; then
+    $grub_editenv ${grubdir}/grubenv create
+fi
+
 # Create the core image. First, auto-detect the filesystem module.
 fs_module=`$grub_probe --target=fs --device-map=${device_map} ${grubdir}`
 if test "x$fs_module" = x -a "x$modules" = x; then
Index: util/sparc64/ieee1275/grub-install.in
===================================================================
--- util/sparc64/ieee1275/grub-install.in       (revision 2561)
+++ util/sparc64/ieee1275/grub-install.in       (working copy)
@@ -35,6 +35,7 @@
 grub_mkimage=${bindir}/`echo grub-mkimage | sed ${transform}`
 grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed ${transform}`
 grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
+grub_editenv=${bindir}/`echo grub-editenv | sed ${transform}`
 rootdir=
 grub_prefix=`echo /boot/grub | sed ${transform}`
 modules=
@@ -206,6 +207,10 @@
     cp -f $file ${grubdir} || exit 1
 done
 
+if ! test -f ${grubdir}/grubenv; then
+    $grub_editenv ${grubdir}/grubenv create
+fi
+
 # Write device to a variable so we don't have to traverse /dev every time.
 grub_device=`$grub_probe --target=device ${grubdir}`
 
Index: conf/common.rmk
===================================================================
--- conf/common.rmk     (revision 2561)
+++ conf/common.rmk     (working copy)
@@ -175,6 +175,20 @@
 sbin_SCRIPTS += grub-dumpbios
 CLEANFILES += grub-dumpbios
 
+# For grub-set-default.
+grub-set-default: util/grub-set-default.in config.status
+       ./config.status --file=$@:$<
+       chmod +x $@
+sbin_SCRIPTS += grub-set-default
+CLEANFILES += grub-set-default
+
+# For grub-reboot.
+grub-reboot: util/grub-reboot.in config.status
+       ./config.status --file=$@:$<
+       chmod +x $@
+sbin_SCRIPTS += grub-reboot
+CLEANFILES += grub-reboot
+
 # Filing systems.
 pkglib_MODULES += fshelp.mod fat.mod ufs1.mod ufs2.mod ext2.mod ntfs.mod \
        ntfscomp.mod minix.mod hfs.mod jfs.mod iso9660.mod xfs.mod      \

-- 
Colin Watson                                       address@hidden




reply via email to

[Prev in Thread] Current Thread [Next in Thread]