3 # Copyright 2013 Intel Corporation
4 # Author: Artem Bityutskiy
7 PROG="setup-extlinux-conf"
10 srcdir="$(readlink -ev -- ${0%/*})"
11 PATH="/usr/share/setup-ivi:$srcdir:$PATH"
13 . setup-ivi-sh-functions
15 # This is a small trick which I use to make sure my scripts are portable -
16 # check if 'dash' is present, and if yes - use it.
17 if can_switch_to_dash; then
18 exec dash -euf "$srcdir/$PROG" "$@"
22 # Common preparations for all subcommands.
25 verbose "Boot directory is $bootdir"
26 [ -d "$bootdir" ] || \
27 fatal "boot directory path \"$bootdir\" does not exist"
29 # The extlinux configuration directory
30 conf_dir="$bootdir/extlinux"
31 # The extlinux configuration file
32 conf_file="$conf_dir/extlinux.conf"
35 # Create the default extlinux configuration file.
36 create_default_conf_file()
38 verbose "creating the default configuration file \"$conf_file\""
40 mkdir -p $verbose "$conf_dir" >&2
41 cat > "$conf_file" <<-EOF
49 # Check wheter the extlinux configuration file exist and if not:
50 # o create the default one if --force option was specified
51 # o fail if no --force option was specified
52 check_and_create_default_conf_file()
54 if [ -s "$conf_file" ]; then
58 if [ -n "$force" ]; then
59 create_default_conf_file "$1"
61 fatal "cannot find the extlinux configuration file" \
62 "(\"$conf_file\") (use -f to force creating the" \
67 # Get a regular expression for matching extlinux configuration file option "$1"
70 local opt="$(esc_regexp "$1")"
72 opt="$(case_insensitive_regexp "$opt")"
73 printf "%s" "\(^[[:blank:]]*$opt[[:blank:]]\+\)\([^[:blank:]]\+\)\([[:blank:]]*$\)"
76 # Return a regular expression for matching label with name "$1".
79 local opt="$(case_insensitive_regexp "label")"
80 local label="$(esc_regexp "$1")"
82 printf "%s" "\(^[[:blank:]]*$opt[[:blank:]]\+\)\($label\)\([[:blank:]]*$\)"
88 local l_regexp="$(label_regexp "$label")"
89 local anyl_regexp="$(get_regexp "label")"
91 LC_ALL=C sed -i -n -e "/$l_regexp/ { # mathes our label line
92 :l n; # get the next line
93 /$l_regexp/bl # same label again, keep skipping
94 /$anyl_regexp/!bl # a different label, stop skipping
96 /$l_regexp/!p # print all other lines
99 remove_trailing_empty_lines "$conf_file"
103 # -----------------------------------------------------------------------------
104 # The "add" subcommand
105 # -----------------------------------------------------------------------------
111 Usage: $PROG add [options] <label> <title> <kernel> <options>
113 Add a new extlinux boot menu entry. The mandatory arguments are:
115 <label> - a unique name of the boot menu entry to add, the entry will be
116 further refurred by the label
117 <title> - the boot menu entry title
118 <kernel> - name of the kernel binary corresponding to the entry
119 <options> - kernel boot options
122 -f, --force if <label> already exists - re-create it, if
123 <bootdir>/extlinux/extlinux.conf does not exist - create it, if
124 <bootdir>/<kernel> does not exist - do not fail
125 -h, --help show this text and exit
129 show_add_usage_fail()
131 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
138 if [ "$#" -eq 0 ]; then
144 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
145 show_add_usage_fail "cannot parse command-line options"
160 *) show_add_usage_fail "unrecognized option \"$1\""
166 if [ "$#" -lt 4 ]; then
167 show_add_usage_fail "too little arguments"
169 if [ "$#" -gt 4 ]; then
170 show_add_usage_fail "too many arguments: \"$1\""
175 local label="$1"; shift
176 local title="$1"; shift
177 local kernel="$1"; shift
178 local options="$1"; shift
179 local kernel_path="$bootdir/$kernel"
181 verbose "label is \"$label\""
182 verbose "title is \"$title\""
183 verbose "kernel is \"$kernel\""
184 verbose "options are \"$options\""
186 if ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
187 fatal "cannot find kernel \"$kernel_path\"" \
188 "(use -f to ignore this error)"
191 # Make sure the extlinux configuration file exists
192 check_and_create_default_conf_file "$label"
194 if LC_ALL=C grep -q -e "$(label_regexp "$label")" "$conf_file" && \
195 [ -z "$force" ]; then
196 fatal "extlinux boot menu label \"$label\" already exists" \
197 "(use -f to force re-creating it)"
200 # Find out the kernel version from its name
201 local kernel_version="$(printf "%s" "$kernel" | LC_ALL=C \
202 sed -e 's/[^[:digit:]]\+-\([[:digit:]]\+.*\)/\1/')"
203 [ -n "$kernel_version" ] || \
204 fatal "cannot fetch kernel version from \"$kernel\""
206 # Remove the label if it exists, since we are going to add a new one
207 remove_label "$label"
209 local block="label $label
210 menu label $title ($kernel_version)
214 printf "\n%s\n" "$block" >> "$conf_file"
216 if [ -n "$verbose" ]; then
217 verbose "Added the following to \"$conf_file\":"
218 printf "%s\n" "$block" >&2
223 # -----------------------------------------------------------------------------
224 # The "remove" subcommand
225 # -----------------------------------------------------------------------------
231 Usage: $PROG remove [options] <label>
233 Delete extlinux boot entry which has label <label>.
236 -f, --force do not fail if the entry doesn't exist
237 -h, --help show this text and exit
241 show_remove_usage_fail()
243 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
244 show_remove_usage >&2
250 if [ "$#" -eq 0 ]; then
256 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
257 show_remove_usage_fail "cannot parse command-line options"
272 *) show_remove_usage_fail "unrecognized option \"$1\""
278 if [ "$#" -lt 1 ]; then
279 show_remove_usage_fail "too little arguments"
281 if [ "$#" -gt 1 ]; then
282 show_remove_usage_fail "too many arguments: \"$1\""
289 if ! LC_ALL=C grep -q -e "$(label_regexp "$label")" "$conf_file" && \
290 [ -z "$force" ]; then
291 fatal "cannot find label \"$label\" in \"$conf_file\"" \
292 "(use -f to ignore this error)"
295 remove_label "$label"
296 verbose "removed $label"
300 # -----------------------------------------------------------------------------
301 # The "default" subcommand
302 # -----------------------------------------------------------------------------
305 # Get the kernel binary name by its extlinux.conf label name
306 get_kernel_by_label()
309 local l_regexp="$(label_regexp "$label")" # Regexp for our label
310 local anyl_regexp="$(get_regexp "label")" # Regexp for any label
311 local linux_regexp="$(get_regexp "linux")"
312 local kernel_regexp="$(get_regexp "kernel")"
315 [ -z "$label" ] && return 0
317 result="$(LC_ALL=C sed -n -e "/$l_regexp/ {
319 /$linux_regexp/ { s/$linux_regexp/\2/p }
320 /$kernel_regexp/ { s/$kernel_regexp/\2/p }
321 /$anyl_regexp/!bl # Loop till the next label
324 printf "%s" "${result##*/}"
330 Usage: $PROG default [options] <label>
332 Set the default boot kernel to be the kernel which is marked with label <label>
333 the extlinux configuration file. If <label> is omited, this command prints the
334 currently default entry name.
337 -f, --force <bootdir>/extlinux/extlinux.conf does not exist - create it, if
338 <label> does not exist - do not fail
339 -h, --help show this text and exit
343 show_default_usage_fail()
345 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
346 show_default_usage >&2
353 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
354 show_default_usage_fail "cannot parse command-line options"
369 *) show_default_usage_fail "unrecognized option \"$1\""
375 if [ "$#" -gt 1 ]; then
376 show_default_usage_fail "too many arguments: \"$1\""
381 local label="${1:-}";
383 # Make sure the extlinux configuration file exists
384 check_and_create_default_conf_file "$label"
386 # Find the current default label
387 local regexp="$(get_regexp "default")"
388 local default_label="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$conf_file")"
390 if [ -z "$label" ]; then
391 printf "%s\n" "label: $default_label"
392 printf "%s\n" "kernel: $(get_kernel_by_label "$default_label")"
396 local l_regexp="$(label_regexp "$label")"
397 local labels="$(LC_ALL=C grep -e "$l_regexp" "$conf_file" | wc -l)"
399 if [ "$labels" -eq "0" ] && [ -z "$force" ]; then
400 fatal "cannot find label \"$label\" in \"$conf_file\"" \
401 "(use -f to ignore this error)"
404 if [ "$labels" -gt "1" ]; then
405 fatal "more than one labels \"$label\" in \"$conf_file\""
408 if [ -z "$default_label" ]; then
409 verbose "no default label found, adding \"$label\""
411 local def="$(esc_sed_replacement "default $label")"
412 local anyl_regexp="$(get_regexp "label")"
415 $ { s/.*/&\n$def/; q }
416 /^[[:blank:]]*$/ { s/.*/$def\n&/; q }
417 /$(anyl_regexp)/ { s/.*/$def\n&/; q }
422 # Escape special sed characters in "$entry" and replace the old default
423 # entry with the new one
424 local esc_label="$(esc_sed_replacement "$label")"
425 LC_ALL=C sed -i -e "s/$regexp/\1$esc_label\3/" "$conf_file"
426 verbose "set the default boot kernel to \"$label"\"
430 # -----------------------------------------------------------------------------
436 Usage: $PROG [options] <subcommand> [options] <arguments>
438 This program changes the extlinux bootloader configuration. Supported
440 add - add an extlinux boot menu entry for a kernel
441 remove - remove an extlinux boot menu entry
442 default - get or set the default extlinux boot menu entry
444 Run "$PROG <subcommand>" to see subcommand-specific help.
447 -b, --bootdir path to the boot directory (default is "/boot")
448 --version show the program version and exit
449 -v, --verbose be verbose
450 -h, --help show this text and exit
456 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
463 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
467 # If there is no argument or it starts with "-", complain
468 if [ -z "${1:-""}" ] || [ -z "${1##-*}" ]; then
469 fatal "--bootdir requires an argument"
486 *) show_usage_fail "unrecognized option \"$1\""
494 if [ "$#" -eq 0 ]; then
500 subcommand="$1"; shift
502 case "$subcommand" in
508 remove_subcommand "$@"
512 default_subcommand "$@"
515 *) show_usage_fail "unrecognized subcommand \"$subcommand\""