Andy’s Blog

Just another WordPress.com weblog

Linux Sol #1: Ejecting

Linux Solution #1

Fixing Unmounting/Ejecting Problems

This is the first in (hopefully) a series of posts about how to fix those
annoying little problems you run into but are too afraid to ask how to
fix. I am not entirely sure how I came to discover these useful tools but
now I have I thought I should write it down somewhere in the hope others
may find it useful.

I am using Ubuntu 6.06 (Dapper), a distribution of Gnu Linux. These
solutions will most likely work across other distributions of
Gnu Linux. Some commands may work on other POSIX systems but
the command line arguments may differ.

This information is supplied with ABSOLUTELY NO WARRANTY.

Please be careful when entering commands. Particularly ones you
find on the Internet. Remember that anyone can post commands on
the web so you should only run something if you know what it does. If in
doubt use the manual. This is a good idea anyway as it can help improve
your knowledge.

For more information about the commands listed use the Linux manual
pages (accessed via the man command)

The Problem

When using a USB stick you all know to unmount (or Eject) the device
before removing it. But what happens if the OS refuses to do this. I have
seen this happen on both Gnu Linux and Windows OSes. On Gnome you will
be presented with a dialog (shown below) when you attempt to eject the
drive.

Unable to eject media

Even clicking to show more details isn’t much help.

/media/usbdisk: device is busy

This just tells us that the drive could not be ejected because umount
couldn’t unmount it. It couldn’t be unmounted because it was “busy”.

So what do we do now? If we pull the USB stick out we could lose any
data waiting to be written. Read on for the solution.

The Solution

It would appear that the USB Stick is in use. Now we need to find out
precisely what is using it and put a stop to it. We can use the
fuser command to help us but first we must know either the
name of the device file for the USB stick or the location where it is
mounted.

The location it is mounted at will often be a folder in /media . If
you need to find the mount location or device file type mount at
the terminal. Note: You do not need to be root (or use sudo) for
this as you are only listing the mounted filesystems not trying to alter
them. One of those lines will refer to your USB stick.

In my case this line starts with:

      /dev/sde1 on /media/usbdisk type vfat

The mount folder is /media/usbdisk the device file is /dev/sde1.

Now you invoke fuser, with the following options. -v
is used to enable verbose output and -m MOUNTFOLDER is used to
tell fuser which device we are interested in.
In my example I get:

      $ fuser -v -m /media/usbdisk/                           USER        PID ACCESS COMMAND

      /media/usbdisk/:     andy      15354 ..c.. bash

                           andy      15795 ..c.. nano

The PID column indicates the “Process ID” of the program that is using
the device. The ACCESS column indicates the type of access, in this
case the c means that the program is using the directory
(or sub directory) as it’s working directory. This happens if you have a
shell open and have used cd to move to that folder or you have
invoked a program from that location.

If you have several copies of a program running and you can’t work out
which one it is you can get the full command used to open the program by
using the ps command with -fp and the PID listed.
For example:

      $ ps -p 15795 -f

      UID        PID  PPID  C STIME TTY          TIME CMD

      andy     15795 15354  0 19:40 pts/2    00:00:00 nano ftpscript.txt

Remember that some programs reuse a previous copy of themselves. For
instance if you use Gedit to open a file on your USB disk (from the
command line) and then close the file and open another (without closing
Gedit itself) it may still have it’s working directory set to the one on
your USB disk.

Once you have identified the program causing problems you can either
close the program or change it’s working directory. fuser should
no longer list any programs. The drive should now unmount or
Eject without any problems.

There may of course be other things that cause the device to be
considered busy. For example executing a file from the device.
This is indicated by the ACCESS column in the fuser output.
For more information consult the appropriate manual page.

Advertisements

February 5, 2008 - Posted by | Linux, Ubuntu

3 Comments »

  1. thank you:)

    Comment by amin | February 5, 2008 | Reply

  2. hmm. interesting

    Comment by Anaerne | May 23, 2009 | Reply

  3. […] eject pccardctl insert Usb equivalent??? google + usb eject command line linux = first result . __________________ I like an escalator because an escalator can never break, it can only […]

    Pingback by Command to mimic unplug/plug of usb dongle? - Remote Exploit Forums | November 8, 2009 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: