zerofree 详细返回什么?

pla*_*rue 11 filesystems ext4 ext2 ext3 sparse-files

zerofree -v /dev/sda1回来了
123642/1860888/3327744

手册页没有解释这些数字是什么:http : //manpages.ubuntu.com/manpages/natty/man8/zerofree.8.html

我在 github 上找到了一些代码:https : //github.com/haggaie/zerofree/blob/master/zerofree.c

还有这一行:

if ( verbose ) {
  printf("\r%u/%u/%u\n", modified, free, fs->super->s_blocks_count);
}
Run Code Online (Sandbox Code Playgroud)

所以我猜中间的数字是可用空间(以 kB 为单位?),第一个可能是用零覆盖的数量,最后一个丢失了我。

你怎么认为?

slm*_*slm 16

我在 Fedora 19 上安装了相同的工具,我在.spec文件中注意到一个 URL,该 URL 指向此页面,标题为:Keeping filesystem images sparse。此页面包含一些创建测试数据的示例,因此我运行命令来创建相应的文件。

例子

$ dd if=/dev/zero of=fs.image bs=1024 seek=2000000 count=0
$ /sbin/mke2fs fs.image

$ ls -l fs.image 
-rw-rw-r--. 1 saml saml 2048000000 Jan  4 21:42 fs.image

$ du -s fs.image 
32052   fs.image
Run Code Online (Sandbox Code Playgroud)

当我运行zerofree -v命令时,我得到以下信息:

$ zerofree -v fs.image 
...counting up percentages 0%-100%...
0/491394/500000
Run Code Online (Sandbox Code Playgroud)

使用 filefrag 进行查询

当我使用该工具filefrag查询fs.image文件时,我得到了以下信息。

$ filefrag -v fs.image 
Filesystem type is: ef53
File size of fs.image is 2048000000 (500000 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..     620:   11714560..  11715180:    621:            
   1:    32768..   32769:   11716608..  11716609:      2:   11715181:
   2:    32892..   33382:   11716732..  11717222:    491:   11716610:
   3:    65536..   66026:   11722752..  11723242:    491:   11717223:
...
Run Code Online (Sandbox Code Playgroud)

s_block_count在源代码中引用也正好与我的版本的源代码zerofree.c

    if ( verbose ) {
            printf("\r%u/%u/%u\n", nonzero, free,
                            current_fs->super->s_blocks_count) ;
    }
Run Code Online (Sandbox Code Playgroud)

所以我们现在知道这s_blocks_count是 500,000 个 4096 字节的块。

使用 tune2fs 进行查询

我们还可以fs.image使用tune2fs.

$ sudo tune2fs -l fs.image | grep -i "block"
Block count:              500000
Reserved block count:     25000
Free blocks:              491394
First block:              0
Block size:               4096
Reserved GDT blocks:      122
Blocks per group:         32768
Inode blocks per group:   489
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
Run Code Online (Sandbox Code Playgroud)

从这个输出我们可以肯定地看到,报告的第二和第三个数字zerofree实际上是:

Free blocks:              491394
Block count:              500000
Run Code Online (Sandbox Code Playgroud)

回到源代码

报告的第一个数字实际上是找到的不为零的块数。这可以通过查看zerofree.

有一个计数器被调用,nonzero它在分析空闲块的主循环中递增。

            if ( i == current_fs->blocksize ) {
                    continue ;
            }

            ++nonzero ;

            if ( !dryrun ) {
                    ret = io_channel_write_blk(current_fs->io, blk, 1, empty) ;
                    if ( ret ) {
                            fprintf(stderr, "%s: error while writing block\n", argv[0]) ;
                            return 1 ;
                    }
            }
Run Code Online (Sandbox Code Playgroud)

结论

因此,经过一些详细分析后,这些数字看起来如下:

  • 遇到的非零空闲块数(随后被清零)
  • 文件系统中的空闲块数
  • 文件系统中的块总数