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.

When I connect a device (tablet, camera, flash drive, external harddrive) to my laptop with Linux I often type the following sequence of commands.

$ ls /dev/sd*
Let's say that my device is /dev/sdc
optional $ sudo mkdir /media/my_flash
optional $ sudo chmod 777 /media/my_flash
$ sudo mount /dev/sdc /media/my_flash

Then I will copy some files and
$ sudo umount /dev/sdc

This sequence guarantees me that the files are copied and nothing bad will happen.

Could you tell me how to automatize this sequence of commands? So I will only plug the device, it will recognize to which directory it should be mounted, then I will make some file transfer and then it will signal me (in some way) I can unplug the device? Is there some settings in Linux OS for this?

thank you

share|improve this question
add comment

3 Answers 3

up vote 5 down vote accepted

The system component that reacts to the connection of a removable device is Udev, as mentioned by SHW. Even the udev tutorial can be a little daunting; I'll show you a couple of examples.

There are two steps involved: associating a device file (e.g. /dev/sdc) with the hardware device, and mounting the device to access the filesystem. Udev's job is the first step, though you can tell it to run an external command such as mount.

For known removable devices, I like to use a dedicated device name under /dev/removable (that directory name is a personal convention). The following udev rules (to be placed in /etc/udev/rules.d/my_removable_disks.rules) create symbolic links with known names for two disks, both identified by a property of the filesystem on their partition 1:

KERNEL=="sd?", PROGRAM=="/sbin/blkid -o value -s UUID %N1", RESULT=="1234-5678", SYMLINK+="removable/foo"
KERNEL=="sd?", PROGRAM=="/sbin/blkid -o value -s LABEL %N1", RESULT=="Joe's disk", SYMLINK+="removable/joe"

Older versions of udev may need /udev/lib/vol_id -u %N1 (for the UUID, -l for the label) instead of the blkid call. There are more things you can match on, e.g. ATTRS{vendor}=="Yoyodine", ATTRS{serial}=="XYZZY12345PDQ97" (instead of PROGRAM==…, RESULT==…) to match a device's vendor and serial number.

Then you can use a line like this in /etc/fstab:

/dev/removable/joe  /media/joe  vfat  noauto,user

If you prefer an automatic mount, you can add something like , RUN="mkdir /media/foo && mount /dev/removable/foo /media/foo" to the udev line. Don't forget to umount /media/foo before unplugging.

share|improve this answer
    
Thanks for great solution. I have LVM on some external drives, so longer sequence of commends (including VG activation etc), now I can even add backup scripts to this :). –  Grzegorz Wierzowiecki Aug 20 '12 at 9:25
    
Dear reader - if you want to know about available ATTRS, try this with your device: udevadm info --name=/dev/DEVICE --attribute-walk –  Grzegorz Wierzowiecki Aug 24 '12 at 21:19
add comment

There are several auto-mounting daemons nowadays, as well as the pmount command which is specifically meant to let users mount removable devices under /media without requiring sudo access.

Both Gnome and KDE have the option to automatically mount removable volumes when they're connected; depending on what distribution you installed this function might even default to 'on'.

In GNOME, the options governing this feature are avalaible under System -> Preferences -> Removable Media; I've no recent experience with KDE.

share|improve this answer
    
I'd like to do it independently on Window Management Environement (like KDE, GNOME etc.) (As if X was not started). –  xralf Apr 22 '11 at 10:39
    
Then you'll have to read that link SHW posted, and read pretty much all of it. –  Shadur Apr 22 '11 at 12:36
add comment

You can do this by writing a UDEV rules, which will "run" the bash script ontaining the same bash commands, you mentioned. Whenever you plug the said devices, UDEV will recognize this, and start behave according to rule.

For rule writing, you can follow this link.

share|improve this answer
    
Ooh, nice. Didn't know that one yet -- Bookmarking the link and adding it to my repertoire. –  Shadur Apr 22 '11 at 9:59
    
Could you please be more specific? Which paragraphs from the link are relevant to me? –  xralf Apr 22 '11 at 10:42
    
It looks like the thing I'm looking for. I only have not time to read it all these days. I will study it and paste the right answer into your comment in the time I will understand it. Thanks –  xralf Apr 22 '11 at 14:49
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.