6

How to obtain the partition device name and mount point when the file system label is known?

There are several possibilities:

Linux

File system label to device name

The device name can be obtained with blkid:

blkid -l -o device -t LABEL="$label" -c /dev/null

Device name to mount point

I tried obtaining the mount point with blkid, but I failed. Using the -s switch all other fields can be queried when the label is provided:

blkid -l -t LABEL="$label" -o value -s LABEL      # yields label
blkid -l -t LABEL="$label" -o value -s TYPE       # yields partition type
blkid -l -t LABEL="$label" -o value -s UUID       # yields UUID
blkid -l -t LABEL="$label" -o value -s MOUNTPOINT # yields nothing

Obtaining the mount point is not supported apparently, though it is listed when the -o list option is given, but not in an easy parseable way:

blkid -o list -c /dev/null <device>

The next attempt was to use df -P. Here's what I came up with, assuming the device is stored in the variable device:

export device
mountpoint=$(df -P |
  awk --assign dev="$device" \
    '{if ($1==dev)             \
      {for (i=1; i<=NF-5; i++) \
        $i=$(i+5);             \
        NF-=5;                 \
        print                  \
      }                        \
    }')

This code works. However, given it's ugliness I'm not convinced that parsing df is the preferred way.

Other Unices

File system label to device name

blkid works beautifully on Linux, but it's not available on Solaris or FreeBSD (at least one can't expect it to be installed). The same goes for findfs.

How do I obtain the device name in a portable way on Linux, Solaris and FreeBSD?

Device name to mount point

Happily df -P produces very similar output on all tested systems. The problem is that my ugly awk script doesn't seem to be portable (tested with Sun awk, gawk on Solaris and FreeBSDs awk). What's the preferred way?

Related questions

How to get the mount point of flash drive by using uuid? Problem: Parsing /proc/mounts is not portable and troublesome even on Linux. Parsing mount seems even worse. Perl is generally not available.

Is there a command to see where a disk is mounted? Problem: Parses /proc/mounts with a buggy awk script. The solutions aren't portable.

Summary

I'd like to obtain the device name and mount point when the file system label is provided in a portable way (assuming a POSIX shell). The disk label might contain white space or other non-ASCII characters which seems to complicate things even more.

         Label-To-Device    Device-To-Mount-Point
         ---------------    ---------------------
Linux           ✓                    ✓ (ugly)
Solaris         .                    .
FreeBSD         .                    .
1
  • Unnecessary I think, udisks already did the job
    – daisy
    Commented Mar 25, 2013 at 1:38

1 Answer 1

1

You can follow these steps to do it:

  1. get devname using blkid_dev_devname(...)
  2. get prob for this partition using blkid_new_probe_from_filename(...)
  3. use setmntent(...), getmntent(...), & endmntent(...), where mnt->mnt_fsname equals to devname copy mnt->mnt_dir which is mount point!

See these references for further details

mntent data structure

struct mntent {    
    char *mnt_fsname;   /* name of mounted file system */    
    char *mnt_dir;      /* file system path prefix */      
    ...
}; 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .