Press "Enter" to skip to content

Linux模拟硬盘资源耗尽故障

Linux硬盘资源包括[容量]及[文件数量(i节点)]两种,接下来,我们来模拟一下这两种资源分别被耗尽的故障。

环境搭建:
添加一块硬盘sdb,并在其中划分一块15M大小的分区/dev/sdb1,并将分区挂载至/mnt/111下。最后的挂载情况:

[root@localhost ~]# df -m        #查看容量
Filesystem           1M-blocks      Used Available Use% Mounted on
……省略内容
/dev/sdb1                   16         2        14   8% /mnt/111
[root@localhost ~]# df -i        #查看文件数量
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
……省略内容
/dev/sdb1               4016      11    4005    1% /mnt/111

1,模拟文件数量(i节点)耗尽故障
首先写一段bash脚本1.sh,不停地向分区系统里写入空文件。脚本内容如下:

#! /bin/bash
i=124918
while [ $i -le 129000 ]
do
touch /mnt/111/file$i
let i++
done

然后赋予该脚本可执行权限,并且执行之。

[root@localhost ~]# chmod u+x 1.sh
[root@localhost ~]# ./1.sh
touch: cannot touch `/mnt/111/file128923': No space left on device
#提示容量不够,注意此时可能弹出来大量重复提示容量不够的语句,需要按Ctrl+c取消。

此时我们再看一下分区情况:

[root@localhost ~]# df -i        #查看文件数量   
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
……省略内容
/dev/sdb1               4016    4016       0  100% /mnt/111
[root@localhost ~]# df -m        #查看容量
Filesystem           1M-blocks      Used Available Use% Mounted on
……省略内容
/dev/sdb1                   16         2        14   9% /mnt/111

可以看见,分区容量尚未占满,但文件数量已占用100%,因此不能创建新的文件了。也说明我们实验的目的达到了。

实验完成后,/mnt/111目录下产生了大量的file*文件,在用rm -rf /mnt/111/file*批量删除时,可能会出现如下错误,下面也给出了解决方案:

[root@localhost ~]# rm -rf /mnt/111/file*
bash: /bin/rm: Argument list too long    #删除时产生错误
                                         #执行下面的删除语句就可以了
[root@localhost ~]# find /mnt/111 -type f -name 'file*' -print0 | xargs -0 rm 
[root@localhost ~]#                     #删除成功

2,模拟容量耗尽故障
我们划分的分区只有15M,因为一条简单的语句就可以把容量占满了:

[root@localhost ~]# dd if=/dev/zero of=/mnt/111/test bs=1M count=16
dd: writing `/mnt/111/test': No space left on device  #提示容量不够
14+0 records in
13+0 records out
14516224 bytes (15 MB) copied, 7.54486 seconds, 1.9 MB/s

[root@localhost ~]# df -i
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
……省略内容
/dev/sdb1               4016      12    4004    1% /mnt/111
[root@localhost ~]# df -m
Filesystem           1M-blocks      Used Available Use% Mounted on
……省略内容
/dev/sdb1                   16        16         0 100% /mnt/111

注意第一条dd语句,if表示从哪个设备(或文件)读取,of表示写到哪个设备(或文件),bs表示每次读取的文件块体积,count表示文件块的数量,执行完成以后,相当于在/mnt/111/目录下创建了一个16M大小的文件test。

执行完dd语句以后,马上提示“No space left on device”,此时再看一下分区的情况,可以看见,容量已经耗尽,但文件数量才使用了1%。说明我们的测试目的也达到了。

2 Comments

  1. zhiwei 2012-10-20

    ext3文件系统的单个目录能容纳的文件数量是3万,超过之后就不能写入了。无论是Inode和磁盘容易是否占满。

    • bear 2012-10-20

      小样!在新浪混了一年果然非同寻常!

Leave a Reply to zhiwei Cancel reply

Your email address will not be published. Required fields are marked *