Find the answer to your Linux question:
Results 1 to 8 of 8
Hello, Does anyone have any suggestion as to how to automatically run fsck.msdos -n on a USB flash drive when it is plugged in? I am developing hardware that writes ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2014
    Posts
    5

    automatically run fsck.msdos when USB flash drive is plugged in


    Hello,

    Does anyone have any suggestion as to how to automatically run fsck.msdos -n on a USB flash drive when it is plugged in? I am developing hardware that writes stuff to a USB stick and as you can imagine all kinds of bad stuff occasionally happens to the filesystem of the sticks that are inserted into the device when it is connected to the debugger. It would be very helpful to get immediate feedback when something is wrong to help simplify what can be a convoluted debugging process.

    I have tried adding the following line in it's own file in udev rules:
    BUS=="usb", SUBSYSTEM=="block", PROGRAM="/home/john/bin/checkDrive.pl &"

    in checkDrive.pl currently I pop up a big red popup window, the intent is once I see that the script is being called then to issue the fsck command and put the results in the popup window. One thing I don't like about this approach is that I need to run the fsck command as root which is probably not a good idea in a perl script. Not sure I understand why fsck needs to be run as root when I am not attempting to make any changes...

    I have run checkDrive.pl from a command line and it functions as expected.

    If anyone has a better approach I would welcome it, or if anyone has any suggestions as to how to make the approach I am trying to use work I would appreciate the input.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,533
    Will the udev rule PROGRAM="..." let you sudo the command?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux Engineer docbop's Avatar
    Join Date
    Nov 2009
    Location
    Woodshed, CA
    Posts
    930

  4. #4
    Just Joined!
    Join Date
    Feb 2014
    Posts
    5
    Right now I am not doing anything that requires sudo, I am simply poping up a big red window using a perl script. Once I am sure the perl script is bing called then I will add the useful content. So far using PROGRAM yields no results whatsoever. The big red window thing was intended to simply do something noticeable. When I change PROGRAM to RUN+="..." it changes the behavior of what happens when I plug in a device. Without that change when I plug in the flash drive it pops up nemo showing the flash drive's root window. When I alter the line to say run+=... it quits poping up nemo. Looking in syslog and the apparent udev log does not show anything useful. Is there a better way to get fsck to run on insert of usb media? It needs to run before the media is actually mounted so I have to use PROGRAM since I think RUN is executed after the device is mounted. Doing this should be a lot simpler than I am making it out to be. Also, I really don't want fsck to fix any problems, I just want it to tell me about them.

    Is it possible that udev is unable to call anything that interacts with the windowing environment?

  5. #5
    Just Joined!
    Join Date
    Feb 2014
    Posts
    5

    Additional information

    Instead of the perl script I am now calling a shell script that pops up a gxMessage, then launches an xterm, then sleeps 20. The gxMessage does not launch, nor does the xterm but there is a 20 second pause before the drive shows up in the "Places" menu.

    The rule that does seem to get triggered is:
    ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z]1", ATTRS{vendor}=="SanDisk ", RUN+="/usr/local/bin/driveChecker"

    I will need to eventually change this to match earlier in the connection process.

  6. #6
    Just Joined!
    Join Date
    Feb 2014
    Posts
    5
    I now have it somewhat working, here is the rule I am using:
    Code:
    KERNEL=="sdc*", SUBSYSTEM=="block", PROGRAM+="/usr/local/bin/driveChecker %k"
    Here is the one line in my shell script:
    Code:
    `fsck -n /dev/$1 >/home/john/temp/driveChecker.txt`
    I then watch that file while inserting the stick. It would be really nice to 1) have a messagebox or something pop up with the results of the command instead of directing it at a file, and 2) Restore the functionality of a filemanager window launching with the contents of the USB thumbdrive. With that rule in place I no longer get nemo launching when I insert a flashdrive.

  7. #7
    Linux Guru rokytnji's Avatar
    Join Date
    Jul 2008
    Location
    Desert
    Posts
    4,043
    It would be really nice to 1) have a messagebox or something pop up with the results of the command instead of directing it at a file,
    It's over my head (whoooosh) pay grade but YAD | Free Development software downloads at SourceForge.net

    It is what we use for a custom script gui in AntiX.
    Linux Registered User # 475019
    Lead,Follow, or get the heck out of the way. I Have a Masters in Raising Hell
    Tech Books
    Free Linux Books
    Newbie Guide
    Courses at Home

  8. #8
    Just Joined!
    Join Date
    Feb 2014
    Posts
    5

    My current solution

    My current solution so far involves a couple of scripts, first there is the rule itself:
    Code:
    KERNEL=="sdc*", SUBSYSTEM=="block", PROGRAM+="/usr/local/bin/driveChecker %k"
    Then the shell script at /usr/local/bin:
    Code:
    #!/bin/bash
    
    `fsck -n /dev/$1 >/home/john/temp/driveChecker.txt`
    I am considering replacing the last line with something like this:
    `fsck -n /dev/$1 | wall`

    Not terribly clean, but would get the job done.
    Currently my solution includes one last file that when I want to be monitoring USB insertions I launch this from a terminal:
    Code:
    #!/usr/bin/perl
    use Tk;
    use strict;
    
    my $oldDateTimeStamp=`ls -lart /home/john/temp/driveChecker.txt`;
    my $newDateTimeStamp=0; 
    my $fileContents="";
    
    my $mw = new MainWindow;
    $mw->withdraw();#hide the main window
    
    while(1)
    {
    	$newDateTimeStamp=`ls -lart /home/john/temp/driveChecker.txt`;
    
    	if($oldDateTimeStamp ne $newDateTimeStamp)
    	{
    		$fileContents=`cat /home/john/temp/driveChecker.txt`;
    		$oldDateTimeStamp=$newDateTimeStamp;
    		#Here we are eliminating device removals which also trigger the udev rule
    		#index returns -1 if the substring is not found in the string, 
    		#otherwise the index is returned
    		if( (index($fileContents, "Possibly non-existent device?") lt 0) && (index($fileContents, "correct ext2") lt 0 ) )
    		{
    			$mw -> messageBox(-message=>"$fileContents", -width=>"80", -height=>"10", -wraplength=>"6i" );
    		}
    	}
    	sleep 1
    }
    One of the troubles is that the rule that monitors insertions gets fired multiple times on both insertion and removals. At some point I will have to become more versed in udev rule making to stop this undesirable behavior. Ideally it will only fire on insertion, just before the drive is actually mounted.

    It would appear that you may not interact directly with the windowing environment from within a udev rule or a script called by udev, which is why the last perl script.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •