3 # Copyright 2013 Intel Corporation
4 # Author: Artem Bityutskiy
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
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
22 # May be there are few more implicit assumption.
24 #This script requires a number of environment variables to be defined. Namely:
26 # 1. INSTALLERFW_MOUNT_PREFIX - where the target partitions are mounted (the
27 # "root" directory of the file-system we install gummiboot to)
29 PROG="setup-gummiboot-conf"
31 # This is a helper function which printfs an error message and exits
34 printf "%s\n" "$PROG: error: $1" 1>&2
38 # Make sure the installer framework variables are defined
39 [ "${INSTALLERFW_MOUNT_PREFIX:+x}" == "x" ] ||
40 fatal "installer framework environment variables not found"
42 # Get the ESP location
43 esp="$INSTALLERFW_MOUNT_PREFIX/boot"
45 # The gummiboot configuration file
46 conf_file="$esp/loader/loader.conf"
47 # The gummiboot kernel entries directory
48 entries_dir="$esp/loader/entries"
50 # Make sure the gummiboot configuration file exists
51 [ -f "$conf_file" ] || \
52 fatal "cannot find the gummiboot configuration file (\"$conf_file\")"
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\""
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\""
64 # Get the default entry name
65 default_entry="$(cat "$conf_file" | sed -n -e 's/[ \t]*default[ \t]\+\(.\+\)[ \t]*/\1/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\""
71 if ! [ -f "$entries_dir/$default_entry" ]; then
72 # The default entry does not exist anymore. Pick the entry
73 # corresponding to the newest kernel then.
74 default_entry="$(printf "%s" "$entries" | head -n1)"
77 # Use the default entry to prepare the pattern for other entries
78 pattern="$(cat "$entries_dir/$default_entry")"
79 # Drop few keywords which we won't need
80 pattern="$(printf "%s" "$pattern" | sed -e '/[ \t]*linux[ \t]\+/d')"
81 pattern="$(printf "%s" "$pattern" | sed -e '/[ \t]*efi[ \t]\+/d')"
82 pattern="$(printf "%s" "$pattern" | sed -e '/[ \t]*version[ \t]\+/d')"
84 # Create a gummiboot entry for every new kernel
85 printf "%s\n" "$kernels" | while IFS= read -r kernel; do
86 if [ -f "$entries_dir/$kernel.conf" ]; then
90 kernel_version="$(printf "%s" $kernel | sed -e 's/vmlinuz-\(.*\)/\1/')"
92 printf "%s" "$pattern" > "$entries_dir/$kernel.conf"
93 printf "\n%s" "version $kernel_version" >> "$entries_dir/$kernel.conf"
94 printf "\n%s" "efi /$kernel" >> "$entries_dir/$kernel.conf"
97 # Update the default entry
98 newest_kernel="$(printf "%s" "$kernels" | head -n1)"
99 sed -i -e "s/[ \t]*default[ \t]\+.*/default $newest_kernel.conf/" "$conf_file"
101 # Remove gummiboot entries for non-existing kernels
102 printf "%s\n" "$entries" | while IFS= read -r entry; do
104 [ -f "$esp/$kernel" ] || rm "$entries_dir/$entry.conf"