automatic mount any external drive under /media with read/write access to everyone
Here is my issue:
My computer has 4 USB ports and is running Ubuntu 12.10. I receive different USBs every day, plug to my computer, read/write data to them, and then send them away.
What I want is to automatically mount any USB I plug into my computer. The first detected USB will go to /media/HDD1
, the second will go to /media/HDD2
and so on.
Or it would be great if USB plugged to port1 will go to /media/HDD1
, USB plugged to port2 will go to /media/HDD2
, and so on.
The path /media
is not important but I would like the HDD1-4 directories in one place so that I can easily access them.
The USBs are normally ext2 but sometimes I get FAT32, NTFS USBs as well.
I also want USB to be mounted without logging in to Ubuntu as I often access /media/HDD..
from another Windows computer.
How can I do that? What change in udev
rules, fstab
should I make? Any suggestion is highly appreciated.
Well, here we can use the same trick that fstab
uses for Optical media (aka CD's and DVD's):
/dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0
The first column indicates the file system, I'm sure your USB's will have fixed values, so lets presume that everything after sda
is a USB, and since you can only have 4 USB's at the same time, the list goes from sdb
, sdc
, sdd
and sde
.
$ ls /dev/sd*
/dev/sda /dev/sdb /dev/sdb3 /dev/sdb6 /dev/sdc /dev/sdf
/dev/sda1 /dev/sdb1 /dev/sdb4 /dev/sdb7 /dev/sdd
/dev/sda2 /dev/sdb2 /dev/sdb5 /dev/sdb8 /dev/sde
(In my case I use a memory reader, so my USB drivers start from sdg
but lets continue.)
Now, we assume each USB have one and only one partition, so the lines we need, for our fstab
, are:
/dev/sdb1
/dev/sdc1
/dev/sdd1
/dev/sde1
Then you said your mount points has to be fixed, so after you have created your directories, lets add them:
/dev/sdb1 /media/HDD1
/dev/sdc1 /media/HDD2
/dev/sdd1 /media/HDD3
/dev/sde1 /media/HDD4
Since you can have almost all type of file systems, lets use the auto
so fstab guess the file type to use:
/dev/sdb1 /media/HDD1 auto
/dev/sdc1 /media/HDD2 auto
/dev/sdd1 /media/HDD3 auto
/dev/sde1 /media/HDD4 auto
Ok, now lets boil down to the options, which will be the very same for CD's with the plus of read/write permissions:
/dev/sdb1 /media/HDD1 auto rw,users,noauto,allow_other,umask=0
/dev/sdc1 /media/HDD2 auto rw,users,noauto,allow_other,umask=0
/dev/sde1 /media/HDD3 auto rw,users,noauto,allow_other,umask=0
/dev/sde1 /media/HDD4 auto rw,users,noauto,allow_other,umask=0
rw
tells that we want read and write permission, users
allows any user to mount a device, noauto
prevents that the driver gets mounted automatically when mount -a
is called, like it does at boot, preventing boot ERRORS and WARNINGS, allow_other
allows other but the user that mounted the driver to have the same permissions, and umask=0
prevents the defaults umask from being applied.
Now the only 2 left fields are pretty much just about the dump
order and fsck
order, which could be in 0
both, leaving us with the final result:
/dev/sdb1 /media/HDD1 auto rw,user,noauto,allow_other 0 0
/dev/sdc1 /media/HDD2 auto rw,user,noauto,allow_other 0 0
/dev/sdd1 /media/HDD3 auto rw,user,noauto,allow_other 0 0
/dev/sde1 /media/HDD4 auto rw,user,noauto,allow_other 0 0
With this, each time that you plug a USB device it will being automagically mounted in the mount point without the intervention of the user. You should modify according to your necessities.
Here is the solution I found:
Firstly I install autofs on my linux PC:
sudo apt-get install autofs
It will create auto.master file under /etc/auto.master
Add the following line at the end of that file:
/media/mount /etc/auto.mount --timeout=10 --ghost
/media/mount
is a directory where I want to mount all external drives into. /etc/auto.mount
is the file to put in mounting rules. --timeout=10
means it will automatically unmount the drive after 10 sec of inactivity. --ghost
means it still leaves a picture of content when it is unmounted. Change this line according to your needs.
Edit the /etc/auto.mount
file and add the following lines:
disk1 -fstype=auto,sync :/dev/sdc1
disk2 -fstype=auto,sync :/dev/sdd1
disk3 -fstype=auto,sync :/dev/sde1
disk4 -fstype=auto,sync :/dev/sdf1
disk5 -fstype=auto,sync :/dev/sdg1
disk6 -fstype=auto,sync :/dev/sdh1
disk7 -fstype=auto,sync :/dev/sdi1
disk8 -fstype=auto,sync :/dev/sdj1
DVD -fstype=iso9660,ro :/dev/sr0
For normal drives such as USB or eSATA set -fstype=auto,sync
. I have 8 USB+eSATA ports so I declare 8 mount points here. My PC already has sda
and sdb
as local disks so I put sdc1
-> sdj1
for external devices. They are actually device nodes in /dev
when you plug your drives in.
An exception of mounting rule is DVD mounting as you see above.
When I insert any drives they will show up as disk1, disk2, ... under /media/mount
directory, and will auto-unmount after 10 seconds. I don't need to touch my PC and can access it from somewhere else (such as Virtual machine)