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
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
17 diff --git a/Makefile b/Makefile
18 index e021f35..93fa41b 100644
21 @@ -56,6 +56,11 @@ install: all
22 install -m 755 grubby $(DESTDIR)$(PREFIX)/sbin ; \
23 install -m 644 grubby.8 $(DESTDIR)/$(mandir)/man8 ; \
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
32 $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(grubby_LIBS)
33 diff --git a/__init__.py b/__init__.py
35 index 0000000..5f30af6
39 +#!/usr/bin/env python
41 +# Copyright (c) 2018 Wind River Systems, Inc.
42 +# SPDX-License-Identifier: Apache-2.0
47 \ No newline at end of file
48 diff --git a/grub-cfg-update b/grub-cfg-update
50 index 0000000..5e457e9
54 +#!/usr/bin/env python
57 +Copyright (c) 2018 Wind River Systems, Inc.
58 + SPDX-License-Identifier: Apache-2.0
66 +from grubby.grub_cfg_update import main
68 +if __name__ == "__main__":
71 diff --git a/grub_cfg_update.py b/grub_cfg_update.py
73 index 0000000..f5cd174
75 +++ b/grub_cfg_update.py
77 +#!/usr/bin/env python
79 +# Copyright (c) 2018 Wind River Systems, Inc.
80 +# SPDX-License-Identifier: Apache-2.0
92 +LINUX_KERNEL_RE = "^[ \t]*module2[ \t]{1,}/vmlinuz-[^ \n\t]*"
93 +INITRD_RE = "^[ \t]*module2[ \t]{1,}/initramfs-[^ \n\t]*"
97 + m = re.search('^[ ]*menuentry ', line)
103 +def update_title(line, ver):
104 + m = re.search("Linux [^ \n\t']*", line)
106 + print "Title pattern not understandable, not updated"
108 + new_line = re.sub("Linux [^ \n\t']*", "Linux %s" % ver, line)
112 +def is_kernel(line):
113 + m = re.search(LINUX_KERNEL_RE, line)
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,
127 +def is_initrd(line):
128 + m = re.search(INITRD_RE, line)
134 +def update_initrd(line, initrd):
135 + initrd_name = ntpath.basename(initrd)
136 + new_line = re.sub(INITRD_RE,
137 + " module2 /%s" % initrd_name,
142 +def convert_line(line, version):
143 + pattern = "^[ \t]*echo[ \t]*['\"]Loading Linux [^ \n\t]*"
144 + m = re.search(pattern, line)
148 + return " echo 'Loading Linux %s ...'\n" % version
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
156 + if not os.path.isfile(kernel):
157 + print "specified kernel file %s not found\n" % kernel
160 + if not os.path.isfile(initramfs):
161 + print "specified initrd file %s not found\n" % initramfs
164 + new_file_content = []
165 + with open(cfg) as f:
168 + new_line = update_title(line, ver)
170 + elif is_kernel(line):
171 + new_line = update_kernel(line, kernel)
173 + elif is_initrd(line):
174 + new_line = update_initrd(line, initramfs)
177 + new_line = convert_line(line, ver)
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)
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:
199 + update_cfg(args.cfg, args.kernel, args.initramfs, args.version, cfg_out)
200 + except Exception as e:
203 diff --git a/new-kernel-pkg b/new-kernel-pkg
204 index 977ef2d..1bb0a64 100755
207 @@ -185,6 +185,11 @@ install() {
211 + grep -q 'tboot=true' /proc/cmdline 2>/dev/null
212 + if [ $? == 0 ] ; then
217 if [ -f $initrdfile ]; then
218 [ -n "$verbose" ] && echo "found $initrdfile and using it with grubby"
219 @@ -334,6 +339,11 @@ remove() {
223 + grep -q 'tboot=true' /proc/cmdline 2>/dev/null
224 + if [ $? == 0 ] ; then
230 files="/etc/kernel/prerm.d/*[^~] /etc/kernel/prerm.d/$version/*[^~]"
231 @@ -483,14 +493,21 @@ update() {
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\""
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
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\""
256 + rungrubby --debug ${ARGS}
259 [ -n "$verbose" ] && echo "$grub2EfiConfig does not exist, not running grubby"