Blame SOURCES/weak-modules

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