Blame SOURCES/weak-modules

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