# Automount different USB storage?

## z_sfeng

I have a USB hard driver and some usb memory. I can mount them without problem.

But something annoying is:

I have 2 NTFS partitions in my hard driver. My fstab looks like this:

```

/dev/sda1      /mnt/usbhd1     ntfs

/dev/sda2      /mnt/usbhd2     ntfs

```

But when I plug a memory stick, I need to change the fstab to:

```

/dev/sda1       /mnt/digicam    vfat

```

If I connet both, I need to change one of the "sda" to "sdb" depending on which one I plug first.

So I want to know, if there's anything or any script can do this automatically? 

question 2: if there's any way to automatically mount/unmount the usb filesystem when I plug/unplug the device? I think in WinXP, it is not necessary to umount the device.

----------

## adsmith

yo -- you need udev rules to name devices.

here is my personal rules file for two different USB devices.

```

# abe added this for usb flash storage (SD card reader).

BUS="usb",KERNEL="sd?1",SYSFS{product}="Mass Storage Device",NAME="%k",SYMLINK="usbmem",MODE="0660",GROUP="usb"

# abe added this for external hard drive

BUS="usb",KERNEL="sd?1",SYSFS{product}="VERSION 2.0",NAME="%k",SYMLINK="ubiquitin",MODE="0660",GROUP="usb"

```

details in gentoo docs and in the gentoo wiki.

----------

## tutaepaki

to get automounting....

```
emerge autofs
```

----------

## z_sfeng

Thanks a lot for your quick replies. I'm struggling with udev now. 

1. I'm trying the udev. I already have udev running, no devfs. But the udev howto documentation is no so clear for me  :Sad: 

 I create the rule file: /etc/udev/rules.d/10-local.rules

```
BUS="usb", SYSFS{model}="USB SD Reader", KERNEL="sd?1", NAME="%k", SYMLINK="usbmem" 
```

What should I do next? I unplug/plug the sd reader, the "/dev/usbmem" doesn't appear. 

And how should I modify my fstab? just use the /dev/usbmem instead of /dev/sda1?

2. what is the difference between autofs and submount/supermount? I emerged autofs, but never used that.

----------

## z_sfeng

This is my log:

```

26718 Jun 24 14:00:06 tux usb 1-1: new full speed USB device using uhci_hcd and address 11

  26719 Jun 24 14:00:07 tux scsi7 : SCSI emulation for USB Mass Storage devices

  26720 Jun 24 14:00:07 tux usb-storage: device found at 11

  26721 Jun 24 14:00:07 tux usb-storage: waiting for device to settle before scanning

  26722 Jun 24 14:00:12 tux Vendor: Generic   Model: USB SD Reader     Rev: 2.00

  26723 Jun 24 14:00:12 tux Type:   Direct-Access                      ANSI SCSI revision: 00

  26724 Jun 24 14:00:13 tux SCSI device sda: 29120 512-byte hdwr sectors (15 MB)

  26725 Jun 24 14:00:13 tux sda: assuming Write Enabled

  26726 Jun 24 14:00:13 tux sda: assuming drive cache: write through

  26727 Jun 24 14:00:13 tux SCSI device sda: 29120 512-byte hdwr sectors (15 MB)

  26728 Jun 24 14:00:13 tux sda: assuming Write Enabled

  26729 Jun 24 14:00:13 tux sda: assuming drive cache: write through

  26730 Jun 24 14:00:13 tux sda: sda1

  26731 Jun 24 14:00:13 tux Attached scsi removable disk sda at scsi7, channel 0, id 0, lun 0

  26732 Jun 24 14:00:13 tux usb-storage: device scan complete

  26733 Jun 24 14:00:13 tux udev[31089]: configured rule in '/etc/udev/rules.d/50-udev.rules[28]' applied, 'sda' becomes '%k'

  26734 Jun 24 14:00:13 tux udev[31089]: creating device node '/dev/sda'

  26735 Jun 24 14:00:13 tux udev[31091]: configured rule in '/etc/udev/rules.d/50-udev.rules[28]' applied, 'sda1' becomes            '%k'

  26736 Jun 24 14:00:13 tux udev[31091]: creating device node '/dev/sda1'

  26737 Jun 24 14:00:14 tux scsi.agent[31063]: disk at devices/pci0000:00/0000:00:07.2/usb1/1-1/1-1:1.0/host7/target7:0:0/7:        0:0:0

```

so the 10-local.rulse is not applied? I did env-update. Should I also try to restart some service?

----------

## z_sfeng

OK. The udev works now. SYSFS{model} does not work. So I use {idVendor} and {idProduct} instead.

the submount still does not work automatically...

----------

## Headrush

 *z_sfeng wrote:*   

> OK. The udev works now. SYSFS{model} does not work. So I use {idVendor} and {idProduct} instead.
> 
> the submount still does not work automatically...

 

I would suggest trying one of the other mounting options: pmount, fstab-sync (hal, dbus), gnome-volume-manager

----------

## z_sfeng

Why? Could you please explain little bit more? Since I don't use gnome, gvm maybe not the right option.

I read some posts here, but I still don't understand what's the differences.

----------

## Headrush

 *z_sfeng wrote:*   

> Why? Could you please explain little bit more? Since I don't use gnome, gvm maybe not the right option.
> 
> I read some posts here, but I still don't understand what's the differences.

 

From my experience they work better.

Plus the others seem more generally accepted, or people are going those routes so probably get better help and compatibility.

Submount uses a kernel modules and a daemon. The others rely on hal and messages from hotplug, which looks like the future of device support.

----------

## btlee

 *z_sfeng wrote:*   

> Thanks a lot for your quick replies. I'm struggling with udev now. 
> 
> 1. I'm trying the udev. I already have udev running, no devfs. But the udev howto documentation is no so clear for me 
> 
>  I create the rule file: /etc/udev/rules.d/10-local.rules
> ...

 

Here is my local rules file.

```

BUS="scsi",SYSFS{removable}="1",NAME="%k",SYMLINK="removable"

BUS="scsi",SYSFS{removable}="0",NAME="%k1",SYMLINK="external"

```

After modifying, just 'udevstart' as root.

good luck  :Very Happy: 

----------

## johntramp

 *z_sfeng wrote:*   

> 
> 
> question 2: if there's any way to automatically mount/unmount the usb filesystem when I plug/unplug the device? I think in WinXP, it is not necessary to umount the device.

 In windows xp you do have to umount the device before removing it.  They don't make it seem as an important thing to do tho,  have a look in that thing in the bottom right when you use it again.

There are a couple ways to make this easier.  In WMs like xfce there is a plugin you can get to have a mount icon in your taskbar,  you click this and umount it as required for you to remove the device.  The other way is an app I saw which mounts your usb drive when it is requested, ie you cd to the dir where it mounts to, and then automatically umounts it x seconds after you last used it.

If you set this to something like 60 seconds then you just need to wait a minute after you last used the drive and then it is ok to unplug it.

http://gentoo-wiki.com/HOWTO_Auto_mount_filesystems_%28AUTOFS%29

Hope this helps.

----------

## z_sfeng

 *Quote:*   

> 
> 
> Here is my local rules file.
> 
> ```
> ...

 

But I didn't see any such info from udevinfo on my usb devices. How does it work?[/quote]

----------

## btlee

 *z_sfeng wrote:*   

>  *Quote:*   
> 
> Here is my local rules file.
> 
> ```
> ...

 [/quote]

Well...

The above information can be shown by /sys/block.

I think that udev refers the sys directory information or it's source information.

Usually, usb memory stick has a value '1' in removable field,

while external hdd has not one.

----------

## z_sfeng

```

BUS="scsi",SYSFS{removable}="1",NAME="%k",SYMLINK="removable"

BUS="scsi",SYSFS{removable}="0",NAME="%k1",SYMLINK="external"

```

What is the "k1" there?

In my case, the "removable" label only applies on /dev/sda, not /dev/sda1 and /dev/sda2. The default 50-udev.rules still applies on sda1 and sda2.

----------

## z_sfeng

Finally, things are settling down with udev and submount:

10-local.rules

```

BUS="usb", KERNEL="sd*", SYSFS{product}="USB 2.0 Hard Drive", NAME="%k", SYMLINK="exthd%n"

BUS="usb", KERNEL="sd?1", SYSFS{product}="Mass Storage Device", NAME="%k", SYMLINK="usbmem"

```

/etc/fstab:

```

/dev/usbmem     /mnt/usbmem     subfs   fs=vfat,users,defaults  0 0

/dev/exthd1     /mnt/exthd1     subfs   fs=ntfs,ro,users,exec,defaults,gid=100,u

id=1001,umask=007,nls=utf8      0 0

/dev/exthd2     /mnt/exthd2     subfs   fs=ntfs,ro,users,exec,defaults,gid=100,u

id=1001,umask=077,nls=utf8      0 0

```

submount works after I put it to modules.autoload. (manually modprobe doesn't work).  I still prefer submount, because it is simple, and I don't like my fstab to be changed on-fly.

Thanks for all your helps.

----------

