How to automount USB pendrive and CD/DVD, using udev/HAL/D-BUS

How to automount USB pendrive and CD/DVD without desktop environment (gnome, KDE etc) to the specified mount point, using udev/HAL/D-BUS ?

No votes yet

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

How to automount USB

Go to:

http://www.kernel.org/pub/linux/utils/kernel/hotplug/udev.html

and follow the link to "writing udev rules" - that is probably the most up-to-date document even though it was a few months out of date when I last looked at it (a few months ago). It is an excellent document in any case and it even shows you how to use 'udevinfo' - I find "man udevinfo" very confusing (and very out of date).

Basically:
1. Write the rule to create a symlink to the device - this way you can always access it as, say, /dev/usbstick rather than /dev/sda, /dev/sdb etc.
2. Write the rule to automatically mount; you can use something like:
RUN+="/bin/mount -t auto /dev/usbstick /media/usbstick"
(you can drop the '-t auto /dev/usbstick' if you have an fstab entry)

You might have to read that udev doc a few times before you understand everything. Then you'll have all the fun of reading and deciphering all the default Debian rules...

How to automount USB

Maybe I will read this, but there is a small but... first, how can I automatically unmount pendrive with this metod? I don't think it is possibile at all with udev. And second, this propably will not work with CD/DVD discs as disc isn't a device which appears in the kernel (eg. hdd is DVD-ROM device but not DVD disc).

Well, HAL daemon can detect CD but how to make it mount this device?

How to automount USB

As far as I know there is no such thing as an automatic "unmount". If you pull out the USB stick and data has not been written out from the buffers, you've lost that data. The only correct procedure is to unmount, then remove the drive.

If you have a GUI, KDE and Gnome each have their own scheme for putting a disc icon on the desktop and you can right-click it to mount/unmount/eject. KDE also has a utility that listens to the DBUS so it can automatically mount something (or ask you if you want it mounted) - personally I disable it because I find it extremely annoying. In the Control Center, under Peripherals and then Storage Media.

How to automount USB

When CD/DVD is mounted I cannot eject the disc, I have to unmount manually first, this is not very handful. I need some daemon which will mount the disc when it is detected and unmount when I eject the disc. Similarly with USB pendrive (obviously when bufferng for this disk is disabled) and I don't want to install GNOME or KDE for this trivial thing. If some soft from GNOME can do it, why there isn't any similar app without GUI ?

Besides if HAL daemon is so powerful (if it can detect any disk attached to the system) why I cannot tell "him" to do simple mount or umount when certain device is detected/attached or detached ??? Please explain it because It is very weird for me

How to automount USB

As far as I know HAL only sends messages. It would be very poor design to try to make HAL do anything else on its own - the specialization is for other tools. So these other tools need to listen to HAL to be able to do anything. If you just use 'udev' there is no need to deal directly with HAL.

If you use the 'eject' command, that automatically unmounts a device before ejecting. If a device such as a CDROM is 'locked' (CD writer software will normally lock the device to prevent accidental removal) then eject can do nothing.

You cannot safely remove a device without unmounting it first; since there is no such thing as psychic software, there is absolutely no software on the planet which will automatically unmount before you remove a device; at best the software can attempt to clean up after you did something evil like pulling out a storage device without unmounting.

Another trick with unmounting is that there must be no open references to any subdirectory or file. For example, let's say you did something like this:

mount /media/cdrom
cd /media/cdrom
unmount /media/cdrom <-- This will never complete

Just to make life more interesting, one of the older file monitoring programs uses an incredibly stupid way of monitoring files and invariably prevented users from unmounting anything (famd). So, check any file/directory monitoring tools you may have like famd or whatever and switch them off, then attempt to unmount a CD or USB stick or whatever.

How to automount USB

I didn't mean auto unmount before device is detach (this is of course impossible) I want unmount it after detach (e. remove it's info from /etc/mtab file or something similar). The last example that you wrote could be harmful but I think it will only produce bash's error.

Well, udev can auto mount devices as you wrote, can it mount also CD/DVD?

HAL isn't for messaging, at last I read that. D-BUS is responsible for this.

HAL have something like Mount(), but I don't know how to deal with this

How to automount USB

I don't know if udev generates an event for plugging in a CD or DVD since the CD Reader drivers are actually already loaded.

Anyway, you can use "ivman" to handle them:
apt-get install ivman

Syndicate content