Blame SOURCES/weak-modules

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