Subject: bug#34143: [coreutils 8.28] du -x is reporting a lower disk usage for /mnt when partitions are mounted

From: Assaf Gordon <[email-masked]>
Date: Sun, 20 Jan 2019 03:30:14 -0700

tags 34143 notabug
close 34143
stop

Hello,

On 2019-01-19 3:11 p.m., Joseph Paul wrote:
>     It may not be a bug at all, but I was surprised to find out that 'du
>     -x' is reporting a lower disk usage on /mnt when partitions are
>     mounted.

This is not a bug.

Technically, as you wrote below, du simply skips (and does not count)
any directory that is not on the same filesystem.

[...]
>     linux$ du -x /mnt
>     4    /mnt/data
>     4    /mnt/VL1800
>     4    /mnt/nfs/nas
>     8    /mnt/nfs
>     20    /mnt
> 
>     /mnt is now bigger.
> 
>     Is this a normal result, because even when mounted, physically, the
>     directories '/mnt/VL1800' and '/mnt/data' still  exist on the '/'
>     filesystem, or not ?
>     Shouldn't they still occupy 4Kb of disk space each on the '/'
>     filesystem when partitions are mounted ?

They do occupy as much disk space as before,
but du has no way to know how much they occupy,
because the kernel reports that they are on a different device
and you requested -x/--one-file-system.

We can even take it a step further, and mount a new filesyetem
on a non-empty directory - all the directory's content won't be counted:

As root, create the directory structure:

     cd /tmp
     mkdir -p a a/b a/c a/d

Now fill the "b" directory with a large file:

     dd if=/dev/zero of=a/b/bigfile bs=1M count=1

Before any mounts, "b" is counted:

     # du -x a
     4     a/c
     1028  a/b
     4     a/d
     1040  a

Now create a temporary file system loop file, and mount it over "b":

     dd if=/dev/zero of=disk.img bs=1M count=10
     mkfs.ext3 disk.img
     mount -o loop disk.img a/b

Re-checking disk-usage, "b" is not even listed,
and its content (1MB) is not counted:

    # du -x a
    4   a/c
    4   a/d
    12  a

---

To see why du skips it, you can check the Device-ID associated with each 
directory:

    # stat -c "%n   Device-ID: %D   Mount-Point: %m" a a/b a/c a/d
    a   Device-ID: 812   Mount-Point: /tmp
    a/b   Device-ID: 700   Mount-Point: /tmp/a/b
    a/c   Device-ID: 812   Mount-Point: /tmp
    a/d   Device-ID: 812   Mount-Point: /tmp

Your device numbers will differ, but the number for "a/b" will not be
the same as for the rest.

When du sees a different device number, it simply skips the directory.

Once unmounted, the device-id returns to the old value,
and "a/b" will be counted with its content:

    # umount a/b
    # stat -c "%n   Device-ID: %D   Mount-Point: %m" a a/b a/c a/d
    a   Device-ID: 812   Mount-Point: /tmp
    a/b   Device-ID: 812   Mount-Point: /tmp
    a/c   Device-ID: 812   Mount-Point: /tmp
    a/d   Device-ID: 812   Mount-Point: /tmp

As such, I'm closing this as "not a bug",
but discussion can continue by replying to this thread.

regards,
  - assaf