1

Does anyone know how df command knows the free disk space?

Does it use kernal calls and if yes which ones?

Or does it use virtual files in /dev or /proc?

1 Answer 1

4

You can check its source code, you can run strace which is faster but not always easy to parse, e.g.

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 will give you more info.

TLDR: it gets it from the kernel, the kernel gets it from VFS, VFS gets it from the underlying FS.

1
  • I check the source code, but the strace is a good idea. In this case much, much faster than the source check :)
    – K-attila-
    Commented Dec 29, 2021 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .