Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:
Gvfs: http://fedoraproject.org/wiki/Features/Gvfs
gvfs-fuse-daemon: http://old.nabble.com/What-is-gvfs-fuse-daemon--td19458605.html
FUSE: http://www.mjmwired.net/kernel/Documentation/filesystems/fuse.txt
GIO: http://library.gnome.org/devel/gio/unstable/ch01.html

share|improve this question
    
is there an fstab entry for the /media/LaCie file system and/or the /dev/sdb1 device? Is /dev/sdb1 labelled in anyway (e2label /dev/sdb1)? –  Tok Feb 25 '11 at 19:00
    
Nope there is no fstab entry. I believe it is labeled as it gets mounted at /media/LaCie. I'd need to install ntfslabel for sure to find out but I'm just about to leave work. It also comes up on my desktop as LaCie. –  fthinker Feb 25 '11 at 23:13
    
add comment

4 Answers

On the topic of how the kernel detects hardware, here is a very nice article what happens.

In summary:

  • the kernel constantly scans your PC bus'es and maps devices to the virtual file system, typically under /sys/.
  • the kernel sends a message that new hardware is available to udev, which:
    • process udev rules on the device
    • symlink the device into /dev/
    • load device drivers
    • notify user space of the device via dbus

At this point the device is ready to be used by userspace. Gvfs and FUSE are both userspace filesystems.

share|improve this answer
    
No, udev typically doesn't notify applications via dbus this is done by third party applications. If you use qdbus --system you won't see any udev names. Typically applicaitons just subscribes themself to uevents or ship their own rules –  Ulrich Dangel Jul 31 '12 at 17:47
    
Thanks for pointing that out @ulrich. My summary gives a very basic outline excluding many in-between steps. See the linked article for gritty details. –  invert Aug 1 '12 at 9:03
    
the article is also false as there is normally no polling involved to monitor the buses –  Ulrich Dangel Aug 1 '12 at 12:37
add comment

As Ulrich Dangel said, udisks should be the reference nowadays. I've yet to see how it is notified (udev->dbus->udisks?), but manually it can be used to mount a device with the options you need.

Udisks talks to polkit to see what a user is allowed to do with volumes. I'm guessing here, but if we could find a place to set properties for a named disk/device it should work.

Using udisksctl, your user should be able to mount the device with the options you need, something like this:

udisksctl mount -b /dev/sdxy -o ro /mountpoint

Yet I don't where to place the options.

UPDATE: Following Ulrich suggestions, I went to Gnome disk utility and I have achieved what you need but not sure whether it's the most elegant solution or not. I'm using Kororaa 17 (Fedora 17 based) with Cinnamon+Gnome3, went to "Disks" utility and modified mount options for my usb drive, added "ro" and saved.

It adds an entry to fstab (that's where I don't see it as an elegant solution, I think it should remain as a user configuration not system wide), I plug in the drive and it's mounted automatically on the selected mount point with options I saved at Gnome disks utility.

share|improve this answer
    
What do you mean with you have to see how it is notified? What step is missing? –  Ulrich Dangel Jul 30 '12 at 5:13
    
And it is not udev→dbus→udisk but udev/uevent→udisk→dbus –  Ulrich Dangel Jul 30 '12 at 5:25
    
I meant I wasn't really sure and I wanted to verify it but now I see I wasn't 100% wrong. –  tripledes Jul 30 '12 at 8:43
    
[strike]Please show the line that was added into fstab. I'd like to confirm what I think it is.[/strike] So the fstab line matches that of @profy's answer? i.e. with the ro option. –  invert Aug 1 '12 at 9:10
add comment

Nowadays the default solutions is as far as i know udisk an daemon providing an dbus api as well as an commandline interface. There are also multiple udisks wrapper and automounter available.

Typically you don't configure udisk itself as it just does what it is told so you have to look into Gnome. Maybe the Gnome Disk Utility allows you to modify/change the mount flags itself.

share|improve this answer
add comment

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.

share|improve this answer
    
Thanks profy :) But the main thrust of my question is to alleviate the extreme frustration I feel every time I am forced to understand how automount and usb devices works. I am here to decipher the magic that is behind these complex systems. Besides, using fstab and usb devices is a bit tricky because they aren't always guaranteed to be plugged in on start up! –  fthinker Feb 25 '11 at 23:17
1  
I don't know how it's work, but I'm pretty sure that new devices is detected by an udev rules, take a look at /etc/udev/rules.d/ and at /lib/udev/rules.d/. –  profy Feb 26 '11 at 11:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.