4

For some debugging purposes, I'd like to be able to manually set the dirty bit of a FAT32 partition to true.

I found tons of information on how to use fsck.vfat to remove the dirty bit, but none on how to set it.

It's possible, since mount does it. When a FAT32 partition (where dirty is false) is mounted, then mount sets dirty to true (and umount sets it to false again). I'm looking for a way to set the dirty bit without mounting the partition, i.e., simulating that it was not cleanly unmounted.

Malte Skoruppa
  • 1,694
  • 1
  • 14
  • 20

1 Answers1

6

The dirty bit is set and cleared in the kernel, when mounting and unmounting a device; see http://lxr.free-electrons.com/source/fs/fat/inode.c?v=3.19#L578 for the implementation. There's no way currently to access this function outside the kernel, except by mounting and unmounting...

To set it yourself, you'd need to tweak the device directly; the state byte is at offset 0x25 in the boot sector on FAT16 devices, and offset 0x41 on FAT32 devices; the dirty bit is bit 0. See https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system for details.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • Is this why fsck always reports my mounted FAT32 partition to be dirty? Because its currently mounted, therefore it's dirty? What would happen if I "repair" it and set it to non-dirty while the partition is still mounted? – CMCDragonkai Sep 01 '16 at 15:23
  • Yes, exactly; it behaves that way so that the filesystem will be identified as dirty if the system crashes (or loses power...). [You shouldn't ever repair a mounted partition](http://serverfault.com/questions/29889/why-cant-you-fsck-a-mounted-partition). I'm guessing if you did try, and `fsck.fat` allowed you to do it, then the dirty flag would remain cleared — it's only set when the kernel mounts the partition, there's nothing to check it continuously. (Presumably if an actual error occurs at runtime then it will be set again; I haven't checked.) – Stephen Kitt Sep 01 '16 at 15:49