Use the findmnt Command on Linux - WindowsTips.net - Windows Tips and Tricks with Geek

Thursday, January 27, 2022

Use the findmnt Command on Linux

 

Illustration of a document management system floating over an open laptop

The findmnt Command

The findmnt command was already installed on the Ubuntu, Manjaro, and Fedora builds we checked. If it isn’t installed on your Linux computer you’ll be able to easily find it using the package manager for your distribution.

The command-line tool you use to mount file systems is called mount. File systems that are mounted at boot time are defined in the “/etc/fstab” file. You can use the mount command to get a dump of all the mount points configured in your “/etc/fstab” file.

mount

The mount command

The output is detailed, but formatted in a dense wall of text.

The raw dump of information from the mount command

With a bit of effort, you can pick your way through it, or pipe it through utilities such as grep to winkle out the bits you’re interested in. The output from findmnt is much more accessible.

findmnt

The findmnt command

By contrast, the default output from findmnt is tabulated and contains a tree showing the hierarchy of the mount points.

The tabulated output from findmnt

The columns are:

  • Target: The location of the mount point in the file system
  • Source: The source device that contains the file system. Note that this might be a pseudo-device like a loopback device.
  • Fstype: The file system type.
  • Options: The options that were used with the command line mount command or in the “/etc/fstab” file to mount the file system.

To see the output without the tree, use the -l (list) option.

findmnt -l

The findmnt command with the -l list option

The columns are the same, but the mount point hierarchy is not represented as an indented tree.

The findmnt plain listing output

Selecting Specific File System Types

The -t (type) option causes findmnt to restrict its report to only include the file system type you request. For example, to only see ext4 file systems, you’d use:

findmnt -t exta

Inspecting ext4 file system mount points with findmnt

To see only squashfs file systems you’d type:

findmnt -t squashfs

Inspecting squashfs file system mount points with findmnt

To invert the selection so that you see everything else apart from the type you’ve specified on the command line, use the -i (invert) option.

findmnt -t squashfs -i

Excluding squashfs mount points with the -i invert option

The squashfs file systems are not reported on.

Output from findmnt with squashfs file systems ignored

The -t (type) option lets you use a comma-separated list of file system types. Don’t put spaces between them, as whitespace isn’t allowed between the file system types.

findmnt -t squashfs,proc,ext4

Selecting three types of file system mount point at once with findmnt

Choosing the Data Source

By default, findmnt gets its information from “/etc/fstab”, “/etc/mtab”, and “/proc/self/mountinfo”.

  • /etc/fstab: This is the file that holds the details of configured mounts. These are acted upon at boot time.
  • /etc/mtab: This file holds the details of the currently mounted mounts.
  • /proc/self/mountinfo: This queries the kernel for the most authoritative account of your system’s mounts.

You can tell findmnt to use one particular source if you wish. The options are:

  • —fstab or -s: Look in “/etc/fstab” only.
  • —mtab or -m: Look in “/etc/mtab” only.
  • —kernel or -k: Look in “/proc/self/mountinfo” only.

We can see the difference this can make if we look for vfat file systems. First, we’ll use the -s (fstab) option. This finds one vfat file system, mounted at “/boot/efi.”

findmnt -s -t vfat

Using findmnt to search /etc/fstab for vfat file system mounts

We’ll try again, and this time we’ll use the -k (kernel) option.

findmnt -k -t vfat

Using findmnt to search /proc/self/mount for vfat file system mounts

This reports on four entries. One is the same vfat file system that the -s option found. The other three are ad-hoc mounts that have occurred because two USB memory sticks have been plugged in. The -s option didn’t find them because they’re not configured in the “/etc/fstab” file.

One USB memory stick is connected as “/dev/sdc1”, which is the first partition on device sdc. The other memory stick has two partitions on it and these have been mounted as “/dev/sdb1” and “/dev/sdb2.”

Selecting by Mount Point

If you know the mount point you can pass that to findmnt to find out the settings and other details.

findmnt /media/dave/PINK
findmnt /media/dave/WHITEUSB

Inspecting mount points by name with findmnt

We can see these two USB memory sticks have vfat file systems, and they’ve been mounted as “/dev/sdb2” and “/dev/sdc1.”

Using Polling Mode in findmnt

Possibly the coolest feature of findmnt is its polling feature. Writing images to USB devices is something that you can find yourself doing periodically. Identifying the correct drive is critical of course. You don’t want to overwrite the wrong device. findmnt makes it easy to see which device a removable drive is connected as.

There are two ways to do this. You can ask findmnt to monitor new mounts for a period of time, expressed in milliseconds. Any mounts that happen during that period are reported on. This uses the --timeout option.

The second way tells findmnt to wait until it has detected a new mount. This uses the --first-only option. It will wait for as long as it takes for a new mount to occur, but it’ll only report on the first mount that takes place. The --timeout option will report on all new mounts that occur during the specified polling period.

This command tells findmnt to monitor new mounts for 30 seconds.

findmnt -p --timeout 30000

Making findmnt monitor for new mount points for 30 seconds

A single USB memory stick has been plugged in during that period, reported on, and findmnt is continuing to monitor for the rest of the 30 seconds.

This command tells findmnt to monitor for new mounts until it sees one new mount point created.

findmnt -p --first-only

waiting for the next new mount point with findmnt

When a new device is plugged in, it reports on a new mount point, then exits to the command prompt.

No comments:

Post a Comment