当磁盘空间满之后,为什么文件依然可以被创建

一、当磁盘满了之后创建文件
在有些服务器进程中,可能需要在进程启动之后在文件中写入进程的pid,从而可以通过读取这个pid文件对进程进行reload、stop、start之类的控制操作。但是,在磁盘空间满了之后,虽然pid文件创建成功,但是进程id却无法写入,这个时候如果依赖文件存在,然后从中读取pid就会出现一些问题。
下面通过一个文件虚拟出一个设备,格式化为ext2文件系统,然后把一个文件写满,之后尝试创建文件。
tsecer@harry: dd if=/dev/zero of=./diskimg bs=10k count=10
10+0 records in
10+0 records out
102400 bytes (102 kB) copied, 0.0011623 s, 88.1 MB/s
tsecer@harry: mkfs.ext2 ./diskimg 
mke2fs 1.41.12 (17-May-2010)
./diskimg is not a block special device.
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
16 inodes, 100 blocks
5 blocks (5.00%) reserved for the super user
First data block=1
1 block group
8192 blocks per group, 8192 fragments per group
16 inodes per group
 
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
tsecer@harry: mount  -t ext2 -o loop ./diskimg ./mnt
tsecer@harry: cd mnt
tsecer@harry: yes > full.txt
yes: standard output: No space left on device
yes: write error
tsecer@harry: df
Filesystem           1K-blocks     Used Available Use% Mounted on
/dev/sda2             18143556 14050220   3165032  82% /
tmpfs                   969388      224    969164   1% /dev/shm
/dev/sda1               289293    30703    243230  12% /boot
/dev/sr0                 50676    50676         0 100% /media/CDROM
/dev/sr1               3116336  3116336         0 100% /media/RHEL-6.6 Server.i386
/home/tsecer/CodeTest/diskfull/diskimg
                            93       93         0 100% /home/tsecer/CodeTest/diskfull/mnt
tsecer@harry: echo 3333 >>./full.txt 
bash: echo: write error: No space left on device
tsecer@harry: ll
total 91
-rw-r--r--. 1 root root 78848 Nov 25 06:35 full.txt
drwx------. 2 root root 12288 Nov 25 06:33 lost+found
虽然文件创建成功,但是写入失败。
tsecer@harry: echo $$ > pid.pid
bash: echo: write error: No space left on device
tsecer@harry: ll 
total 92
-rw-r--r--. 1 root root 78848 Nov 25 06:35 full.txt
drwx------. 2 root root 12288 Nov 25 06:33 lost+found
-rw-r--r--. 1 root root     0 Nov 25 06:36 pid.pid
tsecer@harry: 
 
二、问题简单说明
在磁盘格式化时,inode和数据block的数量是在格式化的时候就已经分配好,所以文件inode的创建和文件内容的分配使用的是各自独立的存储空间,所以即使文件可以创建成功,文件依然可以被写入。
 
tsecer@harry: umount ./mnt
从输出看,只剩余3个inode可供选择
tsecer@harry: losetup -f ./diskimg 
tsecer@harry: e2fsck /dev/loop2
e2fsck 1.41.12 (17-May-2010)
/dev/loop2: clean, 13/16 files, 100/100 blocks
tsecer@harry: 
 
再尝试创建新的文件
tsecer@harry: for (( i = 0; i < 20; i++ )) do touch $i.txt ; done
touch: cannot touch `3.txt': No space left on device
touch: cannot touch `4.txt': No space left on device
touch: cannot touch `5.txt': No space left on device
touch: cannot touch `6.txt': No space left on device
touch: cannot touch `7.txt': No space left on device
touch: cannot touch `8.txt': No space left on device
touch: cannot touch `9.txt': No space left on device
touch: cannot touch `10.txt': No space left on device
touch: cannot touch `11.txt': No space left on device
touch: cannot touch `12.txt': No space left on device
touch: cannot touch `13.txt': No space left on device
touch: cannot touch `14.txt': No space left on device
touch: cannot touch `15.txt': No space left on device
touch: cannot touch `16.txt': No space left on device
touch: cannot touch `17.txt': No space left on device
touch: cannot touch `18.txt': No space left on device
touch: cannot touch `19.txt': No space left on device
tsecer@harry: ll
total 95
-rw-r--r--. 1 root root     0 Nov 25 06:48 0.txt
-rw-r--r--. 1 root root     0 Nov 25 06:48 1.txt
-rw-r--r--. 1 root root     0 Nov 25 06:48 2.txt
-rw-r--r--. 1 root root 78848 Nov 25 06:35 full.txt
drwx------. 2 root root 12288 Nov 25 06:33 lost+found
-rw-r--r--. 1 root root     0 Nov 25 06:36 pid.pid
tsecer@harry: 
原文地址:https://www.cnblogs.com/tsecer/p/10487757.html