3 # Copyright 2013 Intel Corporation
4 # Author: Artem Bityutskiy
7 PROG="setup-extlinux-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 extlinux configuration directory
28 conf_dir="$bootdir/extlinux"
29 # The extlinux configuration file
30 conf_file="$conf_dir/extlinux.conf"
33 # Create the default extlinux configuration file.
34 create_default_conf_file()
36 verbose "creating the default configuration file \"$conf_file\""
38 mkdir -p $verbose "$conf_dir" >&2
39 cat > "$conf_file" <<-EOF
48 # Check wheter the extlinux configuration file exist and if not:
49 # o create the default one if --force option was specified
50 # o fail if no --force option was specified
51 check_and_create_default_conf_file()
53 if [ -s "$conf_file" ]; then
57 if [ -n "$force" ]; then
58 create_default_conf_file "$1"
60 fatal "cannot find the extlinux configuration file" \
61 "(\"$conf_file\") (use -f to force creating the" \
66 # Get a regular expression for matching extlinux configuration file option "$1"
69 local opt="$(esc_regexp "$1")"
71 opt="$(case_insensitive_regexp "$opt")"
72 printf "%s" "\(^[[:blank:]]*$opt[[:blank:]]\+\)\([^[:blank:]]\+\)\([[:blank:]]*$\)"
75 # Return a regular expression for matching label with name "$1".
78 local opt="$(case_insensitive_regexp "label")"
79 local label="$(esc_regexp "$1")"
81 printf "%s" "\(^[[:blank:]]*$opt[[:blank:]]\+\)\($label\)\([[:blank:]]*$\)"
87 local l_regexp="$(label_regexp "$label")"
88 local anyl_regexp="$(get_regexp "label")"
90 LC_ALL=C sed -i -n -e "/$l_regexp/ { # mathes our label line
91 :l n; # get the next line
92 /$l_regexp/bl # same label again, keep skipping
93 /$anyl_regexp/!bl # a different label, stop skipping
95 /$l_regexp/!p # print all other lines
98 remove_trailing_empty_lines "$conf_file"
102 # -----------------------------------------------------------------------------
103 # The "add" subcommand
104 # -----------------------------------------------------------------------------
110 Usage: $PROG add [options] <label> <title> <kernel> <options>
112 Add a new extlinux boot menu entry. The mandatory arguments are:
114 <label> - a unique name of the boot menu entry to add, the entry will be
115 further refurred by the label
116 <title> - the boot menu entry title
117 <kernel> - name of the kernel binary corresponding to the entry
118 <options> - kernel boot options
121 -f, --force if <label> already exists - re-create it, if
122 <bootdir>/extlinux/extlinux.conf does not exist - create it, if
123 <bootdir>/<kernel> does not exist - do not fail
124 -h, --help show this text and exit
128 show_add_usage_fail()
130 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
137 if [ "$#" -eq 0 ]; then
143 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
144 show_add_usage_fail "cannot parse command-line options"
159 *) show_add_usage_fail "unrecognized option \"$1\""
165 if [ "$#" -lt 4 ]; then
166 show_add_usage_fail "too little arguments"
168 if [ "$#" -gt 4 ]; then
169 show_add_usage_fail "too many arguments: \"$1\""
174 local label="$1"; shift
175 local title="$1"; shift
176 local kernel="$1"; shift
177 local options="$1"; shift
178 local kernel_path="$bootdir/$kernel"
180 verbose "label is \"$label\""
181 verbose "title is \"$title\""
182 verbose "kernel is \"$kernel\""
183 verbose "options are \"$options\""
185 if ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
186 fatal "cannot find kernel \"$kernel_path\"" \
187 "(use -f to ignore this error)"
190 # Make sure the extlinux configuration file exists
191 check_and_create_default_conf_file "$label"
193 if LC_ALL=C grep -q -e "$(label_regexp "$label")" "$conf_file" && \
194 [ -z "$force" ]; then
195 fatal "extlinux boot menu label \"$label\" already exists" \
196 "(use -f to force re-creating it)"
199 # Find out the kernel version from its name
200 local kernel_version="$(printf "%s" "$kernel" | LC_ALL=C \
201 sed -e 's/[^[:digit:]]\+-\([[:digit:]]\+.*\)/\1/')"
202 [ -n "$kernel_version" ] || \
203 fatal "cannot fetch kernel version from \"$kernel\""
205 # Remove the label if it exists, since we are going to add a new one
206 remove_label "$label"
208 local block="label $label
209 menu label $title ($kernel_version)
213 printf "\n%s\n" "$block" >> "$conf_file"
215 if [ -n "$verbose" ]; then
216 verbose "Added the following to \"$conf_file\":"
217 printf "%s\n" "$block" >&2
222 # -----------------------------------------------------------------------------
223 # The "remove" subcommand
224 # -----------------------------------------------------------------------------
230 Usage: $PROG remove [options] <label>
232 Delete extlinux boot entry which has label <label>.
235 -f, --force do not fail if the entry doesn't exist
236 -h, --help show this text and exit
240 show_remove_usage_fail()
242 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
243 show_remove_usage >&2
249 if [ "$#" -eq 0 ]; then
255 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
256 show_remove_usage_fail "cannot parse command-line options"
271 *) show_remove_usage_fail "unrecognized option \"$1\""
277 if [ "$#" -lt 1 ]; then
278 show_remove_usage_fail "too little arguments"
280 if [ "$#" -gt 1 ]; then
281 show_remove_usage_fail "too many arguments: \"$1\""
288 if ! LC_ALL=C grep -q -e "$(label_regexp "$label")" "$conf_file" && \
289 [ -z "$force" ]; then
290 fatal "cannot find label \"$label\" in \"$conf_file\"" \
291 "(use -f to ignore this error)"
294 remove_label "$label"
295 verbose "removed $label"
299 # -----------------------------------------------------------------------------
300 # The "default" subcommand
301 # -----------------------------------------------------------------------------
304 # Get the kernel binary name by its extlinux.conf label name
305 get_kernel_by_label()
308 local l_regexp="$(label_regexp "$label")" # Regexp for our label
309 local anyl_regexp="$(get_regexp "label")" # Regexp for any label
310 local linux_regexp="$(get_regexp "linux")"
311 local kernel_regexp="$(get_regexp "kernel")"
314 [ -z "$label" ] && return 0
316 result="$(LC_ALL=C sed -n -e "/$l_regexp/ {
318 /$linux_regexp/ { s/$linux_regexp/\2/p }
319 /$kernel_regexp/ { s/$kernel_regexp/\2/p }
320 /$anyl_regexp/!bl # Loop till the next label
323 printf "%s" "${result##*/}"
329 Usage: $PROG default [options] <label>
331 Set the default boot kernel to be the kernel which is marked with label <label>
332 the extlinux configuration file. If <label> is omited, this command prints the
333 currently default entry name.
336 -f, --force <bootdir>/extlinux/extlinux.conf does not exist - create it, if
337 <label> does not exist - do not fail
338 -h, --help show this text and exit
342 show_default_usage_fail()
344 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
345 show_default_usage >&2
352 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
353 show_default_usage_fail "cannot parse command-line options"
368 *) show_default_usage_fail "unrecognized option \"$1\""
374 if [ "$#" -gt 1 ]; then
375 show_default_usage_fail "too many arguments: \"$1\""
380 local label="${1:-}";
382 # Make sure the extlinux configuration file exists
383 check_and_create_default_conf_file "$label"
385 # Find the current default label
386 local regexp="$(get_regexp "default")"
387 local default_label="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$conf_file")"
389 if [ -z "$label" ]; then
390 printf "%s\n" "label: $default_label"
391 printf "%s\n" "kernel: $(get_kernel_by_label "$default_label")"
395 local l_regexp="$(label_regexp "$label")"
396 local labels="$(LC_ALL=C grep -e "$l_regexp" "$conf_file" | wc -l)"
398 if [ "$labels" -eq "0" ] && [ -z "$force" ]; then
399 fatal "cannot find label \"$label\" in \"$conf_file\"" \
400 "(use -f to ignore this error)"
403 if [ "$labels" -gt "1" ]; then
404 fatal "more than one labels \"$label\" in \"$conf_file\""
407 if [ -z "$default_label" ]; then
408 verbose "no default label found, adding \"$label\""
410 local def="$(esc_sed_replacement "default $label")"
411 local anyl_regexp="$(get_regexp "label")"
414 $ { s/.*/&\n$def/; q }
415 /^[[:blank:]]*$/ { s/.*/$def\n&/; q }
416 /$(anyl_regexp)/ { s/.*/$def\n&/; q }
421 # Escape special sed characters in "$entry" and replace the old default
422 # entry with the new one
423 local esc_label="$(esc_sed_replacement "$label")"
424 LC_ALL=C sed -i -e "s/$regexp/\1$esc_label\3/" "$conf_file"
425 verbose "set the default boot kernel to \"$label"\"
429 # -----------------------------------------------------------------------------
435 Usage: $PROG [options] <subcommand> [options] <arguments>
437 This program changes the extlinux bootloader configuration. Supported
439 add - add an extlinux boot menu entry for a kernel
440 remove - remove an extlinux boot menu entry
441 default - get or set the default extlinux boot menu entry
443 Run "$PROG <subcommand>" to see subcommand-specific help.
446 -b, --bootdir path to the boot directory (default is "/boot")
447 --version show the program version and exit
448 -v, --verbose be verbose
449 -h, --help show this text and exit
455 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
462 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
466 # If there is no argument or it starts with "-", complain
467 if [ -z "${1:-""}" ] || [ -z "${1##-*}" ]; then
468 fatal "--bootdir requires an argument"
485 *) show_usage_fail "unrecognized option \"$1\""
493 if [ "$#" -eq 0 ]; then
499 subcommand="$1"; shift
501 case "$subcommand" in
507 remove_subcommand "$@"
511 default_subcommand "$@"
514 *) show_usage_fail "unrecognized subcommand \"$subcommand\""