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