3

When I umount a SD flash card in a USB card reader, and then pull the card, filesystem stays "dirty".

System:

  • RPi or xubuntu 16.04.3
  • The card reader is some super cheap Chinese one. I have tried a few different ones
  • I have tried a bunch of different SD cards.

How to reproduce:

  1. connect card reader
  2. insert SD card with vfat on the first partition
  3. wait for system to detect SD card
  4. wait for system to automount filesystem or mount it manually
  5. update a random file, I do: date >> /media/mogul/2E3E-AE54/d
  6. un-mount: sudo umount /dev/sdd1
  7. (placeholder, do nothing here, yet)
  8. pull sd card from card reader

Now repeat from step 2. Keep an eye on your dmesg, it will say:

[357207.805594] FAT-fs (sdd1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.

(newer linux's support dmesg -w)

Now, if I add an additional action after the umount

  1. read a random byte on the SD card, like: dd if=/dev/sdd1 skip=1000000 ibs=1 count=1 of=/dev/null

the filesystem seems to survive.

This seems a bit hackish to me, am I missing something fundamental?

Do you have more elegant solutions?

I prefer not to use eject, but only umount, sinceeject` powers down the card reader too; the system won’t detect a new SD card before I re-plug the card reader.

  • Currently I'm using fdisk -l /dev/sdd, look a little less odd in the surrounding program – mogul Dec 19 '17 at 18:09
4

As your step 7, try the following:

echo 1 | sudo tee /sys/block/sdd/device/delete

or if you're running as root, just

echo 1 > /sys/block/sdd/device/delete

This signals the kernel that device /dev/sdd is about to be removed, and should trigger a controlled flushing of any remaining write buffers to the card, to avoid the filesystem corruption.

This may cause the reader to power down similar to the the eject command; if it does, an alternative way would be to just flush the buffers without the implication of an imminent device removal. This can be achieved with the blockdev command:

sudo blockdev --flushbufs /dev/sdd

If this does not help, then I'm afraid the card reader might not support hot-unplugging the card. This is possible with cheap readers. The only safe way to use such a reader could then be to first unplug the reader from the USB port, and only then remove the card from the reader.

  • As "expected" signalling delete to the kernel did the same as eject, removing /dev/sdd. The blockdev --flushbufs did not fix the problem either. – mogul Dec 19 '17 at 18:05
  • Then it appears the card reader cannot handle hot-unplugging the card at all. Added that to my answer. – telcoM Dec 21 '17 at 13:20
  • And you are correct. Apparently my collection of card readers was too cheap. Tried a few built-in ones in laptops, they all performed much better. No need for weird reads after the umount – mogul Dec 22 '17 at 15:44
0

Issuing the command sync at step 7 will write the buffer cache to the SD card and will result in a clean file system.

  • nope, sync was one of my first attempts. didn't work – mogul Dec 22 '17 at 15:40

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.