Linux – Mounting USB disks automatically (How it works)

automountinglinux

Background:
I am trying to mount a usb disk as read only but my ubuntu install is mounting it
rw when I plug the disk in.

I can unmount the disk manually and remount it manually as read only with the umount and mount commands but thats no fun. Could someone give me a quick explanation on how exactly usb mounts are automatically done on a typical linux system (udev? historical background is nice too) and maybe how I can tweak this process into letting me read the disk ro?

Thanks.

Edit: I'm using gnome if that helps at all.
Edit2: In my haste I forgot to provide a bit more information. This is what the disk looks like from the output of 'mount'.

/dev/sdb1 on /media/LaCie type fuseblk (rw,nosuid,nodev,allow_other,default_permissions,blksize=4096)

Edit3:
This also may be relavent in its own way. In the mount output I also have the following:

gvfs-fuse-daemon on /home/fletcher/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=fletcher)  

I thought this might have been related to the above fuseblk mount, but what I found out was this.

Gvfs is the Gnome virtual file system. It is a virtual filesystem built on top of the already existing kernel vfs. gvfs uses the GIO library (which is a VFS API) to access files, devices, remote network locations, etc. In this case above ('gvfs-fuse-daemon') gvfs is using FUSE to mount files/locations/devices. This is essentially what happens when you mount a remote network connection in Nautilus. It will use FUSE to mount the location (inside?) the .gvfs directory, and then it will communicate with the gnome virtual file system layer to communicate with the new mount.

Basically this structure allows the user to dynamically mount new locations and interact with them through nautilus.

Just for reference: FUSE is a userspace filesystem, aka it allows the user to run mount even when that user is not root)

So where does that leave me? Well the LaCie disk is being mounted with type fuseblk. This is just a block device mounted with fuse. So some daemon has autodetected the drive when it was plugged in and then gone ahead and run fuse to mount my block device. So what daemon is this, and how is it configured (my
guess is it is some internal gnome thing) is the most important question. A secondary question is how the system automatically detected a newly inserted usb disk, but I think thats a bit of an aside and much lower level here (read: udev?).

Links:

Best Answer

I tried to do this on my computer and it's work :)

First I get a name for my device :

ls -l /proc/disk/by-id/

In my case it is /proc/disk/by-id/usb-09a6_8001

I added this line in /etc/fstab :

/dev/disk/by-id/usb-09a6_8001   /media/macle ext2 ro,users 0 2

And it's working, when I plug my usbkey, it's mounted ro and owned by my user.

Related Question