Clean mount lists in Linux

Default installations of Linux distributions mount more filesystems than they used to. This is because of loop devices, cgroups, and, in Ubuntu, snaps. As a result, the output from GNU df(1) as well as from lsblk(8) and mount(8) is more difficult to understand at a glance.

It is possible to make the output of these commands more readable by removing some of the “noise” devices. The following is a list of command arguments that remove irrelevant devices. After the list, I show how to replace the default commands in fish. You can adapt the replacement script for other shells.

GNU df(1) allows you to exclude tmpfs with a simple option. In Ubuntu 22.04, this removes /dev/shm, /run, /run/user/*, and other mount points from its output. In an improvement from Ubuntu 20.04, df(1) is already patched to exclude Squashfs (snaps).

Command:

df -x tmpfs

The improvement on my system is as follows:

> df | wc -l
25
> df -x tmpfs | wc -l
19

The command lsblk(8) can exclude devices by major device type. Snaps are loopback devices. According to devices.txt in the Linux kernel admin guide, the device type for loopback devices is 7.

Command:

lsblk -e 7

Improvement:

> lsblk | wc -l
56
> lsblk -e 7 | wc -l
28

We can simplify the output of GNU mount(8) by either excluding certain filesystem types or only including certain filesystem types.

Example command:

mount -l -t btrfs,fat,exfat,ext2,ext4,iso9660,ntfs3,ufs,vfat,xfs,zfs

Choose the type based on what filesystems you work with. For a list of supported filesystem type, look in /proc/filesystems and /lib/modules/"$(uname -r)"/kernel/fs/.

This is a little more involved, since mount(8) seems to lack a built-in “exclude” option. We can filter the output by the type column with awk.

Example command:

mount | awk '$5 !~ /(autofs|binfmt_misc|bpf|cgroup2|configfs|debugfs|devpts|devtmpfs|fuse|hugetlbfs|mqueue|nfsd|nsfs|proc|pstore|ramfs|rpc_pipefs|securityfs|squashfs|sysfs|tmpfs|tracefs)/'

The improvement with the exclude list is,

> mount | wc -l
77
> mount | awk '...' | wc -l
19

This configuration script replaces commands with wrappers that use arguments from the previous sections by default. They only use the arguments if you do not give arguments of your own. To ignore the wrapper and run, for example, mount without any arguments, use command mount or mount --.

cd ~/.config/fish/conf.d/
curl -O https://dbohdan.com/clean-mount-lists.fish
less clean-mount-lists.fish  # Examine the code.

# clean-mount-lists 0.3.1
# Wrap commands to show cleaner mount lists in Linux.
# See https://dbohdan.com/clean-mount-lists
# License: MIT.
# https://dbohdan.mit-license.org/@2023/license.txt

if status is-interactive; and test "$(uname)" = Linux
    if not set --query clean_mount_lists_exclude
        set --universal clean_mount_lists_exclude \
            autofs binfmt_misc bpf cgroup2 configfs debugfs devpts \
            devtmpfs fuse hugetlbfs mqueue nfsd nsfs proc pstore ramfs \
            rpc_pipefs securityfs squashfs sysfs tmpfs tracefs
    end

    function df --wraps df
        if test (count $argv) -eq 0 -o \( (count $argv) -eq 1 -a "x$argv[1]" = x-h \)
            # `-x` requires GNU df(1).
            command df -h -x tmpfs
        else
            command df $argv
        end
    end

    function mount --wraps mount
        if test (count $argv) -eq 0
            set --local re (string join '|' $clean_mount_lists_exclude)

            command mount | awk -v re=$re '$5 !~ re'
        else
            command mount $argv
        end
    end

    function lsblk --wraps lsblk
        if test (count $argv) -eq 0
            command lsblk -e 7
        else
            command lsblk $argv
        end
    end
end

findmnt(8) from util-linux is an alternative to df(1) and mount(8) with better options for filtering. For example,

findmnt --df --options rw --real

produces output like df -h, but only for real and read-write filesystems.

Thanks to the Hacker News discussion thread for suggesting findmnt(8) and the --options rw --real invocation.