3 # Copyright 2013 Intel Corporation
4 # Author: Artem Bityutskiy
7 PROG="setup-gummiboot-conf"
10 srcdir="$(readlink -ev -- ${0%/*})"
11 if [ -f "$srcdir/setup-ivi-sh-functions" ]; then
12 . "$srcdir/setup-ivi-sh-functions"
14 . /usr/share/setup-ivi/setup-ivi-sh-functions
17 # This is a small trick which I use to make sure my scripts are portable -
18 # check if 'dash' is present, and if yes - use it.
19 if can_switch_to_dash; then
20 exec dash -euf -- "$srcdir/$PROG" "$@"
24 # Common preparations for all subcommands.
27 verbose "Boot directory is $bootdir"
28 [ -d "$bootdir" ] || \
29 fatal "boot directory path \"$bootdir\" does not exist"
31 # The gummiboot configuration directory
32 conf_dir="$bootdir/loader"
33 # The gummiboot configuration file
34 conf_file="$conf_dir/loader.conf"
35 # The gummiboot kernel entries directory
36 entries_dir="$bootdir/loader/entries"
39 # Get a regular expression for matching gummiboot configuration file option
43 local opt="$(esc_regexp "$1")"
45 opt="$(case_insensitive_regexp "$opt")"
46 printf "%s" "\(^[[:blank:]]*$opt[[:blank:]]\+\)\([^[:blank:]]\+\)\([[:blank:]]*$\)"
49 # Create the default gummiboot configuration file.
50 create_default_conf_file()
52 verbose "creating the default configuration file \"$conf_file\""
54 mkdir -p $verbose -- "$conf_dir" >&2
55 cat > "$conf_file" <<-EOF
62 # Check wheter the gummiboot configuration file exist, and if not:
63 # o create the default one if --force option was specified
64 # o fail if no --force option was specified
65 check_and_create_default_conf_file()
67 if [ -s "$conf_file" ]; then
71 if [ -n "$force" ]; then
72 create_default_conf_file "$1"
74 fatal "cannot find the gummiboot configuration file" \
75 "(\"$conf_file\") (use -f to create the default" \
81 # -----------------------------------------------------------------------------
82 # The "add" subcommand
83 # -----------------------------------------------------------------------------
89 Usage: $PROG add [options] <entry> <title> <kernel> <options>
91 Add a new gummiboot entry. The mandatory arguments are:
93 <entry> - name of the gummiboot entry to add
94 (<bootdir>/loader/entries/<entry>.conf)
95 <title> - the title of the entry
96 <kernel> - name of the kernel binary to add the entry for
98 <options> - kernel boot options
101 -f, --force if the entry already exists - re-write it, if
102 <bootdir>/loader/loader.conf does not exist - create one,
103 if <bootdir>/<kernel> does not exist - do not fail
104 -h, --help show this text and exit
108 show_add_usage_fail()
110 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
117 if [ "$#" -eq 0 ]; then
123 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
124 show_add_usage_fail "cannot parse command-line options"
139 *) show_add_usage_fail "unrecognized option \"$1\""
145 if [ "$#" -lt 4 ]; then
146 show_add_usage_fail "too little arguments"
148 if [ "$#" -gt 4 ]; then
149 show_add_usage_fail "too many arguments: \"$1\""
154 local entry="$1"; shift
155 local title="$1"; shift
156 local kernel="$1"; shift
157 local options="$1"; shift
158 local kernel_path="$bootdir/$kernel"
159 local entry_path="$entries_dir/${entry}.conf"
161 verbose "entry is \"$entry\""
162 verbose "title is \"$title\""
163 verbose "kernel is \"$kernel\""
164 verbose "options are \"$options\""
166 if [ -f "$entry_path" ] && [ -z "$force" ]; then
167 fatal "gummiboot entry \"$entry_path\" already exists" \
168 "(use -f to force re-creating it)"
171 if ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
172 fatal "cannot find kernel \"$kernel_path\"" \
173 "(use -f to ignore this error)"
176 # Make sure the gummiboot configuration file exists
177 check_and_create_default_conf_file "$entry"
179 # Find out the kernel version from its name
180 local kernel_version="$(printf "%s" "$kernel" | LC_ALL=C \
181 sed -e 's/[^[:digit:]]\+-\([[:digit:]]\+.*\)/\1/')"
182 [ -n "$kernel_version" ] || \
183 fatal "cannot fetch kernel version from \"$kernel\""
185 # Create the new entry
186 mkdir -p $verbose -- "$entries_dir" >&2
187 cat > "$entry_path" <<-EOF
190 version $kernel_version
195 if [ -n "$verbose" ]; then
196 verbose "contents of \"$entry_path\":"
197 cat -- "$entry_path" >&2
202 # -----------------------------------------------------------------------------
203 # The "remove" subcommand
204 # -----------------------------------------------------------------------------
210 Usage: $PROG remove [options] <entry>
212 Delete gummiboot entry <entry> (<bootdir>/loader/entries/<entry>.conf).
215 -f, --force do not fail if the entry doesn't exist
216 -h, --help show this text and exit
220 show_remove_usage_fail()
222 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
223 show_remove_usage >&2
229 if [ "$#" -eq 0 ]; then
235 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
236 show_remove_usage_fail "cannot parse command-line options"
251 *) show_remove_usage_fail "unrecognized option \"$1\""
257 if [ "$#" -lt 1 ]; then
258 show_remove_usage_fail "too little arguments"
260 if [ "$#" -gt 1 ]; then
261 show_remove_usage_fail "too many arguments: \"$1\""
267 local entry_path="$entries_dir/${entry}.conf"
269 if ! [ -f "$entry_path" ] && [ -z "$force" ]; then
270 fatal "gummiboot entry \"$entry_path\" doesn't exist" \
271 "(use -f to ignore this error)"
274 rm -rf $verbose -- "$entry_path" >&2
275 verbose "removed $entry_path"
279 # -----------------------------------------------------------------------------
280 # The "default" subcommand
281 # -----------------------------------------------------------------------------
284 # Get the kernel name from a gummiboot entry
285 get_kernel_from_entry()
288 local entry="$entries_dir/$1.conf"
290 if [ -f "$entry" ]; then
291 local regexp="$(get_regexp "efi")"
292 local result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
295 if [ -z "$result" ]; then
296 regexp="$(get_regexp "linux")"
297 result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
300 [ -n "$result" ] || return 0
304 printf "%s" "${result##*/}"
310 Usage: $PROG default [options] <entry>
312 Set the default boot kernel to <entry>, which is the gummiboot entry name to
313 become the default (without the ".conf" suffix, like in
314 <bootdir>/loader/entries/<entry>.conf). If <entry> is omited, this command
315 prints the currently default entry name.
318 -f, --force if <bootdir>/loader/loader.conf does not exist - create the
319 default one, if <bootdir>/loader/entries/<entry>.conf does not
321 -h, --help show this text and exit
325 show_default_usage_fail()
327 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
328 show_default_usage >&2
335 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
336 show_default_usage_fail "cannot parse command-line options"
351 *) show_default_usage_fail "unrecognized option \"$1\""
357 if [ "$#" -gt 1 ]; then
358 show_default_usage_fail "too many arguments: \"$1\""
363 local entry="${1:-}";
365 # Make sure the gummiboot configuration file exists
366 check_and_create_default_conf_file "$entry"
368 # Find the current default entry
369 local regexp="$(get_regexp "default")"
370 local default_entry="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
373 if [ -z "$entry" ]; then
374 printf "%s\n" "entry: $default_entry"
375 printf "%s\n" "kernel: $(get_kernel_from_entry "$default_entry")"
379 local entry_path="$entries_dir/${entry}.conf"
380 if ! [ -f "$entry_path" ] && [ -z "$force" ]; then
381 fatal "cannot find the gummiboot entry \"$entry_path\"" \
382 "(use -f to ignore this error)"
385 if [ -z "$default_entry" ]; then
386 verbose "no default entry found, adding \"$entry\""
387 printf "%s" "default $entry" >> "$conf_file"
391 [ "$(printf "%s\n" "$default_entry" | wc -l)" -eq "1" ] || \
392 fatal "more than one default entry in \"$conf_file\""
394 # Escape special sed characters in "$entry" and replace the old default
395 # entry with the new one
396 local entry_esc="$(esc_sed_replacement "$entry")"
397 LC_ALL=C sed -i -e "s/$regexp/\1$entry_esc\3/" -- "$conf_file"
399 verbose "set the default boot kernel to \"$entry"\"
403 # -----------------------------------------------------------------------------
409 Usage: $PROG [options] <subcommand> [options] <arguments>
411 This program changes the gummiboot bootloader configuration. Supported
413 add - add a gummiboot entry for a kernel
414 remove - remove a gummiboot entry
415 default - get or set the default gummiboot entry
417 Run "$PROG <subcommand>" to see subcommand-specific help.
420 -b, --bootdir path to the boot directory (default is "/boot")
421 --version show the program version and exit
422 -v, --verbose be verbose
423 -h, --help show this text and exit
429 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
436 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
440 # If there is no argument or it starts with "-", complain
441 if [ -z "${1:-""}" ] || [ -z "${1##-*}" ]; then
442 fatal "--bootdir requires an argument"
459 *) show_usage_fail "unrecognized option \"$1\""
467 if [ "$#" -eq 0 ]; then
473 subcommand="$1"; shift
475 case "$subcommand" in
481 remove_subcommand "$@"
485 default_subcommand "$@"
488 *) show_usage_fail "unrecognized subcommand \"$subcommand\""