Revert "Revert "oran-shell-release: release image for F""
[pti/rtp.git] / meta-starlingx / meta-stx-integ / recipes-devtools / grubby / files / 1001-Add-support-for-updating-grub-cfg-with-multiboot-2.patch
1 From b2fc58bcd1f18cbc3e0b3d303e9f2132d0e36cd8 Mon Sep 17 00:00:00 2001
2 From: Bin Qian <bin.qian@windriver.com>
3 Date: Tue, 13 Feb 2018 22:48:54 -0500
4 Subject: [PATCH 1/1] Add support for updating grub.cfg with multiboot 2
5
6 ---
7  Makefile           |   5 +++
8  __init__.py        |   8 ++++
9  grub-cfg-update    |  17 ++++++++
10  grub_cfg_update.py | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++
11  new-kernel-pkg     |  33 ++++++++++----
12  5 files changed, 181 insertions(+), 8 deletions(-)
13  create mode 100644 __init__.py
14  create mode 100644 grub-cfg-update
15  create mode 100644 grub_cfg_update.py
16
17 diff --git a/Makefile b/Makefile
18 index e021f35..93fa41b 100644
19 --- a/Makefile
20 +++ b/Makefile
21 @@ -56,6 +56,11 @@ install: all
22                 install -m 755 grubby $(DESTDIR)$(PREFIX)/sbin ; \
23                 install -m 644 grubby.8 $(DESTDIR)/$(mandir)/man8 ; \
24         fi
25 +       mkdir -p $(DESTDIR)/usr/lib64/python2.7/site-packages/grubby
26 +       install -m 644 grub_cfg_update.py $(DESTDIR)/usr/lib64/python2.7/site-packages/grubby/grub_cfg_update.py
27 +       install -m 644 __init__.py $(DESTDIR)/usr/lib64/python2.7/site-packages/grubby/__init__.py
28 +       install -m 500 grub-cfg-update $(DESTDIR)$(PREFIX)/sbin/grub-cfg-update
29 +
30  
31  grubby:: $(OBJECTS)
32         $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(grubby_LIBS)
33 diff --git a/__init__.py b/__init__.py
34 new file mode 100644
35 index 0000000..5f30af6
36 --- /dev/null
37 +++ b/__init__.py
38 @@ -0,0 +1,8 @@
39 +#!/usr/bin/env python
40 +#
41 +# Copyright (c) 2018 Wind River Systems, Inc.
42 +# SPDX-License-Identifier: Apache-2.0
43 +#
44 +#
45 +#
46 +#
47 \ No newline at end of file
48 diff --git a/grub-cfg-update b/grub-cfg-update
49 new file mode 100644
50 index 0000000..5e457e9
51 --- /dev/null
52 +++ b/grub-cfg-update
53 @@ -0,0 +1,17 @@
54 +#!/usr/bin/env python
55 +
56 +"""
57 +Copyright (c) 2018 Wind River Systems, Inc.
58 + SPDX-License-Identifier: Apache-2.0
59 +
60 +
61 +
62 +"""
63 +
64 +import sys
65 +
66 +from grubby.grub_cfg_update import main
67 +
68 +if __name__ == "__main__":
69 +    main()
70 +
71 diff --git a/grub_cfg_update.py b/grub_cfg_update.py
72 new file mode 100644
73 index 0000000..f5cd174
74 --- /dev/null
75 +++ b/grub_cfg_update.py
76 @@ -0,0 +1,126 @@
77 +#!/usr/bin/env python
78 +#
79 +# Copyright (c) 2018 Wind River Systems, Inc.
80 +# SPDX-License-Identifier: Apache-2.0
81 +#
82 +#
83 +#
84 +#
85 +import sys
86 +import argparse
87 +import os.path
88 +import re
89 +import ntpath
90 +
91 +
92 +LINUX_KERNEL_RE = "^[ \t]*module2[ \t]{1,}/vmlinuz-[^ \n\t]*"
93 +INITRD_RE = "^[ \t]*module2[ \t]{1,}/initramfs-[^ \n\t]*"
94 +
95 +
96 +def is_title(line):
97 +    m = re.search('^[ ]*menuentry ', line)
98 +    if m:
99 +        return True
100 +    return False
101 +
102 +
103 +def update_title(line, ver):
104 +    m = re.search("Linux [^ \n\t']*", line)
105 +    if not m:
106 +        print "Title pattern not understandable, not updated"
107 +        return line
108 +    new_line = re.sub("Linux [^ \n\t']*", "Linux %s" % ver, line)
109 +    return new_line
110 +
111 +
112 +def is_kernel(line):
113 +    m = re.search(LINUX_KERNEL_RE, line)
114 +    if m:
115 +        return True
116 +    return False
117 +
118 +
119 +def update_kernel(line, kernel):
120 +    kernel_name = ntpath.basename(kernel)
121 +    new_line = re.sub(LINUX_KERNEL_RE,
122 +                      "        module2 /%s" % kernel_name,
123 +                      line)
124 +    return new_line
125 +
126 +
127 +def is_initrd(line):
128 +    m = re.search(INITRD_RE, line)
129 +    if m:
130 +        return True
131 +    return False
132 +
133 +
134 +def update_initrd(line, initrd):
135 +    initrd_name = ntpath.basename(initrd)
136 +    new_line = re.sub(INITRD_RE,
137 +                      "        module2 /%s" % initrd_name,
138 +                      line)
139 +    return new_line
140 +
141 +
142 +def convert_line(line, version):
143 +    pattern = "^[ \t]*echo[ \t]*['\"]Loading Linux [^ \n\t]*"
144 +    m = re.search(pattern, line)
145 +    if not m:
146 +        return line
147 +
148 +    return "        echo     'Loading Linux %s ...'\n" % version
149 +
150 +
151 +def update_cfg(cfg, kernel, initramfs, ver, cfg_out):
152 +    if not os.path.isfile(cfg):
153 +        print "grub config file %s not found\n" % cfg
154 +        sys.exit(-1)
155 +
156 +    if not os.path.isfile(kernel):
157 +        print "specified kernel file %s not found\n" % kernel
158 +        sys.exit(-1)
159 +
160 +    if not os.path.isfile(initramfs):
161 +        print "specified initrd file %s not found\n" % initramfs
162 +        sys.exit(-1)
163 +
164 +    new_file_content = []
165 +    with open(cfg) as f:
166 +        for line in f:
167 +            if is_title(line):
168 +                new_line = update_title(line, ver)
169 +                print new_line
170 +            elif is_kernel(line):
171 +                new_line = update_kernel(line, kernel)
172 +                print new_line
173 +            elif is_initrd(line):
174 +                new_line = update_initrd(line, initramfs)
175 +                print new_line
176 +            else:
177 +                new_line = convert_line(line, ver)
178 +                print new_line
179 +
180 +            new_file_content.append(new_line)
181 +    with open(cfg_out, 'w') as f:
182 +        for line in new_file_content:
183 +            f.write("%s" % line)
184 +
185 +
186 +def main():
187 +    try:
188 +        parser = argparse.ArgumentParser(description='Update tboot enabled grub config')
189 +        parser.add_argument('cfg', help='original grub.cfg file path')
190 +        parser.add_argument('kernel', help='kernel file path')
191 +        parser.add_argument('initramfs', help='initramfs file path')
192 +        parser.add_argument('version', help='new version of kernel')
193 +        parser.add_argument('--cfg-out', help='updated grub.cfg target file path')
194 +        args = parser.parse_args()
195 +        cfg_out = args.cfg_out
196 +        if cfg_out is None:
197 +            cfg_out = args.cfg
198 +
199 +        update_cfg(args.cfg, args.kernel, args.initramfs, args.version, cfg_out)
200 +    except Exception as e:
201 +        print e
202 +        sys.exit(-1)
203 diff --git a/new-kernel-pkg b/new-kernel-pkg
204 index 977ef2d..1bb0a64 100755
205 --- a/new-kernel-pkg
206 +++ b/new-kernel-pkg
207 @@ -185,6 +185,11 @@ install() {
208         return
209      fi
210  
211 +    grep -q 'tboot=true' /proc/cmdline 2>/dev/null
212 +    if [ $? == 0 ] ; then
213 +        return
214 +    fi
215 +
216      INITRD=""
217      if [ -f $initrdfile ]; then
218         [ -n "$verbose" ] && echo "found $initrdfile and using it with grubby"
219 @@ -334,6 +339,11 @@ remove() {
220         return
221      fi
222  
223 +    grep -q 'tboot=true' /proc/cmdline 2>/dev/null
224 +    if [ $? == 0 ] ; then
225 +        return
226 +    fi
227 +
228      local files
229      local f
230      files="/etc/kernel/prerm.d/*[^~] /etc/kernel/prerm.d/$version/*[^~]"
231 @@ -483,14 +493,21 @@ update() {
232      fi
233  
234      if [ -n "$cfgGrub2Efi" ]; then
235 -       [ -n "$verbose" ] && echo "updating $version from $grub2EfiConfig"
236 -       ARGS="--grub2 -c $grub2EfiConfig --efi --update-kernel=$kernelImage \
237 -               $INITRD ${kernargs:+--args=\"$kernargs\"} \
238 -               ${removeargs:+--remove-args=\"$removeargs\"} \
239 -               --title=\"$title\$debugtitle\""
240 -
241 -       rungrubby ${ARGS}
242 -       rungrubby --debug ${ARGS}
243 +        grep -q 'tboot=true' /proc/cmdline 2>/dev/null
244 +        if [ $? == 0 ] ; then
245 +            [ -n "$verbose" ] && echo "calling grub-cfg-update $grub2EfiConfig $kernelImage $initrdfile $version"
246 +            grub-cfg-update $grub2EfiConfig $kernelImage $initrdfile $version
247 +            return
248 +        else
249 +            [ -n "$verbose" ] && echo "updating $version from $grub2EfiConfig"
250 +            ARGS="--grub2 -c $grub2EfiConfig --efi --update-kernel=$kernelImage \
251 +                $INITRD ${kernargs:+--args=\"$kernargs\"} \
252 +                ${removeargs:+--remove-args=\"$removeargs\"} \
253 +                --title=\"$title\$debugtitle\""
254 +
255 +            rungrubby ${ARGS}
256 +            rungrubby --debug ${ARGS}
257 +        fi
258      else
259         [ -n "$verbose" ] && echo "$grub2EfiConfig does not exist, not running grubby"
260      fi
261 -- 
262 1.8.3.1
263