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