Linux下数据文件删除文件系统空间不释放的问题
(2016-08-29 10:45:03)| 分类: Linux |
|
'du' and 'df' tools report different space utilization (文档 ID 457444.1) |
|
In this Document
Applies to:
Linux OS - Version Oracle Linux 4.4 and later
Linux x86-64
Linux x86
Linux Kernel - Version: 4.4 to 5.3
Symptoms
The 'du' (/usr/bin/du) and 'df' (/bin/df) command output displays conflicting space utilisation values, for example:
#
Filesystem
/dev/sda6
#
2.1G
In the example above, 'df' reports 8.6 Gb to have been used on the root (/) filesystem, whereas 'du' reports only 2.1 Gb to have been used.
Cause
The 'df' command reports how many disk blocks are used, whilst 'du' traverses the filesystem and reports the actual number of blocks used (directory by directory), including any relating to files in use by processes.
In most cases, space utilisation values returned from 'df' and 'du'
will be consistent. However, the potential exists for a running
process to delete a large file,
say.
Solution
The solution is to identify and stop (or kill) the process that continues to hold a file descriptor open for the deleted file. To do so, run the lsof command (/usr/sbin/lsof | grep deleted) as root to identify the holding process, for example:
#
COMMAND
cannaserv
vmware
vmware-re
vmnet-nat
vmware-se
gdm-binar
gconfd-2
vmware-vm
vmware-vm
kdeinit
kdeinit
kdeinit
In the example above, the 7th column in the output denotes the size of deleted files (in bytes). The 9th column denotes which file remains held open. The 1st and second columns denotes the process and pid that still holds the open file descriptor.
>lsof(list open files)是一个列出当前系统打开文件的工具。
>fuser用于标识访问文件或socket的进程信息。
另外,大家都知道,在Linux平台文件系统中,即使某个文件被删除,但是它如果任然有进程持有它,这个文件是可以被恢复的,这就是为什么数据库所有数据文件被删除,在实例没有停机的情况下可以完整恢复数据文件的原因,下面这篇MOS文章就讨论了在此情况下如何对数据文件进行恢复:
|
How To Recover Deleted Files on ext3/ext4 Filesystem (文档 ID 2056343.1) |
|
|
In this Document
Applies to:
Linux OS - Version Oracle Linux 5.0 and later
Linux x86-64
Linux x86
Goal
How to recover deleted file on ext3/ext4 filesystem, Which still
has file
Solution
A file in Linux is a pointer to an inode which contains the file data (permissions, owner and where its actual content resides on the disk).
Deleting the file removes the link, but not the inode itself.
If any other process has this file open then inode is not released for writing until the process releases it.
So if a process still has the file open, the data are there somewhere, even though the directory listing shows no files.
# ll
total 4
-rw-r--r-- 1 root root 27 Sep 16 05:19 test
# rm test
#
COMMAND
less
Understanding output of "lsof" command:
COMMAND: Command using the file.
PID: PID of the file
USER: Owner of the file
FD: File descriptor. Different flags of File descriptor are as below:
#:
u:
r:
w:
W:
mem: Memory mapped file, usually for share library
TYPE: File type. Different flags of File type are as below:
REG - Regular file
DIR - Directory
DEVICE: major, minor number of the device where file resides.
SIZE/OFF: File size
NODE: inode number
NAME: File name
Now we know that process 21353 still has the file open,
and
Now we can look into /proc and there will be a reference to the inode, from which the deleted file can be copied.
Following steps will help to recover the deleted files:
#
#
Now verify the content of the restored file.
Note: Don't use the -a flag with cp, as this will copy the (broken) symbolic link, rather than the actual file contents.
另外,找到是某个进程持有的文件,通过下面的方法可以看到这个进程相关的环境信息:
Checking the environment variables of ASM pmon process: It shows ORACLE_HOME is set to /oracle_grid/product/11.2.0.3/grid/ ( with 'slash' at the end )
oracle
#
__CLSAGFW_TYPE_NAME=ora.asm.typeORA_CRS_HOME=/oracle_grid/product/11.2.0.3/grid/HOSTNAME=aude3od015naboi.basdev.aurdev.national.com.auTERM=xtermSHELL=/bin/bash__CR......
最后通过一个简单的例子来结束这篇文章:
1.首先确保lsof工具已经安装到操作系统。
[root@rac01 Server]# rpm -ivh lsof-4.78-6.x86_64.rpm
Preparing...
[root@rac01 Server]# which lsof
/usr/sbin/lsof
2.在其中一个会话通过tail -f install2.log命令使tail进程持有该文件,在另一个会话通过rm -rf install2.log命令删除该文件。
3.使用lsof执行如下的操作:
[root@rac01 ~]#
tail
[root@rac01 ~]# cd /proc/6006/
[root@rac01 6006]# ls
attr
auxv
cgroup
clear_refs
[root@rac01 6006]# cd fd
[root@rac01 fd]# ll
total 0
lrwx------ 1 root root 64 Dec
lrwx------ 1 root root 64 Dec
lrwx------ 1 root root 64 Dec
lr-x------ 1 root root 64 Dec
[root@rac01 fd]# cd ..
[root@rac01 6006]#
HOSTNAME=rac01TERM=vt100SHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=172.168.4.123 56823 22OLDPWD=/mnt/ServerSSH_TTY=/dev/pts/0USER=rootLS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:MAIL=/var/spool/mail/rootPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binINPUTRC=/etc/inputrcPWD=/rootLANG=en_US.UTF-8SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpassSHLVL=1HOME=/rootLOGNAME=rootSSH_CONNECTION=172.168.4.123 56823 172.168.4.200 22LESSOPEN=|/usr/bin/lesspipe.sh %sG_BROKEN_FILENAMES=1_=/usr/bin/tail
[root@rac01 6006]# lsof /root/
COMMAND
bash
tail
[root@rac01 6006]# lsof -c tail
COMMAND
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
[root@rac01 6006]# lsof +d /root/
COMMAND
bash
tail
[root@rac01 6006]# lsof +D /root/
COMMAND
bash
tail
[root@rac01 6006]# lsof -d 3 | grep -v grep | grep deleted
tail
[root@rac01 6006]# lsof -p 6006
COMMAND
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
[root@rac01 6006]# lsof -u root | grep deleted
tail
[root@rac01 6006]# cd /proc/6006/fd/
[root@rac01 fd]# cp 3 /root/install2.log
[root@rac01 fd]# cd /proc/6006/fd
[root@rac01 fd]# ll
total 0
lrwx------ 1 root root 64 Dec
lrwx------ 1 root root 64 Dec
lrwx------ 1 root root 64 Dec
lr-x------ 1 root root 64 Dec
[root@rac01 fd]# cd /root/
[root@rac01 ~]# ls
anaconda-ks.cfg
--end--

加载中…