Linux 文件刪除原理_009

 

 

 

 

 

  ***瞭解Linux文件刪除原理先了解一下文件inode索引節點,每一個文件在Linux系統裏都有惟一的索引節點(身份證號)html

inode。若是文件存在硬連接,那這個文件和這個文件的硬連接的inode是相同的,而且能夠建立許多硬連接。node

  參照瞭解inode索引節點 博客    https://www.cnblogs.com/zoulongbin/p/10456285.htmlapache

 

 

[root@oldboy test]# ls -livim

total 0centos

140028 -rw-r--r-- 1 root root 0 Nov 16 17:25 file.txttomcat

[root@oldboy test]# ln file.txt /tmp/file_hard_link1.txt服務器

[root@oldboy test]# ln file.txt file_hare_link2.txtapp

[root@oldboy test]# ls -lih file.txt file_hare_link2.txt /tmp/file_hard_link1.txtdom

140028 -rw-r--r-- 3 root root 0 Nov 16 17:25 file_hare_link2.txtcurl

140028 -rw-r--r-- 3 root root 0 Nov 16 17:25 file.txt

140028 -rw-r--r-- 3 root root 0 Nov 16 17:25 /tmp/file_hard_link1.txt

 

 

一個文件被刪除須要知足兩個條件:

  i_link 硬連接數爲0 而且 i_count 進程引用計數也爲0,文件纔算被刪除,不然文件不能說被刪除。

文件沒被刪除,文件的inode索引結點號系統是沒有回收的,文件只有徹底被刪除了後,系統才

會回收文件inode索引節點,然後被建立的新文件使用。

 

 

 

 

實驗

 

一、建立兩文件以前先df –i查看分區inode,建立兩個文件,一個file.txt,

內容testfile,另外一個文件text.txt,內容abcdef,再查看分區inode變化

[root@oldboy test]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      593344 55834 537510   10% /

tmpfs          125596     1 125595    1% /dev/shm

/dev/sda1       51200    38  51162    1% /boot

[root@oldboy test]# echo "testfile" > file.txt

[root@oldboy test]# echo "abcdef" > text.txt

[root@oldboy test]# ls -l

total 8

-rw-r--r-- 1 root root 9 Nov 16 18:22 file.txt

-rw-r--r-- 1 root root 7 Nov 16 18:23 text.txt

[root@oldboy test]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      593344 55836 537508   10% /

tmpfs          125596     1 125595    1% /dev/shm

/dev/sda1       51200    38  51162    1% /boot

 

 

二、建立硬連接對inode大小沒有影響

[root@oldboy test]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      593344 55836 537508   10% /

tmpfs          125596     1 125595    1% /dev/shm

/dev/sda1       51200    38  51162    1% /boot

[root@oldboy test]# ln file.txt file_hard_link.txt

[root@oldboy test]# ln text.txt text_hard_link.txt

[root@oldboy test]# ls -lih

total 16K

140028 -rw-r--r-- 2 root root 9 Nov 16 18:22 file_hard_link.txt

140028 -rw-r--r-- 2 root root 9 Nov 16 18:22 file.txt

140787 -rw-r--r-- 2 root root 7 Nov 16 18:23 text_hard_link.txt

140787 -rw-r--r-- 2 root root 7 Nov 16 18:23 text.txt

[root@oldboy test]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      593344 55836 537508   10% /

tmpfs          125596     1 125595    1% /dev/shm

/dev/sda1       51200    38  51162    1% /boot

 

 

三、刪除text.txt文件自己外還要刪除硬連接文件,這樣才text.txt文件纔算被刪除

[root@oldboy test]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      593344 55836 537508   10% /

tmpfs          125596     1 125595    1% /dev/shm

/dev/sda1       51200    38  51162    1% /boot

[root@oldboy test]# find /test/ -type f -name "text.txt" -delete

[root@oldboy test]# ls -l

total 12

-rw-r--r-- 2 root root 9 Nov 16 18:22 file_hard_link.txt

-rw-r--r-- 2 root root 9 Nov 16 18:22 file.txt

-rw-r--r-- 1 root root 7 Nov 16 18:23 text_hard_link.txt

[root@oldboy test]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      593344 55836 537508   10% /

tmpfs          125596     1 125595    1% /dev/shm

/dev/sda1       51200    38  51162    1% /boot

[root@oldboy test]# find /test/ -type f -name "text_hard_link.txt" -delete

[root@oldboy test]# ls -l

total 8

-rw-r--r-- 2 root root 9 Nov 16 18:22 file_hard_link.txt

-rw-r--r-- 2 root root 9 Nov 16 18:22 file.txt

[root@oldboy test]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      593344 55835 537509   10% /

tmpfs          125596     1 125595    1% /dev/shm

/dev/sda1       51200    38  51162    1% /boot

 

 

四、使用vim file.txt打開文件,而後打開另外一個SSH刪除file.txt和file_hard_link.txt後觀察分區inode

[root@oldboy test]# vim file.txt

[root@oldboy test]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      593344 55835 537509   10% /

tmpfs          125596     1 125595    1% /dev/shm

/dev/sda1       51200    38  51162    1% /boot

[root@oldboy test]# ls

file_hard_link.txt  file.txt

[root@oldboy test]# find /test/ -type f -delete

[root@oldboy test]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      593344 55835 537509   10% /

tmpfs          125596     1 125595    1% /dev/shm

/dev/sda1       51200    38  51162    1% /boot

[root@oldboy test]#                          ##退出file.txt編輯模式

[root@oldboy test]# ls

file.txt

[root@oldboy test]# find /test/ -type f -delete

[root@oldboy test]# df -i

Filesystem     Inodes IUsed  IFree IUse% Mounted on

/dev/sda3      593344 55834 537510   10% /

tmpfs          125596     1 125595    1% /dev/shm

/dev/sda1       51200    38  51162    1% /boot

 

 

 

 

 

企業案例1:Web服務器磁盤分區爆滿故障深刻解析

 

緣由:

  tomcat記錄日誌access_log過大致使分區爆滿,直接刪除了tomcatl日誌文件access_log,刪除access_log記錄日誌文件後使用df –h查看磁盤分區仍是爆滿。

 

 

 

建立模擬場景:

 

##yum安裝httpd(tomcat)

[root@oldboy ~]# yum -y install httpd

[root@oldboy ~]# rpm -qa httpd

httpd-2.2.15-69.el6.centos.x86_64

 

##啓動httpd服務

[root@oldboy ~]# /etc/init.d/httpd start

Starting httpd: httpd: apr_sockaddr_info_get() failed for oldboy

httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

                                                           [  OK  ]

 

##查看htttpd是否運行

[root@oldboy ~]# lsof -i :80

COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

httpd   2812   root    4u  IPv6  15539      0t0  TCP *:http (LISTEN)

httpd   2814 apache    4u  IPv6  15539      0t0  TCP *:http (LISTEN)

httpd   2815 apache    4u  IPv6  15539      0t0  TCP *:http (LISTEN)

httpd   2816 apache    4u  IPv6  15539      0t0  TCP *:http (LISTEN)

httpd   2817 apache    4u  IPv6  15539      0t0  TCP *:http (LISTEN)

httpd   2818 apache    4u  IPv6  15539      0t0  TCP *:http (LISTEN)

httpd   2819 apache    4u  IPv6  15539      0t0  TCP *:http (LISTEN)

httpd   2820 apache    4u  IPv6  15539      0t0  TCP *:http (LISTEN)

httpd   2821 apache    4u  IPv6  15539      0t0  TCP *:http (LISTEN)

 

##關閉防火牆

[root@oldboy ~]# /etc/init.d/iptables stop

 

##修改httpd服務配置文件httpd.conf日誌存放路徑

[root@oldboy ~]# grep "#CustomLog logs/access_log" /etc/httpd/conf/httpd.conf

#CustomLog logs/access_log common

[root@oldboy logs]#sed -i 's@#CustomLog logs/access_log common@CustomLog /app/logs/access_log common@g' /etc/httpd/conf/httpd.conf

[root@oldboy logs]# grep "CustomLog /app/logs/access_log" /etc/httpd/conf/httpd.conf

CustomLog /app/logs/access_log common

 

##建立一個模擬的分區/dev/sdb,block大小8K

[root@oldboy ~]# dd if=/dev/zero of=/dev/sdb bs=8k count=10

10+0 records in

10+0 records out

81920 bytes (82 kB) copied, 0.000203364 s, 403 MB/s

 

##格式化/dev/sdb爲ext3系統文件格式

[root@oldboy ~]# mkfs -t ext3 /dev/sdb

 

##建立一個/dev/sdb掛載點/app/logs

[root@oldboy ~]# mkdir -p /app/logs

 

#把/dev/sdb掛載到/app/logs

[root@oldboy ~]# mount -o loop /dev/sdb /app/logs/

[root@oldboy ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       8.8G  1.5G  7.0G  17% /

tmpfs           491M     0  491M   0% /dev/shm

/dev/sda1       190M   36M  145M  20% /boot

/dev/sdb         73K   14K   55K  21% /app/logs

[root@oldboy ~]# ls -l /app/logs/

total 12

drwx------ 2 root root 12288 Nov 16 19:27 lost+found

 

##重啓httpd服務使配置文件生效

[root@oldboy ~]# /etc/init.d/httpd restart

Stopping httpd:                                            [  OK  ]

Starting httpd: httpd: apr_sockaddr_info_get() failed for oldboy

httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

                                                           [  OK  ]

[root@oldboy ~]# cat /app/logs/access_log

 

##把httpd服務首頁內容修改爲oldboy

[root@oldboy ~]# echo oldboy > /var/www/html/index.html

 

##訪問httpd服務,使其產生日誌記錄

[root@oldboy ~]# curl 127.0.0.1

oldboy

[root@oldboy ~]# cat /app/logs/access_log

127.0.0.1 - - [16/Nov/2018:19:33:26 +0800] "GET / HTTP/1.1" 200 7

[root@oldboy ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       8.8G  1.5G  7.0G  17% /

tmpfs           491M     0  491M   0% /dev/shm

/dev/sda1       190M   36M  145M  20% /boot

/dev/sdb         73K   15K   54K  22% /app/logs

 

##打開一個新SSH窗口,使用這條命令自動刷訪問,把tomcat的記錄日誌access_log增大,撐滿/dev/sdb分區容量

[root@oldboy ~]# for n in `seq 1000`;do curl 127.0.0.1;done

[root@oldboy ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       8.8G  1.5G  7.0G  17% /

tmpfs           491M     0  491M   0% /dev/shm

/dev/sda1       190M   36M  145M  20% /boot

/dev/sdb         73K   71K     0 100% /app/logs

 

##刪除/app/logs記錄日誌文件

[root@oldboy ~]# find /app/logs/ -type f -name "access_log" -delete

root@oldboy ~]# ls -lh /app/logs/

total 12K

drwx------ 2 root root 12K Nov 16 19:27 lost+found

 

##查看/dev/sdb分區容量依然爆滿

[root@oldboy ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       8.8G  1.5G  7.0G  17% /

tmpfs           491M     0  491M   0% /dev/shm

/dev/sda1       190M   36M  145M  20% /boot

/dev/sdb         73K   73K     0 100% /app/logs

 

 

解決辦法1:重啓httpd服務

標註:使用這種辦法是由於進程正在調用access_log記錄日誌文件,直接刪除並不能直接刪除這個文件,只能重啓httpd服務釋放,建議使用下面的解決辦法2

 

[root@oldboy ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       8.8G  1.5G  7.0G  17% /

tmpfs           491M     0  491M   0% /dev/shm

/dev/sda1       190M   36M  145M  20% /boot

/dev/sdb         73K   73K     0 100% /app/logs

[root@oldboy ~]# /etc/init.d/httpd restart

Stopping httpd:                                            [  OK  ]

Starting httpd: httpd: apr_sockaddr_info_get() failed for oldboy

httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

                                                           [  OK  ]

[root@oldboy ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       8.8G  1.5G  7.0G  17% /

tmpfs           491M     0  491M   0% /dev/shm

/dev/sda1       190M   36M  145M  20% /boot

/dev/sdb         73K   14K   55K  21% /app/logs

 

 

 

解決辦法2:不刪除access_log文件,直接清空access_log文件內容

 

[root@oldboy ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       8.8G  1.5G  7.0G  17% /

tmpfs           491M     0  491M   0% /dev/shm

/dev/sda1       190M   36M  145M  20% /boot

/dev/sdb         73K   73K     0 100% /app/logs

[root@oldboy ~]# > /app/logs/access_log

[root@oldboy ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3       8.8G  1.5G  7.0G  17% /

tmpfs           491M     0  491M   0% /dev/shm

/dev/sda1       190M   36M  145M  20% /boot

/dev/sdb         73K   14K   55K  21% /app/logs

 

 

 

 

 

 

 

 

 

感謝老男孩博客提供參考  https://blog.51cto.com/oldboy

相關文章
相關標籤/搜索