简体   繁体   中英

Linux: How do I determine if a mounted vfat volume is removed?

I have a system (running under CentOS 6.3) that likes to archive data to flash drives.

We have a UI that only tries to mount the drive when the user is trying to archive the data - the user selects the archive function, and we mount the drive (if any) and check it's stats. If it's no good (drive smaller than data set, for instance), we prompt the user to try another drive.

Now, what I'm dealing with is that I don't want to keep unmounting and remounting the volume, so I just leave it mounted. But then, when the user does pull it out, I have NO clue how to tell it's gone!

I've got a loop polling the drive, and I'm looking at things like statvfs64() (which keeps on acting as if the drive is there). stat() also doesn't change when the drive is physically removed.

I tried having an open directory ( opendir() ) and reading from that, but that doesn't fail either (if the drive had any files on it, those no longer show up, but of course, the drive may well be empty to start with, so I can't go by that).

I don't want to repeatedly unmount and remount the drive, as the user might have plugged in something like an NTFS drive, which seems to take a bit of time to mount.

The mount may be read-only, so attempting to write to the filesystem in question is not something I can do.

So far the only thing I've figured out is that I can check the mount device and see if it still exists (when I pull the flash key, the /dev/sd* entry goes away). It seems to me that there's a better way to do this, but I haven't been able to figure it out yet.

Even if the /dev/sd* exists, that doesn't mean the drive is mounted, only present on the system. Maybe you could try parsing /proc/mounts file, and see if you find it there.

OK, what I've ended up doing is to periodically check that the device still exists. So if I've got /dev/sdb1 mounted on /mnt/usbdrive, I periodically check that /dev/sdb1 still exists. If it doesn't, I unmount and go back to looking.

This is not optimal, but it does work, so it's the best I can do for now, especially for read-only mounts of empty drives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM