´Ó lsof ×îÏÈ£¬ÉîÈëÃ÷È· Linux ÐéÄâÎļþϵͳ
Åä¾°
ÓÐʱ»á·ºÆðÕâÑùµÄÇéÐΣ¬´ÅÅÌ¿Õ¼äÏÔʾÒѾ±»Õ¼Âú£¬¿ÉÊÇÔÚÉó²é´ÅÅ̵ÄÏêϸÎļþÕ¼ÓÃÇéÐÎʱ£¬·¢Ã÷´ÅÅÌÈÔÈ»ÓкܴóµÄ¿ÕÓà¿Õ¼ä¡£
1.Ö´ÐÐ
df
ÏÂÁîÉó²é´ÅÅÌʹÓÃÇéÐΣ¬·¢Ã÷´ÅÅÌÒѾÂúÁË¡£
-bash-4.2$ df -ThFilesystem Type Size Used Avail Use% Mounted on/dev/vda1 ext4 30G 30G 0 100% /devtmpfs devtmpfs 489M 0 489M 0% /devtmpfs tmpfs 497M 0 497M 0% /dev/shmtmpfs tmpfs 497M 50M 447M 11% /runtmpfs tmpfs 497M 0 497M 0% /sys/fs/cgroup
µÇ¼ºó¸´ÖÆ
2.Ö´ÐÐ du ÏÂÁîÉó²é¸÷¸öĿ¼µÄ´ÅÅÌÕ¼ÓÃÇéÐΣ¬°Ñ¸÷¸öĿ¼ÎļþµÄ¾ÞϸÏà¼Ó£¬·¢Ã÷²¢Ã»ÓÐÕ¼Âú´ÅÅÌ£¬ÓÐ10¶àG¿Õ¼äĪÃûʧ×Ù¡£
-bash-4.2$ du -h --max-depth=1 /home16M /home/logs11G /home/serverdog11G /home
µÇ¼ºó¸´ÖÆ
3.ΪºÎ»á·ºÆðÕâÑùµÄÇéÐÎÄØ£¿
ÓÉÓÚËäÈ»ÎļþÒѱ»É¾³ý£¬¿ÉÊÇһЩÀú³ÌÈÔÈ»·¿ªÕâЩÎļþ£¬Òò´ËÆäÕ¼ÓõĴÅÅ̿ռ䲢ûÓб»ÊÍ·Å¡£Ö´ÐÐ
lsof
ÏÂÁîÏÔʾ·¿ªÒÑɾ³ýµÄÎļþ¡£½«ÓÐÎÊÌâµÄÀú³ÌÖØÆô£¨»ò£¬Çå¿Õ£©£¬´ÅÅÌ¿Õ¼ä¾Í»á»ñµÃÊÍ·Å¡£
-bash-4.2# lsof | grep deletemysqld 2470 mysql 4u REG 253,1 0 523577 /var/tmp/ibfTeQFn (deleted)mysqld 2470 mysql 5u REG 253,1 0 523579 /var/tmp/ibaHcIdW (deleted)mysqld 2470 mysql 6u REG 253,1 0 523581 /var/tmp/ibLjiALu (deleted)mysqld 2470 mysql 7u REG 253,1 0 523585 /var/tmp/ibCFnzTB (deleted)mysqld 2470 mysql 11u REG 253,1 0 523587 /var/tmp/ibCjuqva (deleted)
µÇ¼ºó¸´ÖÆ
ÄÇô£¬Linux µÄÎļþϵͳ£¬¾¿¾¹ÎªÊ²Ã´ÕâôÉè¼ÆÄØ£¿ÒªÏàʶÕâЩ£¬¾ÍÒªÏÈŪÇåÎú²¢½ûÖ¹Ò×£¬ÏÂÃ潫´ÓһЩ»ù±¾¿´·¨ÈëÊÖ£¬Ò»²½²½½«ÕâЩÊáÀíÇåÎú£º
ʲôÊÇÐéÄâÎļþϵͳ£¨VFS£ºvirtual filesystem£©£¿
ʲôÊÇͨÓÃÎļþÄ£×Ó£¿
³¬µÈ¿é¹¤¾ß£¨superblock object£©
Ë÷Òý½Úµã¹¤¾ß£¨inode object£©
Îļþ¹¤¾ß£¨file object£©
Ŀ¼Ï¾ß£¨dentry object£©
ÎļþµÄ¿´·¨
ÎļþµÄ±í´ï
ÄÚ´æ±í´ï
´ÅÅ̱í´ï
Ŀ¼Ê÷µÄ¹¹½¨
ÈíÁ´½Ó vs Ó²Á´½Ó
Îļþ & ´ÅÅÌÖÎÀí
Ë÷Òý½Úµã״̬
Îļþ & Àú³ÌÖÎÀí
²Ù×÷£º
·¿ª&ɾ³ý
ÐéÄâÎļþϵͳ£¨virtual filesystem£©
ÏÂͼÏÔʾÁË Linux ²Ù×÷ϵͳÖÐÈÏÕæÎļþÖÎÀíµÄ»ù±¾×é¼þ¡£ÉÏ°ëÇøÓòΪÓû§Ä£Ê½£¬Ï°ëÇøÓòΪÄÚºËģʽ¡£Ó¦ÓóÌÐòʹÓñê×¼¿âlibcÀ´»á¼ûÎļþ£¬¿â½«ÇëÇóÓ³É䵽ϵͳŲÓã¬ÒÔ±ã½øÈëÄÚºËģʽ¡£
ËùÓÐÓëÎļþÏà¹ØµÄ²Ù×÷µÄÈë¿Ú¶¼ÊÇÐéÄâÎļþϵͳ£¨VFS£©£¬¶ø·ÇÌض¨µÄ¶îÎļþϵͳ£¨ÈçExt3¡¢ReiserFSºÍNFS£©¡£VFS ÌṩÁËϵͳ¿âºÍÌض¨Îļþϵͳ֮¼äµÄ½Ó¿Ú¡£Òò´Ë£¬VFS ²»µ«³äµ±Áýͳ²ã£¬²¢ÇÒÏÖʵÉÏËüÌṩÁËÒ»¸öÎļþϵͳµÄ»ù±¾ÊµÏÖ£¬¿ÉÒÔÓɲî±ðµÄʵÏÖÀ´Ê¹ÓúÍÀ©Õ¹¡£Òò´Ë£¬ÒªÏàʶÎļþϵͳÊÇÔõÑùÊÂÇéµÄ£¬¾ÍÒªÏÈÏàʶVFS ¡£
ͨÓÃÎļþÄ£×Ó
VFS µÄÖ÷ҪͷÄÔÔÚÓÚÒýÈëÁËÒ»¸öͨÓÃÎļþÄ£×Ó£¨common file model£©¡£Í¨ÓÃÎļþÄ£×ÓÓÉÒÔϹ¤¾ßÀàÐÍ×é³É£º
³¬µÈ¿é¹¤¾ß£¨superblock object£©
Äڴ棺ÎļþϵͳװÖÃʱ½¨É裬´æ·ÅÎļþϵͳµÄÓйØÐÅÏ¢
´ÅÅÌ£º¶ÔÓ¦ÓÚ´æ·ÅÔÚ´ÅÅÌÉϵÄÎļþϵͳ¿ØÖƿ飨filesystem control block£©
Ë÷Òý½Úµã¹¤¾ß£¨inode object£©
Äڴ棺»á¼ûʱ½¨É裬´æ·Å¹ØÓÚÏêϸÎļþµÄÒ»Ñùƽ³£ÐÅÏ¢£¨inode ½á¹¹£©
´ÅÅÌ£º¶ÔÓ¦ÓÚ´æ·ÅÔÚ´ÅÅÌÉϵÄÎļþ¿ØÖƿ飨file control block£©
ÿ¸öË÷Òý½Úµã¹¤¾ß¶¼ÓÐÒ»¸öË÷Òý½ÚµãºÅ£¬Î¨Ò»µØ±êʶÎļþϵͳµÄÎļþ
Îļþ¹¤¾ß£¨file object£©
Äڴ棺·¿ªÎļþʱ½¨É裬´æ·Å ·¿ªÎļþ ÓëÀú³ÌÖ®¼ä¾ÙÐн»»¥µÄÓйØÐÅÏ¢£¨file ½á¹¹£©
·¿ªÎļþÐÅÏ¢£¬½öµ±Àú³Ì»á¼ûÎļþʱ´ú±£´æÓÚÄÚºËÄÚ´æÖС£
Ŀ¼Ï¾ß£¨dentry object£©
Äڴ棺Ŀ¼ÏîÒ»µ©±»¶ÁÈëÄڴ棬VFS¾Í»á½«Æäת»»³Édentry ½á¹¹µÄĿ¼Ï¾ß
´ÅÅÌ£ºÌض¨ÎļþϵͳÒÔÌض¨µÄ·½·¨´æ´¢ÔÚ´ÅÅÌÉÏ
´æ·ÅĿ¼Ï¼´£¬ÎļþÃû³Æ£©Óë¶ÔÓ¦Îļþ¾ÙÐÐÁ´½ÓµÄÓйØÐÅÏ¢
Ŀ¼Ê÷
×ÛºÏÀ´Ëµ£¬Linux µÄ ¸ùÎļþϵͳ(system¡¯s root filessystem) ÊÇÄÚºËÆô¶¯mountµÄµÚÒ»¸öÎļþϵͳ¡£Äں˴úÂëÓ³ÏñÎļþÉúÑÄÔÚ¸ùÎļþϵͳÖУ¬¶øϵͳָµ¼Æô¶¯³ÌÐò»áÔÚ¸ùÎļþϵͳ¹ÒÔØÖ®ºó£¬´ÓÖаÑһЩ»ù±¾µÄ³õʼ»¯¾ç±¾ºÍЧÀ͵ȼÓÔص½ÄÚ´æÖÐÈ¥ÔËÐУ¨ÎļþϵͳºÍÄÚºËÊÇÍêÈ«×ÔÁ¦µÄÁ½¸ö²¿·Ö£©¡£ÆäËûÎļþϵͳ£¬ÔòºóÐøͨ¹ý¾ç±¾»òÏÂÁî×÷Ϊ×ÓÎļþϵͳװÖÃÔÚÒÑ×°ÖÃÎļþϵͳµÄĿ¼ÉÏ£¬×îÖÕÐγÉÕû¸öĿ¼Ê÷¡£
start_kernel ¡¡¡¡vfs_caches_init ¡¡¡¡¡¡¡¡mnt_init ¡¡¡¡¡¡¡¡¡¡¡¡init_rootfs // ×¢²árootfsÎļþϵͳ¡¡¡¡¡¡¡¡¡¡¡¡init_mount_tree // ¹ÒÔØrootfsÎļþϵͳ ¡¡¡¡¡ ¡¡¡¡rest_init ¡¡¡¡kernel_thread(kernel_init, NULL, CLONE_FS);
µÇ¼ºó¸´ÖÆ
¾Íµ¥¸öÎļþϵͳ¶øÑÔ£¬ÔÚÎļþϵͳװÖÃʱ£¬½¨É賬µÈ¿é¹¤¾ß£»ÑØÊ÷²éÕÒÎļþʱ£¬×ÜÊÇÊ×ÏÈ´Ó³õʶĿ¼µÄÖвéÕÒÆ¥ÅäµÄĿ¼ÏÒÔ±ã»ñÈ¡ÏìÓ¦µÄË÷Òý½Úµã£¬È»ºó¶ÁÈ¡Ë÷Òý½ÚµãµÄĿ¼Îļþ£¬×ª»¯Îªdentry¹¤¾ß£¬ÔÙ¼ì²éÆ¥ÅäµÄĿ¼ÏÖظ´Ö´ÐÐÒÔÉÏÀú³Ì£¬Ö±ÖÁÕÒµ½¶ÔÓ¦µÄÎļþµÄË÷Òý½Úµã£¬²¢½¨ÉèË÷Òý½Úµã¹¤¾ß¡£
ÈíÁ´½Ó vs Ó²Á´½Ó
ÈíÁ´½ÓÊÇÒ»¸öͨË×µÄÎļþ£¬ÆäÖдæ·ÅµÄÊÇÁíÍâÒ»¸öÎļþµÄ·¾¶Ãû¡£Ó²Á´½ÓÔòÖ¸Ïòͳһ¸öË÷Òý½Úµã£¬Ó²Á´½ÓÊý¼Í¼ÔÚË÷Òý½Úµã¹¤¾ßµÄ i_nlink ×ֶΡ£µ±
i_nlink
×Ö¶ÎΪÁãʱ£¬ËµÃ÷ûÓÐÓ²Á´½ÓÖ¸Ïò¸ÃÎļþ¡£
Îļþ & Àú³ÌÖÎÀí
ÏÂͼÊÇÒ»¸ö¼òÆÓʾÀý£¬ËµÃ÷Àú³ÌÊÇÔõÑùÓëÎļþ¾ÙÐн»»¥¡£Èý¸ö²î±ðÀú³Ì·¿ªÍ³Ò»¸öÎļþ£¬Ã¿¸öÀú³Ì¶¼ÓÐ×Ô¼ºµÄÎļþ¹¤¾ß£¬ÆäÖÐÁ½¸öÀú³ÌʹÓÃͳһ¸öÓ²Á´½Ó£¨Ã¿¸öÓ²Á´½Ó¶ÔÓ¦Ò»¸öĿ¼¹¤¾ß£©£¬Á½¸öĿ¼Ï¾ß¶¼Ö¸Ïòͳһ¸ö Ë÷Òý½Úµã¹¤¾ß¡£
Ë÷Òý½ÚµãµÄÊý¾ÝÓÖÓÉÁ½²¿·Ö×é³É£ºÄÚ´æÊý¾ÝºÍ´ÅÅÌÊý¾Ý¡£Linux ʹÓà Write back ×÷ΪË÷Òý½ÚµãµÄÊý¾ÝÒ»ÖÂÐÔÕ½ÂÔ¡£¹ØÓÚË÷Òý½ÚµãµÄÊý¾Ý£¬µ±Îļþ±»·¿ªÊ±£¬²Å»á¼ÓÔØË÷Òý½Úµãµ½Äڴ棻µ±²»ÔÙ±»Àú³ÌʹÓã¬Ôò´ÓÄÚ´æÌß³ö£»ÈôÊÇÖÐÐÄÓиüУ¬ÔòÐèÒª°ÑÊý¾Ýд»Ø´ÅÅÌ¡£
* "in_use" - valid inode, i_count > 0, i_nlink > 0* "dirty" - as "in_use" but also dirty* "unused" - valid inode, i_count = 0
µÇ¼ºó¸´ÖÆ
Ë÷Òý½ÚµãÊÇ·ñÈÔÔÚʹÓã¬ÊÇͨ¹ý
open()
ºÍ
close()
²Ù×÷½¨ÉèºÍÏú»ÙÎļþ¹¤¾ß£¬Îļþ¹¤¾ßͨ¹ýË÷Òý½ÚµãÌṩµÄ
iget
ºÍ
iput
¸üÐÂË÷Òý½ÚµãµÄi_count×ֶΣ¬ÒÔÍê³ÉʹÓüÆÊý¡£open ²Ù×÷ʹµÃ i_count ¼ÓÒ»£¬ close ²Ù×÷ʹµÃ i_count ¼õÒ»¡£ÔÚ close ²Ù×÷ʱÅжÏË÷Òý½ÚµãÊÇ·ñÊÍ·Å£¬ÈôÊÇ i_count = 0£¬ÔòÒâζ×Ų»ÔÙÓÐÀú³ÌÒýÓ㬽«»á´ÓÄÚ´æÊÍ·Å¡£
Îļþ & ´ÅÅÌÖÎÀí
ÎļþÓë´ÅÅÌÖÎÀíÁªÏµ×îϸÃܵIJÙ×÷£¬Äª¹ýÓÚtouchºÍrm²Ù×÷£¬¶øÓÈÒÔºóÕß×îΪҪº¦¡£Í¨¹ýstrace(»ò dtruss)£¬Éó²é rm µÄÏÖʵµÄϵͳŲÓÃ
# dtruss rm tmp...geteuid(0x0, 0x0, 0x0) = 0 0ioctl(0x0, 0x4004667A, 0x7FFEE06F09C4) = 0 0lstat64("tmp\0", 0x7FFEE06F0968, 0x0) = 0 0access("tmp\0", 0x2, 0x0) = 0 0unlink("tmp\0", 0x0, 0x0) = 0 0
µÇ¼ºó¸´ÖÆ
¿ÉÒÔ·¢Ã÷ rm ÏÖʵÊÇͨ¹ý unlink Íê³ÉµÄ¡£unlink´ú±íɾ³ýĿ¼ÏÒÔ¼°ïÔÌÆäË÷Òý½ÚµãµÄ¼ÆÊý¡£ÓÉͨÓÃÎļþÄ£×Ó¿ÉÖª£¬¸¸Ä¿Â¼×Ô¼ºÍ¬ÑùÊÇÒ»¸öÎļþ£¬Ò²¾ÍÒâζ×ÅĿ¼ÏîÊÇÆäÎļþÊý¾ÝµÄÒ»²¿·Ö¡£É¾³ýĿ¼ÏîµÈ¼ÛÓÚ´Ó¸¸Ä¿Â¼µÄÎļþÖÐɾ³ýÊý¾Ý£¬Ò²¾ÍÒâζ×ÅÊ×ÏÈÒª·¿ª¸¸Ä¿Â¼µÄÎļþ¡£ÄÇô£¬É¾³ý²Ù×÷¼´¿ÉÃ÷ȷΪ£º
ɾ³ýÏÂÁһ¸öÀú³Ì£©Ê¹Óà open ²Ù×÷»ñµÃ¸¸Ä¿Â¼Îļþ¹¤¾ß
ͨ¹ý iget ÔöÌí Ŀ¼ÎļþµÄË÷Òý½Úµã¹¤¾ß¼ÆÊý
¶ÁȡĿ¼ÎļþÊý¾Ý
½«Ä¿Â¼ÎļþÊý¾Ýת»¯ÎªÄ¿Â¼Ï¾ß
ÓÉÓÚĿ¼Ïî°üÀ¨ÎļþµÄË÷Òý½Úµã£¬ÀàËƵģ¬ÐèҪͨ¹ý iget ÔöÌíÎļþµÄË÷Òý½Úµã¹¤¾ß¼ÆÊý
ɾ³ýĿ¼µÄĿ¼Ïî
ïÔÌÎļþË÷Òý½Úµã¹¤¾ßµÄÓ²Á´½Ó¼ÆÊýi_nlink
ͨ¹ý iput ¿¢Ê¶ÔÎļþË÷Òý½Úµã¹¤¾ßµÄ²Ù×÷£¬Ê¹ÓüÆÊý i_count ¼õÒ»
ÅжÏi_countÊÇ·ñΪÁ㣬ÈôÊÇΪÁ㣬ÔòÊÍ·ÅÄÚ´æ
È»ºó£¬ÅжÏi_nlinkÊÇ·ñΪÁ㣬ÈôÊÇΪÁ㣬ÔòÊÍ·Å´ÅÅÌ¿Õ¼ä
ͨ¹ý iput ¿¢Ê¶ÔĿ¼Ë÷Òý½Úµã¹¤¾ßµÄ²Ù×÷¡£
×ܽá
תͷÀ´¿´Óöµ½µÄÎÊÌ⣬×Åʵ¿ÉÒÔ´ÓÁ½¸ö½Ç¶ÈÀ´Ã÷È·£º
Ë÷ÒýÓëÊý¾Ý
ÎļþϵͳÓëÎļþ¡¢´ÅÅÌÖÎÀíÓëÎļþ¡¢Àú³ÌÖÎÀíÓëÎļþ£¬×î½¹µãµÄ¶¼ÊÇÎļþµÄË÷Òý£¬¶ø²»ÊÇÎļþµÄÊý¾Ý¡£°ÑÊý¾ÝºÍË÷ÒýÍÑÀëÊÇÃ÷È·ÎļþϵͳµÄÒªº¦¡£
»º´æÕ½ÂÔ
ÓÉÓÚ²Ù×÷ϵͳʹÓà Write back µÄÕ½ÂÔ£¬Òâζ×ÅÖ»ÓÐÏÈÊÍ·ÅÄڴ棬²ÅÓпÉÄÜÊÍ·Å´ÅÅÌ¡£
Why lsof ?
´ÓÉÏÃæµÄÄ£×Ó¿ÉÒÔºÜÇåÎúµÄÃ÷È·£¬ÓÉÓÚĿ¼ÒѾûÓÐË÷Òýµ½ÎļþÁË£¬¿ÉÊÇ·¿ªÎļþÉÐÓÐË÷Òýµ½Îļþ£¬ÒÔÊDz»¿ÉÁ¬Ã¦ÊÍ·Å´ÅÅ̿ռ䡣
Ϊʲô lsof ¿ÉÒÔÕÒµ½ÒÑɾ³ýδÊͷŵÄÎļþÄØ£¿
lsof£¬¹ËÃû˼Ò壺list open files£¬¸ÃÏÂÁîµÄÔÀí¾ÍÊDzéÕÒ·¿ªÎļþµÄÁÐ±í£¬Òò´Ë¿ÉÒÔÕÒµ½ÒÑɾ³ýδÊͷŵÄÎļþ¡£
ÒÔÉϾÍÊÇ´Ó lsof ×îÏÈ£¬ÉîÈëÃ÷È· Linux ÐéÄâÎļþϵͳµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡