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.
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.
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
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.
Which distro are you having this problem w/? I'm running SimplyMEPIS 6.0, & this is a non-issue.
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.
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\//
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.
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.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.