grub-reboot.in 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. transform="@program_transform_name@"
  20. prefix=@prefix@
  21. exec_prefix=@exec_prefix@
  22. bindir=@bindir@
  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}/`echo grub-editenv | sed ${transform}`
  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, for the next boot only."; echo
  43. echo
  44. print_option_help "-h, --help" "$(gettext "print this message and exit")"
  45. print_option_help "-v, --version" "$(gettext "print the version information and exit")"
  46. dirmsg="$(gettext_printf "expect GRUB images under the directory DIR/%s instead of the %s directory" "@grubdirname@" "$grubdir")"
  47. print_option_help "--boot-directory=$(gettext "DIR")" "$dirmsg"
  48. echo
  49. gettext "MENU_ENTRY is a number, a menu item title or a menu item identifier."; echo
  50. echo
  51. gettext "Report bugs to <bug-grub@gnu.org>."; echo
  52. }
  53. argument () {
  54. opt=$1
  55. shift
  56. if test $# -eq 0; then
  57. gettext_printf "%s: option requires an argument -- \`%s'\n" "$0" "$opt" 1>&2
  58. exit 1
  59. fi
  60. echo $1
  61. }
  62. # Check the arguments.
  63. while test $# -gt 0
  64. do
  65. option=$1
  66. shift
  67. case "$option" in
  68. -h | --help)
  69. usage
  70. exit 0 ;;
  71. -v | --version)
  72. echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
  73. exit 0 ;;
  74. # Accept for compatibility
  75. --root-directory)
  76. rootdir=`argument $option "$@"`; shift ;;
  77. --root-directory=*)
  78. rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
  79. --boot-directory)
  80. bootdir=`argument $option "$@"`; shift;;
  81. --boot-directory=*)
  82. bootdir=`echo "$option" | sed 's/--boot-directory=//'` ;;
  83. -*)
  84. gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
  85. usage
  86. exit 1
  87. ;;
  88. *)
  89. if test "x$entry" != x; then
  90. gettext "More than one menu entry?" 1>&2
  91. echo >&2
  92. usage
  93. exit 1
  94. fi
  95. entry="${option}" ;;
  96. esac
  97. done
  98. if test "x$entry" = x; then
  99. gettext "Menu entry not specified." 1>&2
  100. echo >&2
  101. usage
  102. exit 1
  103. fi
  104. if [ -z "$bootdir" ]; then
  105. # Default bootdir if bootdir not initialized.
  106. bootdir=/@bootdirname@
  107. if [ -n "$rootdir" ] ; then
  108. # Initialize bootdir if rootdir was initialized.
  109. bootdir=${rootdir}/@bootdirname@
  110. fi
  111. fi
  112. grubdir=`echo "${bootdir}/@grubdirname@" | sed 's,//*,/,g'`
  113. prev_saved_entry=`$grub_editenv ${grubdir}/grubenv list | sed -n 's/^saved_entry=//p'`
  114. if [ "$prev_saved_entry" ]; then
  115. $grub_editenv ${grubdir}/grubenv set prev_saved_entry="$prev_saved_entry"
  116. else
  117. # We need some non-empty value for prev_saved_entry so that GRUB will
  118. # recognise that grub-reboot has been used and restore the previous
  119. # saved entry. "0" is the same as an empty value, i.e. the first menu
  120. # entry.
  121. $grub_editenv ${grubdir}/grubenv set prev_saved_entry=0
  122. fi
  123. $grub_editenv ${grubdir}/grubenv set saved_entry="$entry"
  124. # Bye.
  125. exit 0