Debian Codegraph @

tests/ostree-grub-generator

raw
1 #!/bin/sh
2
3 # The builtin grub.cfg generator. This script is called by
4 # ostree/src/libostree/ostree-bootloader-grub2.c whenever boot loader
5 # configuration file needs to be updated on systems which do not use
6 # grub2-mkconfig (and thus, the `ostree admin instutil grub2-generate` path).
7 #
8 # It can be used as a template for a custom grub.cfg generator. What to consider
9 # when writing a custom grub.cfg generator:
10 #
11 # - The populate_menu() function converts boot loader entries as defined by
12 # https://www.freedesktop.org/wiki/Specifications/BootLoaderSpec/ into GRUB2
13 # menuentry sections. This is the core logic that is required by OSTree
14 # based system.
15 #
16 # - Embedded systems: Be aware that this script is executed not only on a host machine by OS
17 # installer, but also on a target device, thus think about shell portability. A target device
18 # for example might be using busybox with a limited shell.
19 #
20 # Feel free to edit this script to fit your requirements.
21
22 set -e
23
24 script=$(basename ${0})
25 # Atomically safe location where to generete grub.cfg when executing system upgrade.
26 new_grub2_cfg=${2}
27 entries_path=$(dirname $new_grub2_cfg)/entries
28
29 read_config()
30 {
31 config_file=${1}
32 title=""
33 initrd=""
34 options=""
35 linux=""
36 devicetree=""
37
38 while read -r line
39 do
40 record=$(echo ${line} | cut -f 1 -d ' ')
41 value=$(echo ${line} | cut -s -f2- -d ' ')
42 case "${record}" in
43 "title")
44 title=${value}
45 ;;
46 "initrd")
47 initrd=${value}
48 ;;
49 "linux")
50 linux=${value}
51 ;;
52 "devicetree")
53 devicetree=${value}
54 ;;
55 "options")
56 options=${value}
57 ;;
58 esac
59 done < ${config_file}
60
61 if [ -z "${title}" ]; then
62 title="(Untitled)"
63 fi
64 }
65
66 populate_menu()
67 {
68 # Default to /boot if OSTREE_BOOT_PARTITION is not set and /boot is on the same device as /ostree/repo
69 if [ -z ${OSTREE_BOOT_PARTITION+x} ] && [ -d /boot/ostree ] && [ -d /ostree/repo ] && [ $(stat -c '%d' /boot/ostree) -eq $(stat -c '%d' /ostree/repo) ]; then
70 boot_prefix="/boot"
71 else
72 boot_prefix="${OSTREE_BOOT_PARTITION}"
73 fi
74 for config in $(ls -v -r $entries_path/*.conf); do
75 read_config ${config}
76 menu="${menu}menuentry '${title}' {\n"
77 menu="${menu}\t linux ${boot_prefix}${linux} ${options}\n"
78 if [ -n "${initrd}" ] ; then
79 menu="${menu}\t initrd ${boot_prefix}${initrd}\n"
80 fi
81 if [ -n "${devicetree}" ] ; then
82 menu="${menu}\t devicetree ${boot_prefix}${devicetree}\n"
83 fi
84 menu="${menu}}\n\n"
85 done
86 # The printf command seems to be more reliable across shells for special character (\n, \t) evaluation
87 printf "$menu" >> ${new_grub2_cfg}
88 }
89
90 populate_warning()
91 {
92 cat >> ${new_grub2_cfg} <<EOF
93 # This file was generated by ${script}. Do not modify the generated file - all changes will
94 # be lost the next time file is regenerated. For more details refer to the ${script} script.
95 EOF
96 }
97
98 populate_header()
99 {
100 cat >> ${new_grub2_cfg} <<EOF
101 serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
102 default=boot
103 timeout=10
104
105 EOF
106 }
107
108 generate_grub2_cfg()
109 {
110 populate_warning
111 populate_header
112 populate_menu
113 }
114
115 generate_grub2_cfg