I’m going to start by asking, why do you want to do this? I ask because sometimes people ask how to achieve something which restricts a user’s ability to do something and when someone asks them why they want to do that, their answer reveals some flaw in their plan or that it’s just not really worth the hassle.
I’m fairly sure it’s possible to have a USB flash drive mount read-only for everyone, though I’ve not been able to figure out exactly how as yet. There’s two pieces to the puzzle I think - you need to give the user read-only and you also need to make sure they don’t own the device node or mount point. By default if you plug a USB drive in it gets mounted at /media/something and /media/something is owned by the currently logged in user. So even if you changed the permissions of /media/something to 444 the user owns it so could just use chmod to add write permission for themselves.
Exactly how devices get mounted is a bit tangled and opaque. You plug the device in and udev creates device nodes for it. Then hal creates mount a mount point and sets the ownership and permissions. If you’re running GNOME, those ownerships and permissions are controlled, at least in part by gvfs-hal-volume-monitor.
You can make udev do stuff with the device nodes by adding files in to /lib/udev/rules.d/ For example I’ve put a file in there that looks like this:
[CODE]
makes group ownership of USB and firewire drives cdrom
users are added to cdrom group by pam upon gdm login
setting this group ownership means users can use fdisk to re-partition
their usb drives if they want and also allows them to format them.
SUBSYSTEMS==“usb|firewire”, KERNEL==“sd?*”, GROUP=“cdrom”[/CODE]
You can’t use udev to control the mount point ownership or permissions though. (I tried, when the udev rules are processed the mount point does not yet exist.)
Assuming you’re using GNOME, if you open gconf-editor and look under /system/storage/default_options/vfat you’ll see various options. If you remove the uid= option and change the umask option to umask=222 then plug in a in a USB flash drive formatted as vfat you’ll find the mount point owner is root and permissions on the mountpoint are dr-xr-xr-x So now you have a USB flash drive mounted, the logged in user only does not have write permissions and they don’t own the mount point so they can’t give themselves write permissions. If you enforce those gconf settings as mandatory then you have the scenario you want, albeit only for vfat filesystems. To get around this the user logs in with KDE.
gvfs-hal-volume-monitor is passing options for ownership and permissions to hal. So I think you’d need to figure out how to configure hal to set the desired ownership and permissions independently for all USB drives. You may possibly also need to prevent gvfs-hal-volume-monitor and any KDE equivalent running when people log in, I don’t know. hal rules are in /usr/share/hal/fdi/policy
I will be interested to see if anyone has a solution, if only for the technical interest.