]> git.proxmox.com Git - grub2.git/blob - util/grub-reboot.in
merge with mainline
[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 # Check the arguments.
52 for option in "$@"; do
53 case "$option" in
54 -h | --help)
55 usage
56 exit 0 ;;
57 -v | --version)
58 echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
59 exit 0 ;;
60 --root-directory=*)
61 rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
62 -*)
63 echo "Unrecognized option \`$option'" 1>&2
64 usage
65 exit 1
66 ;;
67 *)
68 if test "x$entry" != x; then
69 echo "More than one entry?" 1>&2
70 usage
71 exit 1
72 fi
73 entry="${option}" ;;
74 esac
75 done
76
77 if test "x$entry" = x; then
78 echo "entry not specified." 1>&2
79 usage
80 exit 1
81 fi
82
83 # Initialize these directories here, since ROOTDIR was initialized.
84 case "$host_os" in
85 netbsd* | openbsd*)
86 # Because /boot is used for the boot block in NetBSD and OpenBSD, use /grub
87 # instead of /boot/grub.
88 grub_prefix=`echo /grub | sed ${transform}`
89 bootdir=${rootdir}
90 ;;
91 *)
92 # Use /boot/grub by default.
93 bootdir=${rootdir}/boot
94 ;;
95 esac
96
97 grubdir=${bootdir}/`echo grub | sed ${transform}`
98
99 prev_saved_entry=`$grub_editenv ${grubdir}/grubenv list | sed -n 's/^saved_entry=//p'`
100 if [ "$prev_saved_entry" ]; then
101 $grub_editenv ${grubdir}/grubenv set prev_saved_entry="$prev_saved_entry"
102 else
103 # We need some non-empty value for prev_saved_entry so that GRUB will
104 # recognise that grub-reboot has been used and restore the previous
105 # saved entry. "0" is the same as an empty value, i.e. the first menu
106 # entry.
107 $grub_editenv ${grubdir}/grubenv set prev_saved_entry=0
108 fi
109 $grub_editenv ${grubdir}/grubenv set saved_entry="$entry"
110
111 # Bye.
112 exit 0