3 # Copyright 2013 Intel Corporation
4 # Author: Artem Bityutskiy
7 PROG="setup-gummiboot-conf"
10 srcdir="$(readlink -ev -- ${0%/*})"
11 . "$srcdir/setup-ivi-sh-functions"
13 # This is a small trick which I use to make sure my scripts are portable -
14 # check if 'dash' is present, and if yes - use it.
15 if can_switch_to_dash; then
16 exec dash -euf "$srcdir/$PROG" "$@"
20 # Common preparations for all subcommands.
23 verbose "Boot directory is $bootdir"
24 [ -d "$bootdir" ] || \
25 fatal "boot directory path \"$bootdir\" does not exist"
27 # The gummiboot configuration directory
28 conf_dir="$bootdir/loader"
29 # The gummiboot configuration file
30 conf_file="$conf_dir/loader.conf"
31 # The gummiboot kernel entries directory
32 entries_dir="$bootdir/loader/entries"
35 # Get a regular expression for matching gummiboot configuration file option
39 local opt="$(esc_regexp "$1")"
41 opt="$(case_insensitive_regexp "$opt")"
42 printf "%s" "\(^[[:blank:]]*$opt[[:blank:]]\+\)\([^[:blank:]]\+\)\([[:blank:]]*$\)"
45 # Create the default gummiboot configuration file.
46 create_default_conf_file()
48 verbose "creating the default configuration file \"$conf_file\""
50 mkdir -p $verbose "$conf_dir" >&2
51 cat > "$conf_file" <<-EOF
58 # Check wheter the gummiboot configuration file exist, and if not:
59 # o create the default one if --force option was specified
60 # o fail if no --force option was specified
61 check_and_create_default_conf_file()
63 if [ -s "$conf_file" ]; then
67 if [ -n "$force" ]; then
68 create_default_conf_file "$1"
70 fatal "cannot find the gummiboot configuration file" \
71 "(\"$conf_file\") (use -f to create the default" \
77 # -----------------------------------------------------------------------------
78 # The "add" subcommand
79 # -----------------------------------------------------------------------------
85 Usage: $PROG add [options] <entry> <title> <kernel> <options>
87 Add a new gummiboot entry. The mandatory arguments are:
89 <entry> - name of the gummiboot entry to add
90 (<bootdir>/loader/entries/<entry>.conf)
91 <title> - the title of the entry
92 <kernel> - name of the kernel binary to add the entry for
94 <options> - kernel boot options
97 -f, --force if the entry already exists - re-write it, if
98 <bootdir>/loader/loader.conf does not exist - create one,
99 if <bootdir>/<kernel> does not exist - do not fail
100 -h, --help show this text and exit
104 show_add_usage_fail()
106 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
113 if [ "$#" -eq 0 ]; then
119 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
120 show_add_usage_fail "cannot parse command-line options"
135 *) show_add_usage_fail "unrecognized option \"$1\""
141 if [ "$#" -lt 4 ]; then
142 show_add_usage_fail "too little arguments"
144 if [ "$#" -gt 4 ]; then
145 show_add_usage_fail "too many arguments: \"$1\""
150 local entry="$1"; shift
151 local title="$1"; shift
152 local kernel="$1"; shift
153 local options="$1"; shift
154 local kernel_path="$bootdir/$kernel"
155 local entry_path="$entries_dir/${entry}.conf"
157 verbose "entry is \"$entry\""
158 verbose "title is \"$title\""
159 verbose "kernel is \"$kernel\""
160 verbose "options are \"$options\""
162 if [ -f "$entry_path" ] && [ -z "$force" ]; then
163 fatal "gummiboot entry \"$entry_path\" already exists" \
164 "(use -f to force re-creating it)"
167 if ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
168 fatal "cannot find kernel \"$kernel_path\"" \
169 "(use -f to ignore this error)"
172 # Make sure the gummiboot configuration file exists
173 check_and_create_default_conf_file "$entry"
175 # Find out the kernel version from its name
176 local kernel_version="$(printf "%s" "$kernel" | LC_ALL=C \
177 sed -e 's/[^[:digit:]]\+-\([[:digit:]]\+.*\)/\1/')"
178 [ -n "$kernel_version" ] || \
179 fatal "cannot fetch kernel version from \"$kernel\""
181 # Create the new entry
182 mkdir -p $verbose "$entries_dir" >&2
183 cat > "$entry_path" <<-EOF
186 version $kernel_version
191 if [ -n "$verbose" ]; then
192 verbose "contents of \"$entry_path\":"
193 cat "$entry_path" >&2
198 # -----------------------------------------------------------------------------
199 # The "remove" subcommand
200 # -----------------------------------------------------------------------------
206 Usage: $PROG remove [options] <entry>
208 Delete gummiboot entry <entry> (<bootdir>/loader/entries/<entry>.conf).
211 -f, --force do not fail if the entry doesn't exist
212 -h, --help show this text and exit
216 show_remove_usage_fail()
218 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
219 show_remove_usage >&2
225 if [ "$#" -eq 0 ]; then
231 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
232 show_remove_usage_fail "cannot parse command-line options"
247 *) show_remove_usage_fail "unrecognized option \"$1\""
253 if [ "$#" -lt 1 ]; then
254 show_remove_usage_fail "too little arguments"
256 if [ "$#" -gt 1 ]; then
257 show_remove_usage_fail "too many arguments: \"$1\""
263 local entry_path="$entries_dir/${entry}.conf"
265 if ! [ -f "$entry_path" ] && [ -z "$force" ]; then
266 fatal "gummiboot entry \"$entry_path\" doesn't exist" \
267 "(use -f to ignore this error)"
270 rm -rf $verbose "$entry_path" >&2
271 verbose "removed $entry_path"
275 # -----------------------------------------------------------------------------
276 # The "default" subcommand
277 # -----------------------------------------------------------------------------
280 # Get the kernel name from a gummiboot entry
281 get_kernel_from_entry()
284 local entry="$entries_dir/$1.conf"
286 if [ -f "$entry" ]; then
287 local regexp="$(get_regexp "efi")"
288 local result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$entry")"
290 if [ -z "$result" ]; then
291 regexp="$(get_regexp "linux")"
292 result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$entry")"
294 [ -n "$result" ] || return 0
298 printf "%s" "${result##*/}"
304 Usage: $PROG default [options] <entry>
306 Set the default boot kernel to <entry>, which is the gummiboot entry name to
307 become the default (without the ".conf" suffix, like in
308 <bootdir>/loader/entries/<entry>.conf). If <entry> is omited, this command
309 prints the currently default entry name.
312 -f, --force if <bootdir>/loader/loader.conf does not exist - create the
313 default one, if <bootdir>/loader/entries/<entry>.conf does not
315 -h, --help show this text and exit
319 show_default_usage_fail()
321 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
322 show_default_usage >&2
329 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
330 show_default_usage_fail "cannot parse command-line options"
345 *) show_default_usage_fail "unrecognized option \"$1\""
351 if [ "$#" -gt 1 ]; then
352 show_default_usage_fail "too many arguments: \"$1\""
357 local entry="${1:-}";
359 # Make sure the gummiboot configuration file exists
360 check_and_create_default_conf_file "$entry"
362 # Find the current default entry
363 local regexp="$(get_regexp "default")"
364 local default_entry="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$conf_file")"
366 if [ -z "$entry" ]; then
367 printf "%s\n" "entry: $default_entry"
368 printf "%s\n" "kernel: $(get_kernel_from_entry "$default_entry")"
372 local entry_path="$entries_dir/${entry}.conf"
373 if ! [ -f "$entry_path" ] && [ -z "$force" ]; then
374 fatal "cannot find the gummiboot entry \"$entry_path\"" \
375 "(use -f to ignore this error)"
378 if [ -z "$default_entry" ]; then
379 verbose "no default entry found, adding \"$entry\""
380 printf "%s" "default $entry" >> "$conf_file"
384 [ "$(printf "%s\n" "$default_entry" | wc -l)" -eq "1" ] || \
385 fatal "more than one default entry in \"$conf_file\""
387 # Escape special sed characters in "$entry" and replace the old default
388 # entry with the new one
389 local entry_esc="$(esc_sed_replacement "$entry")"
390 LC_ALL=C sed -i -e "s/$regexp/\1$entry_esc\3/" "$conf_file"
392 verbose "set the default boot kernel to \"$entry"\"
396 # -----------------------------------------------------------------------------
402 Usage: $PROG [options] <subcommand> [options] <arguments>
404 This program changes the gummiboot bootloader configuration. Supported
406 add - add a gummiboot entry for a kernel
407 remove - remove a gummiboot entry
408 default - get or set the default gummiboot entry
410 Run "$PROG <subcommand>" to see subcommand-specific help.
413 -b, --bootdir path to the boot directory (default is "/boot")
414 --version show the program version and exit
415 -v, --verbose be verbose
416 -h, --help show this text and exit
422 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
429 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
433 # If there is no argument or it starts with "-", complain
434 if [ -z "${1:-""}" ] || [ -z "${1##-*}" ]; then
435 fatal "--bootdir requires an argument"
452 *) show_usage_fail "unrecognized option \"$1\""
460 if [ "$#" -eq 0 ]; then
466 subcommand="$1"; shift
468 case "$subcommand" in
474 remove_subcommand "$@"
478 default_subcommand "$@"
481 *) show_usage_fail "unrecognized subcommand \"$subcommand\""