A comment on another question made me wonder:

Assuming I redirect the output from a command to a block device, and the length of that output is less than the block size of the block device:

% blockdev --getbsz /dev/sda

% head -c 100 /dev/zero > /dev/sda

Will that overwrite the first 100 bytes of /dev/sda, or the first 4096 bytes (i.e. one complete block)?
Does the behaviour depend on the shell, output buffering settings, or even the *nix flavour (which might use character devices instead of block devices, like *BSD)?

  • Are you mixing blocks and sectors? – Rui F Ribeiro Nov 25 '18 at 18:35

If you write to a block device using some size N that is smaller than the underlying block size, then I'd expect the OS to (1) read a full block from the device, (2) update the first N blocks (assuming a zero offset) with what you're writing, and (3) re-write the entire block to the device. The end result would be that it would appear to have written the first N bytes.

  • 1
    Why should the fs read the block(s) (unless the fd is open read-write)? – RudiC Nov 25 '18 at 23:18
  • @RudiC Because a block device, as the name implies, operates on "blocks" -- a "block" is the smallest unit that you can read/write. If you want to modify less than an entire block, you read the block, modify the applicable portion, then re-write the block. – Andy Dalton Nov 26 '18 at 15:34
  • Not if the redirector is >, for example. Or any other "write only" ("create") opening, I'd guess. – RudiC Nov 26 '18 at 15:59
  • @RudiC Any non-block-size, non-block-aligned write – Andy Dalton Nov 26 '18 at 16:35

According to my experience with dd, at least some software writes only the specified number of bytes (and not complete blocks). For example, it is possible to write to the very head end of a drive to modify the BIOS boot sector (MBR) and the partition table.

But it is possible that some other software is made to write whole blocks.

I suggest that you test how a particular program behaves (or ask for help to test it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.