SkarmoutsosV
SkarmoutsosV

Reputation: 70

How to list names and descriptions of all loaded kernel modules

How to list module names and descriptions in the following format?

...
ext4 Fourth Extended Filesystem
video ACPI Video Driver
...

To list all loaded kernel modules (with some usage info for each one)

$ lsmod

To list only the module names (first column of lsmod, remove title)

$ lsmod | cut -d " " -f 1 | tail -n +2
...
ext4
video
...

To list all the descriptions of loaded kernel modules

$ sudo modinfo $(lsmod | cut -d " " -f 1 | tail -n +2) |grep "description" | cut -c17-
...
Fourth Extended Filesystem
ACPI Video Driver
...

How to combine both names and descriptions?

Upvotes: 2

Views: 1593

Answers (2)

Léa Gris
Léa Gris

Reputation: 19625

Do this with a standard shell:

#!/usr/bin/env sh

lsmod | {
  read -r _ || exit 1 # Ignore header line
  while read -r name _; do
    modinfo -F description "$name" | {
      read -r desc || true # Ignore read failure if no description
      # Print at least a line per module
      printf '%-24s %s\n' "$name" "$desc"
      # Iterate remaining description lines if any
      while read -r desc; do
        # Print description line without repeating module name
        printf '%-24s %s\n' '' "$desc"
      done
    }
  done
}
  • lsmod | while: Pipe the lsmod's output to a while loop
  • while read -r name _; do: Iterate reading the module name, and ignore “size” and “used by” into _ place-holder variables.
  • modinfo -F description "$name" | while: Query modinfo's description, and pipe the output to a while loop as it can be multiple lines.
  • while read -r desc; do: Iterate reading each description line.
  • printf '%-24s %s\n' "$name" "$desc": Print formatted module name and description line.

Upvotes: 2

0stone0
0stone0

Reputation: 44093

#!/bin/bash

while IFS= read -r name; do
    printf "%s\t\t%s\n" "${name}" "$(sudo modinfo "$name" | grep "description" | cut -c17-)"
done <<< "$(lsmod | cut -d " " -f 1 | tail -n +2)"
  1. Read all the names "$(lsmod | cut -d " " -f 1 | tail -n +2)"
  2. Loop through each line
  3. printf the name, and add the result of sudo modinfo ... behind it

Small section of example output:

...
psmouse              PS/2 mouse driver
virtio_blk           Virtio block driver
virtio_net           Virtio network driver
...

Copy-Paste one-liner;

while IFS= read -r name; do printf "%s\t\t%s\n" "${name}" "$(sudo modinfo "$name" | grep "description" | cut -c17-)"; done <<< "$(lsmod | cut -d " " -f 1 | tail -n +2)"

Upvotes: 0

Related Questions