packaging: release out (setup-efi-ivi improvements)
[profile/ivi/setup-efi-ivi.git] / setup-gummiboot-conf
1 #!/bin/sh -efu
2
3 # Copyright 2013 Intel Corporation
4 # Author: Artem Bityutskiy
5 # License: GPLv2
6
7 # This script assumes there is a working gummiboot configuration exists. This
8 # scripts then scanse the boot partition, finds out which kernels are
9 # available, and updates the gummiboot kernel entries: adds missing kernel
10 # entries and delets non-existing kernel entries. The default entry is always
11 # set to the latest kernel.
12 #
13 # We make several assumptions in this script.
14 # 1. The kernel binary names are 'vmlinuz-<version>'
15 # 2. Kernel entry file names are 'vmlinuz-<version>.conf'
16 # 3. There is always the "default" keyword in loader.conf, and we use the
17 #    corresponding kernel entry for adding new kernels.
18 # 4. The 'default' entry in loader.conf does not does not use the wildcard ("*")
19 # 5. Kernels binaries are placed in the ESP root
20 #
21 # May be there are few more implicit assumption. This all can be improved and
22 # made to be more flexible if needed.
23
24 PROG="setup-gummiboot-conf"
25
26 # This is a helper function which printfs an error message and exits
27 fatal()
28 {
29         printf "%s\n" "$PROG: error: $1\n" 1>&2
30         exit 1
31 }
32
33 # Right now we assume that ESP is always in "/boot", but later we can add a
34 # command line option for this.
35 esp="/home/dedekind/tmp/boot"
36 conf_file="$esp/loader/loader.conf"
37 entries_dir="$esp/loader/entries"
38
39 # Make sure the gummiboot configuration file exists
40 [ -f "$conf_file" ] || \
41         fatal "cannot find the gummiboot configuration file (\"$conf_file\")"
42
43 # Get the list of installed kernels
44 kernels="$(ls -1 "$esp" | grep '^vmlinuz-' | sort -r)"
45 [ -n "$kernels" ] || \
46         fatal "no vmlinuz-* files found in \"$esp\""
47
48 # Get the list of gummiboot kernel entries
49 entries="$(ls -1 "$entries_dir" | sed -e 's/\.conf$//g' | sort -r)"
50 [ -n "$entries" ] || \
51         fatal "no gummiboot entries found in \"$entries_dir\""
52
53 # Get the default entry name
54 default_entry="$(cat "$conf_file" | sed -n -e 's/[ \t]*default[ \t]\+\(.\+\)[ \t]*/\1/p')"
55 [ -n "$default_entry" ] || \
56         fatal "cannot find the default entry in \"$conf_file\""
57 [ "$(printf "%s\n" "$default_entry" | wc -l)" -eq "1" ] || \
58         fatal "more than one default entry in \"$conf_file\""
59
60 if ! [ -f "$entries_dir/$default_entry" ]; then
61         # The default entry does not exist anymore. Pick the entry
62         # corresponding to the newest kernel then.
63         default_entry="$(printf "%s" "$entries" | head -n1)"
64 fi
65
66 # Use the default entry to prepare the pattern for other entries
67 pattern="$(cat "$entries_dir/$default_entry")"
68 # Drop few keywords which we won't need
69 pattern="$(printf "%s" "$pattern" | sed -e '/[ \t]*linux[ \t]\+/d')"
70 pattern="$(printf "%s" "$pattern" | sed -e '/[ \t]*efi[ \t]\+/d')"
71 pattern="$(printf "%s" "$pattern" | sed -e '/[ \t]*version[ \t]\+/d')"
72
73 # Create a gummiboot entry for every new kernel
74 printf "%s\n" "$kernels" | while IFS= read -r kernel; do
75
76         if [ -f "$entries_dir/$kernel.conf" ]; then
77                 continue
78         fi
79
80         kernel_version="$(printf "%s" $kernel | sed -e 's/vmlinuz-\(.*\)/\1/')"
81
82         printf "%s" "$pattern" > "$entries_dir/$kernel.conf"
83         printf "\n%s" "version $kernel_version" >> "$entries_dir/$kernel.conf"
84         printf "\n%s" "efi /$kernel" >> "$entries_dir/$kernel.conf"
85 done
86
87 # Update the default entry
88 newest_kernel="$(printf "%s" "$kernels" | head -n1)"
89 sed -i -e "s/[ \t]*default[ \t]\+.*/default $newest_kernel.conf/" "$conf_file"
90
91 # Remove gummiboot entries for non-existing kernels
92 printf "%s\n" "$entries" | while IFS= read -r entry; do
93         kernel="$entry"
94         [ -f "$esp/$kernel" ] || rm "$entries_dir/$entry.conf"
95 done