summaryrefslogtreecommitdiffstats
path: root/meta/classes/kernel-grub.bbclass
blob: 5d92f3b636f5f9d96a08aef27a6953cd6b137fb9 (plain)
1
2

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.Str
#
# While installing a rpm to update kernel on a deployed target, it will update
# the boot area and the boot menu with the kernel as the priority but allow
# you to fall back to the original kernel as well.
#
# - In kernel-image's preinstall scriptlet, it backs up original kernel to avoid
#   probable confliction with the new one.
#
# - In kernel-image's postinstall scriptlet, it modifies grub's config file to
#   updates the new kernel as the boot priority.
#

python __anonymous () {
    import re

    preinst = '''
	# Parsing confliction
	[ -f "$D/boot/grub/menu.list" ] && grubcfg="$D/boot/grub/menu.list"
	[ -f "$D/boot/grub/grub.cfg" ] && grubcfg="$D/boot/grub/grub.cfg"
	if [ -n "$grubcfg" ]; then
		# Dereference symlink to avoid confliction with new kernel name.
		if grep -q "/KERNEL_IMAGETYPE \+root=" $grubcfg; then
			if [ -L "$D/boot/KERNEL_IMAGETYPE" ]; then
				kimage=`realpath $D/boot/KERNEL_IMAGETYPE 2>/dev/null`
				if [ -f "$D$kimage" ]; then
					sed -i "s:KERNEL_IMAGETYPE \+root=:${kimage##*/} root=:" $grubcfg
				fi
			fi
		fi

		# Rename old kernel if it conflicts with new kernel name.
		if grep -q "/KERNEL_IMAGETYPE-${KERNEL_VERSION} \+root=" $grubcfg; then
			if [ -f "$D/boot/KERNEL_IMAGETYPE-${KERNEL_VERSION}" ]; then
				timestamp=`date +%s`
				kimage="$D/boot/KERNEL_IMAGETYPE-${KERNEL_VERSION}-$timestamp-back"
				sed -i "s:KERNEL_IMAGETYPE-${KERNEL_VERSION} \+root=:${kimage##*/} root=:" $grubcfg
				mv "$D/boot/KERNEL_IMAGETYPE-${KERNEL_VERSION}" "$kimage"
			fi
		fi
	fi
'''

    postinst = '''
	get_new_grub_cfg() {
		grubcfg="$1"
		old_image="$2"
		title="Update KERNEL_IMAGETYPE-${KERNEL_VERSION}-${PV}"
		if [ "${grubcfg##*/}" = "grub.cfg" ]; then
			rootfs=`grep " *linux \+[^ ]\+ \+root=" $grubcfg -m 1 | \
				 sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"`

			echo "menuentry \"$title\" {"
			echo "    set root=(hd0,1)"
			echo "$rootfs"
			echo "}"
		elif [ "${grubcfg##*/}" = "menu.list" ]; then
			rootfs=`grep "kernel \+[^ ]\+ \+root=" $grubcfg -m 1 | \
				 sed "s#${old_image}#${old_image%/*}/KERNEL_IMAGETYPE-${KERNEL_VERSION}#"`

			echo "default 0"
			echo "timeout 30"
			echo "title $title"
			echo "root  (hd0,0)"
			echo "$rootfs"
		fi
	}

	get_old_grub_cfg() {
		grubcfg="$1"
		if [ "${grubcfg##*/}" = "grub.cfg" ]; then
			cat "$grubcfg"
		elif [ "${grubcfg##*/}" = "menu.list" ]; then
			sed -e '/^default/d' -e '/^timeout/d' "$grubcfg"
		fi
	}

	if [ -f "$D/boot/grub/grub.cfg" ]; then
		grubcfg="$D/boot/grub/grub.cfg"
		old_image=`grep ' *linux \+[^ ]\+ \+root=' -m 1 "$grubcfg" | awk '{print $2}'`
	elif [ -f "$D/boot/grub/menu.list" ]; then
		grubcfg="$D/boot/grub/menu.list"
		old_image=`grep '^kernel \+[^ ]\+ \+root=' -m 1 "$grubcfg" | awk '{print $2}'`
	fi

	# Don't update grubcfg at first install while old bzImage doesn't exist.
	if [ -f "$D/boot/${old_image##*/}" ]; then
		grubcfgtmp="$grubcfg.tmp"
		get_new_grub_cfg "$grubcfg" "$old_image"  > $grubcfgtmp
		get_old_grub_cfg "$grubcfg" >> $grubcfgtmp
		mv $grubcfgtmp $grubcfg
		echo "Caution! Update kernel may affect kernel-module!"
	fi
'''

    imagetypes = d.getVar('KERNEL_IMAGETYPES')
    imagetypes = re.sub(r'\.gz$', '', imagetypes)

    for type in imagetypes.split():
        typelower = type.lower()
        preinst_append = preinst.replace('KERNEL_IMAGETYPE', type)
        postinst_prepend = postinst.replace('KERNEL_IMAGETYPE', type)
        d.setVar('pkg_preinst_kernel-image-' + typelower + '_append', preinst_append)
        d.setVar('pkg_postinst_kernel-image-' + typelower + '_prepend', postinst_prepend)
}