Blame SOURCES/weak-modules

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