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