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 rootdir="$(installerfw_mnt_prefix "/")"
35 bootdir="$(installerfw_mnt_prefix "/boot")"
37 if installerfw_is_efi_boot_system; then
45 # -----------------------------------------------------------------------------
46 # The "add" subcommand
47 # -----------------------------------------------------------------------------
53 Usage: $PROG add [options] <kernel>
55 Add bootloader entries for kernel <kernel>.
58 -f, --force if bootloader entries for <kernel> already exist in bootloader's
59 config file(s) - re-write them, if <kernel> does not exist - do
61 -h, --help show this text and exit
67 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
74 if [ "$#" -eq 0 ]; then
80 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
81 show_add_usage_fail "cannot parse command-line options"
96 *) show_add_usage_fail "unrecognized option \"$1\""
102 if [ "$#" -lt 1 ]; then
103 show_add_usage_fail "please, specify the kernel"
105 if [ "$#" -gt 1 ]; then
106 show_add_usage_fail "too many arguments: \"$1\""
112 local kernel_path="$bootdir/$kernel"
114 if ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
115 fatal "cannot find kernel \"$kernel_path\"" \
116 "(use -f to ignore this error)"
119 # Get root partition PARTUUID
120 installerfw_get_part_info "/" "PARTUUID" "root_partuuid"
121 [ -n "$root_partuuid" ] || \
122 fatal "cannot find PARTUUID of the root partition"
125 local options="${INSTALLERFW_KERNEL_OPTS:-} root=PARTUUID=$root_partuuid"
129 get_os_name "os_name"
131 # Add the default bootloader entry
132 setup-$boot-conf $verbose --bootdir "$bootdir" add $force \
133 "$kernel" "$os_name" "$kernel" "$options"
135 # Add the debug bootloader entry. If there is the "quiet" option,
136 # create a non-quiet configuration.
137 local verbose_opts="$(printf "%s" "$options" | LC_ALL=C \
138 sed -e "s/[[:blank:]]\+quiet[[:blank:]]\+/ /
139 s/^quiet[[:blank:]]\+//
140 s/[[:blank:]]\+quiet$//
143 local debug_opts="$verbose_opts ignore_loglevel log_buf_len=2M"
144 local debug_opts="$debug_opts initcall_debug"
147 setup-$boot-conf $verbose --bootdir "$bootdir" add \
148 $force "$kernel-debug" "Debug $os_name" \
149 "$kernel" "$verbose_opts"
151 # Add the clone bootloader entry, but only if the cloning tool is
153 if [ -f "$rootdir/usr/sbin/setup-ivi-clone" ]; then
154 clone_opts="$options systemd.unit=ivi-clone.service"
155 clone_opts="$clone_opts ivi-clone-target=autodetect"
156 setup-$boot-conf $verbose --bootdir "$bootdir" add \
157 $force "$kernel-clone" "Clone $os_name" \
158 "$kernel" "$clone_opts"
163 # -----------------------------------------------------------------------------
164 # The "remove" subcommand
165 # -----------------------------------------------------------------------------
171 Usage: $PROG remove [options] <kernel>
173 Delete bootloader entries for kernel <kernel> (only those which were previously
174 created with "$PROG add").
177 -f, --force do not fail if <kernel> does not have corresponding bootloader
179 -h, --help show this text and exit
183 show_remove_usage_fail()
185 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
186 show_remove_usage >&2
192 if [ "$#" -eq 0 ]; then
198 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
199 show_remove_usage_fail "cannot parse command-line options"
214 *) show_remove_usage_fail "unrecognized option \"$1\""
220 if [ "$#" -lt 1 ]; then
221 show_add_usage_fail "please, specify the kernel"
223 if [ "$#" -gt 1 ]; then
224 show_remove_usage_fail "too many arguments: \"$1\""
231 # Get the current default entry
232 local default="$(setup-$boot-conf $verbose --bootdir "$bootdir" \
235 fatal "cannot get the default kernel, setup-$boot-conf failed"
237 local default_kernel="$(printf "%s" "$default" | LC_ALL=C \
238 sed -n -e 's/^kernel: \(.\+\)$/\1/p')"
240 if [ -n "$default_kernel" ]; then
241 verbose "current default boot kernel is " \
242 "\"$default_kernel\""
244 verbose "did not find the default kernel," \
245 "setup-$boot-conf returned: $default"
249 setup-$boot-conf $verbose --bootdir "$bootdir" \
250 remove $force "$kernel" || \
251 fatal "setup-$boot-conf failed to remove" \
253 setup-$boot-conf $verbose --bootdir "$bootdir" \
254 remove $force "$kernel-debug" || \
255 fatal "setup-$boot-conf failed to remove" \
256 "entry \"$kernel-verbose\""
257 # The "clone" entry does not necessary exist, so use --force
258 setup-$boot-conf $verbose --bootdir "$bootdir" \
259 remove --force "$kernel-clone" || \
260 fatal "setup-$boot-conf failed to remove" \
261 "entry \"$kernel-clone\""
263 # If this is not the default kernel, we are done
264 [ "$kernel" = "$default_kernel" ] || return 0
266 # We've just removed the default kernel, find the kernel with the
267 # latest version and make it to be the default
269 verbose "removed the default kernel, find the newest available"
271 local newest_kernel="$(get_newest_kernel "$bootdir" "$kernel")"
273 if [ -z "$newest_kernel" ]; then
274 verbose "no more kernels, set the kernel to none"
275 setup-$boot-conf $verbose --bootdir "$bootdir" \
276 default --force "<none>"
278 verbose "new default kernel is \"$newest_kernel\""
279 setup-$boot-conf $verbose --bootdir "$bootdir" \
280 default "$newest_kernel"
283 if [ "$?" -ne 0 ]; then
284 fatal "cannot set default kernel, \"setup-$boot-conf\" failed"
289 # -----------------------------------------------------------------------------
290 # The "default" subcommand
291 # -----------------------------------------------------------------------------
297 Usage: $PROG default [options] <kernel>
299 Set the default boot kernel to <kernel>. If <kernel> is omited, print the
300 current default boot kernel name.
303 -f, --force do not fail if <kernel> doesn't exist
304 -h, --help show this text and exit
308 show_default_usage_fail()
310 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
311 show_default_usage >&2
318 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
319 show_default_usage_fail "cannot parse command-line options"
334 *) show_default_usage_fail "unrecognized option \"$1\""
340 if [ "$#" -gt 1 ]; then
341 show_default_usage_fail "too many arguments: \"$1\""
346 local kernel="${1:-}"
347 local kernel_path="$bootdir/$kernel"
349 if [ -n "$kernel" ] && ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
350 fatal "cannot find kernel \"$kernel_path\"" \
351 "(use -f to ignore this error)"
354 setup-$boot-conf $verbose --bootdir "$bootdir" default $force "$kernel"
358 # -----------------------------------------------------------------------------
364 Usage: $PROG [options] <subcommand> [options] <arguments>
366 This program adds or removes a kernel to/from the bootloader configuration
367 file(s). This is a Tizen IVI-specific program and it currently supports only 2
368 bootloader types - extlinux and gummiboot. Each new kernel gets 2 boot menu
369 entries - the default and verbose, and both of these are removed by the
372 The supported subcommands are:
373 add - add bootloader entries for a kernel
374 remove - remove bootloader entries for a kernel
375 default - get or set the default boot kernel
377 Run "$PROG <subcommand>" to see subcommand-specific help.
380 --version show the program version and exit
381 -v, --verbose be verbose
382 -h, --help show this text and exit
388 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
394 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
409 *) show_usage_fail "unrecognized option \"$1\""
415 if [ "$#" -eq 0 ]; then
422 subcommand="$1"; shift
424 case "$subcommand" in
430 remove_subcommand "$@"
434 default_subcommand "$@"
437 *) show_usage_fail "unrecognized subcommand \"$subcommand\""