1. 程式人生 > >當磁盤空間滿之後,為什麽文件依然可以被創建

當磁盤空間滿之後,為什麽文件依然可以被創建

label 14. lose 文件系統 內容 pid top may umount

一、當磁盤滿了之後創建文件 在有些服務器進程中,可能需要在進程啟動之後在文件中寫入進程的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:

當磁盤空間滿之後,為什麽文件依然可以被創建