3 # Copyright 2013 Intel Corporation
4 # Author: Artem Bityutskiy
7 PROG="setup-ivi-bootloader-conf"
10 srcdir="$(readlink -ev -- ${0%/*})"
11 PATH="/usr/share/setup-ivi:$srcdir:$PATH"
13 if [ -f "$srcdir/setup-ivi-sh-functions" ]; then
14 . "$srcdir/setup-ivi-sh-functions"
15 . "$srcdir/installerfw-sh-functions"
17 . /usr/share/setup-ivi/setup-ivi-sh-functions
18 . /usr/share/setup-ivi/installerfw-sh-functions
21 # This is a small trick which I use to make sure my scripts are portable -
22 # check if 'dash' is present, and if yes - use it.
23 if can_switch_to_dash; then
24 exec dash -euf "$srcdir/$PROG" "$@"
28 # Preparation stuff common for all subcommands
31 # Get the installer framework environment
32 installerfw_restore_env
34 bootdir="$(installerfw_mnt_prefix "/boot")"
36 if installerfw_is_efi_boot_system; then
44 # -----------------------------------------------------------------------------
45 # The "add" subcommand
46 # -----------------------------------------------------------------------------
52 Usage: $PROG add [options] <kernel>
54 Add bootloader entries for kernel <kernel>.
57 -f, --force if bootloader entries for <kernel> already exist in bootloader's
58 config file(s) - re-write them, if <kernel> does not exist - do
60 -h, --help show this text and exit
66 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
73 if [ "$#" -eq 0 ]; then
79 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
80 show_add_usage_fail "cannot parse command-line options"
95 *) show_add_usage_fail "unrecognized option \"$1\""
101 if [ "$#" -lt 1 ]; then
102 show_add_usage_fail "please, specify the kernel"
104 if [ "$#" -gt 1 ]; then
105 show_add_usage_fail "too many arguments: \"$1\""
111 local kernel_path="$bootdir/$kernel"
113 if ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
114 fatal "cannot find kernel \"$kernel_path\"" \
115 "(use -f to ignore this error)"
118 # Get root partition PARTUUID
119 installerfw_get_part_info "/" "PARTUUID" "root_partuuid"
120 [ -n "$root_partuuid" ] || \
121 fatal "cannot find PARTUUID of the root partition"
124 local options="${INSTALLERFW_KERNEL_OPTS:-} root=PARTUUID=$root_partuuid"
128 get_os_name "os_name"
130 # Add the bootloader entry
131 setup-$boot-conf $verbose --bootdir "$bootdir" add $force \
132 "$kernel" "$os_name" "$kernel" "$options"
134 # If there is the "quiet" option, create a non-quiet configuration
135 local dbgopts="$(printf "%s" "$options" | LC_ALL=C \
136 sed -e "s/[[:blank:]]\+quiet[[:blank:]]\+/ /
137 s/^quiet[[:blank:]]\+//
138 s/[[:blank:]]\+quiet$//
141 dbgopts="$dbgopts ignore_loglevel initcall_debug log_buf_len=2M"
143 setup-$boot-conf $verbose --bootdir "$bootdir" add \
144 $force "$kernel-debug" "$os_name (debug)" \
149 # -----------------------------------------------------------------------------
150 # The "remove" subcommand
151 # -----------------------------------------------------------------------------
157 Usage: $PROG remove [options] <kernel>
159 Delete bootloader entries for kernel <kernel> (only those which were previously
160 created with "$PROG add").
163 -f, --force do not fail if <kernel> does not have corresponding bootloader
165 -h, --help show this text and exit
169 show_remove_usage_fail()
171 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
172 show_remove_usage >&2
178 if [ "$#" -eq 0 ]; then
184 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
185 show_remove_usage_fail "cannot parse command-line options"
200 *) show_remove_usage_fail "unrecognized option \"$1\""
206 if [ "$#" -lt 1 ]; then
207 show_add_usage_fail "please, specify the kernel"
209 if [ "$#" -gt 1 ]; then
210 show_remove_usage_fail "too many arguments: \"$1\""
217 # Get the current default entry
218 local default="$(setup-$boot-conf $verbose --bootdir "$bootdir" \
221 fatal "cannot get the default kernel, setup-$boot-conf failed"
223 local default_kernel="$(printf "%s" "$default" | LC_ALL=C \
224 sed -n -e 's/^kernel: \(.\+\)$/\1/p')"
226 if [ -n "$default_kernel" ]; then
227 verbose "current default boot kernel is " \
228 "\"$default_kernel\""
230 verbose "did not find the default kernel," \
231 "setup-$boot-conf returned: $default"
235 setup-$boot-conf $verbose --bootdir "$bootdir" \
236 remove $force "$kernel" || \
237 fatal "setup-$boot-conf failed to remove" \
239 setup-$boot-conf $verbose --bootdir "$bootdir" \
240 remove $force "$kernel-verbose" || \
241 fatal "setup-$boot-conf failed to remove" \
242 "entry \"$kernel-verbose\""
244 # If this is not the default kernel, we are done
245 [ "$kernel" = "$default_kernel" ] || return 0
247 # We've just removed the default kernel, find the kernel with the
248 # latest version and make it to be the default
250 verbose "removed the default kernel, find the newest available"
252 local newest_kernel="$(get_newest_kernel "$bootdir" "$kernel")"
254 if [ -z "$newest_kernel" ]; then
255 verbose "no more kernels, set the kernel to none"
256 setup-$boot-conf $verbose --bootdir "$bootdir" \
257 default --force "<none>"
259 verbose "new default kernel is \"$newest_kernel\""
260 setup-$boot-conf $verbose --bootdir "$bootdir" \
261 default "$newest_kernel"
264 if [ "$?" -ne 0 ]; then
265 fatal "cannot set default kernel, \"setup-$boot-conf\" failed"
270 # -----------------------------------------------------------------------------
271 # The "default" subcommand
272 # -----------------------------------------------------------------------------
278 Usage: $PROG default [options] <kernel>
280 Set the default boot kernel to <kernel>. If <kernel> is omited, print the
281 current default boot kernel name.
284 -f, --force do not fail if <kernel> doesn't exist
285 -h, --help show this text and exit
289 show_default_usage_fail()
291 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
292 show_default_usage >&2
299 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
300 show_default_usage_fail "cannot parse command-line options"
315 *) show_default_usage_fail "unrecognized option \"$1\""
321 if [ "$#" -gt 1 ]; then
322 show_default_usage_fail "too many arguments: \"$1\""
327 local kernel="${1:-}"
328 local kernel_path="$bootdir/$kernel"
330 if [ -n "$kernel" ] && ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
331 fatal "cannot find kernel \"$kernel_path\"" \
332 "(use -f to ignore this error)"
335 setup-$boot-conf $verbose --bootdir "$bootdir" default $force "$kernel"
339 # -----------------------------------------------------------------------------
345 Usage: $PROG [options] <subcommand> [options] <arguments>
347 This program adds or removes a kernel to/from the bootloader configuration
348 file(s). This is a Tizen IVI-specific program and it currently supports only 2
349 bootloader types - extlinux and gummiboot. Each new kernel gets 2 boot menu
350 entries - the default and verbose, and both of these are removed by the
353 The supported subcommands are:
354 add - add bootloader entries for a kernel
355 remove - remove bootloader entries for a kernel
356 default - get or set the default boot kernel
358 Run "$PROG <subcommand>" to see subcommand-specific help.
361 --version show the program version and exit
362 -v, --verbose be verbose
363 -h, --help show this text and exit
369 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
375 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
390 *) show_usage_fail "unrecognized option \"$1\""
396 if [ "$#" -eq 0 ]; then
403 subcommand="$1"; shift
405 case "$subcommand" in
411 remove_subcommand "$@"
415 default_subcommand "$@"
418 *) show_usage_fail "unrecognized subcommand \"$subcommand\""