LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   How to write udev rules for USB hard disk drives (https://www.linuxquestions.org/questions/slackware-14/how-to-write-udev-rules-for-usb-hard-disk-drives-826618/)

mscole 08-16-2010 05:10 PM

How to write udev rules for USB hard disk drives
 
Hi, I just bought a USB hard disk and when I plug it in it gets mounted as owner root and group root. I tried writing a udev rule for it based on the following guide:

http://www.reactivated.net/writing_udev_rules.html

Nothing seems to work, it always mounts as root. Slackware 13.1, KDE, Dell Optiplex GX280, Western Digital Passport USB 320 gig drive.

I am a member of the plugdev group, so it seems to me a rule should not even be necessary. Any ideas? Mike.

Richard Cranium 08-16-2010 05:34 PM

Well, the mounting is done via HAL so you'd have to change stuff there.

What desktop environment are you running? That's what should be listening to hald via dbus and generating the mount command.

mscole 08-16-2010 05:59 PM

KDE. Can you point me to something that explains how to make the changes in HAL?

GazL 08-16-2010 06:11 PM

HAL/The Desktop should obey any settings in /etc/fstab when mounting, so just put an entry in there specifying whatever options you want it to use.

Because it's an external usb device I'd recommend you use LABEL= or UUID= rather than the /dev/sd?? device in the /etc/fstab because the device name may vary when the drive is plugged-in in future.

T3slider 08-16-2010 06:18 PM

Is it an NTFS-formatted drive? If so it is possible that it is trying to mount read-only using the standard kernel module instead of using ntfs-3g through FUSE. I do not know why it would mount as root:root though -- it should still be using the plugdev group. An fstab entry is a foolproof solution but it would not explain why it is getting mounted with the wrong ownership (and permissions?) by default when using HAL.

mscole 08-16-2010 06:21 PM

The first thing I did was delete the original partition, create a linux partition, and format it ext3. Mike.

GazL 08-16-2010 06:25 PM

Ahh, I was working on the principle that it was a NTFS.
if it's an ext filesystem, then the permissions will be those of the top level directory of the filesystem it contains. Just mount it as root and then do a standard chown/chmod on it.

mscole 08-16-2010 06:35 PM

But won't that be temporary? I would have to do that every time I boot, or unplug and re-plug, wouldn't I?

GazL 08-16-2010 06:43 PM

No. the permissions/ownership are stored in the filesystem itself. So next time you mount the filesystem the values will still be as you set them.

Just remember to chmod after you mount it, otherwise you'll be setting the permissions of the mountpoint directory it's mounted on and not of the top directory of the filesystem.

You might want to consider chmod 1777 which will set it up to work exactly like /tmp. i.e. everyone can use it, but you can only delete your own files/directories.

Then if you add an fstab entry with "noauto,user" any user should be able to mount it.


That's how I do this sort of thing anyway.

Richard Cranium 08-16-2010 06:46 PM

Quote:

Originally Posted by mscole (Post 4068257)
KDE. Can you point me to something that explains how to make the changes in HAL?

http://www.mythic-beasts.com/~mark/random/hal/ is the best thing that I could find.

http://freedesktop.org/wiki/Software/hal has what little else there is (and it leads you to the above link). You'd think that someone would have documented the damned thing after a year or so.

Richard Cranium 08-16-2010 06:48 PM

Quote:

Originally Posted by GazL (Post 4068294)
No. the permissions/ownership are stored in the filesystem itself.

Unless he's using the links autogenerated in /media. In Gnome, Nautilus would be responsible for creating the directories; in KDE something else is doing that work.

GazL 08-16-2010 06:56 PM

Quote:

Originally Posted by Richard Cranium (Post 4068299)
Unless he's using the links autogenerated in /media. In Gnome, Nautilus would be responsible for creating the directories; in KDE something else is doing that work.

Once the filesystem is mounted though, those directories will have been overmounted so their permissions won't matter. That's Unless gnome/kde desktops do a chmod/chown after the filesystem is mounted to alter the permissions of the top directory (In which case; OMFG! NO!!!!! and I'm even more glad I do things the old fashioned way! ;) ).

mscole 08-16-2010 07:01 PM

GazL, your solution worked. I added a line to /etc/fstab, made a directory for it: /mnt/passport, mounted as root, chown, re-booted, and the disk was mine. I too still prefer the old-fashioned way. Mike.

GazL 08-16-2010 07:09 PM

Quote:

Originally Posted by mscole (Post 4068314)
GazL, your solution worked. I added a line to /etc/fstab, made a directory for it: /mnt/passport, mounted as root, chown, re-booted, and the disk was mine. I too still prefer the old-fashioned way. Mike.

It's certainly simpler than all this udev/hal nonsense. ;)

Glad you got it sorted Mike.

:)

/bin/bash 11-04-2010 05:46 PM

Here is how I mount usb sticks. The udev rules are not that hard for usb drives, you just create one rule then copy it for all your drives and all you have to change is the serial no. ( ATTRS{serial}== ) which is easy to find using 'lsusb -v|less'.

This is one of my rules in /etc/udev/rules.d/01-automount.rules
KERNEL=="sd?1", ATTRS{serial}=="01234567890" , ACTION=="add", RUN+="/usr/local/bin/automountit KINGDT2G01 -m -r"
KERNEL=="sd?1", ATTRS{serial}=="01234567890" , ACTION=="remove", RUN+="/usr/local/bin/automountit KINGDT2G01 -u"

The automountit script will create the directory /media/KINGDT2G01 then mount the device and set the permissions. And as an added convenience it modifies the Dolphin.desktop file so when I clik on it dolphin opens with split panels and it is already set to the device's root directory and the other panel is $HOME.

So all I do is plug in the device, wait a couple seconds and it is mounted and ready to go. Also when I remove the device the automountit script will remove the /media directory and return my Dolphin.desktop back to normal.

NOTE: KINGDT2G01 is just a name I made up to identify the device, it stands for Kingston DataTraveler 2G No. 1. I have quite a few usb sticks and this makes it alot easier to keep track of them.

Also I use /dev/disk/by-id or /dev/disk/by-label in the /etc/fstab file, as was mentioned in an earlier post /dev/sdX1 is not a reliable way to mount removable usb drives.


All times are GMT -5. The time now is 10:50 PM.