Blame SOURCES/weak-modules

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