1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7#
8# While installing a rpm to update kernel on a deployed target, it will update
9# the boot area and the boot menu with the kernel as the priority but allow
10# you to fall back to the original kernel as well.
11#
12# - In kernel-image's preinstall scriptlet, it backs up original kernel to avoid
13#   probable confliction with the new one.
14#
15# - In kernel-image's postinstall scriptlet, it modifies grub's config file to
16#   updates the new kernel as the boot priority.
17#
18
19python __anonymous () {
20    import re
21
22    preinst = '''
23	# Parsing confliction
24	[ -f "$D/boot/grub/menu.list" ] && grubcfg="$D/boot/grub/menu.list"
25	[ -f "$D/boot/grub/grub.cfg" ] && grubcfg="$D/boot/grub/grub.cfg"
26	if [ -n "$grubcfg" ]; then
27		# Dereference symlink to avoid confliction with new kernel name.
28		if grep -q "/KERNEL_IMAGETYPE \+root=" $grubcfg; then
29			if [ -L "$D/boot/KERNEL_IMAGETYPE" ]; then
30				kimage=`realpath $D/boot/KERNEL_IMAGETYPE 2>/dev/null`
31				if [ -f "$D$kimage" ]; then
32					sed -i "s:KERNEL_IMAGETYPE \+root=:${kimage##*/} root=:" $grubcfg
33				fi
34			fi
35		fi
36
37		# Rename old kernel if it conflicts with new kernel name.
38		if grep -q "/KERNEL_IMAGETYPE-${KERNEL_VERSION} \+root=" $grubcfg; then
39			if [ -f "$D/boot/KERNEL_IMAGETYPE-${KERNEL_VERSION}" ]; then
40				timestamp=`date +%s`
41				kimage="$D/boot/KERNEL_IMAGETYPE-${KERNEL_VERSION}-$timestamp-back"
42				sed -i "s:KERNEL_IMAGETYPE-${KERNEL_VERSION} \+root=:${kimage##*/} root=:" $grubcfg
43				mv "$D/boot/KERNEL_IMAGETYPE-${KERNEL_VERSION}" "$kimage"
44			fi
45		fi
46	fi
47'''
48
49    postinst = '''
50	get_new_grub_cfg() {
51		grubcfg="$1"
52		old_image="$2"
53		title="Update KERNEL_IMAGETYPE-${KERNEL_VERSION}-${PV}"
54		if [ "${grubcfg##*/}" = "grub.cfg" ]; then
55			rootfs=`grep " *linux \+[^ ]\+ \+root=" $grubcfg -m 1 | \
56				 sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"`
57
58			echo "menuentry \"$title\" {"
59			echo "    set root=(hd0,1)"
60			echo "$rootfs"
61			echo "}"
62		elif [ "${grubcfg##*/}" = "menu.list" ]; then
63			rootfs=`grep "kernel \+[^ ]\+ \+root=" $grubcfg -m 1 | \
64				 sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"`
65
66			echo "default 0"
67			echo "timeout 30"
68			echo "title $title"
69			echo "root  (hd0,0)"
70			echo "$rootfs"
71		fi
72	}
73
74	get_old_grub_cfg() {
75		grubcfg="$1"
76		if [ "${grubcfg##*/}" = "grub.cfg" ]; then
77			cat "$grubcfg"
78		elif [ "${grubcfg##*/}" = "menu.list" ]; then
79			sed -e '/^default/d' -e '/^timeout/d' "$grubcfg"
80		fi
81	}
82
83	if [ -f "$D/boot/grub/grub.cfg" ]; then
84		grubcfg="$D/boot/grub/grub.cfg"
85		old_image=`grep ' *linux \+[^ ]\+ \+root=' -m 1 "$grubcfg" | awk '{print $2}'`
86	elif [ -f "$D/boot/grub/menu.list" ]; then
87		grubcfg="$D/boot/grub/menu.list"
88		old_image=`grep '^kernel \+[^ ]\+ \+root=' -m 1 "$grubcfg" | awk '{print $2}'`
89	fi
90
91	# Don't update grubcfg at first install while old bzImage doesn't exist.
92	if [ -f "$D/boot/${old_image##*/}" ]; then
93		grubcfgtmp="$grubcfg.tmp"
94		get_new_grub_cfg "$grubcfg" "$old_image"  > $grubcfgtmp
95		get_old_grub_cfg "$grubcfg" >> $grubcfgtmp
96		mv $grubcfgtmp $grubcfg
97		echo "Caution! Update kernel may affect kernel-module!"
98	fi
99'''
100
101    imagetypes = d.getVar('KERNEL_IMAGETYPES')
102    imagetypes = re.sub(r'\.gz$', '', imagetypes)
103
104    for type in imagetypes.split():
105        typelower = type.lower()
106        preinst_append = preinst.replace('KERNEL_IMAGETYPE', type)
107        postinst_prepend = postinst.replace('KERNEL_IMAGETYPE', type)
108        d.setVar('pkg_preinst:kernel-image-' + typelower + ':append', preinst_append)
109        d.setVar('pkg_postinst:kernel-image-' + typelower + ':prepend', postinst_prepend)
110}
111
112