How to determine the mount point for a device on Linux

Author: ads

While reinstalling a server and moving it to Debian, I ran into the problem that I need to sync the EFI partitions. The system is using a RAID on two disks (RAID 1), but the EFI partitions can’t be a RAID and need to exist on both disks. Or rather it is useful to have the EFI partition on every disk: if one disk fails, the system can boot from the other disk.

grub only installs the necessary files on one partition. However one can use a script as grub hook, and sync all partitions.

This script needs to determine if a device (the EFI partition) is already mounted somewhere, or needs to be mounted. Turns out there is a little and not well-known tool for this.

A hard disk with an USB connector and a blue LED

Mounting a device on Linux

Devices on Linux can be mounted in several ways. One is by manually specifying the device name and the mount point:

mount /dev/sda2 /mnt

The above commandline will mount the device /dev/sda2 on the mount pont /mnt (which must already exist). The mount command will determine the filesystem on the partition, if that doesn’t work it can be specified using the -t option:

mount -t vfat /dev/sda2 /mnt

A second way to mount devices is by pre-defining which device will be mounted on which mount point. This information is stored in the file /etc/fstab, and this file contains one line per device and mount point.

# grep sda2 /etc/fstab
/dev/sda2  /boot/efi       vfat    umask=0077      0       1

This allows to mount the mount point /boot/efi by issuing the following command:

mount /boot/efi

It is also possible to use the label of the disk:

# grep efi /etc/fstab
UUID=48F7-B093  /boot/efi       vfat    umask=0077      0       1

This is safer. The device name can change if a disk configuration changes. The label however is unique and identifies this specific device and partition.

Identify the mount point

What if I don’t know if the device is already mounted somewhere or not, and if I don’t know the exact mount point. The findmnt tool will help with that:

# findmnt /dev/sda2
TARGET    SOURCE    FSTYPE OPTIONS
/boot/efi /dev/sda2 vfat   rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro

That’s very helpful!

If the device is currently not mounted, the findmnt tool will not output anything, and also exit with a return code RC=1. This comes handy in scripts.

The output of the tool can be formatted. I need the following options:

  • --first-only: Only show the first find - not that I expect more mount points, but play it safe
  • --noheadings: Skip the headers (like TARGET, SOURCE, FSTYPE, and OPTIONS)
  • --output=TARGET: Only output the TARGET column, that is the actual mount point

Using these options together:

# findmnt --first-only --noheadings --output=TARGET /dev/sda2
/boot/efi

The device /dev/sda2 is mounted on /boot/efi.

See this blog posting and this repository with scripts how the findmnt tool is used to identify mount points for grub.


Comments

With an account on the Fediverse or Mastodon, you can respond to this post. Known replies are displayed below:

Learn how this is implemented here.

Categories:[linux] [software]

Tags:[comment-option] [debian] [efi] [grub] [hardware] [linux] [mount] [raid] [script] [software] [software-raid] [uefi] [unified-extensible-firmware-interface]

Share: