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" "$entry")"
294 if [ -z "$result" ]; then
295 regexp="$(get_regexp "linux")"
296 result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$entry")"
298 [ -n "$result" ] || return 0
302 printf "%s" "${result##*/}"
308 Usage: $PROG default [options] <entry>
310 Set the default boot kernel to <entry>, which is the gummiboot entry name to
311 become the default (without the ".conf" suffix, like in
312 <bootdir>/loader/entries/<entry>.conf). If <entry> is omited, this command
313 prints the currently default entry name.
316 -f, --force if <bootdir>/loader/loader.conf does not exist - create the
317 default one, if <bootdir>/loader/entries/<entry>.conf does not
319 -h, --help show this text and exit
323 show_default_usage_fail()
325 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
326 show_default_usage >&2
333 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
334 show_default_usage_fail "cannot parse command-line options"
349 *) show_default_usage_fail "unrecognized option \"$1\""
355 if [ "$#" -gt 1 ]; then
356 show_default_usage_fail "too many arguments: \"$1\""
361 local entry="${1:-}";
363 # Make sure the gummiboot configuration file exists
364 check_and_create_default_conf_file "$entry"
366 # Find the current default entry
367 local regexp="$(get_regexp "default")"
368 local default_entry="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$conf_file")"
370 if [ -z "$entry" ]; then
371 printf "%s\n" "entry: $default_entry"
372 printf "%s\n" "kernel: $(get_kernel_from_entry "$default_entry")"
376 local entry_path="$entries_dir/${entry}.conf"
377 if ! [ -f "$entry_path" ] && [ -z "$force" ]; then
378 fatal "cannot find the gummiboot entry \"$entry_path\"" \
379 "(use -f to ignore this error)"
382 if [ -z "$default_entry" ]; then
383 verbose "no default entry found, adding \"$entry\""
384 printf "%s" "default $entry" >> "$conf_file"
388 [ "$(printf "%s\n" "$default_entry" | wc -l)" -eq "1" ] || \
389 fatal "more than one default entry in \"$conf_file\""
391 # Escape special sed characters in "$entry" and replace the old default
392 # entry with the new one
393 local entry_esc="$(esc_sed_replacement "$entry")"
394 LC_ALL=C sed -i -e "s/$regexp/\1$entry_esc\3/" "$conf_file"
396 verbose "set the default boot kernel to \"$entry"\"
400 # -----------------------------------------------------------------------------
406 Usage: $PROG [options] <subcommand> [options] <arguments>
408 This program changes the gummiboot bootloader configuration. Supported
410 add - add a gummiboot entry for a kernel
411 remove - remove a gummiboot entry
412 default - get or set the default gummiboot entry
414 Run "$PROG <subcommand>" to see subcommand-specific help.
417 -b, --bootdir path to the boot directory (default is "/boot")
418 --version show the program version and exit
419 -v, --verbose be verbose
420 -h, --help show this text and exit
426 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
433 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
437 # If there is no argument or it starts with "-", complain
438 if [ -z "${1:-""}" ] || [ -z "${1##-*}" ]; then
439 fatal "--bootdir requires an argument"
456 *) show_usage_fail "unrecognized option \"$1\""
464 if [ "$#" -eq 0 ]; then
470 subcommand="$1"; shift
472 case "$subcommand" in
478 remove_subcommand "$@"
482 default_subcommand "$@"
485 *) show_usage_fail "unrecognized subcommand \"$subcommand\""