grub-set-default.in 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #! /bin/sh
  2. #
  3. # Set a default boot entry for GRUB.
  4. # Copyright (C) 2004,2009 Free Software Foundation, Inc.
  5. #
  6. # GRUB is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # GRUB is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. # Initialize some variables.
  19. prefix=@prefix@
  20. exec_prefix=@exec_prefix@
  21. bindir=@bindir@
  22. sysconfdir="@sysconfdir@"
  23. PACKAGE_NAME=@PACKAGE_NAME@
  24. PACKAGE_VERSION=@PACKAGE_VERSION@
  25. datarootdir="@datarootdir@"
  26. datadir="@datadir@"
  27. if [ "x$pkgdatadir" = x ]; then
  28. pkgdatadir="${datadir}/@PACKAGE@"
  29. fi
  30. self=`basename $0`
  31. grub_editenv=${bindir}/@grub_editenv@
  32. rootdir=
  33. bootdir=
  34. grubdir=`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`
  35. export TEXTDOMAIN=@PACKAGE@
  36. export TEXTDOMAINDIR="@localedir@"
  37. . "${pkgdatadir}/grub-mkconfig_lib"
  38. # Usage: usage
  39. # Print the usage.
  40. usage () {
  41. gettext_printf "Usage: %s [OPTION] MENU_ENTRY\n" "$self"
  42. gettext "Set the default boot menu entry for GRUB."; echo
  43. gettext_printf "This requires setting GRUB_DEFAULT=saved in %s/default/grub.\n" "$sysconfdir"
  44. echo
  45. print_option_help "-h, --help" "$(gettext "print this message and exit")"
  46. print_option_help "-V, --version" "$(gettext "print the version information and exit")"
  47. dirmsg="$(gettext_printf "expect GRUB images under the directory DIR/%s instead of the %s directory" "@grubdirname@" "$grubdir")"
  48. print_option_help "--boot-directory=$(gettext "DIR")" "$dirmsg"
  49. echo
  50. gettext "MENU_ENTRY is a number, a menu item title or a menu item identifier."; echo
  51. echo
  52. gettext "Report bugs to <bug-grub@gnu.org>."; echo
  53. }
  54. argument () {
  55. opt=$1
  56. shift
  57. if test $# -eq 0; then
  58. gettext_printf "%s: option requires an argument -- \`%s'\n" "$self" "$opt" 1>&2
  59. exit 1
  60. fi
  61. echo $1
  62. }
  63. # Check the arguments.
  64. while test $# -gt 0
  65. do
  66. option=$1
  67. shift
  68. case "$option" in
  69. -h | --help)
  70. usage
  71. exit 0 ;;
  72. -V | --version)
  73. echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
  74. exit 0 ;;
  75. # Accept for compatibility
  76. --root-directory)
  77. rootdir=`argument $option "$@"`; shift ;;
  78. --root-directory=*)
  79. rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
  80. --boot-directory)
  81. bootdir=`argument $option "$@"`; shift;;
  82. --boot-directory=*)
  83. bootdir=`echo "$option" | sed 's/--boot-directory=//'` ;;
  84. -*)
  85. gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
  86. usage
  87. exit 1
  88. ;;
  89. *)
  90. if test "x$entry" != x; then
  91. gettext "More than one menu entry?" 1>&2
  92. echo >&2
  93. usage
  94. exit 1
  95. fi
  96. entry="${option}" ;;
  97. esac
  98. done
  99. if test "x$entry" = x; then
  100. gettext "Menu entry not specified." 1>&2
  101. echo >&2
  102. usage
  103. exit 1
  104. fi
  105. if [ -z "$bootdir" ]; then
  106. # Default bootdir if bootdir not initialized.
  107. bootdir=/@bootdirname@
  108. if [ -n "$rootdir" ] ; then
  109. # Initialize bootdir if rootdir was initialized.
  110. bootdir=${rootdir}/@bootdirname@
  111. fi
  112. fi
  113. grubdir=`echo "${bootdir}/@grubdirname@" | sed 's,//*,/,g'`
  114. $grub_editenv ${grubdir}/grubenv unset prev_saved_entry
  115. $grub_editenv ${grubdir}/grubenv unset next_entry
  116. $grub_editenv ${grubdir}/grubenv set saved_entry="$entry"
  117. # Bye.
  118. exit 0