Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
How do I use hotplug to automatically mount things?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
g4c9z
Apprentice
Apprentice


Joined: 03 Jun 2004
Posts: 178

PostPosted: Sat Aug 21, 2004 5:37 pm    Post subject: How do I use hotplug to automatically mount things? Reply with quote

Hello,

What is the correct way to set up hotplugging to automatically mount a USB Mass Storage device (such as my ZIP drive) when it gets plugged in?

I created a script to mount it, which I saved as /etc/hotplug.d/usb/mount.hotplug, and it gets run when any USB device is plugged in it seems. However, the script depends on the device file for the ZIP disk to exist in /dev, and it seems like that gets created AFTER the script runs.

The hotplug documentation seems to suggest that your script is supposed to be saved as /etc/hotplug/usb/usb_storage (more generally, as /etc/hotplug/NAME/MODULE, where NAME is the type of hotplugging going on and MODULE is the name of the module that gets loaded). But that script didn't even run.

I know about supermounting and I don't like it because it does continuous polling rather than waiting for an interrupt. I also know about usb-mount and it's too complicated plus I want to give it a nice mount point rather than a general one, which that script does.

I'm using udev - I don't know whether it can help out.
Back to top
View user's profile Send private message
Nate_S
Guru
Guru


Joined: 18 Mar 2004
Posts: 414

PostPosted: Sat Aug 21, 2004 6:27 pm    Post subject: Reply with quote

I'm not sure you can use hotplug for that purpose, however, I can think of a couple options that you could use. First, I believe you can have udev run scripts, you might be able to use that somehow. Or better yet, look into using HAL/DBUS/[GVM/IVMAN] I'm using Gnome Volume Manager for automounting, and it's working great. Ivman would be better if you don't use gnome. Search the forum, there's been a lot of threads about it.

-Nate
Back to top
View user's profile Send private message
Nate_S
Guru
Guru


Joined: 18 Mar 2004
Posts: 414

PostPosted: Sat Aug 21, 2004 6:28 pm    Post subject: Reply with quote

I'm not sure you can use hotplug for that purpose, however, I can think of a couple options that you could use. First, I believe you can have udev run scripts, you might be able to use that somehow. Or better yet, look into using HAL/DBUS/[GVM/IVMAN] I'm using Gnome Volume Manager for automounting, and it's working great. Ivman would be better if you don't use gnome. Search the forum, there's been a lot of threads about it.

-Nate
Back to top
View user's profile Send private message
MighMoS
Guru
Guru


Joined: 24 Apr 2003
Posts: 416
Location: @ ~

PostPosted: Sat Aug 21, 2004 6:37 pm    Post subject: Reply with quote

Hotplug is the base of hal, dbus, and ivman. I think breakmygentoo has all the ebuilds you would need (as I don't think ivman is in portage yet, and the rest are masked)
_________________
jabber: MighMoS@jabber.org

localhost # export HOME=`which heart`
Back to top
View user's profile Send private message
g4c9z
Apprentice
Apprentice


Joined: 03 Jun 2004
Posts: 178

PostPosted: Sat Aug 21, 2004 6:45 pm    Post subject: Reply with quote

Quote:
First, I believe you can have udev run scripts, you might be able to use that somehow

Do you mean the PROGRAM= section of a udev rule? I too considered that, but I think it's for doing a query using a certain program to find out what to name a device. That means it would also be run before the /dev device exists.

I'll have to search for IVMAN.

Quote:
I think breakmygentoo has all the ebuilds you would need


What's breakmygentoo?
Back to top
View user's profile Send private message
g4c9z
Apprentice
Apprentice


Joined: 03 Jun 2004
Posts: 178

PostPosted: Sat Aug 21, 2004 7:04 pm    Post subject: Reply with quote

It seems like ivman is for CD-ROM drives; do you really think it handles USB devices? Though I am also looking for a way to automount my CD-ROM drives. It also seems to still be in active development.

I wonder: does ivman sleep until an interrupt, or do constant polling to check whether a disk is in the drive? If the latter then it's not really what I'm looking for.

I'm sure hotplugging must allow it; otherwise how could usb-mount work? (By the way, it's at:
http://users.actrix.co.nz/michael/usbmount.html
)
Back to top
View user's profile Send private message
g4c9z
Apprentice
Apprentice


Joined: 03 Jun 2004
Posts: 178

PostPosted: Tue Aug 24, 2004 1:34 am    Post subject: Reply with quote

I figured out how to do it using udev!

According to "man udev":
Quote:
After device node creation, removal, or network device renaming, udev executes the programs in the directory tree under /etc/dev.d/.
So programs named /etc/dev.d/default/*.dev are run when anything is plugged/unplugged, while /etc/dev.d/$DEVICENAME/*.dev get run when the device named $DEVICENAME is plugged/unplugged. It seems like /etc/dev.d/usb/*.dev are supposed to be run when any USB device is plugged/unplugged, but I couldn't get that one to work - maybe "usb" isn't the name of the subsystem?

So anyway, I created the following script to mount / unmount the device:

Code:
#!/bin/bash
if [ -b $DEVNAME ]; then
        su adam -c "mount $DEVNAME"
else
        su adam -c "umount $DEVNAME"
fi


I put links to it at /etc/dev.d/zip/mount.dev and /etc/dev.d/camera/mount.dev for my ZIP disk and digital camera repectively. And now they get mounted when I plug them in!

Actually, the unmounting doens't get done, though it seems like that line of the script should make it unmount. Anyway, it's usually bad practice to unplug something before unmounting it (even Windows doesn't let you! :))

The reason I run the commands as "adam" is so the disks are mounted for me instead of root. Since I'm practically the only user of my computer, I can safely assume I'm the one who wants to use the device. This is actually a major design issue on a mulit-user system - if we want automounting, and there are multiple users logged in locally, what kind of permissions do you give the disk that was just inserted? This dilemma is probably the main reason mounting doesn't normally happen automatically in Linux. (I wonder if there should be a way to distinguish the "active" user from the other users, kind of like the way windows get the focus in a GUI environment?)

By the way, for my method to work, you have to set up /etc/fstab, and you have to configure udev so that the NAME of the device (NOT the symbolic link) corresponds with the device's name in fstab.

The more I learn about udev, the more I realize how well-designed it is.
Back to top
View user's profile Send private message
Nate_S
Guru
Guru


Joined: 18 Mar 2004
Posts: 414

PostPosted: Tue Aug 24, 2004 9:23 pm    Post subject: Reply with quote

If you want to be able to remove drives safely (well semi-safely, but as safe as windows does it) without unmounting, add the sync option to your fstab. on the downside you lose performance.

IVMAN will work just fine for USB devices. I'm using GVM (like ivman but for gnome) to mount my usb drives and it works just fine.

As far as for polling vs interrupt, I'm pretty sure it waits for an interrupt. It runs off of DBUS and HAL (which in turn run off hotplug) For all you ever wanted to know and more about them, look here:
http://www.freedesktop.org/Software/dbus
http://www.freedesktop.org/Software/hal

The permissions thing is more a filesystme issue than anything. If you don't need to share with a windows machine, just put ext3 on the drive, and set up permissions like on your hard drive. As an added bouns, it's journaling, so there's even less risk of damage if you unplug it without unmounting.

Or, if you need it to be FAT, then you can always just set umask=777 in your fstab, then everyone has full access.

Also, windows doesn't let multiple users be "active" at once; linux does. Really, the active user is the one logged in, however, if this needs to be root, then no, there is no good way to know which user root is running commands on behalf of. (by no good way, I mean it'd be pretty much impossible to make it secure)

-Nate
Back to top
View user's profile Send private message
g4c9z
Apprentice
Apprentice


Joined: 03 Jun 2004
Posts: 178

PostPosted: Tue Aug 24, 2004 9:55 pm    Post subject: Reply with quote

Quote:

The permissions thing is more a filesystme issue than anything. If you don't need to share with a windows machine, just put ext3 on the drive, and set up permissions like on your hard drive. As an added bouns, it's journaling, so there's even less risk of damage if you unplug it without unmounting.

That's true, actually. Most of those problems could be resolved using a filesystem with permissions, which FAT doesn't have. Too bad in my case I want to have a FAT-formatted ZIP disk so I can use it in the compuers at my university. And in the case of all digital cameras I know of, they must be FAT-formatted if they act like a disk at all.

There's still the problem of who is allowed to unmount it, but in my case it's OK to allow anyone to unmount it no matter who mounted it.

Also, when you use external disks with permissions, problems can arise if the disk has files owned by a user of another computer (remember that a main purpose of disks is to move files between arbitrary computers). How can the computer know about that user? So basically either files owned by that user are unavailable (the safe but annoying alternative) or they're available to anyone (which that user mgiht not want). For that reason it seems best to me to just keep removable disks with a permissionless file system, i.e. FAT.

Quote:
Or, if you need it to be FAT, then you can always just set umask=777 in your fstab, then everyone has full access.

That's something I'd rather not do, because it means if someone can log in remotely to my computer, even as a guest user with practically no privelages, they could lay waste to my ZIP disk - or, maybe worse, the pictures I just took with my digital camera!

Quote:

Also, windows doesn't let multiple users be "active" at once; linux does.

Really? How do you tell who is the active user in Linux? Can't you push Ctrl+Alt+F2 and log in as someone else, with no difference between that user and the original user?

And I think Windows XP has an "active" user, because you have to go to Start -> Switch User or something if someone else wants to log in. Of course, older Windows versions have only 1 user.
Back to top
View user's profile Send private message
keyser_soze
Apprentice
Apprentice


Joined: 19 Oct 2004
Posts: 162
Location: Australia

PostPosted: Tue Oct 19, 2004 2:53 am    Post subject: Reply with quote

I know this thread was a while ago

I worked out why your usb device was not unmounting.

It is becuase your script checks to see if the device is not in /dev but the script is run prior to the device node being removed from /dev so it will never run the umount commands.

I made an alternate script which creates a flag file called umounted and mounted to deal with this. They contain verbose info about the mounts and unmounts just for fun and can help debug a bit too.

I also used a similar process to start up cups and hpoj for scanning when my printer/scanner is switched on... pretty cool actually. don't think windows can do that! not as easily anyway.

This one will mount and unmount a usb device.
at the moment you have to have a folder $DEVNAME in /etc/dev.d for it to work and an entry in /etc/fstab for the device

The 10sec wait is just cause my md player took a while to let me mount it. its just failsafe stuff. you prob won't need it with most things.

Also note that as mentioned above with this method you are actually unplugging the device before it is unmounted... be aware.

Code:
#!/bin/bash
MOUNTED=mounted
UMOUNTED=umounted
DEVNAME=HiMD
DEV=/dev
DEVD=/etc/dev.d
MOUNTOPTS=-v
WAIT="10"

if [ -f "$DEVD/$DEVNAME/$UMOUNTED" ]; then
   if [ -b "$DEV/$DEVNAME" ]; then
      sleep "$WAIT"
      mount "$MOUNTOPTS" "$DEV/$DEVNAME" >> "$DEVD/$DEVNAME/$MOUNTED"
      rm -f "$DEVD/$DEVNAME/$UMOUNTED"
   fi
elif [ -f "$DEVD/$DEVNAME/$MOUNTED" ]; then
   if [ -b "$DEV/$DEVNAME" ]; then
      umount "$MOUNTOPTS" "$DEV/$DEVNAME" >> "$DEVD/$DEVNAME/$UMOUNTED"
      rm -f "$DEVD/$DEVNAME/$MOUNTED"
   fi
fi
exit 0



This one will start cups and hpoj when you start your printer up.
Naturally you will want to change the parameters to suit your printer but it should work no worries.

Code:
#!/bin/bash
MOUNTED=mounted
UMOUNTED=umounted
DEVNAME=psc1315
DEV=/dev
DEVD=/etc/dev.d
CUPSD=/etc/init.d/cupsd
HPOJD=/etc/init.d/hpoj

if [ -f "$DEVD/$DEVNAME/$UMOUNTED" ]; then
   if [ -c "$DEV/$DEVNAME" ]; then
      "$CUPSD" start >> "$DEVD/$DEVNAME/$MOUNTED"
      "$HPOJD" start >> "$DEVD/$DEVNAME/$MOUNTED"
      rm -f "$DEVD/$DEVNAME/$UMOUNTED"
elif [ -f "$DEVD/$DEVNAME/$MOUNTED" ]; then
   if [ -c "$DEV" ]; then
      "$CUPSD" stop >> "$DEVD/$DEVNAME/$UMOUNTED"
      "$HPOJD" stop >> "$DEVD/$DEVNAME/$UMOUNTED"
      rm -f "$DEVD/$DEVNAME/$MOUNTED"
   fi
fi
exit 0
Back to top
View user's profile Send private message
g4c9z
Apprentice
Apprentice


Joined: 03 Jun 2004
Posts: 178

PostPosted: Tue Oct 19, 2004 6:21 pm    Post subject: Reply with quote

Thanks for telling me what was wrong. Is it really necessary to make things that complicated, though? I would think there would be a way, in the udev script, to test whether it was a removal or an insertion of a device.
Back to top
View user's profile Send private message
keyser_soze
Apprentice
Apprentice


Joined: 19 Oct 2004
Posts: 162
Location: Australia

PostPosted: Wed Oct 20, 2004 12:54 am    Post subject: Reply with quote

dont' know.

I just discovered this yesterday.

As far as I know it just runs whatever scripts are there when it removes and adds the devices... don't know if there is any kind of flag it sends??
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware 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