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