0%

debugfs

1. debugfs 简介

对于ext2/ext3/ext4 系列文件系统, 可以使用debugfs来跟踪文件系统的相关信息

首先看下常见的命令

  • stats 查看文件系统super block的数据, 类似dumpe2fs的功能
  • icheck 根据提供的block块号反查其inode号
    1
    2
    3
    debugfs:  icheck 13440000
    Block Inode number
    13440000 3358728
  • ncheck 根据提供的inode 号反查其文件名, 文件名的信息存放在目录项的data block中
    1
    2
    3
    debugfs:  ncheck 3358728
    Inode Pathname
    3358728 /unencrypted/mode
  • stat 根据提供的文件路径查看文件的综合信息, 比在本地用提供的信息更多, 包括extents段也给出了
1
2
3
4
stat unencrypted
Inode: 3358728 Type: regular Mode: 0600 Flags: 0x80000
EXTENTS:
(0):13435394
  • bd 打印块的内容
    1
    2
    3
    4
    5
    6
    7
    8
    9
    debugfs:  bd 13435394
    # 目录下文件名和inode号的映射, 对应ext4_dir_entry_2结构
    0000 0140 3300 0c00 0102 2e00 0000 0200 0000 .@3.............
    0020 0c00 0202 2e2e 0000 0840 3300 0c00 0401 .........@3.....
    0040 6d6f 6465 0240 3300 0c00 0302 6b65 7900 mode.@3.....key.
    0060 0940 3300 d00f 0301 7265 6600 0000 0000 .@3.....ref.....
    0100 0000 0000 0000 0000 0000 0000 0000 0000 ................
    # ext4_dir_entry_2 entry总长在rec_len字段, 文件名长度在name_len字段.
    0840 3300 0x334008 = 3358728 正好是mode的inode号
    dir block
  • filefrag 打印当前目录文件系统碎片情况
    1
    2
    3
    4
    debugfs:  filefrag
    ./mode: 1 contiguous extents
    ./key: 1 contiguous extents (dir)
    ./ref: 1 contiguous extents
  • testb 查询给定的块号是否已被使用 Test a block’s in-use flag
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    debugfs:  testb 20 20
    Block 20 marked in use
    Block 21 marked in use
    Block 22 marked in use
    Block 23 marked in use
    Block 24 marked in use
    Block 25 marked in use
    Block 26 marked in use
    Block 27 marked in use
    Block 28 marked in use
    Block 29 marked in use
    Block 30 marked in use
    Block 31 marked in use
    Block 32 marked in use
    Block 33 marked in use
    Block 34 marked in use
    Block 35 marked in use
    Block 36 marked in use
    Block 37 marked in use
    Block 38 marked in use
    Block 39 marked in use
  • ffi 查询下一个可用的inode号是多少, 下一次创建文件时会使用该inode号
  • freefrag/e2freefrag 查询free extent的直方图
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    HISTOGRAM OF FREE EXTENT SIZES:
    Extent Size Range : Free extents Free Blocks Percent
    4K... 8K- : 85 85 0.00%
    8K... 16K- : 119 301 0.00%
    16K... 32K- : 230 1218 0.01%
    32K... 64K- : 260 2814 0.02%
    64K... 128K- : 259 5864 0.05%
    128K... 256K- : 227 10680 0.09%
    256K... 512K- : 157 14647 0.12%
    512K... 1024K- : 114 20521 0.16%
    1M... 2M- : 119 44132 0.35%
    2M... 4M- : 51 35681 0.29%
    4M... 8M- : 59 84734 0.68%
    8M... 16M- : 12 33740 0.27%
    16M... 32M- : 24 144008 1.15%
    32M... 64M- : 14 162912 1.31%
    64M... 128M- : 360 11392885 91.32%
  • id . dump inode号块
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #id test
    0000 b681 0000 0200 0000 3e60 045e 3e60 045e ........>`.^>`.^
    0020 3e60 045e 0000 0000 0000 0100 0800 0000 >`.^............
    0040 0000 0800 0100 0000 0af3 0100 0400 0000 ................
    0060 0000 0000 0000 0000 0100 0000 4d08 0000 ............M...
    0100 0000 0000 0000 0000 0000 0000 0000 0000 ................
    *
    0140 0000 0000 6752 620f 0000 0000 0000 0000 ....gRb.........
    0160 0000 0000 0000 0000 0000 0000 0000 0000 ................
    0200 2000 0000 3458 ea2a 3458 ea2a 3458 ea2a ...4X.*4X.*4X.*
    0220 3e60 045e 3458 ea2a 0000 0000 0000 0000 >`.^4X.*........
    0240 0000 02ea 0706 3c00 0000 0000 1f00 0000 ......<.........
    0260 0000 0000 7365 6c69 6e75 7800 0000 0000 ....selinux.....
    0300 0000 0000 0000 0000 0000 0000 0000 0000 ................
    *
    0340 753a 6f62 6a65 6374 5f72 3a73 7973 7465 u:object_r:syste
    0360 6d5f 6461 7461 5f66 696c 653a 7330 0000 m_data_file:s0..