Find the answer to your Linux question:
Results 1 to 8 of 8
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

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


    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/ &"

    in 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 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
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    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
    Woodshed, CA

  4. $spacer_open
  5. #4
    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?

  6. #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.

  7. #6
    I now have it somewhat working, here is the rule I am using:
    KERNEL=="sdc*", SUBSYSTEM=="block", PROGRAM+="/usr/local/bin/driveChecker %k"
    Here is the one line in my shell script:
    `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.

  8. #7
    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

    It is what we use for a custom script gui in AntiX.
    I refuse to let fear and fear of others rule my life. It puts my humanity at risk.
    Accepting Death is the only way to stay alive.

  9. #8

    My current solution

    My current solution so far involves a couple of scripts, first there is the rule itself:
    KERNEL=="sdc*", SUBSYSTEM=="block", PROGRAM+="/usr/local/bin/driveChecker %k"
    Then the shell script at /usr/local/bin:
    `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:
    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
    	$newDateTimeStamp=`ls -lart /home/john/temp/driveChecker.txt`;
    	if($oldDateTimeStamp ne $newDateTimeStamp)
    		$fileContents=`cat /home/john/temp/driveChecker.txt`;
    		#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