1

What size blocks does GNU truncate --io-blocks use?

      -o, --io-blocks
             treat SIZE as number of IO blocks instead of bytes
  • 512 bytes
  • blockdev --getbsz
  • blockdev --getpbsz

1 Answer 1

1

None of the above.

The blocksize is related to the filesystem parameters chosen at mkfs time, and can be found by running stat() against the file. It has no relation to the underlying block device the file system is stored on (if any).

For example, with GNU stat:

$ /usr/bin/stat . | grep IO.Block       
  Size: 71680           Blocks: 144        IO Block: 2048   directory

If you prefer a more programmatic view, the stat() system call can be made in perl with:

$ perl -e '@x=stat("."); print $x[11]'
2048

In both cases we get "2048" as the answer for this filesystem.

We can verify this:

$ truncate -o -s 1 foo      
$ ls -l foo
-rw-r--r-- 1 sweh sweh 2048 Sep 17 10:28 foo

Different filesystems can have different block sizes. eg on my machine I made the /news disk use smaller block sizes because it stores mostly smaller files

$ perl -e '@x=stat("/"); print $x[11]'
4096

$ perl -e '@x=stat("/news"); print $x[11]'
2048

For Linux extx file systems, this is done with mke2fs with the -b flag:

   -b block-size
          Specify  the  size  of blocks in bytes.  Valid block-size values
          are 1024, 2048 and 4096 bytes per block.  If omitted, block-size
          is  heuristically  determined  by  the  filesystem  size and the
          expected usage of the filesystem (see the -T option).  If block-
          size  is preceded by a negative sign ('-'), then mke2fs will use
          heuristics to determine the appropriate  block  size,  with  the
          constraint  that  the  block  size  will  be at least block-size
          bytes.  This  is  useful  for  certain  hardware  devices  which
          require that the blocksize be a multiple of 2k.
3
  • 1
    Also keep in mind that the block size of a NFS mounted share here relates to the rsize mount option, nevertheless the block size of the underlaying filesystem.
    – Thomas
    Sep 17, 2017 at 14:36
  • @Thomas yes, there are many ways the blocksize can change, which is why running stat() against the file (which is what the code does) is required to get the right answer. And it should, really, be against the file and not the directory, because you can get different results there, as well. Sep 17, 2017 at 14:42
  • 1
    It's unclear why truncate has that --io-block in the first place though (FreeBSD where that utility first came from in 2000) doesn't have it, nor why it's not implemented as a suffix for the size (like truncate -s1B). It's been there from the start apparently. I can't imagine it being a common use case. Sep 17, 2017 at 21:04

You must log in to answer this question.

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