summaryrefslogtreecommitdiffstats
path: root/qt-ostree/ostree-grub-generator
blob: e2dd00bd21465182cbc4b245bfc7e0c87fbabe2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/sh
#############################################################################
##
## Copyright (C) 2016 The Qt Company Ltd.
## Contact: https://www.qt.io/licensing/
##
## This file is part of the Qt OTA Update module of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:GPL$
## Commercial License Usage
## Licensees holding valid commercial Qt licenses may use this file in
## accordance with the commercial license agreement provided with the
## Software or, alternatively, in accordance with the terms contained in
## a written agreement between you and The Qt Company. For licensing terms
## and conditions see https://www.qt.io/terms-conditions. For further
## information use the contact form at https://www.qt.io/contact-us.
##
## GNU General Public License Usage
## Alternatively, this file may be used under the terms of the GNU
## General Public License version 3 or (at your option) any later version
## approved by the KDE Free Qt Foundation. The licenses are as published by
## the Free Software Foundation and appearing in the file LICENSE.GPL3
## included in the packaging of this file. Please review the following
## information to ensure the GNU General Public License requirements will
## be met: https://www.gnu.org/licenses/gpl-3.0.html.
##
## $QT_END_LICENSE$
##
#############################################################################

# This script is called by ostree/src/libostree/ostree-bootloader-grub2.c whenever
# boot loader configuration file needs to be updated. It can be used as a template
# for a custom grub.cfg generator. When writing a custom grub.cfg generator:
#
# 1) cp ostree-grub-generator ostree-grub-generator-mydevice
# 2) Edit ostree-grub-generator-mydevice
# 3) Pass ostree-grub-generator-mydevice to qt-ostree via --grub2-cfg-generator
# 4) Examine the generated grub.cfg file in the OTA sysroot (created by --create-ota-sysroot)
#
# WARNINGS:
#
#   - Do not modify populate_menu() function, unless you know what you are doing. This function
#   converts boot loader entries (https://www.freedesktop.org/wiki/Specifications/BootLoaderSpec/)
#   into GRUB2 menuentry sections.
#
#   - Be aware that this script is executed not only on a host machine, but also on a
#   target device, thus think about shell portability. Target device for example might
#   be using busybox with a limited shell.

set -e

script=$(basename ${0})
# Atomically safe location where to generete grub.cfg when executing OTA update.
new_grub2_cfg=${2}
entries_path=$(dirname $new_grub2_cfg)/entries

read_config()
{
    config_file=${1}
    title=""
    initrd=""
    options=""
    linux=""

    while read -r line
    do
        record=$(echo ${line} | cut -f 1 -d ' ')
        value=$(echo ${line} | cut -s -f2- -d ' ')
        case "${record}" in
            "title")
                title=${value}
                ;;
            "initrd")
                initrd=${value}
                ;;
            "linux")
                linux=${value}
                ;;
            "options")
                options=${value}
                ;;
        esac
    done < ${config_file}

    if [ -z "${title}" ]; then
        title="(Untitled)"
    fi
}

populate_menu()
{
    # We need to prepend "boot/" prefix when boot/ directory is on the same
    # partition as usr/ - this is the layout we have on Intel NUC boards, the
    # only board with GRUB boot loader that we support at the moment. When there
    # will be more boards, determine boot_prefix at runtime.
    boot_prefix="/boot"

    for config in $(ls $entries_path/*.conf); do
        read_config ${config}
        menu="${menu}menuentry '${title}' {\n"
        menu="${menu}\t linux ${boot_prefix}/${linux} ${options}\n"
        if [ -n "${initrd}" ] ; then
            menu="${menu}\t initrd ${boot_prefix}/${initrd}\n"
        fi
        menu="${menu}}\n\n"
    done
    # printf seems to be more reliable across shells for special character (\n, \t) evaluation
    printf "$menu" >> ${new_grub2_cfg}
}

populate_warning()
{
cat >> ${new_grub2_cfg} <<EOF
# This file was generated by ${script}. Do not modify the generated file - all
# changes will be lost when this file will be regenerated by OSTree. For more
# details refer to ${script} script.
EOF
}

populate_header()
{
cat >> ${new_grub2_cfg} <<EOF
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
default=boot
timeout=10

EOF
}

generate_grub2_cfg()
{
    populate_warning
    populate_header
    populate_menu
}

generate_grub2_cfg