How to poll for USB devices available to mount (with auto-mounting disabled)?
Linux - HardwareThis forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
How to poll for USB devices available to mount (with auto-mounting disabled)?
I don't want any old USB thumb drive mounting to my system therefore I'm trying to create a script that controls the mounting of USB devices and the permissions given to them. These permissions depend on other variables (eg. am I connected to a network?) therefore I don't think I can just add a device to a list of approved devices for mounting and block everything else. I need some dynamic way of doing it.
I've disabled the auto-mounting of removable/hot-pluggable drives, I now need some way of polling for the insertion of a USB device. Any ideas what I need to poll?
Whether a local user or remote user can mount a usb drive is usually handled by PolicyKit.
Since you disabled automounting, you might consider manually mounting usb drives instead with entries in /etc/fstab determining the permissions and ownership. The first field in /etc/fstab can use UUID=<filesystem_uid> instead of the device node. This is recommended anyway for removable drives because you can't depend on which device node it will be assigned to.
Also, for removable drives, be sure to use the "noauto" option.
For fat32 filesystems, which is the most common for pendrives, you determine the permissions and ownership in the mount command. So you can have an /etc/fstab entry for each pendrive you own and use different permissions or ownership. Using the "user" or "users" option, you can even mount the pendrive as a normal user. The permissions you use could mean that only you have permission to enter the mounted directory.
I'd really like to be able to run the polling and mounting automatically using a daemon. I think that fdisk -l will give me a list of all devices connected (but not necessarily mounted) so I could run that every few seconds from a script to check if anything has been inserted into a USB socket, but this seems a bit clumsy.
I suppose I could dig through the source for fdisk and find out what it uses but I thought there might perhaps be an easier way?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.