LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-20-2009, 01:08 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Auto-mount Script


I was wondering if you can make a script that will automatically mount external storage devices when they are plugged in?

I have no idea how to know what entry in /dev is what device, and how to find out the name of the device to name the mount point.

I wanted to do this both to learn how to find out information on devices and not to rely on GNOME or KDE to mount stuff, so I can use a separate window manager without having to manually mount my devices.
 
Old 12-20-2009, 01:10 PM   #2
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
You should read up on udev.
 
Old 12-20-2009, 01:36 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I was reading a little about udev and it seems that is associates a particular device file to a particular device. My hard drive is /dev/sda, and DVD burner is /dev/scd. But I have 2 flash drives, both go to /dev/sdb. How do I tell the difference between them? I don't know what happens if I plug both in at once.

The behavior I would like is like GNOME where when you plug in a device it creates a directory in /media named like the device and mounts it.

Last edited by MTK358; 12-20-2009 at 01:41 PM.
 
Old 12-20-2009, 02:12 PM   #4
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
You can specify attributes for the udev rule to tell what drive is being attached. I got this working - have a look at this link for details. in particular look at the Ubuntu forum link in my link posts for details of getting these attributes using udevmonitor.

Since that post I have modifed my udev rule to work better from the Vendor and Product attributes. It now mounts and dismounts my drive partitions when attached or detached - both are mounted or dismounted at the same time as they are partitions on the one USB attached drive. Here's a copy of my most recent udev rule - obviously your attributes will differ.

Note that I need entries in fstab to map UUID's of the partitiions to mount points. Here's my udev rules :
Code:
ATTRS{idVendor}=="059b", ATTRS{idProduct}=="0370",ACTION=="add",RUN+="/bin/mount /home/ed/spare13"
ATTRS{idVendor}=="059b", ATTRS{idProduct}=="0370",ACTION=="add",RUN+="/bin/mount /home/ed/spare14",GOTO="device_end"
ENV{ID_VENDOR}=="ST310005",ACTION=="remove",RUN+="/bin/umount /home/ed/spare13"
ENV{ID_VENDOR}=="ST310005",ACTION=="remove",RUN+="/bin/umount /home/ed/spare14"
LABEL="device_end"
This was the only way I could get the two mount/umount commands to work when the drive was plugged in or unplugged, (there's two partitions to be mounted), and here's the relevant parts of fstab - obviously your UUID's will be different:
Code:
UUID=235ea638-6011-42f9-8f78-8383f117e8a1	/home/ed/spare13 ext2		defaults      0     0
UUID=9f902108-cde7-4cae-bc86-b15f88d59501       /home/ed/spare14 ext3           defaults      0     0
. You will need to look up getting and setting udev device attributes and getting and setting UUID's but there are lots of links around.

This now works if the drive is attached on boot up or perhaps later and when disattached.
Good luck with this.
 
Old 12-20-2009, 03:05 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Still I don't understand this.
 
Old 12-20-2009, 11:48 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Save this page:
http://reactivated.net/writing_udev_rules.html

It takes a whole lot of trial and error, but works after that. I have a rule that runs when a flash drive with a certain serial number is plugged in. The script mounts it, uses encryption keys stored on it to mount encrypted filesystem images, then unmounts it. Hasn't failed since I set it up a few years ago.
Kevin Barry
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Location of Gnome auto-mount script? aembleton Linux - Newbie 4 05-27-2009 06:00 PM
Auto-mount windows share on boot - manual OK, auto not ok! erskie Linux - Newbie 2 02-12-2008 06:42 PM
auto mount partition script SweetLou Linux - General 2 02-19-2007 09:39 PM
autofs enabled in kernel but auto.mount and auto.master are NOT in /etc! Alybyzrp Linux - General 0 11-11-2003 12:26 PM
SCRIPT: check if auto-mount mount-points are still mounted markus1982 Linux - Software 0 05-25-2003 05:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration