Blame tests/ostree-grub-generator

rpm-build 0fba15
#!/bin/sh
rpm-build 0fba15
rpm-build 0fba15
# The builtin grub.cfg generator. This script is called by
rpm-build 0fba15
# ostree/src/libostree/ostree-bootloader-grub2.c whenever boot loader
rpm-build 0fba15
# configuration file needs to be updated on systems which do not use
rpm-build 0fba15
# grub2-mkconfig (and thus, the `ostree admin instutil grub2-generate` path).
rpm-build 0fba15
#
rpm-build 0fba15
# It can be used as a template for a custom grub.cfg generator. What to consider
rpm-build 0fba15
# when writing a custom grub.cfg generator:
rpm-build 0fba15
#
rpm-build 0fba15
#   - The populate_menu() function converts boot loader entries as defined by
rpm-build 0fba15
#   https://www.freedesktop.org/wiki/Specifications/BootLoaderSpec/ into GRUB2
rpm-build 0fba15
#   menuentry sections. This is the core logic that is required by OSTree
rpm-build 0fba15
#   based system.
rpm-build 0fba15
#
rpm-build 0fba15
#   - Embedded systems: Be aware that this script is executed not only on a host machine by OS
rpm-build 0fba15
#   installer, but also on a target device, thus think about shell portability. A target device
rpm-build 0fba15
#   for example might be using busybox with a limited shell.
rpm-build 0fba15
#
rpm-build 0fba15
# Feel free to edit this script to fit your requirements.
rpm-build 0fba15
rpm-build 0fba15
set -e
rpm-build 0fba15
rpm-build 0fba15
script=$(basename ${0})
rpm-build 0fba15
# Atomically safe location where to generete grub.cfg when executing system upgrade.
rpm-build 0fba15
new_grub2_cfg=${2}
rpm-build 0fba15
entries_path=$(dirname $new_grub2_cfg)/entries
rpm-build 0fba15
rpm-build 0fba15
read_config()
rpm-build 0fba15
{
rpm-build 0fba15
    config_file=${1}
rpm-build 0fba15
    title=""
rpm-build 0fba15
    initrd=""
rpm-build 0fba15
    options=""
rpm-build 0fba15
    linux=""
rpm-build 0fba15
    devicetree=""
rpm-build 0fba15
rpm-build 0fba15
    while read -r line
rpm-build 0fba15
    do
rpm-build 0fba15
        record=$(echo ${line} | cut -f 1 -d ' ')
rpm-build 0fba15
        value=$(echo ${line} | cut -s -f2- -d ' ')
rpm-build 0fba15
        case "${record}" in
rpm-build 0fba15
            "title")
rpm-build 0fba15
                title=${value}
rpm-build 0fba15
                ;;
rpm-build 0fba15
            "initrd")
rpm-build 0fba15
                initrd=${value}
rpm-build 0fba15
                ;;
rpm-build 0fba15
            "linux")
rpm-build 0fba15
                linux=${value}
rpm-build 0fba15
                ;;
rpm-build 0fba15
            "devicetree")
rpm-build 0fba15
                devicetree=${value}
rpm-build 0fba15
                ;;
rpm-build 0fba15
            "options")
rpm-build 0fba15
                options=${value}
rpm-build 0fba15
                ;;
rpm-build 0fba15
        esac
rpm-build 0fba15
    done < ${config_file}
rpm-build 0fba15
rpm-build 0fba15
    if [ -z "${title}" ]; then
rpm-build 0fba15
        title="(Untitled)"
rpm-build 0fba15
    fi
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
populate_menu()
rpm-build 0fba15
{
rpm-build 0fba15
    # Default to /boot if OSTREE_BOOT_PARTITION is not set and /boot is on the same device as /ostree/repo
rpm-build 0fba15
    if [ -z ${OSTREE_BOOT_PARTITION+x} ] && [ -d /boot/ostree ] && [ -d /ostree/repo ] && [ $(stat -c '%d' /boot/ostree) -eq $(stat -c '%d' /ostree/repo) ]; then
rpm-build 0fba15
        boot_prefix="/boot"
rpm-build 0fba15
    else
rpm-build 0fba15
        boot_prefix="${OSTREE_BOOT_PARTITION}"
rpm-build 0fba15
    fi
rpm-build 0fba15
    for config in $(ls -v -r $entries_path/*.conf); do
rpm-build 0fba15
        read_config ${config}
rpm-build 0fba15
        menu="${menu}menuentry '${title}' {\n"
rpm-build 0fba15
        menu="${menu}\t linux ${boot_prefix}${linux} ${options}\n"
rpm-build 0fba15
        if [ -n "${initrd}" ] ; then
rpm-build 0fba15
            menu="${menu}\t initrd ${boot_prefix}${initrd}\n"
rpm-build 0fba15
        fi
rpm-build 0fba15
        if [ -n "${devicetree}" ] ; then
rpm-build 0fba15
            menu="${menu}\t devicetree ${boot_prefix}${devicetree}\n"
rpm-build 0fba15
        fi
rpm-build 0fba15
        menu="${menu}}\n\n"
rpm-build 0fba15
    done
rpm-build 0fba15
    # The printf command seems to be more reliable across shells for special character (\n, \t) evaluation
rpm-build 0fba15
    printf "$menu" >> ${new_grub2_cfg}
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
populate_warning()
rpm-build 0fba15
{
rpm-build 0fba15
cat >> ${new_grub2_cfg} <
rpm-build 0fba15
# This file was generated by ${script}. Do not modify the generated file - all changes will
rpm-build 0fba15
# be lost the next time file is regenerated. For more details refer to the ${script} script.
rpm-build 0fba15
EOF
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
populate_header()
rpm-build 0fba15
{
rpm-build 0fba15
cat >> ${new_grub2_cfg} <
rpm-build 0fba15
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
rpm-build 0fba15
default=boot
rpm-build 0fba15
timeout=10
rpm-build 0fba15
rpm-build 0fba15
EOF
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
generate_grub2_cfg()
rpm-build 0fba15
{
rpm-build 0fba15
    populate_warning
rpm-build 0fba15
    populate_header
rpm-build 0fba15
    populate_menu
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
generate_grub2_cfg