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
50 # Check wheter the extlinux configuration file exist and if not:
51 # o create the default one if --force option was specified
52 # o fail if no --force option was specified
53 check_and_create_default_conf_file()
55 if [ -s "$conf_file" ]; then
59 if [ -n "$force" ]; then
60 create_default_conf_file "$1"
62 fatal "cannot find the extlinux configuration file" \
63 "(\"$conf_file\") (use -f to force creating the" \
68 # Get a regular expression for matching extlinux configuration file option "$1"
71 local opt="$(esc_regexp "$1")"
73 opt="$(case_insensitive_regexp "$opt")"
74 printf "%s" "\(^[[:blank:]]*$opt[[:blank:]]\+\)\([^[:blank:]]\+\)\([[:blank:]]*$\)"
77 # Return a regular expression for matching label with name "$1".
80 local opt="$(case_insensitive_regexp "label")"
81 local label="$(esc_regexp "$1")"
83 printf "%s" "\(^[[:blank:]]*$opt[[:blank:]]\+\)\($label\)\([[:blank:]]*$\)"
89 local l_regexp="$(label_regexp "$label")"
90 local anyl_regexp="$(get_regexp "label")"
92 LC_ALL=C sed -i -n -e "/$l_regexp/ { # mathes our label line
93 :l n; # get the next line
94 /$l_regexp/bl # same label again, keep skipping
95 /$anyl_regexp/!bl # a different label, stop skipping
97 /$l_regexp/!p # print all other lines
100 remove_trailing_empty_lines "$conf_file"
104 # -----------------------------------------------------------------------------
105 # The "add" subcommand
106 # -----------------------------------------------------------------------------
112 Usage: $PROG add [options] <label> <title> <kernel> <options>
114 Add a new extlinux boot menu entry. The mandatory arguments are:
116 <label> - a unique name of the boot menu entry to add, the entry will be
117 further refurred by the label
118 <title> - the boot menu entry title
119 <kernel> - name of the kernel binary corresponding to the entry
120 <options> - kernel boot options
123 -f, --force if <label> already exists - re-create it, if
124 <bootdir>/extlinux/extlinux.conf does not exist - create it, if
125 <bootdir>/<kernel> does not exist - do not fail
126 -h, --help show this text and exit
130 show_add_usage_fail()
132 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
139 if [ "$#" -eq 0 ]; then
145 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
146 show_add_usage_fail "cannot parse command-line options"
161 *) show_add_usage_fail "unrecognized option \"$1\""
167 if [ "$#" -lt 4 ]; then
168 show_add_usage_fail "too little arguments"
170 if [ "$#" -gt 4 ]; then
171 show_add_usage_fail "too many arguments: \"$1\""
176 local label="$1"; shift
177 local title="$1"; shift
178 local kernel="$1"; shift
179 local options="$1"; shift
180 local kernel_path="$bootdir/$kernel"
182 verbose "label is \"$label\""
183 verbose "title is \"$title\""
184 verbose "kernel is \"$kernel\""
185 verbose "options are \"$options\""
187 if ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
188 fatal "cannot find kernel \"$kernel_path\"" \
189 "(use -f to ignore this error)"
192 # Make sure the extlinux configuration file exists
193 check_and_create_default_conf_file "$label"
195 if LC_ALL=C grep -q -e "$(label_regexp "$label")" "$conf_file" && \
196 [ -z "$force" ]; then
197 fatal "extlinux boot menu label \"$label\" already exists" \
198 "(use -f to force re-creating it)"
201 # Find out the kernel version from its name
202 local kernel_version="$(printf "%s" "$kernel" | LC_ALL=C \
203 sed -e 's/[^[:digit:]]\+-\([[:digit:]]\+.*\)/\1/')"
204 [ -n "$kernel_version" ] || \
205 fatal "cannot fetch kernel version from \"$kernel\""
207 # Remove the label if it exists, since we are going to add a new one
208 remove_label "$label"
210 local block="label $label
211 menu label $title ($kernel_version)
215 printf "\n%s\n" "$block" >> "$conf_file"
217 if [ -n "$verbose" ]; then
218 verbose "Added the following to \"$conf_file\":"
219 printf "%s\n" "$block" >&2
224 # -----------------------------------------------------------------------------
225 # The "remove" subcommand
226 # -----------------------------------------------------------------------------
232 Usage: $PROG remove [options] <label>
234 Delete extlinux boot entry which has label <label>.
237 -f, --force do not fail if the entry doesn't exist
238 -h, --help show this text and exit
242 show_remove_usage_fail()
244 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
245 show_remove_usage >&2
251 if [ "$#" -eq 0 ]; then
257 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
258 show_remove_usage_fail "cannot parse command-line options"
273 *) show_remove_usage_fail "unrecognized option \"$1\""
279 if [ "$#" -lt 1 ]; then
280 show_remove_usage_fail "too little arguments"
282 if [ "$#" -gt 1 ]; then
283 show_remove_usage_fail "too many arguments: \"$1\""
290 if ! LC_ALL=C grep -q -e "$(label_regexp "$label")" "$conf_file" && \
291 [ -z "$force" ]; then
292 fatal "cannot find label \"$label\" in \"$conf_file\"" \
293 "(use -f to ignore this error)"
296 remove_label "$label"
297 verbose "removed $label"
301 # -----------------------------------------------------------------------------
302 # The "default" subcommand
303 # -----------------------------------------------------------------------------
306 # Get the kernel binary name by its extlinux.conf label name
307 get_kernel_by_label()
310 local l_regexp="$(label_regexp "$label")" # Regexp for our label
311 local anyl_regexp="$(get_regexp "label")" # Regexp for any label
312 local linux_regexp="$(get_regexp "linux")"
313 local kernel_regexp="$(get_regexp "kernel")"
316 [ -z "$label" ] && return 0
318 result="$(LC_ALL=C sed -n -e "/$l_regexp/ {
320 /$linux_regexp/ { s/$linux_regexp/\2/p }
321 /$kernel_regexp/ { s/$kernel_regexp/\2/p }
322 /$anyl_regexp/!bl # Loop till the next label
325 printf "%s" "${result##*/}"
331 Usage: $PROG default [options] <label>
333 Set the default boot kernel to be the kernel which is marked with label <label>
334 the extlinux configuration file. If <label> is omited, this command prints the
335 currently default entry name.
338 -f, --force <bootdir>/extlinux/extlinux.conf does not exist - create it, if
339 <label> does not exist - do not fail
340 -h, --help show this text and exit
344 show_default_usage_fail()
346 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
347 show_default_usage >&2
354 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
355 show_default_usage_fail "cannot parse command-line options"
370 *) show_default_usage_fail "unrecognized option \"$1\""
376 if [ "$#" -gt 1 ]; then
377 show_default_usage_fail "too many arguments: \"$1\""
382 local label="${1:-}";
384 # Make sure the extlinux configuration file exists
385 check_and_create_default_conf_file "$label"
387 # Find the current default label
388 local regexp="$(get_regexp "default")"
389 local default_label="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$conf_file")"
391 if [ -z "$label" ]; then
392 printf "%s\n" "label: $default_label"
393 printf "%s\n" "kernel: $(get_kernel_by_label "$default_label")"
397 local l_regexp="$(label_regexp "$label")"
398 local labels="$(LC_ALL=C grep -e "$l_regexp" "$conf_file" | wc -l)"
400 if [ "$labels" -eq "0" ] && [ -z "$force" ]; then
401 fatal "cannot find label \"$label\" in \"$conf_file\"" \
402 "(use -f to ignore this error)"
405 if [ "$labels" -gt "1" ]; then
406 fatal "more than one labels \"$label\" in \"$conf_file\""
409 if [ -z "$default_label" ]; then
410 verbose "no default label found, adding \"$label\""
412 local def="$(esc_sed_replacement "default $label")"
413 local anyl_regexp="$(get_regexp "label")"
416 $ { s/.*/&\n$def/; q }
417 /^[[:blank:]]*$/ { s/.*/$def\n&/; q }
418 /$(anyl_regexp)/ { s/.*/$def\n&/; q }
423 # Escape special sed characters in "$entry" and replace the old default
424 # entry with the new one
425 local esc_label="$(esc_sed_replacement "$label")"
426 LC_ALL=C sed -i -e "s/$regexp/\1$esc_label\3/" "$conf_file"
427 verbose "set the default boot kernel to \"$label"\"
431 # -----------------------------------------------------------------------------
437 Usage: $PROG [options] <subcommand> [options] <arguments>
439 This program changes the extlinux bootloader configuration. Supported
441 add - add an extlinux boot menu entry for a kernel
442 remove - remove an extlinux boot menu entry
443 default - get or set the default extlinux boot menu entry
445 Run "$PROG <subcommand>" to see subcommand-specific help.
448 -b, --bootdir path to the boot directory (default is "/boot")
449 --version show the program version and exit
450 -v, --verbose be verbose
451 -h, --help show this text and exit
457 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
464 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
468 # If there is no argument or it starts with "-", complain
469 if [ -z "${1:-""}" ] || [ -z "${1##-*}" ]; then
470 fatal "--bootdir requires an argument"
487 *) show_usage_fail "unrecognized option \"$1\""
495 if [ "$#" -eq 0 ]; then
501 subcommand="$1"; shift
503 case "$subcommand" in
509 remove_subcommand "$@"
513 default_subcommand "$@"
516 *) show_usage_fail "unrecognized subcommand \"$subcommand\""