shell - Get device name for a path or volume identifier -


okay, i'm having work within user quotas on linux system, , need able find out device name (e.g - /dev/md2) given path can lookup correct quota path.

now, can mount point enough using:

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

however i'm not sure of best way take mount point , convert device name?

to further complicate matters, mount point above command may in fact encrypted folder, in case may have looks like:

/dev/md2 -> /volume1 /volume1/@foo@ -> /volume1/foo 

meaning above df command identify mount point of /volume1/foo. however, need reliable, platform independent way work way way through mount points , find actual device name need use quota.

specifically; can't rely on first part of path being mount point of device, may working environments mount volumes in more specific locations, such os x puts mounts /volumes/ example.

okay, had go , came following solution; it's not desperately pretty, mount , df should available on unix flavours, , should return correct device identifier working through volumes until can go no further. in cases should require 1 or 2 iterations.

function get_device() {     fs=$(df -k "$1" | tail -1)      # determine device file-system     device=     mnt=$(echo "$fs" | awk '{ print $6 }')     if [ "$cached_mnt" != "$mnt" ]                      cached_mnt="$mnt"              mnts=$(mount)             newmnt="$mnt"              # try root mount point (for encrypted folders etc.)             while [ -n "$newmnt" ]                              newmnt=$(echo "$mnts" | grep " on $mnt " | awk '{ print $1 }')                 [ "$newmnt" = "$mnt" ] && break                  if [ -n "$newmnt" ]                                              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 typos, it's larger script. uses simple caching in case multiple device queries made resolve same disk/partition.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -