linux文件系统文件路径,inode,block号,sector号的相互转换

测试文件:/usr/lib64/libstdc++.so.6.0.8
0. 装载debugfs
    mount -t debugfs none /sys/kernel/debug
1. 正向查找
    a. 根据文件查找inode

    $ls -i /usr/lib64/libstdc++.so.6
    2050380 /usr/lib64/libstdc++.so.6
    b. 根据文件查找sector号
b.1 查看sector大小
$sudo fdisk -lu /dev/sda Disk /dev/sda: 2000.3 GB, 2000398934016 bytes 255 heads, 63 sectors/track, 243201 cylinders, total 3907029168 sectors
Units = sectors of 1 * 512 = 512 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      524287      261120   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2          524288   105381887    52428800   83  Linux
b.2 查看block大小
$sudo blockdev --getbsz /dev/sda2
    4096
b.3 查看文件的block分布
$sudo debugfs /dev/sda2 
debugfs 1.41.12 (17-May-2010)
debugfs:  stat /usr/lib64/libstdc++.so.6.0.8
node: 2051058   Type: regular    Mode:  0755   Flags: 0x0
Generation: 3213202093    Version: 0x00000000
User:     0   Group:     0   Size: 976312
File ACL: 0    Directory ACL: 0
Links: 1   Blockcount: 1920
Fragment:  Address: 0    Number: 0    Size: 0
ctime: 0x54fb5945 -- Sun Mar  8 04:02:13 2015
atime: 0x5640fc6d -- Tue Nov 10 04:05:01 2015
mtime: 0x539053b8 -- Thu Jun  5 19:25:44 2014
Size of extra inode fields: 4
BLOCKS:
(0-11):8197361-8197372, (IND):8197373, (12-238):8197374-8197600
TOTAL: 240
b.4 根据block和sector的大小,将block号换算成sector号
sector号=block号*(block大小/sector大小)
/usr/lib64/libstdc++.so.6.0.8的sector范围:
(0-95)65578888-65578976, (96-1911)65578992-65580800
2. 反向查找
    a. 根据sector查找inode

a.1 将sector号换算成block号
换算方法参考正向查找
a.2 根据block号查找inode
$sudo debugfs -R 'icheck 8197361' /dev/sda2        
debugfs 1.41.12 (17-May-2010)
Block   Inode number
8197361 2051058
    b. 根据inode查找文件
$sudo debugfs -R 'ncheck 2051058' /dev/sda2         
debugfs 1.41.12 (17-May-2010)
Inode   Pathname
2051058 /usr/lib64/libstdc++.so.6.0.8

你可能感兴趣的:(linux文件系统文件路径,inode,block号,sector号的相互转换)