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