码迷,mamicode.com
首页 > 系统相关 > 详细

查看Linux磁盘块大小

时间:2017-04-14 10:50:06      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:ls   df   tune2fs   du   

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

#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 i in range(1, 10):                                         
        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 命令判断,示例如下:

walker@walker-vm:~/apue$ df -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:~/apue$ sudo tune2fs -l /dev/mapper/walker--vm--vg-root | grep Block
Block count:              4651008
Block size:               4096
Blocks per group:         32768


*** walker ***


本文出自 “walker的流水账” 博客,请务必保留此出处http://walkerqt.blog.51cto.com/1310630/1915596

查看Linux磁盘块大小

标签:ls   df   tune2fs   du   

原文地址:http://walkerqt.blog.51cto.com/1310630/1915596

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!