3

Okay, so I'm having to work within user quotas on a linux system, and need to be able to find out the device name (e.g - /dev/md2) for a given path so that I can lookup the correct quota for that path.

Now, I can get the mount point easily enough using:

df -k "/volume1/foo/bar" | tail -1 | awk '{ print $6 }'

However I'm not sure of the best way to then take that mount point and convert it into a device name?

To further complicate matters, the mount point that I get from the above command may in fact be an encrypted folder, in which case I may have something that looks like:

/dev/md2 -> /volume1
/volume1/@Foo@ -> /volume1/Foo

Meaning that the above df command will identify a mount point of /volume1/Foo. However, I need a reliable, platform independent way to work way my way through mount points and find the actual device name I need for use with quota.

Specifically; I can't just rely on the first part of the path being the mount point of the device, as I may be working with environments that mount volumes in more specific locations, such as OS X that puts mounts into /Volumes/ for example.

4
  • 1
    You don't actually need to find the mount point. You just need the device number on which the target file is located which you can get directly with stat -c%d /volume1/foo/bar. Then you have to find the special node in /dev that bears that device number. I can't find any good generic scripting tools that do that last part for you, but if you have grub installed then you can always use grub-probe to solve your entire question with one command line so: /usr/sbin/grub-probe --target=device /volume1/foo/bar
    – Celada
    May 2, 2013 at 14:53
  • You can also try the 'di' program: gentoo.com/di . It will look up the quota information and use it for the space available display.
    – Brad Lanam
    May 2, 2013 at 20:33
  • Unfortunately I don't have either grub or di available; I need to keep the script as general purpose as possible, which is a huge headache since df and quota don't really guarantee the same formatting on different platforms, bah! I'm not even sure I can rely on the -c parameter for stat either, one device I'm using doesn't support it which means grep and awk I suppose. I'm not sure what to do with the device ID from stat though, I got one of "eh/14d" which I don't recognise from anywhere.
    – Haravikk
    May 3, 2013 at 13:49
  • First you wrote "linux system", but now you are talking about different platforms. If you need it to be portable, you absolutely cannot rely on stat- not all platforms have it OOTB (e.g. Solaris) and BSD (OSX) stat uses -f instead of -c. May 4, 2013 at 17:04

1 Answer 1

1

Okay, so I had a go and came up with the following solution; it's not desperately pretty, but mount and df should be available on most unix flavours, and this should return the correct device identifier by working through volumes until it can go no further. In most cases it should only require one or two iterations.

function get_device() {
    fs=$(df -k "$1" | tail -1)

    # Determine the device for the file-system
    device=
    mnt=$(echo "$fs" | awk '{ print $6 }')
    if [ "$cached_mnt" != "$mnt" ] 
        then
            cached_mnt="$mnt"

            mnts=$(mount)
            newmnt="$mnt"

            # Try to get a root mount point (for encrypted folders etc.)
            while [ -n "$newmnt" ] 
            do
                newmnt=$(echo "$mnts" | grep " on $mnt " | awk '{ print $1 }')
                [ "$newmnt" = "$mnt" ] && break

                if [ -n "$newmnt" ] 
                    then
                        device="$newmnt"
                        mnt=$(df "$newmnt" 2> /dev/null | tail -1 | awk '{print $6 }')

                        [ "$mnt" = "$device" -o "$mnt" = "$last" ] && break
                        last="$mnt"
                fi
            done

            cached_device="$device"
        else
            device="$cached_device"
    fi

    echo "$device"
}

Forgive any typos, as it's from a larger script. It uses very simple caching in case multiple device queries are made that resolve to the same disk/partition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.