grub-reboot.in 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #! /bin/sh
  2. #
  3. # Set a default boot entry for GRUB, for the next boot only.
  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. sbindir=@sbindir@
  23. sysconfdir="@sysconfdir@"
  24. PACKAGE_NAME=@PACKAGE_NAME@
  25. PACKAGE_VERSION=@PACKAGE_VERSION@
  26. datarootdir="@datarootdir@"
  27. datadir="@datadir@"
  28. if [ "x$pkgdatadir" = x ]; then
  29. pkgdatadir="${datadir}/@PACKAGE@"
  30. fi
  31. self=`basename $0`
  32. grub_editenv=${bindir}/@grub_editenv@
  33. grub_probe=${sbindir}/@grub_probe@
  34. rootdir=
  35. bootdir=
  36. grubdir=`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`
  37. export TEXTDOMAIN=@PACKAGE@
  38. export TEXTDOMAINDIR="@localedir@"
  39. . "${pkgdatadir}/grub-mkconfig_lib"
  40. # Usage: usage
  41. # Print the usage.
  42. usage () {
  43. gettext_printf "Usage: %s [OPTION] MENU_ENTRY\n" "$self"
  44. gettext "Set the default boot menu entry for GRUB, for the next boot only."; 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. Please note that menu items in
  51. submenus or sub-submenus require specifying the submenu components and then the
  52. menu item component. The titles should be separated using the greater-than
  53. character (>) with no extra spaces. Depending on your shell some characters including > may need escaping. More information about this is available
  54. in the GRUB Manual in the section about the 'default' command. "; echo
  55. echo
  56. gettext "NOTE: In cases where GRUB cannot write to the environment block, such as when it is stored on an MDRAID or LVM device, the chosen boot menu entry will remain the default even after reboot. "; echo
  57. echo
  58. gettext "Report bugs to <bug-grub@gnu.org>."; echo
  59. }
  60. argument () {
  61. opt=$1
  62. shift
  63. if test $# -eq 0; then
  64. gettext_printf "%s: option requires an argument -- \`%s'\n" "$self" "$opt" 1>&2
  65. exit 1
  66. fi
  67. echo $1
  68. }
  69. # Check the arguments.
  70. while test $# -gt 0
  71. do
  72. option=$1
  73. shift
  74. case "$option" in
  75. -h | --help)
  76. usage
  77. exit 0 ;;
  78. -V | --version)
  79. echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
  80. exit 0 ;;
  81. # Accept for compatibility
  82. --root-directory)
  83. rootdir=`argument $option "$@"`; shift ;;
  84. --root-directory=*)
  85. rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
  86. --boot-directory)
  87. bootdir=`argument $option "$@"`; shift;;
  88. --boot-directory=*)
  89. bootdir=`echo "$option" | sed 's/--boot-directory=//'` ;;
  90. -*)
  91. gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
  92. usage
  93. exit 1
  94. ;;
  95. *)
  96. if test "x$entry" != x; then
  97. gettext "More than one menu entry?" 1>&2
  98. echo >&2
  99. usage
  100. exit 1
  101. fi
  102. entry="${option}" ;;
  103. esac
  104. done
  105. if test "x$entry" = x; then
  106. gettext "Menu entry not specified." 1>&2
  107. echo >&2
  108. usage
  109. exit 1
  110. fi
  111. if [ -z "$bootdir" ]; then
  112. # Default bootdir if bootdir not initialized.
  113. bootdir=/@bootdirname@
  114. if [ -n "$rootdir" ] ; then
  115. # Initialize bootdir if rootdir was initialized.
  116. bootdir=${rootdir}/@bootdirname@
  117. fi
  118. fi
  119. grubdir=`echo "${bootdir}/@grubdirname@" | sed 's,//*,/,g'`
  120. abstractions=`$grub_probe --target=abstraction ${grubdir}/grubenv`
  121. for abstraction in $abstractions; do
  122. case "$abstraction" in
  123. diskfilter | lvm)
  124. gettext_printf "\nWARNING: Detected GRUB environment block on $abstraction device\n"
  125. gettext_printf "%s will remain the default boot entry until manually cleared with:\n" "${entry}"
  126. gettext_printf " grub-editenv ${grubdir}/grubenv unset next_entry\n\n"
  127. break
  128. ;;
  129. esac
  130. done
  131. # Restore saved_entry if it was set by previous version
  132. prev_saved_entry=`$grub_editenv ${grubdir}/grubenv list | sed -n 's/^prev_saved_entry=//p'`
  133. if [ "$prev_saved_entry" ]; then
  134. $grub_editenv ${grubdir}/grubenv set saved_entry="$prev_saved_entry"
  135. $grub_editenv ${grubdir}/grubenv unset prev_saved_entry
  136. fi
  137. $grub_editenv ${grubdir}/grubenv set next_entry="$entry"
  138. # Bye.
  139. exit 0