Disk-Usage

df 命令如何知道可用磁碟空間?

  • December 29, 2021

有誰知道 df 命令如何知道可用磁碟空間?

它是否使用核心呼叫,如果是,是哪些?

還是它使用 /dev 或 /proc 中的虛擬文件?

你可以檢查它的原始碼,你可以執行strace它更快但並不總是容易解析,例如

strace du /
newfstatat(AT_FDCWD, "/", {st_mode=S_IFDIR|0555, st_size=4096, ...}, 0) = 0
openat(AT_FDCWD, "/", O_RDONLY|O_NOCTTY) = 3
close(3)                                = 0
openat(AT_FDCWD, "/proc/self/mountinfo", O_RDONLY) = 3
newfstatat(3, "", {st_mode=S_IFREG|0444, st_size=0, ...}, AT_EMPTY_PATH) = 0
lseek(3, 0, SEEK_CUR)                   = 2531
close(3)                                = 0
ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0
newfstatat(AT_FDCWD, "/", {st_mode=S_IFDIR|0555, st_size=4096, ...}, 0) = 0
uname({sysname="Linux", nodename="localhost.localdomain", ...}) = 0
statfs("/", {f_type=EXT2_SUPER_MAGIC, f_bsize=4096, f_blocks=5160596, f_bfree=3455549, f_bavail=3189309, f_files=1310720, f_ffree=1195827, f_fsid={val=[0xb5e732b, 0xb5ec4eab]}, f_namelen=255, f_frsize=4096, f_flags=ST_VALID|ST_NOATIME}) = 0
newfstatat(1, "", {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0x1), ...}, AT_EMPTY_PATH) = 0

man statfs會給你更多的資訊。

TLDR:它從核心獲取它,核心從 VFS 獲取它,VFS 從底層 FS 獲取它。

引用自:https://unix.stackexchange.com/questions/684305