如何获取文件的设备/分区名称? [英] How to get a device/partition name of a file ?
问题描述
我的分区结构如:
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda6 51606140 16939248 34142692 34% /
/dev/sda5 495844 72969 397275 16% /boot
/dev/sda7 113022648 57515608 49765728 50% /home
/dev/sda8 113022648 57515608 49765728 4% /mnt
在使用readdir()解析目录内容时-如何找出哪个文件驻留在哪个设备上?
while parsing directories content using readdir() - how to find out which file resides on what device?
readdir()并分析文件名并打印其大小. 例如从device:/dev/sda6,并列出该分区下的文件名. 从/home读取内容时-应该显示从/dev/sda7读取内容并列出文件名
readdir() invoked from root directory and parses the file name and prints its size. like from device : /dev/sda6 and list the filenames under that partition. When it reads contents from /home - it should display reading content from /dev/sda7 and list filenames
如果您需要更多详细信息/信息,请告诉我
Please let me know,if you need more details/info
推荐答案
struct stat中有一个st_dev成员,它应该唯一地标识一个分区.
There is a st_dev member in struct stat, it should uniquely identify one partition.
bash中的示例:
stat ~/.vimrc
File: `/home2//leonard/.vimrc' -> `local-priv/vimrc'
Size: 16 Blocks: 0 IO Block: 4096 symbolic link
Device: 802h/2050d Inode: 6818899 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 1024/ leonard) Gid: ( 1024/ leonard)
Access: 2012-06-22 16:36:45.341371003 +0300
Modify: 2012-06-22 16:36:45.341371003 +0300
Change: 2012-06-22 16:36:45.341371003 +0300
stat实用程序没有其他作用.这是strace -vvv输出:
The stat utility does no additional magic. Here is strace -vvv output:
lstat64("/home2//leonard/.vimrc", {st_dev=makedev(8, 2), st_ino=6818899, st_mode=S_IFLNK|0777, st_nlink=1, st_uid=1024, st_gid=1024, st_blksize=4096, st_blocks=0, st_size=16, st_atime=2012/06/22-16:36:45, st_mtime=2012/06/22-16:36:45, st_ctime=2012/06/22-16:36:45}) = 0
0x0802是主要的8(sd)分区2,因此/dev/sda2
0x0802 is major 8(sd) partition 2, so /dev/sda2
为了将此映射到实际分区,您可以迭代/proc/mounts并统计所有设备(第一列). /proc/mounts的内容就像mount(1)的输出一样,只不过它直接来自内核.一些发行版将/etc/mtab链接到/proc/mounts.
In order to map this to actual partitions you can iterate /proc/mounts and stat all the devices (first column). The contents of /proc/mounts is just like the output of mount(1) except it comes directly from the kernel. Some distros symlink /etc/mtab to /proc/mounts.
或者您可以解析/proc/partitions:
Or you can parse /proc/partitions:
$ cat /proc/partitions
major minor #blocks name
8 0 976762584 sda
8 1 3998720 sda1
8 2 972762112 sda2
当然/dev/sda可能实际上不存在,设备可能使用了长的udev名称,例如/dev/disk/by-uuid/c4181217-a753-4cf3-b61d-190ee3981a3f.主/次编号应该是分区的可靠唯一标识符.
Of course /dev/sda might not actually exist, the device could be using a long udev name like /dev/disk/by-uuid/c4181217-a753-4cf3-b61d-190ee3981a3f. Major/Minor numbers should be a reliable unique identifier of a partition.
这篇关于如何获取文件的设备/分区名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!