LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 10-14-2015, 10:27 AM   #16
mchroman
LQ Newbie
 
Registered: Feb 2015
Posts: 2

Rep: Reputation: Disabled

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


Code:

#include <usb.h>
#include <stdio.h>
#include <iostream>
#include <fstream>




const std::string currentDateTime() 
{
    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
    // for more information about date/time format
    strftime(buf, sizeof(buf), "Y-%m-%d.%X", &tstruct);
    std::string retval;
    retval.append("[ ");
    retval.append(buf);
    retval.append(" ] ");
    return retval;
}


main()
{
    
    struct usb_bus *bus;
    struct usb_device *dev;
    usb_init();
    int oldCount = 0;
    int newCount = 0;
    int isInStartup = 1;    
    while(1)
    {
	sleep(1);
	oldCount = newCount;
	newCount = 0;
        usb_find_busses();
        usb_find_devices();
        for (bus = usb_busses; bus; bus = bus->next)
        {

            for (dev = bus->devices; dev; dev = dev->next)
            {
		newCount++;
	    }
            if(isInStartup) 
	    {
		isInStartup = 0;
		continue;
	    }
	    if(oldCount > newCount)
	    {
		printf("Device Removed\n");
	    }
	    else if(oldCount < newCount)
	    {
	        std::cout << "Device Added\n";
    		{
			remove("/media/sda1/NewDevice");
			sleep(2);
			std::ifstream source("/media/mmcblk1p1/NewDevice", std::ios::binary);
			sleep(1);
    			std::ofstream dest("/media/sda1/NewDevice", std::ios::binary);
			sleep(1);
    			dest << source.rdbuf();
			sleep(2);
    			source.close();
    			dest.close();
			std::cout << "Finished Copying File\n";
	
    		}//if    
    	    } // else
	} // for
} // while

return 0;
}// main
 
Old 10-28-2015, 08:18 AM   #17
adrianmariano
Member
 
Registered: Dec 2004
Distribution: Ubuntu Yakkety
Posts: 193

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by mchroman View Post
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.
 
Old 10-28-2015, 08:53 AM   #18
adrianmariano
Member
 
Registered: Dec 2004
Distribution: Ubuntu Yakkety
Posts: 193

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Habitual View Post
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.
 
Old 11-09-2015, 10:29 AM   #19
adrianmariano
Member
 
Registered: Dec 2004
Distribution: Ubuntu Yakkety
Posts: 193

Original Poster
Rep: Reputation: 15
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to measure the speed of SATA devices from the command line on Linux LXer Syndicated Linux News 1 04-04-2013 02:05 AM
usb/cd automounting but access through command line stu_mueller Slackware 5 04-24-2008 02:17 AM
Automounting CD/DVD and USB devices Tux-Slack Slackware 16 06-26-2007 02:04 PM
KDE - automounting devices as user Bruno07 Linux - Hardware 1 02-07-2007 02:25 AM
automounting usb devices with usbmount works well but i have one problem mechmg93 Debian 9 10-23-2005 07:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration