有谁知道 df 命令如何知道可用磁盘空间?
它是否使用内核调用,如果是,是哪些?
或者它使用/dev 或/proc 中的虚拟文件?
答案1
你可以检查它的源代码,您可以运行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获取它。