Skip to contentSkip to conversation

udev rule for unmount action

DISCUSSION Christian Schmitt 4 participants 7 messages Jun 6, 2009 – Jun 10, 2009

lore
Share & tools
lore
#0 lore raw

Hi,

I am trying to write a udev rule for my external HD that needs some special "care" when being unmounted (see here:
http://elliotli.blogspot.com/2009/01/safely-remove-usb-hard-drive-in-
linux.html).
The problem is that I can not find a way to make udev react on the unmount command and execute a script. The "ACTION=remove" rule only works when I unplug the device. Is there a way to execute a script when unmounting? I guess doing this on the DE level is not the best way to go.

Cheers,
Chris

#1 lore raw
Quoted 2 lines · Christian Schmitt

unplug the device. Is there a way to execute a script when unmounting? I guess doing this on the DE level is not the best way to go.

The approved method is using inotify(2) on /proc/mounts, which is not related to udev.

Signature · 2 lines
ciao,
Marco
#2 lore raw
In reply to Christian Schmitt
Quoted 2 lines · On Saturday 06 June 2009 18:27:26 Marco d'Itri wrote:

The approved method is using inotify(2) on /proc/mounts, which is not related to udev.

Thanks for this hint. As I am not too familiar with how to best implement it, can you give me a direction or an example for it? Is something like this already available somewhere? I did not find it.

Thanks
Chris

#3 lore raw
In reply to Christian Schmitt
Quoted 6 lines · Marco d'Itri
Quoted 2 lines · Christian Schmitt

unplug the device. Is there a way to execute a script when unmounting? I guess doing this on the DE level is not the best way to go.

The approved method is using inotify(2) on /proc/mounts, which is not related to udev.

/proc and inotify are not good friends, but you can use poll() / select()

Karel

Signature · 1 line
 Karel Zak  <[email protected]>
#4 lore raw
In reply to Christian Schmitt
Quoted 4 lines · On Monday 08 June 2009 14:38:26 Karel Zak wrote:

/proc and inotify are not good friends, but you can use poll() / select()

Karel

Sorry, but I do not want to write a C program, but a script. So I'd need a way to accomplish the detection of an unmount event in the script. As I'm using HAL there should be a way to do it without using inotify manually on /proc/mounts.

Chris

#5 lore raw
In reply to Christian Schmitt

Hi,

Quoted 6 lines · Christian Schmitt

Hi,

I am trying to write a udev rule for my external HD that needs some special "care" when being unmounted (see here:
http://elliotli.blogspot.com/2009/01/safely-remove-usb-hard-drive-in-
linux.html).

That's a very interesting blog entry, thanks for sharing. My experience dealing with the storage stack in GNOME is that people keep asking for this feature.

Quoted 4 lines

The problem is that I can not find a way to make udev react on the unmount command and execute a script. The "ACTION=remove" rule only works when I unplug the device. Is there a way to execute a script when unmounting? I guess doing this on the DE level is not the best way to go.

Actually I think doing this on the desktop level is the way forward. There are two problems here.

First, consider a USB optical drive attached to the system. A typical thing you want to do here is to eject the disc so you can insert another one. Now, to eject media you first have to unmount it before you can send the eject ioctl to the device. But if you hook into the unmount process then you unbind the device before it is mounted and you never get to send the eject ioctl.

The other example is when the user manually wants to unmount a partition instead of ejecting it. Now, in the Nautilus file manager we do default to ejecting stuff (the eject icon in the sidebar) but the user can also right-click and click on Unmount in the context menu. If you hook into the unmount process then the device(s) actually disappears, much against what the user wanted.

Anyway, it's not a problem to do all this from the desktop level and I just added support for this.

First, I wrote a patch to DeviceKit-disks (the successor to HAL for handling storage devices) that offers a DriveDetach() method and also a :drive-can-detach property. Right now we only set :drive-can-detach for USB devices but we can add support for other connection fabrics as needed. The patch is here

http://cgit.freedesktop.org/DeviceKit/DeviceKit-disks/commit/?id[7350df8b63cf4178a2fe69ee740b5222130421

Then I added support for this in GVfs which is what Nautilus uses.

http://git.gnome.org/cgit/gvfs/commit/?id'b14622b7fcc228ddd617ebd723751ccd8dadaa
http://git.gnome.org/cgit/gnome-disk-utility/commit/?id÷6001f6f6beca8b21ff81e70f2633c60783479f
(actually GVfs uses gnome-disk-utility which is a "binding" of some sort of DeviceKit-disks. But that's unimportant for this discussion.)

So, in a nutshell, GNOME 2.28 will support powering down USB enclosures out of the box.

Btw, another change I made is to only unbind the usb-storage driver to better handle multifunction devices. E.g. you don't want to unbind drivers from all the USB interfaces.

Cheers,
David

#6 lore raw
In reply to Christian Schmitt

On Tuesday 09 June 2009 02:45:57 David Zeuthen wrote:

Hi David,

Quoted 3 lines

That's a very interesting blog entry, thanks for sharing. My experience dealing with the storage stack in GNOME is that people keep asking for this feature.

... and KDE users obviously, too ;-)

Quoted 10 lines

Actually I think doing this on the desktop level is the way forward. There are two problems here.

First, consider a USB optical drive attached to the system. A typical thing you want to do here is to eject the disc so you can insert another one. Now, to eject media you first have to unmount it before you can send the eject ioctl to the device. But if you hook into the unmount process then you unbind the device before it is mounted and you never get to send the eject ioctl.

Quoted 6 lines

The other example is when the user manually wants to unmount a partition instead of ejecting it. Now, in the Nautilus file manager we do default to ejecting stuff (the eject icon in the sidebar) but the user can also right-click and click on Unmount in the context menu. If you hook into the unmount process then the device(s) actually disappears, much against what the user wanted.

Perfectly right. And that is why it would make sense IMHO to let the user specify special treatment for certain devices (like it is possible with udev rules (serial number, manufactor, model....)).
Actually, I am asking myself if the eject command is sufficient in this case of a certain external HD device that seems to need some more commands to go into suspend mode. That's why I think it would make more sense not to hard-wire certain detach/supend/spindown commands, but make them selectable for every device. Well, just some brainstorming. :-)

Quoted 2 lines

Anyway, it's not a problem to do all this from the desktop level and I just added support for this.

As you seem to be a GNOME guy, I'll probably have to wait until someone starts to integrate DeviceKit support into Solid. Are you aware of any work in this direction?

Thanks for your effort.

Cheers,
Chris