I have a flash drive formatted with a FAT32 partition. When I pull it out before unmounting it naturally has the dirty bit set and when I use the flash in a Windows machine, Windows complains that the drive should be repaired.
The Linux machine is an embedded device and has no "unmount" in its GUI. But I have SSH access to this machine and I tried to use the command below to clear the dirty bit:
root@system:~# fsck.fat -aw /dev/sda1
fsck.fat 4.1 (2017-01-24)
0x41: Dirty bit is set. Fs was not properly unmounted and some data may be corrupt.
Automatically removing dirty bit.
Performing changes.
/dev/sda1: 4 files, 4/261376 clusters
Then I remove the drive (still no unmount) and when I plug it back in a Windows system it still shows the drive should repaired message.
So the question is, why fsck does not actually clear the dirty bit?
Is there any way to prevent or clear the drty bit so plugging out the drive without a proper unmount doesnt trigger the dirty bit?
The reason: I want to have a script or service to perform fsck
to clear dirty bit as soon as a drive is mounted. I mean I want the device not to set the dirty bit at all or clear it as soon as a drive is inserted. Because the user has no way of asking the system to perform the unmount
umount /dev/sda1
umount
, you could trymount -o remount,ro /dev/sda1
to remount it read-only. This might make the kernel write a clean flag, and you can then safely remove the drive.