I'm on a cubie board (a single-board computer, like a Raspberry Pi) running a Debian derivative called cubian

Linux Cubian 3.4.79-sun4i #1 PREEMPT Wed Feb 25 08:50:31 UTC 2015 armv7l GNU/Linux

USB drives are automatically mounted to a subdirectory of /media via udevil.

I'm trying to manage this as a kiosk (user has no access to a windowing system, etc) so I need to respond to situations programmatically. Recently during testing, my code threw an error indicating that there was more than one directory in /media. I ssh'd in, and, indeed, there were two directories there. I said to myself "yeah, but one is just the stale remainder of a not-properly-unmounted drive, so the directory will be empty [I had seen that case before], and I just need to add code to check for that".

To my surprise, the directory was not empty, and not only was I able to get a listing, I was able to open and read the contents of a file. This question describes a similar situation.

I assume this is some sort of cache that I'm accessing. Is there a way that I can programmatically detect this situation?

I'm not worried about cleaning up the directories as much as I am knowing which one actually corresponds to a physically mounted drive.

Also, unfortunately, I don't know how to reproduce this--I tried killing udevil and mounting it manually with mount and then pulling it out, and the files disappeared.

If there were a reliable way to reproduce it, I could test some things (like seeing if a touch command fails but doesn't hang, maybe?), so that would also be a helpful answer.

  • 2
    Yes, very probably disk-cache: If some process has a current directory or open files on some mounted volume, umount will delay, and everything cached will remain accessible. You need to kill or end those processes. If it happens again, check current mounts with mount. You can catch USB removal with udevadm monitor, and use lsof or fuser to find processes that make this mount "busy". – dirkt Mar 17 '17 at 16:48
  • Thanks @dirkt . Also, whoever downvoted, please explain the issue with the question. – msouth Mar 17 '17 at 20:33

The only way this could be happening is if there was something written to the file while the drive was unmounted. If there is nothing mounted the Linux system will see the folders in /media as regular folders with regular files in it.

In Linux it is perfectly possible to use one directory as a normal directory and the mount point for disks or network drives, one at a time.


the caching side-effect mentioned in the other post can be solved by doing $ sync before unmounting. What they describe is the cache being flushed after the unmount, with the effect that the file is written to the normal folder, like I described before.

Please don't down-vote without explaining why.

  • 1
    Would like to echo the answerer's request that you be respectful of others' efforts and refrain from downvoting without an explanation. It discourages contribution and prevents a learning experience that everyone could benefit from--if you see a problem with the answer, explain it. – msouth Mar 17 '17 at 20:16

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.