Automatizing the sequence of mounting commands
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.
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.
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.