Blame SOURCES/weak-modules

f1da6a
#!/bin/bash
f1da6a
#
f1da6a
# weak-modules - determine which modules are kABI compatible with installed
f1da6a
#                kernels and set up the symlinks in /lib/*/weak-updates.
f1da6a
#
961cf5
# This is an updated version of the script which doesn't support
961cf5
# multiple installation of the same out-of-tree module (stored in the
961cf5
# 'extra' subdirectory) for multiple kernels. This assumption is
961cf5
# supposed to be verified at the rpm level of the packages delivering
961cf5
# these modules.  There are some checks for this assumption, however we
961cf5
# really don't solve this situation. This limitation allows for a much
961cf5
# simpler version of the script. Previous version tried to work in this
961cf5
# case but was incorrect in some cases.
f1da6a
f1da6a
unset LANG LC_ALL LC_COLLATE
f1da6a
f1da6a
tmpdir=$(mktemp -td ${0##*/}.XXXXXX)
f1da6a
trap "rm -rf $tmpdir" EXIT
f1da6a
unset ${!changed_modules_*} ${!changed_initramfs_*}
f1da6a
961cf5
unset BASEDIR
961cf5
unset CHECK_INITRAMFS
961cf5
default_initramfs_prefix="/boot" # will be combined with BASEDIR
f1da6a
dracut="/sbin/dracut"
961cf5
declare -a modules
961cf5
declare -A module_krels
961cf5
declare -A weak_modules_before
f1da6a
f1da6a
# doit:
f1da6a
# A wrapper used whenever we're going to perform a real operation.
f1da6a
doit() {
f1da6a
    [ -n "$verbose" ] && echo "$@"
f1da6a
    [ -n "$dry_run" ] || "$@"
f1da6a
}
f1da6a
961cf5
# pr_verbose:
961cf5
# print verbose -- wrapper used to print extra messages if required
961cf5
pr_verbose() {
961cf5
    [ -n "$verbose" ] && echo "$@"
961cf5
}
961cf5
961cf5
# pr_warning:
961cf5
# print warning
961cf5
pr_warning() {
961cf5
    echo "WARNING: $*"
961cf5
}
961cf5
f1da6a
# rpmsort: The sort in coreutils can't sort the RPM list how we want it so we
f1da6a
# instead transform the list into a form it will sort correctly, then sort.
f1da6a
rpmsort() {
961cf5
    local IFS=$' '
961cf5
    REVERSE=""
961cf5
    rpmlist=($(cat))
961cf5
961cf5
    if [ "-r" == "$1" ];
961cf5
    then
961cf5
        REVERSE="-r"
961cf5
    fi
961cf5
961cf5
    echo ${rpmlist[@]} | \
961cf5
        sed -e 's/-/../g' | \
961cf5
        sort ${REVERSE} -n -t"." -k1,1 -k2,2 -k3,3 -k4,4 -k5,5 -k6,6 -k7,7 \
961cf5
             -k8,8 -k9,9 -k10,10 | \
961cf5
        sed -e 's/\.\./-/g'
961cf5
}
961cf5
961cf5
# krel_of_module:
961cf5
# Compute the kernel release of a module.
961cf5
krel_of_module() {
961cf5
    local module="$1"
f1da6a
961cf5
    if [ x"${module_krels[$module]+set}" = x"set" ]; then
961cf5
        # version cached in the array already
961cf5
        echo "${module_krels[$module]}"
961cf5
    elif [ -f "$module" ]; then
961cf5
        krel_of_module_modinfo "$module"
961cf5
    else
961cf5
        # Try to extract the kernel release from the path
961cf5
        # delete case, the .ko already deleted
961cf5
        set -- "${module#*/lib/modules/}"
961cf5
        echo "${1%%/*}"
961cf5
    fi
961cf5
}
f1da6a
961cf5
# krel_of_module_modinfo:
961cf5
# Fetches module version from internal module info
961cf5
krel_of_module_modinfo() {
961cf5
    local module="$1"
961cf5
    /sbin/modinfo -F vermagic "$module" | awk '{print $1}'
f1da6a
}
f1da6a
f1da6a
# read_modules_list:
f1da6a
# Read in a list of modules from standard input. Convert the filenames into
f1da6a
# absolute paths and compute the kernel release for each module (either using
f1da6a
# the modinfo section or through the absolute path.
961cf5
# If used with input redirect, should be used as read_module_list < input,
961cf5
# not input | read_modules_list, the latter spawns a subshell
961cf5
# and the arrays are not seen in the caller
f1da6a
read_modules_list() {
f1da6a
    local IFS=$'\n'
f1da6a
    modules=($(cat))
f1da6a
f1da6a
    for ((n = 0; n < ${#modules[@]}; n++)); do
f1da6a
        if [ ${modules[n]:0:1} != '/' ]; then
f1da6a
            modules[n]="$PWD/${modules[n]}"
f1da6a
        fi
961cf5
        module_krels["${modules[n]}"]=$(krel_of_module ${modules[n]})
f1da6a
    done
f1da6a
}
f1da6a
6f41c8
decompress_initramfs() {
6f41c8
    local input=$1
6f41c8
    local output=$2
6f41c8
6f41c8
    # First, check if this is compressed at all
6f41c8
    if cpio -i -t < "$input" > /dev/null 2>/dev/null; then
6f41c8
        # If this archive contains a file early_cpio, it's a trick. Strip off
6f41c8
        # the early cpio archive and try again.
6f41c8
        if cpio -i -t < "$input" 2>/dev/null | grep -q '^early_cpio$' ; then
dae7ef
            /usr/lib/dracut/skipcpio "$input" > "${tmpdir}/post_early_cpio.img"
6f41c8
            decompress_initramfs "${tmpdir}/post_early_cpio.img" "$output"
6f41c8
            retval="$?"
6f41c8
            rm -f "${tmpdir}/post_early_cpio.img"
6f41c8
            return $retval
6f41c8
        fi
6f41c8
6f41c8
        cp "$input" "$output"
6f41c8
        return 0
6f41c8
    fi
6f41c8
6f41c8
    # Try gzip
6f41c8
    if gzip -cd < "$input" > "$output" 2>/dev/null ; then
6f41c8
        return 0
6f41c8
    fi
6f41c8
6f41c8
    # Next try xz
6f41c8
    if xz -cd < "$input" > "$output" 2>/dev/null ; then
6f41c8
        return 0
6f41c8
    fi
6f41c8
6f41c8
    echo "Unable to decompress $input: Unknown format" >&2
6f41c8
    return 1
6f41c8
}
6f41c8
dae7ef
# List all module files and modprobe configuration that could require a new
dae7ef
# initramfs. The current directory must be the root of the uncompressed
dae7ef
# initramfs. The unsorted list of files is output to stdout.
dae7ef
list_module_files() {
dae7ef
    find . -iname \*.ko 2>/dev/null
dae7ef
    find etc/modprobe.d usr/lib/modprobe.d -name \*.conf 2>/dev/null
dae7ef
}
dae7ef
f1da6a
# read_old_initramfs:
f1da6a
compare_initramfs_modules() {
f1da6a
    local old_initramfs=$1
f1da6a
    local new_initramfs=$2
f1da6a
f1da6a
    rm -rf "$tmpdir/old_initramfs"
f1da6a
    rm -rf "$tmpdir/new_initramfs"
f1da6a
    mkdir "$tmpdir/old_initramfs"
f1da6a
    mkdir "$tmpdir/new_initramfs"
f1da6a
6f41c8
    decompress_initramfs "$old_initramfs" "$tmpdir/old_initramfs.img"
f1da6a
    pushd "$tmpdir/old_initramfs" >/dev/null
6f41c8
    cpio -i < "$tmpdir/old_initramfs.img" 2>/dev/null
6f41c8
    rm "$tmpdir/old_initramfs.img"
dae7ef
    n=0; for i in `list_module_files|sort`; do
f1da6a
        old_initramfs_modules[n]="$i"
f1da6a
        n=$((n+1))
f1da6a
    done
f1da6a
    popd >/dev/null
f1da6a
6f41c8
    decompress_initramfs "$new_initramfs" "$tmpdir/new_initramfs.img"
f1da6a
    pushd "$tmpdir/new_initramfs" >/dev/null
6f41c8
    cpio -i < "$tmpdir/new_initramfs.img" 2>/dev/null
6f41c8
    rm "$tmpdir/new_initramfs.img"
dae7ef
    n=0; for i in `list_module_files|sort`; do
f1da6a
        new_initramfs_modules[n]="$i"
f1da6a
        n=$((n+1))
f1da6a
    done
f1da6a
    popd >/dev/null
f1da6a
dae7ef
    # Compare the length and contents of the arrays
dae7ef
    if [ "${#old_initramfs_modules[@]}" == "${#new_initramfs_modules[@]}" -a \
dae7ef
         "${old_initramfs_modules[*]}" == "${new_initramfs_modules[*]}" ];
f1da6a
    then
dae7ef
        # If the file lists are the same, compare each file to find any that changed
f1da6a
        for ((n = 0; n < ${#old_initramfs_modules[@]}; n++)); do
dae7ef
            if ! cmp "$tmpdir/old_initramfs/${old_initramfs_modules[n]}" \
961cf5
                     "$tmpdir/new_initramfs/${new_initramfs_modules[n]}" \
961cf5
                     >/dev/null 2>&1
961cf5
            then
f1da6a
                return 1
f1da6a
            fi
f1da6a
        done
f1da6a
    else
f1da6a
        return 1
f1da6a
    fi
f1da6a
f1da6a
    return 0
f1da6a
}
f1da6a
f1da6a
# check_initramfs:
f1da6a
# check and possibly also update the initramfs for changed kernels
f1da6a
check_initramfs() {
f1da6a
    local kernel=$1
f1da6a
f1da6a
    # If there is no initramfs already we will not make one here.
f1da6a
    if [ -e "$initramfs_prefix/initramfs-$kernel.img" ];
f1da6a
    then
f1da6a
        old_initramfs="$initramfs_prefix/initramfs-$kernel.img"
f1da6a
        tmp_initramfs="$initramfs_prefix/initramfs-$kernel.tmp"
f1da6a
        new_initramfs="$initramfs_prefix/initramfs-$kernel.img"
f1da6a
f1da6a
        $dracut -f "$tmp_initramfs" "$kernel"
f1da6a
961cf5
        if ! compare_initramfs_modules "$old_initramfs" "$tmp_initramfs";
f1da6a
        then
f1da6a
            doit mv "$tmp_initramfs" "$new_initramfs"
f1da6a
        else
f1da6a
            rm -f "$tmp_initramfs"
f1da6a
        fi
f1da6a
    fi
f1da6a
}
f1da6a
f1da6a
usage() {
f1da6a
    echo "Usage: ${0##*/} [options] {--add-modules|--remove-modules}"
f1da6a
    echo "${0##*/} [options] {--add-kernel|--remove-kernel} {kernel-release}"
f1da6a
    cat <<'EOF'
f1da6a
--add-modules
f1da6a
        Add a list of modules read from standard input. Create
f1da6a
        symlinks in compatible kernel's weak-updates/ directory.
f1da6a
        The list of modules is read from standard input.
f1da6a
f1da6a
--remove-modules
f1da6a
        Remove compatibility symlinks from weak-updates/ directories
f1da6a
        for a list of modules.  The list of modules is read from
961cf5
        standard input. Note: it doesn't attempt to locate any
f1da6a
        compatible modules to replace those being removed.
f1da6a
f1da6a
--add-kernel
f1da6a
        Add compatibility symlinks for all compatible modules to the
f1da6a
        specified or running kernel.
f1da6a
f1da6a
--remove-kernel
f1da6a
        Remove all compatibility symlinks for the specified or current
f1da6a
        kernel.
f1da6a
f1da6a
--no-initramfs
961cf5
        Do not generate an initramfs.
f1da6a
f1da6a
--verbose
f1da6a
        Print the commands executed.
f1da6a
f1da6a
--dry-run
f1da6a
        Do not create/remove any files.
f1da6a
EOF
f1da6a
    exit $1
f1da6a
}
f1da6a
f1da6a
# module_has_changed:
f1da6a
# Mark if an actual change occured that we need to deal with later by calling
f1da6a
# depmod or mkinitramfs against the affected kernel.
f1da6a
module_has_changed() {
f1da6a
f1da6a
    declare module=$1 krel=$2
f1da6a
f1da6a
    module=${module%.ko}
f1da6a
    module=${module##*/}
f1da6a
f1da6a
    eval "changed_modules_${krel//[^a-zA-Z0-9]/_}=$krel"
f1da6a
    eval "changed_initramfs_${krel//[^a-zA-Z0-9]/_}=$krel"
f1da6a
f1da6a
}
f1da6a
961cf5
# module_weak_link:
961cf5
# Generate a weak link path for the module.
961cf5
# Takes module file name and the target kernel release as arguments
961cf5
# The way of generation intentionally left from the initial version
961cf5
module_weak_link() {
961cf5
    local module="$1"
961cf5
    local krel="$2"
961cf5
    local module_krel
961cf5
    local subpath
961cf5
961cf5
    module_krel="$(krel_of_module "$module")"
961cf5
    subpath=$(echo $module | sed -nre "s:$BASEDIR(/usr)?/lib/modules/$module_krel/([^/]*)/(.*):\3:p")
961cf5
762062
    if [[ -z $subpath ]]; then
762062
        # module is not in /lib/modules/$krel?
762062
        # It's possible for example for Oracle ACFS compatibility check
762062
        # Install it with its full path as a /lib/modules subpath
762062
        subpath="$module"
762062
    fi
762062
961cf5
    echo "$BASEDIR/lib/modules/$krel/weak-updates/${subpath#/}"
961cf5
}
961cf5
961cf5
# module_short_name:
961cf5
# 'basename' version purely in bash, cuts off path from the filename
961cf5
module_short_name() {
961cf5
    echo "${1##*/}"
961cf5
}
961cf5
961cf5
#### Helper predicates
961cf5
961cf5
# is_weak_for_module_valid:
961cf5
# Takes real module filename and target kernel as arguments.
961cf5
# Calculates weak symlink filename for the corresponding module
961cf5
# for the target kernel,
961cf5
# returns 'true' if the symlink filename is a symlink
961cf5
# and the symlink points to a readable file
961cf5
# EVEN if it points to a different filename
961cf5
is_weak_for_module_valid() {
961cf5
    local module="$1"
961cf5
    local krel="$2"
961cf5
    local weak_link
961cf5
961cf5
    weak_link="$(module_weak_link $module $krel)"
961cf5
    [[ -L "$weak_link" ]] && [[ -r "$weak_link" ]]
961cf5
}
961cf5
961cf5
# is_weak_link:
961cf5
# Takes a filename and a kernel release.
961cf5
# 'true' if the filename is symlink under weak-updates/ for the kernel.
961cf5
# It doesn't matter, if it's a valid symlink (points to a real file) or not.
961cf5
is_weak_link() {
961cf5
    local link="$1"
961cf5
    local krel="$2"
961cf5
961cf5
    echo $link | grep -q "lib/modules/$krel/weak-updates" || return 1
961cf5
    [[ -L $link ]]
961cf5
}
961cf5
961cf5
# is_extra_exists:
961cf5
# Takes a module filename, the module's kernel release and target kernel release.
961cf5
# The module filename should be a real, not a symlink, filename (i.e. in extra/).
961cf5
# Returns 'true' if the same module exists for the target kernel.
961cf5
is_extra_exists() {
961cf5
    local module="$1"
961cf5
    local module_krel="$2"
961cf5
    local krel="$3"
961cf5
    local subpath="${module#*/lib/modules/$module_krel/extra/}"
961cf5
961cf5
    [[ -f $BASEDIR/lib/modules/$krel/extra/$subpath ]]
961cf5
}
961cf5
961cf5
is_kernel_installed() {
961cf5
    local krel="$1"
961cf5
961cf5
    [[ -f "$BASEDIR/boot/symvers-$krel.gz" ]]
961cf5
}
961cf5
961cf5
#### Helpers
961cf5
961cf5
# find_modules:
961cf5
# Takes kernel release and a list of subdirectories.
961cf5
# Produces list of module files in the subdirectories for the kernel
961cf5
find_modules() {
961cf5
    local krel="$1"
961cf5
    shift
961cf5
    local dirs="$*"
961cf5
961cf5
    for dir in $dirs; do
961cf5
        find $BASEDIR/lib/modules/$krel/$dir -name '*.ko' 2>/dev/null
961cf5
    done
961cf5
}
961cf5
961cf5
# find_installed_kernels:
961cf5
# Produces list of kernels, which modules are still installed
961cf5
find_installed_kernels() {
961cf5
    ls $BASEDIR/lib/modules/
961cf5
}
961cf5
961cf5
# find_kernels_with_extra:
961cf5
# Produces list of kernels, where exists extra/ directory
961cf5
find_kernels_with_extra() {
961cf5
    local krel
961cf5
    local extra_dir
961cf5
961cf5
    for krel in $(find_installed_kernels); do
961cf5
        extra_dir="$BASEDIR/lib/modules/$krel/extra"
961cf5
        [[ -d "$extra_dir" ]] || continue
961cf5
        echo "$krel"
961cf5
    done
961cf5
}
961cf5
961cf5
# remove_weak_link_quiet:
961cf5
# Takes symlink filename and target kernel release.
961cf5
# Removes the symlink and the directory tree
961cf5
# if it was the last file in the tree
961cf5
remove_weak_link_quiet() {
961cf5
    local link="$1"
961cf5
    local krel="$2"
961cf5
    local subpath="${link#*/lib/modules/$krel/weak-updates}"
961cf5
762062
    rm -f $link
961cf5
    ( cd "$BASEDIR/lib/modules/$krel/weak-updates" && \
762062
          rmdir --parents --ignore-fail-on-non-empty "$(dirname "${subpath#/}")" 2>/dev/null )
961cf5
}
961cf5
961cf5
#### Main logic
961cf5
961cf5
# update_modules_for_krel:
961cf5
# Takes kernel release and "action" function name.
961cf5
# Skips kernel without symvers,
961cf5
# otherwise triggers the main logic of modules installing/removing
961cf5
# for the given kernel, which is:
961cf5
# - save current state of weak modules symlinks
961cf5
# - install/remove the symlinks for the given (via stdin) list of modules
961cf5
# - validate the state and remove invalid symlinks
961cf5
#   (for the modules, which are not compatible (became incompatible) for
961cf5
#   the given kernel)
961cf5
# - check the state after validation to produce needed messages
961cf5
#   and trigger initrd regeneration if the list changed.
961cf5
update_modules_for_krel() {
961cf5
    local krel="$1"
961cf5
    local func="$2"
b6332a
    local do_fallback="$3"
961cf5
961cf5
    [[ -r "$BASEDIR/boot/symvers-$krel.gz" ]] || return
961cf5
961cf5
    global_link_state_save $krel
961cf5
    $func $krel
b6332a
    if ! validate_weak_links $krel && [[ -n "$do_fallback" ]]; then
b6332a
        global_link_state_restore $krel
b6332a
        return;
b6332a
    fi
b6332a
961cf5
    global_link_state_announce_changes $krel
762062
762062
    if [[ -n "$dry_run" ]]; then
762062
        pr_verbose "Dry run: restoring configuration"
762062
        global_link_state_restore $krel
762062
    fi
961cf5
}
961cf5
961cf5
# update_modules:
961cf5
# Common entry point for add/remove modules command
961cf5
# Takes the "action" function, the module list is supplied via stdin.
961cf5
# Reads the module list and triggers modules update for all installed
961cf5
# kernels.
961cf5
# Triggers initrd rebuild for the kernels, which modules are installed.
961cf5
update_modules() {
961cf5
    local func="$1"
b6332a
    local do_fallback="$2"
961cf5
    local module_krel
961cf5
f1da6a
    read_modules_list || exit 1
961cf5
    [[ ${#modules[@]} -gt 0 ]] || return
961cf5
961cf5
    for krel in $(find_installed_kernels); do
b6332a
        update_modules_for_krel $krel $func $do_fallback
961cf5
    done
961cf5
961cf5
    for module in "${modules[@]}"; do
961cf5
        # Module was built against this kernel, update initramfs.
961cf5
        module_krel="${module_krels[$module]}"
961cf5
        module_has_changed $module $module_krel
961cf5
    done
961cf5
}
961cf5
961cf5
# add_weak_links:
961cf5
# Action function for the "add-modules" command
961cf5
# Takes the kernel release, where the modules are added
961cf5
# and the modules[] and module_krels[] global arrays.
961cf5
# Install symlinks for the kernel with minimal checks
961cf5
# (just filename checks, no symbol checks)
961cf5
add_weak_links() {
961cf5
    local krel="$1"
961cf5
    local module_krel
961cf5
    local weak_link
961cf5
961cf5
    for module in "${modules[@]}"; do
961cf5
        module_krel="$(krel_of_module $module)"
961cf5
762062
        case "$module" in
762062
            /lib/modules/$krel/*)
762062
                # Module already installed to the current kernel
762062
                continue ;;
762062
        esac
961cf5
961cf5
        if is_extra_exists $module $module_krel $krel; then
961cf5
            pr_verbose "found $(module_short_name $module) for $krel while installing for $module_krel, update case?"
961cf5
        fi
961cf5
961cf5
        if is_weak_for_module_valid $module $krel; then
961cf5
            pr_verbose "weak module for $(module_short_name $module) already exists for kernel $krel, update case?"
961cf5
            # we should update initrd in update case,
961cf5
            # the change is not seen by the symlink detector
961cf5
            # (global_link_state_announce_changes())
961cf5
            module_has_changed $module $krel
961cf5
        fi
961cf5
961cf5
        weak_link="$(module_weak_link $module $krel)"
961cf5
762062
        mkdir -p "$(dirname $weak_link)"
762062
        ln -sf $module $weak_link
961cf5
961cf5
    done
961cf5
}
961cf5
961cf5
# remove_weak_links:
961cf5
# Action function for the "remove-modules" command
961cf5
# Takes the kernel release, where the modules are removed
961cf5
# and the modules[] and module_krels[] global arrays.
961cf5
# Removes symlinks from the given kernel if they are installed
961cf5
# for the modules in the list.
961cf5
remove_weak_links() {
961cf5
    local krel="$1"
961cf5
    local weak_link
961cf5
    local target
961cf5
    local module_krel
961cf5
961cf5
    for module in "${modules[@]}"; do
961cf5
        module_krel="$(krel_of_module $module)"
961cf5
961cf5
        weak_link="$(module_weak_link $module $krel)"
961cf5
        target="$(readlink $weak_link)"
961cf5
961cf5
        if [[ "$module" != "$target" ]]; then
961cf5
            pr_verbose "Skipping symlink $weak_link"
961cf5
            continue
961cf5
        fi
961cf5
        # In update case the --remove-modules call is performed
961cf5
        # after --add-modules (from postuninstall).
961cf5
        # So, we shouldn't really remove the symlink in this case.
961cf5
        # But in the remove case the actual target already removed.
961cf5
        if ! is_weak_for_module_valid "$module" "$krel"; then
961cf5
            remove_weak_link_quiet "$weak_link" "$krel"
961cf5
        fi
961cf5
    done
961cf5
}
961cf5
961cf5
# validate_weak_links:
961cf5
# Takes kernel release.
961cf5
# Checks if all the weak symlinks are suitable for the given kernel.
961cf5
# Uses depmod to perform the actual symbol checks and parses the output.
961cf5
# Since depmod internally creates the module list in the beginning of its work
961cf5
# accroding to the priority list in its configuration, but without symbol
961cf5
# check and doesn't amend the list during the check, the function runs it
961cf5
# in a loop in which it removes discovered incompatible symlinks
b6332a
#
b6332a
# Returns 0 (success) if proposal is fine or
b6332a
#         1 (false) if some incompatible symlinks were removed
961cf5
validate_weak_links() {
961cf5
    local krel="$1"
961cf5
    local basedir=${BASEDIR:+-b $BASEDIR}
961cf5
    local tmp
961cf5
    declare -A symbols
961cf5
    local is_updates_changed=1
961cf5
    local module
961cf5
    local module_krel
961cf5
    local target
961cf5
    local modpath
961cf5
    local symbol
961cf5
    local weak_link
b6332a
    # to return to caller that original proposal is not valid
b6332a
    # here 0 is true, 1 is false, since it will be the return code
b6332a
    local is_configuration_valid=0
961cf5
961cf5
    tmp=$(mktemp -p $tmpdir)
961cf5
961cf5
    if ! [[ -e $tmpdir/symvers-$krel ]]; then
961cf5
        [[ -e $BASEDIR/boot/symvers-$krel.gz ]] || return
961cf5
        zcat $BASEDIR/boot/symvers-$krel.gz > $tmpdir/symvers-$krel
f1da6a
    fi
961cf5
961cf5
    while ((is_updates_changed)); do
961cf5
        is_updates_changed=0
961cf5
961cf5
        # again $tmp because of subshell, see read_modules_list() comment
961cf5
        # create incompatibility report by depmod
532e2b
        # Shorcut if depmod finds a lot of incompatible modules elsewhere,
532e2b
        # we care only about weak-updates
532e2b
        /sbin/depmod $basedir -aeE $tmpdir/symvers-$krel $krel 2>&1 | \
532e2b
            grep "lib/modules/.*/weak-updates" 2>/dev/null >$tmp
961cf5
        # parse it into symbols[] associative array in form a-la
961cf5
        #   symbols["/path/to/the/module"]="list of bad symbols"
961cf5
        while read line; do
961cf5
            set -- $(echo $line | awk '/needs unknown symbol/{print $3 " " $NF}')
961cf5
            modpath=$1
961cf5
            symbol=$2
961cf5
            if [[ -n "$modpath" ]]; then
961cf5
                symbols[$modpath]="${symbols[$modpath]} $symbol"
961cf5
                continue
961cf5
            fi
961cf5
961cf5
            set -- $(echo $line | awk '/disagrees about version of symbol/{print $3 " " $NF}')
961cf5
            modpath=$1
961cf5
            symbol=$2
961cf5
            if [[ -n "$modpath" ]]; then
961cf5
                symbols[$modpath]="${symbols[$modpath]} $symbol"
961cf5
                continue
961cf5
            fi
961cf5
        done < $tmp
961cf5
961cf5
        # loop through all the weak links from the list of incompatible
961cf5
        # modules and remove them. Skips non-weak incompatibilities
961cf5
        for modpath in "${!symbols[@]}"; do
961cf5
            is_weak_link $modpath $krel || continue
961cf5
961cf5
            target=$(readlink $modpath)
961cf5
            module_krel=$(krel_of_module $target)
961cf5
961cf5
            remove_weak_link_quiet "$modpath" "$krel"
961cf5
961cf5
            pr_verbose "Module $(module_short_name $modpath) from kernel $module_krel is not compatible with kernel $krel in symbols: ${symbols[$modpath]}"
961cf5
            is_updates_changed=1
b6332a
            is_configuration_valid=1 # inversed value
961cf5
        done
961cf5
    done
961cf5
    rm -f $tmp
961cf5
961cf5
    # this loop is just to produce verbose compatibility messages
961cf5
    # for the compatible modules
961cf5
    for module in "${modules[@]}"; do
961cf5
        is_weak_for_module_valid $module $krel || continue
961cf5
961cf5
        weak_link="$(module_weak_link $module $krel)"
961cf5
        target="$(readlink $weak_link)"
961cf5
        module_krel=$(krel_of_module $target)
961cf5
961cf5
        if [[ "$module" == "$target" ]]; then
961cf5
            pr_verbose "Module ${module##*/} from kernel $module_krel is compatible with kernel $krel"
961cf5
        fi
961cf5
    done
b6332a
    return $is_configuration_valid
961cf5
}
961cf5
961cf5
# global_link_state_save:
961cf5
# Takes kernel release
961cf5
# Saves the given kernel's weak symlinks state into the global array
961cf5
# weak_modules_before[] for later processing
961cf5
global_link_state_save() {
961cf5
    local krel="$1"
961cf5
    local link
961cf5
    local target
961cf5
961cf5
    weak_modules_before=()
961cf5
    for link in $(find_modules $krel weak-updates | xargs); do
961cf5
        target=$(readlink $link)
961cf5
        weak_modules_before[$link]=$target
961cf5
    done
961cf5
}
961cf5
b6332a
# global_link_state_restore:
b6332a
# Takes kernel release
b6332a
# Restores the previous weak links state
b6332a
# (for example, if incompatible modules were installed)
b6332a
global_link_state_restore() {
b6332a
    local krel="$1"
b6332a
    local link
b6332a
    local target
b6332a
    local weak_modules_dir="$BASEDIR/lib/modules/$krel/weak-updates"
b6332a
b6332a
    pr_verbose "Falling back weak-modules state for kernel $krel"
b6332a
762062
    ( cd "$weak_modules_dir" 2>/dev/null && rm -rf * )
b6332a
b6332a
    for link in "${!weak_modules_before[@]}"; do
b6332a
        target=${weak_modules_before[$link]}
b6332a
762062
        mkdir -p "$(dirname $link)"
762062
        ln -sf $target $link
b6332a
    done
b6332a
}
b6332a
961cf5
# global_link_state_announce_changes:
961cf5
# Takes kernel release
961cf5
# Reads the given kernel's weak symlinks state, compares to the saved,
961cf5
# triggers initrd rebuild if there were changes
961cf5
# and produces message on symlink removal
961cf5
global_link_state_announce_changes() {
961cf5
    local krel="$1"
961cf5
    local link
961cf5
    local target
961cf5
    local new_target
961cf5
    declare -A weak_modules_after
961cf5
961cf5
    for link in $(find_modules $krel weak-updates | xargs); do
961cf5
        target=${weak_modules_before[$link]}
961cf5
        new_target=$(readlink $link)
961cf5
        weak_modules_after[$link]=$new_target
961cf5
961cf5
        # report change of existing link and appearing of a new link
961cf5
        [[ "$target" == "$new_target" ]] || module_has_changed $new_target $krel
961cf5
    done
961cf5
961cf5
    for link in "${!weak_modules_before[@]}"; do
961cf5
        target=${weak_modules_before[$link]}
961cf5
        new_target=${weak_modules_after[$link]}
961cf5
961cf5
        # report change of existing link and disappearing of an old link
961cf5
        [[ "$target" == "$new_target" ]] && continue
961cf5
        module_has_changed $target $krel
961cf5
        [[ -n "$new_target" ]] ||
961cf5
            pr_verbose "Removing compatible module $(module_short_name $target) from kernel $krel"
961cf5
    done
f1da6a
}
f1da6a
f1da6a
# remove_modules:
f1da6a
# Read in a list of modules from stdinput and process them for removal.
961cf5
# Parameter (noreplace) is deprecated, acts always as "noreplace".
961cf5
# There is no sense in the "replace" functionality since according
961cf5
# to the current requirements RPM will track existing of only one version
961cf5
# of extra/ module (no same extra/ modules for different kernels).
f1da6a
remove_modules() {
b6332a
    local no_fallback=""
b6332a
b6332a
    update_modules remove_weak_links $no_fallback
961cf5
}
f1da6a
961cf5
# add_modules:
961cf5
# Read in a list of modules from stdinput and process them for compatibility
961cf5
# with installed kernels under /lib/modules.
961cf5
add_modules() {
b6332a
    update_modules add_weak_links do_fallback
f1da6a
}
f1da6a
f1da6a
add_kernel() {
961cf5
    local krel=${1:-$(uname -r)}
961cf5
    local tmp
b6332a
    local no_fallback=""
961cf5
961cf5
    tmp=$(mktemp -p $tmpdir)
961cf5
961cf5
    if [ ! -e "$BASEDIR/boot/symvers-$krel.gz" ]; then
961cf5
        echo "Symvers dump file $BASEDIR/boot/symvers-$krel.gz" \
f1da6a
             "not found" >&2
f1da6a
        exit 1
f1da6a
    fi
961cf5
b6332a
    for k in $(find_kernels_with_extra | rpmsort); do
961cf5
        [[ "$krel" == "$k" ]] && continue
961cf5
        find_modules $k extra >> $tmp
f1da6a
    done
961cf5
961cf5
    # to avoid subshell, see the read_modules_list comment
961cf5
    read_modules_list < $tmp
961cf5
    rm -f $tmp
961cf5
b6332a
    update_modules_for_krel $krel add_weak_links $no_fallback
f1da6a
}
f1da6a
f1da6a
remove_kernel() {
f1da6a
    remove_krel=${1:-$(uname -r)}
961cf5
    weak_modules="$BASEDIR/lib/modules/$remove_krel/weak-updates"
f1da6a
    module_has_changed $weak_modules $remove_krel
dae7ef
dae7ef
    # Remove everything beneath the weak-updates directory
dae7ef
    ( cd "$weak_modules" && doit rm -rf * )
f1da6a
}
f1da6a
f1da6a
################################################################################
f1da6a
################################## MAIN GUTS ###################################
f1da6a
################################################################################
f1da6a
f1da6a
options=`getopt -o h --long help,add-modules,remove-modules \
f1da6a
                     --long add-kernel,remove-kernel \
961cf5
                     --long dry-run,no-initramfs,verbose,delete-modules \
961cf5
                     --long basedir:,dracut:,check-initramfs-prog: -- "$@"`
f1da6a
f1da6a
[ $? -eq 0 ] || usage 1
f1da6a
f1da6a
eval set -- "$options"
f1da6a
f1da6a
while :; do
f1da6a
    case "$1" in
f1da6a
    --add-modules)
f1da6a
        do_add_modules=1
f1da6a
        ;;
f1da6a
    --remove-modules)
f1da6a
        do_remove_modules=1
f1da6a
        ;;
f1da6a
    --add-kernel)
f1da6a
        do_add_kernel=1
f1da6a
        ;;
f1da6a
    --remove-kernel)
f1da6a
        do_remove_kernel=1
f1da6a
        ;;
f1da6a
    --dry-run)
f1da6a
        dry_run=1
762062
        # --dry-run option is not pure dry run anymore,
762062
        # because of depmod used internally.
762062
        # For add/remove modules we have to add/remove the symlinks
762062
        # and just restore the original configuration afterwards.
f1da6a
        ;;
f1da6a
    --no-initramfs)
f1da6a
        no_initramfs=1
f1da6a
        ;;
f1da6a
    --verbose)
f1da6a
        verbose=1
f1da6a
        ;;
f1da6a
    --delete-modules)
961cf5
        pr_warning "--delete-modules is deprecated, no effect"
961cf5
        ;;
961cf5
    --basedir)
961cf5
        BASEDIR="$2"
961cf5
        shift
961cf5
        ;;
961cf5
    --dracut)
961cf5
        dracut="$2"
961cf5
        shift
961cf5
        ;;
961cf5
    --check-initramfs-prog)
961cf5
        CHECK_INITRAMFS="$2"
961cf5
        shift
f1da6a
        ;;
f1da6a
    -h|--help)
f1da6a
        usage 0
f1da6a
        ;;
f1da6a
    --)
f1da6a
        shift
f1da6a
        break
f1da6a
        ;;
f1da6a
    esac
f1da6a
    shift
f1da6a
done
f1da6a
961cf5
if [ ! -x "$dracut" ]
961cf5
then
961cf5
    echo "weak-modules: this tool requires a dracut-enabled kernel"
961cf5
    exit 1
961cf5
fi
961cf5
961cf5
initramfs_prefix="$BASEDIR/${default_initramfs_prefix#/}"
961cf5
f1da6a
if [ -n "$do_add_modules" ]; then
961cf5
    add_modules
f1da6a
f1da6a
elif [ -n "$do_remove_modules" ]; then
961cf5
    remove_modules
f1da6a
f1da6a
elif [ -n "$do_add_kernel" ]; then
961cf5
    kernel=${1:-$(uname -r)}
961cf5
    add_kernel $kernel
f1da6a
f1da6a
elif [ -n "$do_remove_kernel" ]; then
961cf5
    kernel=${1:-$(uname -r)}
961cf5
    remove_kernel $kernel
f1da6a
961cf5
    exit 0
f1da6a
else
961cf5
    usage 1
f1da6a
fi
f1da6a
f1da6a
################################################################################
f1da6a
###################### CLEANUP POST ADD/REMOVE MODULE/KERNEL ###################
f1da6a
################################################################################
f1da6a
f1da6a
# run depmod and dracut as needed
f1da6a
for krel in ${!changed_modules_*}; do
f1da6a
    krel=${!krel}
961cf5
    basedir=${BASEDIR:+-b $BASEDIR}
f1da6a
961cf5
    if is_kernel_installed $krel; then
961cf5
        doit /sbin/depmod $basedir -ae -F $BASEDIR/boot/System.map-$krel $krel
961cf5
    else
961cf5
        pr_verbose "Skipping depmod for non-installed kernel $krel"
961cf5
    fi
f1da6a
done
f1da6a
f1da6a
for krel in ${!changed_initramfs_*}; do
f1da6a
    krel=${!krel}
f1da6a
f1da6a
    if [ ! -n "$no_initramfs" ]; then
961cf5
        ${CHECK_INITRAMFS:-check_initramfs} $krel
f1da6a
    fi
f1da6a
done