setup-gummiboot-conf: fix non-existing kernels handling
[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 scripts then scans ESP, finds out which kernels are available, and
8 # updates the gummiboot kernel entries: adds missing kernel entries and delets
9 # non-existing kernel entries.  The default entry is always set to the newest
10 # kernel version.
11 #
12 # This scripts makes several assumptions.
13 # 1. There is already a valid gummiboot configuration in ESP
14 # 2. The kernel binary names are 'vmlinuz-<version>', and the gummiboot kernel
15 #    entry file names are 'vmlinuz-<version>.conf'
16 # 3. There is always the "default" keyword in loader.conf.
17 # 4. The default entry (in 'loader.conf) can safely be used as a pattern for
18 #    adding new entries (e.g., the kernel arguments are taken from there)
19 # 4. The 'default' entry name does not have wildcards ("*")
20 # 5. Kernels binaries are placed in the root of ESP
21 #
22 # May be there are few more implicit assumption.
23 #
24 #This script requires a number of environment variables to be defined. Namely:
25 #
26 # 1. INSTALLERFW_MOUNT_PREFIX - where the target partitions are mounted (the
27 #    "root" directory of the file-system we install gummiboot to)
28
29 PROG="setup-gummiboot-conf"
30
31 # This is a helper function which printfs an error message and exits
32 fatal()
33 {
34         printf "%s\n" "$PROG: error: $1" 1>&2
35         exit 1
36 }
37
38 # Make sure the installer framework variables are defined
39 [ "${INSTALLERFW_MOUNT_PREFIX:+x}" == "x" ] ||
40        fatal "installer framework environment variables not found"
41
42 # Get the ESP location
43 esp="$INSTALLERFW_MOUNT_PREFIX/boot"
44
45 # The gummiboot configuration file
46 conf_file="$esp/loader/loader.conf"
47 # The gummiboot kernel entries directory
48 entries_dir="$esp/loader/entries"
49
50 # Make sure the gummiboot configuration file exists
51 [ -f "$conf_file" ] || \
52         fatal "cannot find the gummiboot configuration file (\"$conf_file\")"
53
54 # Get the list of installed kernels
55 kernels="$(ls -1 "$esp" | grep '^vmlinuz-' | sort -r)"
56 [ -n "$kernels" ] || \
57         fatal "no vmlinuz-* files found in \"$esp\""
58
59 # Get the list of gummiboot kernel entries
60 entries="$(ls -1 "$entries_dir" | sed -e 's/\.conf$//g' | sort -r)"
61 [ -n "$entries" ] || \
62         fatal "no gummiboot entries found in \"$entries_dir\""
63
64 # Get the default entry name
65 default_entry="$(cat "$conf_file" | sed -n -e 's/[ \t]*default[ \t]\+\(.\+\)[ \t]*/\1.conf/p')"
66 [ -n "$default_entry" ] || \
67         fatal "cannot find the default entry in \"$conf_file\""
68 [ "$(printf "%s\n" "$default_entry" | wc -l)" -eq "1" ] || \
69         fatal "more than one default entry in \"$conf_file\""
70
71 while ! [ -f "$entries_dir/$default_entry" ]; do
72         # The default entry does not exist anymore. Remove it and pick the
73         # entry corresponding to the newest installed kerne.
74         rm -f "$entries_dir/$default_entry"
75         entries="$(printf "%s" "$entries" | tail -n+2)"
76         default_entry="$(printf "%s" "$entries" | head -n1).conf"
77 done
78
79 # Use the default entry to prepare the pattern for other entries
80 pattern="$(cat "$entries_dir/$default_entry")"
81 # Drop few keywords which we won't need
82 pattern="$(printf "%s" "$pattern" | sed -e '/[ \t]*linux[ \t]\+/d')"
83 pattern="$(printf "%s" "$pattern" | sed -e '/[ \t]*efi[ \t]\+/d')"
84 pattern="$(printf "%s" "$pattern" | sed -e '/[ \t]*version[ \t]\+/d')"
85
86 # Create a gummiboot entry for every new kernel
87 printf "%s\n" "$kernels" | while IFS= read -r kernel; do
88         if [ -f "$entries_dir/$kernel.conf" ]; then
89                 continue
90         fi
91
92         kernel_version="$(printf "%s" $kernel | sed -e 's/vmlinuz-\(.*\)/\1/')"
93
94         printf "%s" "$pattern" > "$entries_dir/$kernel.conf"
95         printf "\n%s" "version $kernel_version" >> "$entries_dir/$kernel.conf"
96         printf "\n%s" "efi /$kernel" >> "$entries_dir/$kernel.conf"
97 done
98
99 # Update the default entry
100 newest_kernel="$(printf "%s" "$kernels" | head -n1)"
101 sed -i -e "s/[ \t]*default[ \t]\+.*/default $newest_kernel/" "$conf_file"
102
103 # Remove gummiboot entries for non-existing kernels
104 printf "%s\n" "$entries" | while IFS= read -r entry; do
105         kernel="$entry"
106         [ -f "$esp/$kernel" ] || rm "$entries_dir/$entry.conf"
107 done