淺談linux效能調優之三:分區格式化之前的考慮 有這麼一種特殊情況可能在生產環境下發生:系統的某個ext3檔案分區,當使用者往此分區上寫檔案時,提示磁碟空間已滿,但用df -h命令查 看時發現此分區磁碟使用量是60%,請分析出現這種情況是由什麼導致的,答案是inode已經耗盡! 為什麼呢 ?給出一個ext*檔案系統的結構圖 在Linux中進行格式化必須考慮Block與inode,Block還好理解,它是磁碟可以記錄的最小單位,是由數個扇區組成,所以大小通常為n*512Bytes,例如4K。 那麼inode是什麼呢 ? Block是記錄檔案內容的地區,inode則是記錄該檔案的屬性及其放置在哪個Block之內的資訊。每個inode分別記錄一個檔案的屬性與這個檔案分布在哪些datablock上(也就是我們說的指標,有的地方也叫索引編號)。 具體如下: ● inode 編號 ● 用來識別檔案類型,以及用於 stat C 函數的模式資訊 ● 檔案的連結數目 ● 屬主的 UID ● 最近一次訪問的時間 ● 屬主的組 ID(GID) ● 檔案的大小 ● 檔案所使用的磁碟塊的實際數目 ● 最近一次修改的時間 ● 最近一次更改的時間 小結:inode兩個功能:記錄檔案屬性和指標所以,每個檔案都會佔用一個inode。當Linux系統要尋找某個檔案時,它會先搜尋inode table找到這個檔案的屬性及資料存放地點,然後再尋找資料存放的Block進而將資料取出。一個分區被格式化為一個檔案系統之後,基本上它一定會有inode table與資料區域兩大塊,一個用來記錄檔案的屬性資訊與該檔案存放的Block塊,一個用來記錄檔案的內容。 一個邏輯上的概念: 一個block對應一個inode嗎? 答案是否定的,一個大檔案雖然佔用很多的block,但是只使用了一個inode 測試1: 我添加磁碟並劃分分區,/dev/sdb5,6,7各100M 並指定block大小分別是1k,2k,4k格式化時得到結構inode數量都是28000多 (-b) 結論:inode和block沒有直接關係!網上有一種說說“block越大,inode越小的說法”顯然錯誤 測試2: 我使用-i 選項格式化 (-i bytes-per-inode Specify the bytes/inode ratio. mke2fs creates an inode for every bytes-per-inode bytes of space on the disk. The larger the bytes-per-inode ratio, the fewer inodes will be created. This value generally shouldn’t be smaller than the blocksize of the filesystem, since in that case more inodes would be made than can ever be used. Be warned that it is not possible to expand the number of inodes on a filesystem after it is created, so be careful deciding the correct value for this parameter.) 結論:指定i越小,inode越大,注意這還是和block沒關係!只是使用者自訂inode數量而已! 注意:一個檔案佔用一個inode,但是至少佔用一個block,不管block數量有多大,1K,2K,4K,檔案小於blocksize時,佔用一個block,此block的剩餘空間別的檔案無法使用!若檔案大於blocksize時,直接使用多個block 於是,就有了最終結論:(當然這裡不是細算!) 分區總量/block大小 >= inode數 ------ > 能建立的檔案數量的最大值 = inode數 分區總量/block大小 < inode數 --------> 能建立的檔案數量的最大值 = 分區總量/block大小的數量個檔案 若分區是提供給給大檔案應用,一般不做考慮 相反,若分區是提供給小檔案應用,則一定要自己計算並格式化,以免inode耗盡,磁碟分割卻未使用完 *******************************************************************************測試1資料:[root@desktop132 ~]# mkfs.ext3 -b 1024 /dev/sdb5mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: Linux Block size=1024 (log=0)Fragment size=1024 (log=0)Stride=0 blocks, Stripe width=0 blocks 28112 inodes, 112392 blocks5619 blocks (5.00%) reserved for the super userFirst data block=1Maximum filesystem blocks=6737100814 block groups8192 blocks per group, 8192 fragments per group2008 inodes per groupSuperblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Writing inode tables: done Creating journal (4096 blocks): doneWriting superblocks and filesystem accounting information: done [root@desktop132 ~]# mkfs.ext3 -b 2048 /dev/sdb6mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: Linux Block size=2048 (log=1)Fragment size=2048 (log=1)Stride=0 blocks, Stripe width=0 blocks 28160 inodes, 56210 blocks2810 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=576716804 block groups16384 blocks per group, 16384 fragments per group7040 inodes per groupSuperblock backups stored on blocks: 16384, 49152 Writing inode tables: done Creating journal (4096 blocks): doneWriting superblocks and filesystem accounting information: done [root@desktop132 ~]# mkfs.ext3 -b 4096 /dev/sdb7mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: Linux Block size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks 28128 inodes, 28105 blocks1405 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=293601281 block group32768 blocks per group, 32768 fragments per group28128 inodes per group Writing inode tables: done Creating journal (1024 blocks): doneWriting superblocks and filesystem accounting information: done******************************************************************************* *******************************************************************************測試2資料:[root@desktop132 ~]# mkfs.ext3 -i 1024 /dev/sdb5mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=1024 (log=0)Fragment size=1024 (log=0)Stride=0 blocks, Stripe width=0 blocks112448 inodes, 112392 blocks5619 blocks (5.00%) reserved for the super userFirst data block=1Maximum filesystem blocks=6737100814 block groups8192 blocks per group, 8192 fragments per group8032 inodes per groupSuperblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Writing inode tables: done Creating journal (4096 blocks): doneWriting superblocks and filesystem accounting information: done This filesystem will be automatically checked every 31 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.[root@desktop132 ~]# mkfs.ext3 -i 2048 /dev/sdb6mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=1024 (log=0)Fragment size=1024 (log=0)Stride=0 blocks, Stripe width=0 blocks56224 inodes, 112420 blocks5621 blocks (5.00%) reserved for the super userFirst data block=1Maximum filesystem blocks=6737100814 block groups8192 blocks per group, 8192 fragments per group4016 inodes per groupSuperblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Writing inode tables: done Creating journal (4096 blocks): doneWriting superblocks and filesystem accounting information: done This filesystem will be automatically checked every 35 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.[root@desktop132 ~]# mkfs.ext3 -i 4096 /dev/sdb7mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=1024 (log=0)Fragment size=1024 (log=0)Stride=0 blocks, Stripe width=0 blocks28112 inodes, 112420 blocks5621 blocks (5.00%) reserved for the super userFirst data block=1Maximum filesystem blocks=6737100814 block groups8192 blocks per group, 8192 fragments per group2008 inodes per groupSuperblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Writing inode tables: done Creating journal (4096 blocks): doneWriting superblocks and filesystem accounting information: done This filesystem will be automatically checked every 37 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.*******************************************************************************