Linux文件的删除原理
1) Linux是通过link的数量来控制文件删除的,一般来说,每个文件都有2个link计数器:i_count和i_link
i_link是文件的硬链接数(磁盘引计数器)
i_count是文件的被进程调用数(内存引用计数器)
控制文件 文件删除:
2) 文件的硬链接数,有一个硬链接i_link+1,减少一个硬链接,i_link -1,
当i_link=0时,文件就被删除。
例:创建一个文件i_link=1,为这个文件创建一个硬链接,i_link =2
3) 2)是否有进程占用使用这个文件,有一个进程占用这个文件i_count+1,
I_count=1;当减少一个进程占用i_count -1 么有进程调用时为0
当 i_link=0并且i_count=0 文件才会真正被删除
模拟web访问
查看安装httpd服务
[root@web ~]# rpm -q httpd
httpd-2.2.15-29.el6.centos.x86_64
如果没有安装可以用yum -y install httpd 安装
修改apache服务日志路径
[root@web ~]# sed -i 's@CustomLog logs/access_log combined@CustomLog /app/log/access_log combined@g' /etc/httpd/conf/httpd.conf
[root@web ~]# grep app /etc/httpd/conf/httpd.conf
# /etc/sysconfig/httpd must be set appropriately if this location is
# All of these directives may appear inside <VirtualHost> containers,
# e-mailed. This address appears on some server-generated pages, such
# UserDir: The name of the directory that is appended onto a user's home
# a good value. If most of your content is binary, such as applications
# or images, you may want to use "application/octet-stream" instead to
# EnableMMAP: Control whether memory-mapping is used to deliver
CustomLog /app/log/access_log combined #修改默认日志路径
# documents in the realname directory are treated as applications and
# The same rules about trailing "/" apply to ScriptAlias directives as to
# IndexOptions: Controls the appearance of server-generated directory
# default, and append to directory listings.
#AddType application/x-tar .tgz
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
# includes to substitute the appropriate text.
# You can modify the messages' appearance without changing any of the
# problem with Microsoft WebFolders which does not appropriately handle
新建日志存放目录
[root@web ~]# mkdir -p /app/log
模拟分区
[root@web ~]# dd if=/dev/zero of=/dev/sdc bs=8k count=10
记录了10+0 的读入
记录了10+0 的写出
81920字节(82 kB)已复制,0.0139921 秒,5.9 MB/秒
格式化分区
[root@web ~]# mkfs -t ext4 /dev/sdc
mke2fs 1.41.12 (17-May-2010)
/dev/sdc is not a block special device.
无论如何也要继续? (y,n) y
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
16 inodes, 80 blocks
4 blocks (5.00%) reserved for the super user
第一个数据块=1
1 block group
8192 blocks per group, 8192 fragments per group
16 inodes per group
正在写入inode表: 完成
文件系统小得无法记录日志
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
将日志目录挂载到模拟分区内
[root@web ~]# mount /dev/sdc /app/log/
mount: /dev/sdc is not a block device (maybe try `-o loop'?)
[root@web ~]# mount -o loop /dev/sdc /app/log/ ##此处用-o loop
查看分区大小
[root@web ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_root-lv_root 37G 11G 25G 31% /
tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
/dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
/dev/sr0 4.2G 4.2G 0 100% /media/cdrom
/dev/sdc 73K 14K 55K 21% /app/log
写for循环访问网页
[root@web ~]# for n in `seq 100000`;do curl -s http://192.168.15.11>/dev/null;done
[root@web ~]# tail -f /app/log/access_log ##动态查看日志
192.168.1.11 - - [10/May/2017:04:53:29 +0800] "GET / HTTP/1.1" 403 5039 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
[root@web ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_root-lv_root 37G 11G 25G 31% /
tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
/dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
/dev/sr0 4.2G 4.2G 0 100% /media/cdrom
/dev/sdc 73K 68K 1.0K 99% /app/log
此时:
[root@web ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_root-lv_root 2465792 249775 2216017 11% /
tmpfs 126544 5 126539 1% /dev/shm
/dev/sda1 128016 38 127978 1% /boot
/dev/sr0 0 0 0 - /media/CentOS_6.5_Final
/dev/sr0 0 0 0 - /media/cdrom
/dev/sdc 16 12 4 75% /app/log
[root@web ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_root-lv_root 37G 11G 25G 31% /
tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
/dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
/dev/sr0 4.2G 4.2G 0 100% /media/cdrom
/dev/sdc 73K 68K 1.0K 99% /app/log
[root@web ~]# pwd
/root
[root@web ~]# cd /app/log/
[root@web log]# ls
access_log lost+found
如果这样删除,发现文件删除,空间并没有改变
[root@web log]# rm -f access_log
[root@web log]# ls
lost+found
[root@web log]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_root-lv_root 37G 11G 25G 31% /
tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
/dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
/dev/sr0 4.2G 4.2G 0 100% /media/cdrom
/dev/sdc 73K 68K 1.0K 99% /app/log
[root@web log]# lsof |grep del ##查看文件被进程占用情况
httpd 58211 apache mem REG 253,0 46856 1461202 /usr/lib64/libsvn_delta-1.so.0.0.0
httpd 58211 apache 7w REG 7,0 54119 12 /app/log/access_log (deleted)
httpd 58212 apache mem REG 253,0 46856 1461202 /usr/lib64/libsvn_delta-1.so.0.0.0
httpd 58212 apache 7w REG 7,0 54119
发现httpd 服务占用进程 ,找到原因
解决方法:
方法一:
[root@web ~]# /etc/init.d/httpd restart
停止 httpd: [确定]
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for web
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[确定]
[root@web ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_root-lv_root 37G 11G 25G 31% /
tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
/dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
/dev/sr0 4.2G 4.2G 0 100% /media/cdrom
/dev/sdc 73K 15K 54K 22% /app/log
[root@web ~]#
生产场景:最佳解决方法
1:删除文件,重启
2:清空文件
3:不要删除当天的被进程占用的文件,删除以前的
[root@web ~]# for n in `seq 10000`;do curl -s http://192.168.1.11>/dev/null;done
[root@web log]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_root-lv_root 37G 11G 25G 31% /
tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
/dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
/dev/sr0 4.2G 4.2G 0 100% /media/cdrom
/dev/sdc 73K 68K 1.0K 99% /app/log
[root@web log]# >access_log
[root@web log]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_root-lv_root 37G 11G 25G 31% /
tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
/dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
/dev/sr0 4.2G 4.2G 0 100% /media/cdrom
/dev/sdc 73K 15K 54K 22% /app/log
删除文件,空间不释放:
1:存在硬链接(hardlink)
2:有进程占用
3:磁盘空余空间维护出现问题 (突然断电:突然停机)