LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-09-2007, 07:11 PM   #1
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Unmounting USB devices


It is an unfortunate fact of life that we have to manually tell Linux to unmount our USB device before disconnecting it. If we fail to do this udev gets all confused and the device may not remount correctly until you stop/restart udev.

This is annoying.

So I cobbled together a little perl script that I run as a cron every minute or so to determine if I have unplugged a USB device without first dismounting it, and if I have, it does the dismount for me.

Here is how it works;

When you disconnect a mounted usb device, that device's entry in /dev is promptly removed. However, the device is still considered mounted at the mountpoint. Thus, you might have disconnected the camera which causes /dev/camera to go away, but the camera is still shown as mounted at, for instance, /media/camera. This mount will show both in the relevant mtab and in /proc/mounts.

This script reads /proc/mounts, looking for anything defined at any location in /dev. When it finds such a device, it tests for the existence of the node in /dev. If that node does not exist, then this is a device that has been disconnected. In this event, the script does a umount on the specified mountpoint.

It seems to work OK, and I don't think it is dangerous. Some might find it helpful. If anyone can suggest an improvement - or even better, a way to get something like this to happen automatically when something is disconnected, then please step right up and tell me.

Code:
#!/usr/bin/perl
#
# Runs periodically to clean up the mountlist under condition
# where someone unplugs a usb device without first
# unmounting it.
#
# Written by Jim Locker 
#
$DEVFILE = "/proc/mounts";
$ME = $0;
if (! open (DEVNUM, "<$DEVFILE"))
{
	print "$ME: cannot open '$DEVFILE'\n";
	exit 1;
}

while ($line = <DEVNUM>)	# read a text line from DEVNUM
{
	@fields = split / +/, $line;
	if(@fields[0] =~ m/^\/dev\//) {
		$command = "ls -l @fields[0]";
		if(system( "$command &> /dev/null")){
			system("umount @fields[1]");
		}
	}
} # end while DEVNUM
close (DEVNUM);
# END.

Last edited by jiml8; 06-09-2007 at 07:14 PM.
 
Old 06-10-2007, 12:31 PM   #2
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,418

Rep: Reputation: Disabled
I use autofs with a --timeout=3 switch in my /etc/auto.master file. If I or a program is not browsing the mount point dir then it automatically unmounts after 3 seconds
 
Old 06-10-2007, 01:31 PM   #3
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
You can have udev run a script when the device is inserted/removed (i.e. when the device node is created/deleted). The script can do whatever you want, including unmounting the device.

I think you add something like

RUN+="/path/to/my/script"

to your udev rule.

and the script can test the environment variable $ACTION, which will indicate the action that is being performed ("add" or "remove").

Have a look at /usr/share/doc/udev-(version)/docs/writing_udev_rules/index.html.
 
Old 06-10-2007, 09:34 PM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
  1. Which distro are you having this problem w/? I'm running SimplyMEPIS 6.0, & this is a non-issue.

  2. Even if a script were necessary, why are you wrapping what is essentially a bash script inside Perl? -- It's shorter, more direct, & probably faster to call bash commands directly. BTW, when I re-wrote your code in bash. it was just over half the size.

  3. Finally, are you aware that '/' is not the only possible delimiter for regexes? Where the regex follows a command like "m", the next character becomes the delimiter for that regex:
    Code:
    # so
    m,^/dev/,
    # or
    m!^/dev/!
    # not just 
    m/^\/dev\//
 
Old 06-30-2007, 12:16 AM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 116Reputation: 116
Actually, it turns out that my Mandriva system simply isn't running any udev rules when a usb device is removed. I don't know why, but I've confirmed it. My Kubuntu system IS running the rules when a device is removed and is properly unmounting removed devices.

I've had some more issues with USB devices, and started another thread to describe them.

And as for why I wrote that script in perl...just because. No particular reason, other than I felt like writing some perl that day.

Last edited by jiml8; 06-30-2007 at 12:17 AM.
 
Old 06-30-2007, 10:03 AM   #6
dracolich
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 1,224

Rep: Reputation: 56
why are you removing unmounted devices? Isn't the purpose of unmounting to prevent data loss or corruption? I've seen many people upset, some enraged, because their files are missing. The reason was that they yanked the disk out without stopping it with the Windows system tray icon. The same thing can happen in Linux because I've lost a few files that way when a disk refused to unmount, even during shutdown.
 
Old 06-30-2007, 10:54 AM   #7
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by dracolich
why are you removing unmounted devices?
Uhhh...because I don't want the entry in /dev or in mtab if the device has been unplugged? Because I could wind up over time running out of useable entries in /dev? Because I want to take the device with me, and when it is disconnected, the system doesn't need to think it is still connected?

Quote:
Isn't the purpose of unmounting to prevent data loss or corruption?
I suppose that's one reason. Other reasons include to avoid tying up system resources, or to be able to disconnect the device. In the case of a camera or a pen drive, I certainly want to be able to disconnect it.

Quote:
I've seen many people upset, some enraged, because their files are missing. The reason was that they yanked the disk out without stopping it with the Windows system tray icon. The same thing can happen in Linux because I've lost a few files that way when a disk refused to unmount, even during shutdown.
A device REFUSING to unmount always points to a bug. Could be a hardware issue; could be a software issue, but always a bug.

This here isn't a case of a device REFUSING to unmount, it is a case of the system not automatically unmounting a USB device that has been removed. If it happens that I don't disconnect properly and safely, that's my problem. But when the device is removed, the system should dismount it and clean up the hardware tables. Period.
 
Old 06-30-2007, 11:43 AM   #8
dracolich
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 1,224

Rep: Reputation: 56
Quote:
Originally Posted by dracolich
why are you removing unmounted devices?
What I meant was unplugging the device without first unmounting it. I do agree that, when this happens, the system should recognize or be told that the device is physically removed and make necessary adjustments. I'm just the type that tries to encourage people to remember to unmount their disks properly before unplugging them. An ounce of prevention is worth a pound of cure. Sorry for any offense.
 
Old 07-01-2007, 09:50 AM   #9
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
Quote:
Originally Posted by jiml8
A device REFUSING to unmount always points to a bug. Could be a hardware issue; could be a software issue, but always a bug.
No it doesn't. You cannot unmount a device that is in use.

Quote:
Originally Posted by jiml8
But when the device is removed, the system should dismount it and clean up the hardware tables. Period.
Yes, it should, and it's irritating when it doesn't. But this requires userspace support (because for example, the kernel doesn't mess around with userspace files like /etc/mtab, nor should it).

In any case, you should always unmount a writeable filesystem before removing the device it's on, unless you don't care about your data (though I suppose you can "get away" with it if you use the sync option).

As for Mandriva, what does udev output to syslog? This will often indicate the problem.
 
Old 07-02-2007, 10:27 AM   #10
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by dawkcid
No it doesn't. You cannot unmount a device that is in use.
When I say "unmount", the system should say: "busy...what can be done to make it not-busy" and the system should then do that thing. Then unmount it. The system could give me a confirmation requester; "this device is in use, are you really sure you want to unmount it?". Then it should unmount it, if I confirm. This should even apply to the system drive, though this might force an automatic shutdown. I control the system; if I say do it, the system should do it.


Quote:
Yes, it should, and it's irritating when it doesn't. But this requires userspace support (because for example, the kernel doesn't mess around with userspace files like /etc/mtab, nor should it).
OK.

Quote:
In any case, you should always unmount a writeable filesystem before removing the device it's on, unless you don't care about your data (though I suppose you can "get away" with it if you use the sync option).
Stipulated and agreed. But when the removable device is removed, the system should always clean up the mess left behind. Period. Entries in /dev should go away; entries in mtab should go away.

Quote:
As for Mandriva, what does udev output to syslog? This will often indicate the problem.
Entries at connect time are typical and ordinary; identification of the device, establishment of the /dev entries, mounting (or failure to mount) of the filesystem, and so forth. On removal without dismount of a camera or pen drive, dmesg contains messages like this:
Code:
FAT: Directory bread(block 19) failed
 92:0:0:0: rejecting I/O to dead device
which will be repeated a couple of dozen times before aborting, while /var/log/messages contains a routine message from the usb driver that a device has been disconnected.

When the device is properly dismounted (which in Mandriva running KDE means that the "safely remove" option is first selected from the icon menu that represents the device) then both dmesg and /var/log/messages have a routine message that the device has been disconnected.

In neither case does a udev rule get run, and in neither case are the entries in /dev and in mtab removed.

Actually, I'm quite sure that something about this mandriva system isn't right; a few days ago I started another thread here about my experiences plugging in a PNY pen drive:
http://www.linuxquestions.org/questi...31#post2807931

Last edited by jiml8; 07-02-2007 at 04:54 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
USB unmounting stork SUSE / openSUSE 1 10-17-2005 03:51 AM
Unmounting USB Flash drive alanbarnard JDS 2 02-20-2005 11:16 AM
unmounting usb memory stick steven2357 Linux - Newbie 6 10-07-2004 06:35 AM
unmounting usb camera matt3333 Slackware 3 10-15-2003 08:16 AM
unmounting my usb drive flinchumj Linux - General 6 05-29-2003 10:16 AM

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

All times are GMT -5. The time now is 10:05 PM.

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