RSS
 

Posts Tagged ‘delete’

linux下删除文件后没有释放空间

04 Sep

问题

文件被删除后,空间没有被释放

原因

在Linux或者Unix系统中,通过rm或者文件管理器删除文件将会从文件系统的目录结构上解除链接(unlink).然而如果文件是被打开的(有一个进程正在使用),那么进程将仍然可以读取该文件,磁盘空间也一直被占用。

解决方法

首先我们获得一个已经被删除但是仍然被应用程序占用的文件列表:

$ /usr/sbin/lsof|grep deleted
ora    25575 data   33u      REG              65,65  4294983680   31014933 /oradata/DATAPRE/UNDOTBS009.dbf (deleted)

从lsof的输出中,我们可以发现pid为25575的进程持有着以文件描述号(fd)为33打开的文件/oradata/DATAPRE/UNDOTBS009.dbf。

在我们找到了这个文件之后可以通过结束进程的方式来释放被占用的空间。

通过截断proc文件系统中的文件可以强制要求系统回收分配给正在使用的的文件。这是一项高级技术,仅到管理员确定不会对运行中的进程造成影响时使用。应用程序对这种方式支持的并不好,当一个正在使用的文件被截断可能会引发不可预知的问题

$ echo > /proc/pid/fd/fd_number

例如,根据之前lsof的输出:

$ file /proc/25575/fd/33
/proc/25575/fd/33: broken symbolic link to `/oradata/DATAPRE/UNDOTBS009.dbf (deleted)’
$ echo > /proc/25575/fd/33

From lsof output, we find process with pid 25575 has kept file /oradata/DATAPRE/UNDOTBS009.dbf open with file descriptor (fd) number 33.

After a file has been identified you can free space occupied by this reclaimed by shutting down the process in question.

It is possible to force the system to de-allocate the space consumed by an in-use file by forcing the system to truncate the file via the proc file system. This is an advanced technique and should only be carried out when the administrator is certain that this will cause no adverse effects to running processes. Applications may not be designed to deal elegantly with this situation and may produce inconsistent or undefined behaviour when files that are in use are abrubtly truncated in this manner.

$ echo > /proc/pid/fd/fd_number

For example, from the lsof output above


$ file /proc/25575/fd/33
/proc/25575/fd/33: broken symbolic link to `/oradata/DATAPRE/UNDOTBS009.dbf (deleted)'
$ echo > /proc/25575/fd/33

 
No Comments

Posted in linux