How to determine the partition for a file - Unix

This is a discussion on How to determine the partition for a file - Unix ; Which programming interfaces and tools can show to which partition a file belongs to? How do you get from the file name to the corresponding partition name and mount point? Regards, Markus...

+ Reply to Thread
Results 1 to 4 of 4

Thread: How to determine the partition for a file

  1. How to determine the partition for a file

    Which programming interfaces and tools can show to which partition a
    file belongs to?
    How do you get from the file name to the corresponding partition name
    and mount point?

    Regards,
    Markus


  2. Re: How to determine the partition for a file

    Markus.Elfring@web.de wrote:
    > Which programming interfaces and tools can show to which partition a
    > file belongs to?


    Could 'df FILE' provide some information?
    http://www.gnu.org/software/fileutils/fileutils.html

  3. Re: How to determine the partition for a file

    Markus.Elfring@web.de writes:

    > Which programming interfaces and tools can show to which partition a
    > file belongs to?
    > How do you get from the file name to the corresponding partition name
    > and mount point?


    Use stat(2) (or fstat or lstat). The file is identified with st_dev
    and st_ino. st_dev, the device number actually identifies the file
    system, what you call a "partition".

    There's no more portable API. The rest will have to be done with
    implementation specific stuff. On linux, you can get the list of the
    mount points from /proc/mounts. You can also read /etc/mtab, and
    further use stat on the mount points you find to identify the file,
    host or device on which the file system is stored. Indeed, some file
    systems are not stored in a "partition", but in a file or on a remote
    host.

    Use: strace /bin/df file # or ptrace or whatever is provided on your OS.
    to see what system calls are used by df to do the job.

    --
    __Pascal Bourguignon__ http://www.informatimago.com/
    I need a new toy.
    Tail of black dog keeps good time.
    Pounce! Good dog! Good dog!

  4. Re: How to determine the partition for a file

    Pascal Bourguignon wrote:
    > Markus.Elfring@web.de writes:
    >
    > > Which programming interfaces and tools can show to which partition a
    > > file belongs to?
    > > How do you get from the file name to the corresponding partition name
    > > and mount point?

    >
    > Use stat(2) (or fstat or lstat). The file is identified with st_dev
    > and st_ino. st_dev, the device number actually identifies the file
    > system, what you call a "partition".


    File system vs partition is the part that interested me.
    The logical volume layer means multiple partitions can
    be in the same filesystem. "df $FILE" give the filesystem
    as the mount point directory and also the device file that
    filesystem lives in. It it's a regular device you're done
    because that device file is the partition. If it's a
    logical volume then you need to figure out what blocks
    the inode resides in, then which device in the volume
    group those blocks currently reside on. The method
    depends on exactly which LVM is in use.


+ Reply to Thread