查看Linux磁盘块大小

1、用 du 命令结合文件大小判断,封装成Python代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#encoding: utf-8                                                   
#author: walker                                                    
#date: 2017-04-13                                                  
#summary: 获取文件所在磁盘块大小                                   
#sys.version: 2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2] 
                                                                    
import os                                                          
import math                                                        
                                                                    
#获取文件所在磁盘块大小                                            
#参数为:大于0B,没有空洞(hole)的文件                              
#返回值:若正常,返回块大小;否则,返回-1                          
def GetBlockSize(pathfile):                                        
    fileSize = os.path.getsize(pathfile)                           
    print('%s fileSize:%d' % (pathfile, fileSize))                 
    if fileSize < 1:                                               
        return -1                                                  
                                                                    
    blockSize = 512                                                
    for in range(110):                                         
        cmd = 'du -B %d %s' % (blockSize, pathfile)                
        print('cmd:%s' % cmd)                                      
        outText = os.popen(cmd).read()                             
        num = int(outText.split('\t')[0])                          
        print('num:%d' % num)                                      
        if math.ceil(1.0*fileSize/blockSize) == num:               
            break                                                  
        blockSize *= 2                                             
                                                                    
    return blockSize

2、用 tune2fs 结合 df 命令判断,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
walker@walker-vm:~/apuedf -hT
文件系统                        类型      容量  已用  可用 已用% 挂载点
/dev/mapper/walker--vm--vg-root ext4       18G  2.5G   14G   16% /
none                            tmpfs     4.0K     0  4.0K    0% /sys/fs/cgroup
udev                            devtmpfs  987M  4.0K  987M    1% /dev
tmpfs                           tmpfs     200M  616K  199M    1% /run
none                            tmpfs     5.0M     0  5.0M    0% /run/lock
none                            tmpfs     998M     0  998M    0% /run/shm
none                            tmpfs     100M     0  100M    0% /run/user
/dev/sda1                       ext2      236M   37M  187M   17% /boot
walker@walker-vm:~/apuesudo tune2fs -l /dev/mapper/walker--vm--vg-root grep Block
Block count:              4651008
Block size:               4096
Blocks per group:         32768


相关阅读:

1、Linux 下统计文件夹大小及文件数量

2、(网络)文件拷贝(Windows/Linux)


*** walker ***

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1915596如需转载请自行联系原作者

RQSLT
上一篇:分区副本限流机制三部曲(源码篇)


下一篇:PostgreSQL 10.1 手册_部分 III. 服务器管理_第 29 章 监控磁盘使用_29.2. 磁盘满失败