You are not logged in.

#1 2009-01-21 05:13:46

deltaecho
Member
From: Georgia (USA)
Registered: 2008-08-06
Posts: 185

mods, a script to print all available modules of a specific kernel

This is just a little script that prints all available kernel modules.  It accepts either the kernel's name or its path, and will individually display the modules for as many kernels as specified; if no argument is provided, it will default to your active kernel.

#! /bin/bash
#
# Copyright (c) 2009 by Dylon Edwards <deltaecho at archlinux dot us>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#   USA.
#

# Print the usage info
usage() {
    echo "${0##*/}: Lists all the modules of a specific kernel."
    echo "If no arguments are passed, it will default to your"
    echo "active kernel."
    echo
    echo "-h, --help           Print this text."
    echo
    echo "usage: ${0##*/} $( uname -r )"
}

# Collect all the arguments
args="${@-$( uname -r )}"

# ...and check them for validity
for arg in ${args}
do
    case "${arg}" in
        "-h"|"--help")
            usage
            exit 0
            ;;
        *)
            if [[ -d "/lib/modules/${arg}" ]]
            then
                kernels[index++]="/lib/modules/${arg}"
            elif [[ -d "${arg}" ]]
            then
                kernels[index++]="${arg}"
            else
                echo "${0##*/}: ${arg} does not seem to exist." 1>&2
                exit 27
            fi
    esac
done

# List all the kernels' modules
for kernel in ${kernels[@]}
do
    # Print the kernel's name if there
    #+ are more than one
    if [[ ${#kernels[@]} -gt 1 ]]
    then
        echo "${kernel}:"
    fi
    
    # Initialize some local variables
    longest=0    # Holds the longest module's name's length ;)
    curindex=0    # Holds the current index for the modules array
    totlength=0    # Holds the sum of all the modules' name's lengths
    modules=()    # Initialize a new array
    
    # Find all the modules in the current kernel
    for module in $( find ${kernel} -type f -iname '*.o' -or -iname '*.ko' )
    do

        # Format the module's name
        module=${module%.*}    # Remove its extension
        module=${module##*/}    # Remove its path

        # If the length of the module's name
        #+ is greater than longest
        if [[ ${#module} -gt ${longest} ]]
        then
            # Initialize longest to the length
            #+ of the module's name
            longest=${#module}
        fi

        # Insert the module in the next slot
        #+ of the modules array
        modules[curindex++]=${module}
        let "totlength += ${#module}"
    done

    # Sort the modules alphabetically
    modules=(`
        for module in ${modules[@]}
        do
            echo ${module}
        done | sort
    `)

    # Determine the width of the console
    width=$( tput cols )

    # Determine the number of columns to create
    if [[ ${width} -lt ${longest} ]]
    then
        let "cols = longest / width"
    else
        let "cols = width / longest"
    fi

    # Determine the total length of the array of modules
    let "totlength += ( 2 * ${#modules[@]} - 1 )"

    # If the total length of names is
    #+ less than or equal to the width
    #+ of the console window
    if [[ ${width} -ge ${totlength} ]]
    then
        # Print them to the terminal window
        echo ${modules[@]}
    else
        # Print the modules to the terminal window
        for (( index = 0; index < ${#modules[@]}; index ++ ))
        do

            # Initialize module to the element
            #+ at the current index
            module=${modules[index]}

            # Determine whether to print the module on
            #+ the current line or start a new one
            if (( ! ( ( index + 1 ) % cols ) ))
            then
                echo ${module}
            else
                echo -n ${module}

                # Insert white space to format the output better
                for (( fmt = ${#module}; fmt <= ( longest ); fmt ++ ))
                do
                    echo -n " "
                done
            fi
        done

        # Print a new line
        echo
    fi
done

Dylon

Offline

Board footer

Powered by FluxBB