Re: du measures in 4K blocks resulting in inaccuracies

From: Greg 'groggy' Lehey <grog_at_freebsd.org>
Date: Thu, 07 Nov 2024 01:24:36 UTC
On Thursday,  7 November 2024 at 12:01:36 +1100, Dewayne Geraghty wrote:
> I'm trying to size a disk.  Unfortunately /usr/bin/du is misleading.  An
> example referencing two files, one 60 Bytes, other 908 Bytes:
>
> unset BLOCKSIZE
> echo ;         ls -l /projectx/adm/README /projectx/adm/gen_pw.sh
> echo "1 ---" ; ls -lh /projectx/adm/README
> echo "2 ---" ; du /projectx/adm/README
> echo "3 ---" ; du -ckh /projectx/adm/README /projectx/adm/gen_pw.sh
>
> -rw-r-----  1 sysman  wheel   60 Jul 28  2023 /projectx/adm/README  #60B
> -rwx------  1 sysman  wheel  968 Jul 28  2023 /projectx/adm/gen_pw.sh
> 1 ---
> -rw-r-----  1 sysman  wheel    60B Jul 28  2023 /projectx/adm/README
> 2 ---
> 8       /projectx/adm/README    <<< 8 sectors
> 3 ---
> 4.0K    /projectx/adm/README    <<< min count is 4K, so sectorsize?
> 4.0K    /projectx/adm/gen_pw.sh
> 8.0K    total			<<< Expect at most 2K
>
> # diskinfo -v /dev/ada2p3
> /dev/ada2p3
>         512             # sectorsize
>
>
> Perhaps my understanding is wrong, so to authority "man du"
> -k      Display block counts in 1024-byte (1 kiB) blocks.  (incorrect)

What is incorrect about that?

> -h      "Human-readable" output.  Use unit suffixes: Byte, Kilobyte,
>           Megabyte, Gigabyte, Terabyte and Petabyte based on powers of
>           1024.    (?  4K)
>
> 4.1k    /projectx/adm/README   # <<< 4.1K??!
> 4.1k    /projectx/adm/gen_pw.sh
>
> What am I missing?  Should the doc reflect the minimum reporting size is 4K?

No, du is reporting correctly.  From the man page:

   The du utility displays the file system block usage for each file
   argument

It doesn't describe the size of the files in those blocks.  That's
particularly the case for files with holes in them, where it can show
"sizes" that are much less than the file size.

You don't say what file system you're using, but it looks like UFS.
By default, a UFS 2 file system has 32 kB blocks and 4 kB fragments.
Files less than 4 kB in size allocate one fragment, and that's what
you're seeing.

The real question is: what are you trying to do?  "Size a disk"
suggests that you really do want to know how much storage is being
used.  In this case, du is your friend.  Your 60 byte file really does
use 4 kB on disk.

Greg
--
When replying to this message, please copy the original recipients.
If you don't, I may ignore the reply or reply to the original recipients.
For more information, see http://www.lemis.com/questions.html
Sent from my desktop computer.
See complete headers for address and phone numbers.
This message is digitally signed.  If your Microsoft mail program
reports problems, please read http://lemis.com/broken-MUA.php