[SOLVED] automounting of usb devices through command line
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Here is a c++ program that I use to detect and add device for usb
It may not be what you are looking for but it works great for me on Desktop Linux and Embedded Linux
How do you use this code? What exactly is it supposed to do? I couldn't figure out where in the code actual mounting of devices happened. It looks like it copies something between two hard coded devices. When I tried running it I got and endless stream of "Device Removed" messages and an occasionally it said "Device Added" (but nothing appeared to have been mounted).
I could imagine that a program that runs as a user and watches for devices could allow them to subsequently be unmounted by a user.
Well, I'm done telling you the same thing over and over only to be disregarded.
It works, as I have said. automatically. Every disk, every time.
unmounting requires sudo, but that can be programmitcally allowed via sudoers.
End Transmission.
I've tried my best to make your method work for me and to understand how it could be working for you. This is not "disregard."
My findings thus far are as follows. The udisks program appears to be equivalent to pmount in that it can mount and unmount devices. It does not appear to be able to mount or unmount devices automatically. This last observation is based on (1) the udisks man page, (2) the behavior of the program when I run it, and (3) statements made by others about how to configure automounting. I can find no evidence that udisks has any kind of daemon or other mechanism for finding new devices by itself when they are plugged in and then taking action.
Note that I am in the plugdev group. Further note that the ubuntu wiki that you referred to appears to be providing a mechanism for mounting a fixed specified device automatically at system start using udisks and then hard coding the device ID into a startup script. That's not useful, and would seem to provide further evidence that udisks does not do automounting. I want something that automatically mounts whatever device is plugged in, whether or not it's been seen before, and without special effort for each new device.
Assuming that we're communicating clearly and you do mean that if I visit you and plug in my USB stick it will mount without any actions on your part, then I have to assume that this occurs through some mechanism not yet described in this thread (e.g. udev rules that invoke udisks). Do you have anything in /etc/udev that refers to udisks? If you don't look into how your system works and figure it out and tell me...well, I certainly can't do it from here.
As another observation, Teufel posted a link to udev rules for pmount, and that made me aware of the -l option to pumount. I tested this out, and it does, indeed, prevent the problem of millions of unmount attempts filling up the file system. It's still not clean, exactly, but if someone pulls out an active device it doesn't eventually bring the system down.
Regarding the need for sudo, I'm uneasy having sudo work without a password. I think it's an aberration that I need to use sudo to unmount a device and the goal should be to make this easier, not to make trashing the OS easier. Maybe I need to revisit some way to make an suid version of pumount.
I put some effort into refining my udev rules and arrived at the following, posted here for anybody who cares.
Code:
# Place in /etc/udev/udev.rules to get usb device mounting
KERNEL=="sd[a-z]*", GOTO="start_auto_mount"
KERNEL!="mmcblk*", GOTO="end_auto_mount"
LABEL="start_auto_mount"
ACTION=="remove", ENV{dir_name}!="", RUN+="/usr/bin/pumount -l %N", GOTO="end_auto_mount"
ACTION=="add", PROGRAM=="/bin/sh -c 'echo Inserted %N, %E{ID_FS_TYPE}, %E{ID_FS_LABEL}, %E{ID_VENDOR} >> /tmp/ulog'"
ENV{ID_SERIAL}=="OLYMPUS_DVR_1001136620264000-0:0", ENV{dir_name}="ws802main"
ENV{ID_SERIAL}=="OLYMPUS_DVR_1001136620264000-0:1", ENV{dir_name}="ws802extra"
# Check that device has a type, and hence is mountable
ENV{dir_name}=="", ENV{ID_FS_TYPE}=="", GOTO="end_auto_mount"
# Construct mount point name
ENV{dir_name}=="", PROGRAM=="/bin/sh -c 'echo %E{ID_FS_LABEL} | /usr/bin/tr [:upper:] [:lower:] | /bin/sed s/[^a-z0-9-]/_/g'", ENV{dir_name}="%c"
ENV{dir_name}=="", PROGRAM=="/bin/sh -c 'echo %E{ID_VENDOR} | /usr/bin/tr [:upper:] [:lower:] | /bin/sed s/[^a-z]//g'", ENV{dir_name}="%c"
ENV{dir_name}=="", ENV{dir_name}="%k"
ACTION=="add", ENV{pmount_options}="--noatime"
ACTION=="add", ENV{ID_FS_TYPE}=="vfat", ENV{pmount_options}="%E{pmount_options} --umask 0111 --fmask 0111 --dmask 0000 -t vfat"
ACTION=="add", RUN+="/usr/bin/pmount %E{pmount_options} %N %E{dir_name}"
ACTION=="add", RUN+="/bin/sh -c 'export DISPLAY=:0.0;sudo -u adrian /usr/bin/notify-send Mounted\ `df %N --output=target|sed -n -e s#/media/## -e 2p`'"
#ACTION=="add", RUN+="/bin/sh -c 'export DISPLAY=:0.0;sudo -u adrian /usr/bin/notify-send Mounted\ %E{dir_name}'"
LABEL="end_auto_mount"
Note the special rules to handle the Olympus device that seems to appear with no file system type and then get skipped. The "udevadm info" command is helpful for determining how to write such rules.
However, I found that this fails with NTFS file systems. I get the message "transport endpoint is not connected." Hunting around I saw people stating that udev should not be used this way. (Nobody says why.) The proposed alternative is to use a udisks wrapper---presumably that's what habitual is (unknowingly?) doing.
It seems that udevil is a successor/alternative to udisks that was written because udisks2 has limitations, and it is packaged with a script called devmon. The devmon script runs in the background and uses udevil to determine when devices have been plugged in. It then uses udevil (by default) to mount the devices. This script works correctly with ntfs file systems and does a better job of cleaning up old mount points if the system goes down with devices plugged in, for example. So it does appear to be a better solution. It still fails out of the box on my Olympus device. To get it to work the way I wanted I had to force it to mount using pmount instead of udevil, because pmount will look in fstab and mount some devices at pre-specified locations, so if I add the Olympus device to my fstab it will mount. I also had to add code to force the volume label to lower case and pass that to pmount.
The result is working well and doing what I want. And the issue with unmounting has gone away spontaneously: it seems that (at least in my distro) pumount is now being installed suid by default, so I can unmount without sudo in general. It probably doesn't matter, though, because devmon runs as the user rather than as root.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.