顯示具有 du 標籤的文章。 顯示所有文章
顯示具有 du 標籤的文章。 顯示所有文章

2018年6月21日 星期四

df 與 du 不一致


https://unix.stackexchange.com/questions/13828/df-says-i-have-20g-more-disk-space-used-than-du-why

That's because du and df measure different things.
man du says:
du - estimate file space usage (...) Summarize disk usage of each FILE, recursively for directories.
and man df:
df - report file system disk space usage
File system has inode tables, journals etc. which are not summarized by du. It isn't only Linux-specific, but rather UNIX-specific (or even UNIX-filesystem-specific). Because UNIX's processes uses files to everything (I'm simplifying) i.e. writing to log files, there could be also open file descriptorproblem in this case causing different du and df outputs.

例如 inode Table 也會佔空間

The inode table (as well as a few very much smaller things) is included in the used space. It looks like you have about 18GB of inodes. That's around 6% of the space, which is in the expected ballpark for ext2/ext3/ext4 (filesystems that don't have an inode table are likely to have larger directory entries if nothing else). You can find the precise size used for inodes by running tune2fs -l /dev/mapper/VolGroup00-LogVol00, e.g. here are the relevant lines for one of my 20GB ext3 filesystems:
Inode count:              2622368
Inode size:               128
That partition has 2622368×128 B ≈ 320 MB of inodes.
Additional note: the way you've invoked du, you'll see spaced used by every file even on remote mounts and other non-directly-disked-backed filesystems. Run du -x / to see just what's stored on the root filesystem.
可以用 tune2fs -l /dev/mapper/VolGroup00-LogVol00 看 inode Table 大小
可以用 du -x / 看沒有 mount 之前的大小 (排除  mount 的部分)




如何看 mount point 底下原本的東西
https://community.spiceworks.com/topic/1111816-finding-what-data-is-under-an-nfs-mount-point

Here is an example using the --bind option to "mount".
Starting out with nothing mounted at /mnt/nfs
Text
# ls -l /mnt/nfs
total 0
-rw-r--r-- 1 root root 0 Aug  7 09:36 test_file.1
-rw-r--r-- 1 root root 0 Aug  7 09:36 test_file.2
-rw-r--r-- 1 root root 0 Aug  7 09:36 test_file.3
-rw-r--r-- 1 root root 0 Aug  7 09:36 test_file.4
-rw-r--r-- 1 root root 0 Aug  7 09:36 test_file.5

Mount something at /mnt/nfs

Text
# mount -o loop /Archive/ISO/sample.iso  /mnt/nfs
mount: warning: /mnt/nfs seems to be mounted read-only.

Listing the contents of /mnt/nfs  this time confirms the contents of the mounted iso file, not the test files:

Text
# ls -l /mnt/nfs
total 345162
-r-xr-xr-x 1 root root    139264 Jun 24  2011 AUTORUN.EXE
dr-xr-xr-x 1 root root      2048 Jun 24  2011 UNIX
dr-xr-xr-x 1 root root      2048 Jun 24  2011 UTILS
-r-xr-xr-x 1 root root      1954 Jun 24  2011 autorun.inf
-r-xr-xr-x 1 root root        12 Jun 24  2011 media.inf

Mount the root fs using "--bind" to have it appear under a new mount point: 
Text
# mount --bind / /new-root 

Now,  cd'ing to /new-root/mnt/nfs, you are not in the mountpoint directory of the ISO filesystem 

Text
# cd /new-root/mnt/nfs

Confirm by listing the contents of /new-root/mnt/nfs and you will see the contents of /mnt/nfs from before its use a mountpoint.
 
 # ls -l
total 0
-rw-r--r-- 1 root root 0 Aug  7 09:36 test_file.1
-rw-r--r-- 1 root root 0 Aug  7 09:36 test_file.2
-rw-r--r-- 1 root root 0 Aug  7 09:36 test_file.3
-rw-r--r-- 1 root root 0 Aug  7 09:36 test_file.4
-rw-r--r-- 1 root root 0 Aug  7 09:36 test_file.5


還有一種可能

https://www.ndchost.com/wiki/troubleshooting/df-and-du-show-different-usage

There are times when the du and df commands can show different disk usage stats. The usual reason for this is that a running process still has a file that was removed open. This causes df to still report that space as used. The solution to this issue is to reload or restart that service so that it closes that file handle.
lsof is a tool that can be used to find processes holding onto files. If you do not have lsof installed, you will need to install it using your package manager. Examples of how to do this are below.
# Debian and Ubuntu
apt-get install lsof
 
# CentOS, RHEL, and Fedora
yum install lsof
Next we want to use lsof to find the process
lsof | grep deleted
If you get any output changes are it looks something like this.
httpd     32617    nobody  106w      REG        9,4 1835222944     688166 /var/log/apache/awstats_log (deleted)
In this case, the process httpd is whats holding onto an 18GB file. You can either kill that process, or issue a restart using init.d or systemctl
# systemctl systems
systemctl restart httpd
 
# Older init.d systems
service httpd restart
 
# Even older systems
/etc/init.d/httpd restart
If the process doesn't have an init script you will need to restart it another way, or as a last resort kill the process. The second column output from lsof is the process id.
kill -9 32617
Now when running df again you should now see df reporting correctly.