Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Autmounting a USB stick with udev and ivman
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Richard
n00b
n00b


Joined: 21 Nov 2002
Posts: 34
Location: Australia

PostPosted: Tue Sep 06, 2005 12:53 pm    Post subject: Autmounting a USB stick with udev and ivman Reply with quote

Hi,

I am writing this because doing this gave me trouble, so I thought I would share my thoughts on how I got it to work. This is a worked example, using a no-name brand 256mb usb thumb drive. The same process should also work with other usb storage devices like cameras etc.

Before you start

I am using a udev system. If you are not, please consult the excellent documentation at:

http://www.gentoo.org/doc/en/udev-guide.xml

I edited my /etc/conf.d/rc file as follows:

Code:
RC_DEVICE_TARBALL="no"


I also assume that you have compiled the relevant options into your kernel. If not, try here:

http://gentoo-wiki.com/HOWTO_USB_Mass_Storage_Device

Creating the the udev rules

To create a udev rule for my usb thumb drive I needed some basic information about it. Most of this needs to be done as root, so you may as well su to root. I followed this process:

a) plugged in my thumb drive
b) ran dmesg and looked for the information about the usb device initialisation. It should be at the end

Code:
>dmesg
<snip>
Initializing USB Mass Storage driver...
scsi0 : SCSI emulation for USB Mass Storage devices
usbcore: registered new driver usb-storage
USB Mass Storage support registered.
usb-storage: device found at 2
usb-storage: waiting for device to settle before scanning
  Vendor: OTi       Model: Flash Disk        Rev: 2.00
  Type:   Direct-Access                      ANSI SCSI revision: 02
SCSI device sda: 512000 512-byte hdwr sectors (262 MB)
sda: Write Protect is off
sda: Mode Sense: 03 00 00 00
sda: assuming drive cache: write through
SCSI device sda: 512000 512-byte hdwr sectors (262 MB)
sda: Write Protect is off
sda: Mode Sense: 03 00 00 00
sda: assuming drive cache: write through
 sda: sda1
Attached scsi removable disk sda at scsi0, channel 0, id 0, lun 0
usb-storage: device scan complete


c) look for the line giving the kernel device and partition. In this case:

Code:
 sda: sda1


The path is relative to /dev, so the device path is /dev/sda1.

d) locate the /sys path for sda1. You can use this command:
Code:
udevinfo -q path -n /dev/sda1

In my case this did not work, giving me errors. However, a quick look in the /sys directory turned up the path:

Quote:
/sys/block/sda/sda1


e) gather information about the usb thumb drive with

Code:
udevinfo -a -p /block/sda/sda1


This returns a lot of information. You need something to identify the device. I used the product id.

Code:

udevinfo starts with the device the node belongs to and then walks up the
device chain, to print for every device found, all possibly useful attributes
in the udev key format.
Only attributes within one device section may be used together in one rule,
to match the device for which the node will be created.

device '/sys/block/sda/sda1' has major:minor 8:1
  looking at class device '/sys/block/sda/sda1':
    SUBSYSTEM=="block"
    SYSFS{dev}=="8:1"
    SYSFS{size}=="511967"
    SYSFS{start}=="32"
    SYSFS{stat}=="     172      172        0        0"

<snip>

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:03.1/usb2/2-2':
    BUS=="usb"
    ID=="2-2"
    DRIVER=="usb"
    SYSFS{bConfigurationValue}=="1"
    SYSFS{bDeviceClass}=="00"
    SYSFS{bDeviceProtocol}=="00"
    SYSFS{bDeviceSubClass}=="00"
    SYSFS{bMaxPower}=="200mA"
    SYSFS{bNumConfigurations}=="1"
    SYSFS{bNumInterfaces}==" 1"
    SYSFS{bcdDevice}=="0200"
    SYSFS{bmAttributes}=="80"
    SYSFS{configuration}==""
    SYSFS{devnum}=="2"
    SYSFS{idProduct}=="2168"
    SYSFS{idVendor}=="0ea0"
    SYSFS{manufacturer}=="USB     "
    SYSFS{maxchild}=="0"
    SYSFS{product}=="Flash Disk      "
    SYSFS{serial}=="CCFD41AD91B6002E"
    SYSFS{speed}=="12"
    SYSFS{version}==" 2.00"


In this case (note the trailing spaces):

Code:
SYSFS{product}=="Flash Disk      "


f) construct my udev rule

Code:
nano - w /etc/udev/rules.d/10-udev.rules

#usb flash disk
BUS="usb", KERNEL="sd[a-z]1", SYSFS{product}="Flash Disk      ", NAME="usb/flashdisk", SYMLINK="%k"


What does this mean.
    BUS="usb" - is reasonably self explanatory
    KERNEL="sd[a-z]1" makes sure that we use the partition 1 of our device
    SYSFS{product}="Flash Disk " - this is our unique identifier that identifies this usb thumb drive
    NAME="usb/flashdisk" - the name in /dev given to this device. ivman uses this for automounting
    SYMLINK="%k" - create a symlink to the kernel device name (eg /dev/sda1)


Thus when I plug my thumb drive, udev creates two devices:

/dev/usb/flashdisk
/dev/sda1

I can now use /dev/usb/flashdisk to always refer to my thumb drive, no matter how many usb devices are plugged in.

g) add an entry to fstab for the thumb drive

Code:
nano -w /etc/fstab


/dev/usb/flashdisk      /mnt/flash      vfat            noauto,user,sync,noatime        0 0


Automounting with ivman

a) You will need the following software:
# HAL
# D-BUS
# ivman

Code:
emerge -p hal dbus ivman


b) unplug & replug my thumb drive and it mounts after a moment or two.

Tricks and traps

The main trap I ran across was ensuring that the device name in fstab is the same as appears in the NAME parameter in the udev rule. ivman will not work with a symlink.

references

Linux Format magazine, May 2005 edition (lxf66). www.linuxformat.co.uk
http://gentoo-wiki.com/HOWTO_ivman

I hope this is useful for people.

regards
Richard
Back to top
View user's profile Send private message
iggee85
n00b
n00b


Joined: 13 Aug 2005
Posts: 4

PostPosted: Sun Sep 11, 2005 3:43 am    Post subject: Reply with quote

Really great how-to!!! Got it working on first try, so thanks for your work in putting this together.
Consider this how-to bookmarked.
I do have one question though, should i still umount the usb device before i disconnect it? and if so, do i umount both dirs that udev creates?
Back to top
View user's profile Send private message
Richard
n00b
n00b


Joined: 21 Nov 2002
Posts: 34
Location: Australia

PostPosted: Mon Sep 12, 2005 12:02 am    Post subject: Reply with quote

Hi iggee85,

The short answer is that you should always properly umount the drive before removing it. This allows the OS (whether it is Linux, Windows or what ever) to finish writting to the drive. If you just pull it out, you can corrupt the contents of the drive. However there are a few provisos:

1) umount needs root credentials. You can get around this with sudo (see this excellent guide http://www.gentoo.org/doc/en/sudo-guide.xml) or with a bit of messing about with HAL (http://gentoo-wiki.com/HOWTO_ivman#Mounting_mass_storage_devices_in_userspace). Personally I either leave the drive in till I log off or use the next otion.
2) if you specified "sync" in your fstab line (as I did in the example above) you should be able to just pull the drive out with relative safety. This is because the sync option basically tells the os to always keep the actual contents of the drive in sync with its virtual contents. ie it actually writes to the disk imediately when you save or whatever. On the down side, it does slow down disk operations.

Code:
/dev/usb/flashdisk      /mnt/flash      vfat            noauto,user,sync,noatime        0 0


When you do a umount, you only need to umount the mount pount you specified in fstab. Using the above example:

Code:
umount /mnt/flash


regards
Richard
Back to top
View user's profile Send private message
daff
Apprentice
Apprentice


Joined: 02 Jul 2003
Posts: 232
Location: Vienna, Austria

PostPosted: Tue Sep 13, 2005 1:41 am    Post subject: Reply with quote

Great HOWTO, thanks! I'm certainly going to need this in a few weeks.

As for unmounting the the flash disk, try
Code:
# eject /mnt/flash

which is an even more convenient way to sync, close and unmount (removable) devices.
_________________
Instead of asking why a piece of software is using 1970s technology,
start asking why software is ignoring 30 years of accumulated wisdom.
Back to top
View user's profile Send private message
cgmd
Veteran
Veteran


Joined: 17 Feb 2005
Posts: 1585
Location: Louisiana

PostPosted: Sat Sep 17, 2005 4:30 pm    Post subject: Reply with quote

Richard...

Very nice howto!

I'm trying to apply this information for use with a usb 2.0 KanguruZipper Pro external 20 GB drive. I have determined my device to be /dev/sda1, and I can manually mount it to /mnt/extdrv. However when I run
Code:
udevinfo -a -p /block/sda/sda1

... I get the following output: udeinfo

I'm unsure of the information to abstract and use in the udev rule (I don't have /rules.d/10-udev.rules):
Code:

# nano -w /etc/udev/rules.d/50-udev.rules



snippit:

# kanguruzipper pro
BUS="usb", KERNEL="sd[a-z]1", SYSFS{product}=="USB 2.0 IDE Bridge", NAME="usb/kanguru", SYMLINK="%k"


No matter what I try to use as "NAME", nothing new gets added to my /dev directory.
I would surely appreciate some help with this!

Edit: Problem solved... I was violating udev rules, which I discovered, reading http://www.reactivated.net/writing_udev_rules.html. Having made that correction, all is working like a charm!

Awesome!

Thanks...
_________________
"Primum non nocere" ---Galen
Back to top
View user's profile Send private message
Richard
n00b
n00b


Joined: 21 Nov 2002
Posts: 34
Location: Australia

PostPosted: Mon Sep 19, 2005 12:09 am    Post subject: Reply with quote

Hi cgmd,

It doen't look like you are trying to use the right device. You don't really want to be looking at the USB controller or bridge, you need to be looking for something that uniquely describes your external hdd. I am not in front of a linux box at the moment, but off the top of my head, you could try a rule along the lines of:

Code:
touch /etc/udev/rule.d/10-udev.rules
nano /etc/udev/rule.d/10-udev.rules


# kanguruzipper pro
KERNEL="sd[a-z]1", SYSFS{model}=="MK2006GAL       ", NAME="usb/kanguru", SYMLINK="%k"


Also, you sould really create the /etc/udev/rule.d/10-udev.rules file rather than edit /etc/udev/rules.d/50-udev.rules. It will ensure your rules are enacted first and save you trouble when udev needs to update its rules in the future.

I will have a more detailed look this evening.

regards
Richard
Back to top
View user's profile Send private message
cgmd
Veteran
Veteran


Joined: 17 Feb 2005
Posts: 1585
Location: Louisiana

PostPosted: Mon Sep 19, 2005 1:55 am    Post subject: Reply with quote

Richard...

Yes, I already drew those conclusions, and, now have udev working well on my 2 systems for hotplugging my usb devices...

Thank you very much for the excellent HowTo!

I also have a firewire access to my laptop, and that isn't going so smoothly...

Is that a whole different ball game, or should this setup likewise handle firewire access??

Thanks again...
_________________
"Primum non nocere" ---Galen
Back to top
View user's profile Send private message
Richard
n00b
n00b


Joined: 21 Nov 2002
Posts: 34
Location: Australia

PostPosted: Mon Sep 19, 2005 2:31 am    Post subject: Reply with quote

Hi cgmd,

Sorry, I can't offer an opinion about firewire as I don't have any firewire devices. Good luck with it though.

regards
Richard
Back to top
View user's profile Send private message
cgmd
Veteran
Veteran


Joined: 17 Feb 2005
Posts: 1585
Location: Louisiana

PostPosted: Tue Sep 20, 2005 4:39 pm    Post subject: Reply with quote

Richard...

Thanks for your assistance...

I now have a new issue, though. My usb device is a Maxtor2 external drive with both a ntfs partition for windows stuff and an ext2 partition for my linux files. My gentoo box sees both partitions, ntfs as sd?1 and ext2 as sd?2.

My udev.rules names sd?2 "maxtor2" and /etc/fdisk mounts it at /mnt/extdrv. However, sd?1 is not mentioned in udev.rules and defaults to /media/usbdisk1 as its mount point:
Code:
/dev/sda1 on /media/usbdisk1 type ntfs (rw,nosuid,nodev,umask=0007,gid=100)
/dev/usb/maxtor2 on /mnt/extdrv type ext2 (rw,noexec,nosuid,nodev,sync,noatime)


Is there a way to set up udev.rules to avoid auto-sync mounting the sd?1 ntfs partition?

Thanks, once again!
_________________
"Primum non nocere" ---Galen
Back to top
View user's profile Send private message
emp
n00b
n00b


Joined: 31 Oct 2004
Posts: 2
Location: Quebec, Canada

PostPosted: Fri Sep 23, 2005 7:34 pm    Post subject: Doesn't work =S Reply with quote

Hi, i got linux 2.6.12.5 with all kernel modules needed on.

the message i got from dmesg is:

Code:
usb-storage: device found at 7
usb-storage: waiting for device to settle before scanning
  Vendor: VBTM      Model: Store 'n' Go      Rev: 4.90
  Type:   Direct-Access                      ANSI SCSI revision: 00
SCSI device sdc: 497664 512-byte hdwr sectors (255 MB)
sdc: Write Protect is off
sdc: Mode Sense: 45 00 00 08
sdc: assuming drive cache: write through
SCSI device sdc: 497664 512-byte hdwr sectors (255 MB)
sdc: Write Protect is off
sdc: Mode Sense: 45 00 00 08
sdc: assuming drive cache: write through
 sdc: sdc1
Attached scsi removable disk sdc at scsi11, channel 0, id 0, lun 0
usb-storage: device scan complete


the message i got from udev info is:
Code:

device '/sys/block/sdc/sdc1' has major:minor 8:33
  looking at class device '/sys/block/sdc/sdc1':
    SUBSYSTEM=="block"
    SYSFS{dev}=="8:33"
    SYSFS{size}=="497660"
    SYSFS{start}=="4"
    SYSFS{stat}=="      41      164        0        0"

follow the "device"-link to the physical device:
  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2/2-3/2-3:1.0/host11/target11:0:0/11:0:0:0':
    BUS=="scsi"
    ID=="11:0:0:0"
    DRIVER=="sd"
    SYSFS{device_blocked}=="0"
    SYSFS{iocounterbits}=="32"
    SYSFS{iodone_cnt}=="0x19"
    SYSFS{ioerr_cnt}=="0x1"
    SYSFS{iorequest_cnt}=="0x19"
    SYSFS{max_sectors}=="240"
    SYSFS{model}=="Store _n_ Go    "
    SYSFS{queue_depth}=="1"
    SYSFS{queue_type}=="none"
    SYSFS{rev}=="4.90"
    SYSFS{scsi_level}=="3"
    SYSFS{state}=="running"
    SYSFS{timeout}=="30"
    SYSFS{type}=="0"
    SYSFS{vendor}=="VBTM    "

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2/2-3/2-3:1.0/host11/target11:0:0':
    BUS==""
    ID=="target11:0:0"
    DRIVER=="unknown"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2/2-3/2-3:1.0/host11':
    BUS==""
    ID=="host11"
    DRIVER=="unknown"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2/2-3/2-3:1.0':
    BUS=="usb"
    ID=="2-3:1.0"
    DRIVER=="usb-storage"
    SYSFS{bAlternateSetting}==" 0"
    SYSFS{bInterfaceClass}=="08"
    SYSFS{bInterfaceNumber}=="00"
    SYSFS{bInterfaceProtocol}=="50"
    SYSFS{bInterfaceSubClass}=="06"
    SYSFS{bNumEndpoints}=="02"
    SYSFS{modalias}=="usb:v08ECp0012d0200dc00dsc00dp00ic08isc06ip50"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2/2-3':
    BUS=="usb"
    ID=="2-3"
    DRIVER=="usb"
    SYSFS{bConfigurationValue}=="1"
    SYSFS{bDeviceClass}=="00"
    SYSFS{bDeviceProtocol}=="00"
    SYSFS{bDeviceSubClass}=="00"
    SYSFS{bMaxPower}=="178mA"
    SYSFS{bNumConfigurations}=="1"
    SYSFS{bNumInterfaces}==" 1"
    SYSFS{bcdDevice}=="0200"
    SYSFS{bmAttributes}=="80"
    SYSFS{configuration}==""
    SYSFS{devnum}=="7"
    SYSFS{idProduct}=="0012"
    SYSFS{idVendor}=="08ec"
    SYSFS{manufacturer}=="Verbatim"
    SYSFS{maxchild}=="0"
    SYSFS{product}=="Store _n_ Go"
    SYSFS{serial}=="09105C40611298EE"
    SYSFS{speed}=="12"
    SYSFS{version}==" 2.00"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1/usb2':
    BUS=="usb"
    ID=="usb2"
    DRIVER=="usb"
    SYSFS{bConfigurationValue}=="1"
    SYSFS{bDeviceClass}=="09"
    SYSFS{bDeviceProtocol}=="00"
    SYSFS{bDeviceSubClass}=="00"
    SYSFS{bMaxPower}=="  0mA"
    SYSFS{bNumConfigurations}=="1"
    SYSFS{bNumInterfaces}==" 1"
    SYSFS{bcdDevice}=="0206"
    SYSFS{bmAttributes}=="e0"
    SYSFS{configuration}==""
    SYSFS{devnum}=="1"
    SYSFS{idProduct}=="0000"
    SYSFS{idVendor}=="0000"
    SYSFS{manufacturer}=="Linux 2.6.12.5 ohci_hcd"
    SYSFS{maxchild}=="3"
    SYSFS{product}=="nVidia Corporation nForce2 USB Controller _#2_"
    SYSFS{serial}=="0000:00:02.1"
    SYSFS{speed}=="12"
    SYSFS{version}==" 1.10"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.1':
    BUS=="pci"
    ID=="0000:00:02.1"
    DRIVER=="ohci_hcd"
    SYSFS{class}=="0x0c0310"
    SYSFS{device}=="0x0067"
    SYSFS{irq}=="5"
    SYSFS{local_cpus}=="1"
    SYSFS{modalias}=="pci:v000010DEd00000067sv00001043sd00000C11bc0Csc03i10"
    SYSFS{subsystem_device}=="0x0c11"
    SYSFS{subsystem_vendor}=="0x1043"
    SYSFS{vendor}=="0x10de"

  looking at the device chain at '/sys/devices/pci0000:00':
    BUS==""
    ID=="pci0000:00"
    DRIVER=="unknown"


That's my rule for udev:
Code:

xaser3 mnt # cat /etc/udev/rules.d/10-udev.rules

# usb flash disk
BUS="usb", KERNEL="sd[a-z]1", SYSFS{model}=="Store _n_ Go    ", NAME="usb/flashdisk", SYMLINK="%k"


I've tried too to set SYSFS{product}=="Store _n_ Go" and it didn't work either. It doesn't create "/dev/usb/flashdisk" and it is not automounting.

What should i tried? I'm sure the problem is in 10-udev.rules . Each time i update this file, i even do "udevstart" to update!

Thank you and sorry for the long post. I just didn't know what was useful in udevinfo.
Back to top
View user's profile Send private message
krycek
n00b
n00b


Joined: 17 Mar 2005
Posts: 41
Location: Zürich, Switzerland

PostPosted: Sat Sep 24, 2005 3:01 pm    Post subject: Reply with quote

hi,
try
Code:

BUS="scsi" .....


thats what i need for my usb hd

k
Back to top
View user's profile Send private message
Mark Clegg
Apprentice
Apprentice


Joined: 05 Jan 2004
Posts: 270
Location: ZZ9 Plural Z Alpha

PostPosted: Sat Sep 24, 2005 3:27 pm    Post subject: Reply with quote

In your fstab entry

Code:
/dev/usb/flashdisk      /mnt/flash      vfat            noauto,user,sync,noatime        0 0


The user flag means anybody can mount the drive, and that person (and only that person or root) can unmount it. Since ivman runs as root, you are having to su to unmount it.

Change the "user" to "users" and anybody should be able to unmount it, regardless of who mounted it.
Back to top
View user's profile Send private message
AdShea
n00b
n00b


Joined: 10 Mar 2005
Posts: 62

PostPosted: Fri Oct 14, 2005 1:11 pm    Post subject: Reply with quote

A slightly more secure method is to make the stick belong to a group such as "disk" and then apply a umask like rwxrwx--- still using the users flag in fstab. This way only certian users can access or unmount the disk.
Back to top
View user's profile Send private message
Gotterdammerung
l33t
l33t


Joined: 11 Feb 2004
Posts: 627
Location: Rio de Janeiro, Brazil

PostPosted: Fri Oct 14, 2005 8:25 pm    Post subject: Reply with quote

Nice!
_________________
A mind that is stretched by a new experience can never go back to its old dimensions. - Oliver Wendell Holmes
Back to top
View user's profile Send private message
farmorg
Tux's lil' helper
Tux's lil' helper


Joined: 14 Jun 2003
Posts: 76
Location: Leicester (UK)

PostPosted: Wed Nov 09, 2005 2:28 pm    Post subject: Reply with quote

Hi

First off, thanks for the howto, i've got my usb flash drive working.
However, I made a slight change to the udev rule as I don't have an actual partition, just using the whole drive with vfat by doing:

Code:
#usb flash disk
BUS="usb", KERNEL="sdb", SYSFS{product}="Flash Disk      ", NAME="usb/flashdisk", SYMLINK="%k"

and

mkfs.vfat -I /dev/sdb


It's only a 256mb drive & making a sdb1 partition caused 2 mount points to be added (/media/usbdisk & /media/usbdisk1) which seems rather pointless & seemed to confuse things.

I am finding that it's very slow to copy data onto the drive using konqueror, copying some data from /tmp shows as 41.1kb/s and is taking forever.

I have hal,dbus & ivman running and when I plug the drive in, a line gets added to fstab thus:

Code:
/dev/usb/flashdisk      /media/usbdisk          vfat    user,exec,noauto,utf8,noatime,sync,managed 0 0


The kernel (2.6.12-gentoo-r6) is configured to use iso8859-1 as the default NLS for fat (the default NLS is utf8)

I am wondering if the problem is related to the utf8 entry that is added to fstab?

Why is it using utf8 when I told the kernel to use iso8859-1 ?

Can anyone shed any light here?

farmorg
_________________
Master farmorg

May the source be with you
Back to top
View user's profile Send private message
timbo
Apprentice
Apprentice


Joined: 29 Jul 2002
Posts: 231
Location: New Zealand

PostPosted: Sun Nov 13, 2005 1:46 am    Post subject: Reply with quote

Richard

Great little how-to thanks...

Now I can safely use my usb camera and Palm T3's sd card without the two crossing paths...

Just need to make sure you grab information from the "udveinfo..." listing and make the rule match .i.e. if you grab info from bus=scsi then your udev rule should state that too.

Regards
Tim
8)
_________________
Linux User: 303160
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum