why df get result inconsistent with lsblk?

10,640

Solution 1

There's actually two problems. The first is the obvious one that others have pointed out: lsblk lists disk by device and df works on mounted filesystems. So lsblk /dev/sda3 is roughly equivalent to df -h / in your case since /dev/sda3 is mounted on /.

Except that it's not. Because lsblk lists the size of the partition while df lists the size of the filesystem. The difference (93.1GB vs 92GB for sda3 in your example) is a combination of unusable space (if any) and filesystem overhead. Some amount of space needs to go to keeping track of the filesystem itself rather than the contents of the files it stores.

Solution 2

The partitions /dev/sda4 etc. are not mounted. Since they are not mounted, df reports the size of the filesystem in which are located the special files /dev/sda4 etc. themselves; this is a small memory-based filesystem mounted on /dev. Use sudo file -s /dev/sda4 etc. to find out what filesystems, if any, are present on those partitions. Mount them somewhere if you want to use df to find out how much space is used or available.

An example:

$ df /dev/sdb1            # /dev/sdb1 is not currently mounted
Filesystem     1K-blocks  Used Available Use% Mounted on
udev              234040     0    234040   0% /dev

$ sudo mount /dev/sdb1 /mnt
$ df /dev/sdb1             # Now it is mounted and df can show used and free space
Filesystem     1K-blocks   Used Available Use% Mounted on
/dev/sdb1       60787696 498464  60289232   1% /mnt

Solution 3

This is because df only shows info on mounted filesystems. sda1 and sda3 are mounted, so they show the right info.

Share:
10,640

Related videos on Youtube

showkey
Author by

showkey

contact me at [email protected]

Updated on September 18, 2022

Comments

  • showkey
    showkey almost 2 years

    enter image description here

    The output yielded by df consistent with lsblk

    debian8@hwy:~$ df -h   /dev/sda1
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda1        47G   34G   14G  72% /media/xp_c
    debian8@hwy:~$ df -h   /dev/sda3
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        92G   36G   52G  42% /
    

    The output yielded by df inconsistent with lsblk

    debian8@hwy:~$ df -h   /dev/sda4
    Filesystem      Size  Used Avail Use% Mounted on
    udev             10M     0   10M   0% /dev
    debian8@hwy:~$ df -h   /dev/sda5
    Filesystem      Size  Used Avail Use% Mounted on
    udev             10M     0   10M   0% /dev
    debian8@hwy:~$ df -h   /dev/sda6
    Filesystem      Size  Used Avail Use% Mounted on
    udev             10M     0   10M   0% /dev
    debian8@hwy:~$ df -h   /dev/sda7
    Filesystem      Size  Used Avail Use% Mounted on
    udev             10M     0   10M   0% /dev
    

    How to explain the output of lsblk and df -h?
    Sometime df can't get right info about disk.

    sudo fdisk -l 
    
    Disk /dev/sda: 232.9 GiB, 250059350016 bytes, 488397168 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0x3b2662b1
    
    Device     Boot     Start       End   Sectors  Size Id Type
    /dev/sda1  *         2048  97851391  97849344 46.7G  7 HPFS/NTFS/exFAT
    /dev/sda2        97851392 195508223  97656832 46.6G 83 Linux
    /dev/sda3       195508224 390819839 195311616 93.1G 83 Linux
    /dev/sda4       390821886 449411071  58589186   28G  5 Extended
    /dev/sda5       390821888 400584703   9762816  4.7G 82 Linux swap / Solaris
    /dev/sda6       400586752 439646207  39059456 18.6G  b W95 FAT32
    /dev/sda7       439648256 449411071   9762816  4.7G  7 HPFS/NTFS/exFAT