File kgraft-rpm-helper of Package kgraft

#!/bin/bash

USAGE="$0 <check|install|remove> <package-version-release>"

if test "$1" = "-h" -o "$1" = "--help"; then
	echo "$USAGE"
	exit 0
fi
if test "$#" -lt 2; then
	echo "$USAGE" >&2
	exit 1
fi

shopt -s nullglob

do_check()
{
	if test -e /.buildenv; then
		echo "Skipping kGraft patches in buildroot"
		return 0
	fi
	if test "$(uname -r)" != "$KREL"; then
		return 0
	fi
	kgr check >&2
}

package_krel()
{
	local package=$1 n krel

	n=${package%-*}
	n=${n%-*}
	krel=${n#kgraft-patch-}
	krel=${krel//_/.}

	echo "$krel"
}

refresh_initrd()
{
	local image

	/sbin/depmod -F "/boot/System.map-$KREL" -e "$KREL" || return
	# copied from weak-modules2
	for image in vmlinuz image vmlinux linux bzImage uImage Image ""; do
		if test -f "/boot/$image-$KREL"; then
			break
		fi
	done
	if test -z "$image"; then
		return
	fi
	if test "$1" = "--force"; then
		/sbin/mkinitrd -k "/boot/$image-$KREL" -i "/boot/initrd-$KREL"
	else
		mkdir -p /var/run/regenerate-initrd
		touch "/var/run/regenerate-initrd/$image-$KREL"
	fi
}

do_install()
{
	local mod modules err

	if test -e /.buildenv; then
		return 0
	fi

	refresh_initrd
	if test "$(uname -r)" != "$KREL"; then
		return 0
	fi
	err=0
	modules=($(grep -l '^0$' /sys/module/kgraft[-_]patch*/refcnt /dev/null | sed 's:/refcnt::; s:/sys/module/::'))
	for mod in "${modules[@]}"; do
		echo "[kGraft] Unloading $mod"
		# Can't use modprobe -r, as the modules do not exist on disk
		# anymore
		rmmod "$mod" || :
	done
	modules=($(rpm -ql "$PACKAGE" | sed -rn 's:.*/(kgraft-patch[^/]*)\.ko$:\1:p'))
	for mod in "${modules[@]}"; do
		echo "[kGraft] Loading $mod"
		modprobe "$mod" || err=$?
	done
	return $err
}

do_remove()
{
	if test -e /.buildenv; then
		return 0
	fi

	if test "$NUM_PACKAGES" -eq 0; then
		# bnc#904867
		refresh_initrd --force
	else
		refresh_initrd
	fi
}

cmd=$1
PACKAGE=$2
KREL=$(package_krel "$PACKAGE")
NUM_PACKAGES=${3-0}
case "$cmd" in
check|install|remove)
	do_$cmd
	exit
	;;
*)
	echo "$USAGE" >&2
	exit 1
esac
openSUSE Build Service is sponsored by