2

I wanted to mount my flashdrive, and made the mistake of setting the mount path as /home/my_name. Now all of my documents, downloads, pictures, etc. are unavailable since my flashdrive has replaced my previous home folder.

When I go to terminal and type sudo umount /home/my_name I get the error message:

umount: /home/my_name: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

Is there a way to fix this, where I can restore my old /home/my_name folder and then mount the flashdrive to a proper location?

5
  • 2
    I usually find it quicker just to reboot. But I have an SSD that reboots quickly. I have done something similar as all my data is in /mnt/data and to test something I mount it to /mnt losing all my links.You may be able to just unmount flash drive and run sudo mount -a which re-mounts everything in fstab. But that may only work if separate partition.
    – oldfred
    Dec 28, 2016 at 20:51
  • @oldfred the problem is that I mounted my flashdrive onto my main home directory. Now every file that I had in my home directory is gone, given that my mounted flash drive has taken over that position. I tried running sudo fuser -km /home/my_name to kill every process accessing the mount point, but all that did was exit terminal. When I reopened terminal and tried to unmount, i got the same error as in my original post
    – user529161
    Dec 28, 2016 at 20:56
  • You said using fuser to kill the processes using the mount point caused your terminal to be closed. Were you inside the mount when you ran that command? Dec 28, 2016 at 21:36
  • @EliahKagan I figured it out. I had to do a lazy unmount then just mount it properly
    – user529161
    Dec 28, 2016 at 21:38
  • @dhaneku.b I'm glad you were able to solve it! I went ahead and posted my answer anyway (it was already pretty much all the way composed), mainly with the hope that it will help other people with similar problems who find this post by searching. However, I don't think my answer covers the ultimate solution you used to fix the problem. So I'd encourage you to post the details of your solution as an answer. You can then mark that answer as the accepted one (though the system will require that you wait two days to do so). Dec 28, 2016 at 22:06

1 Answer 1

2

A mount on a non-empty directory hides the files contained within it, which is often called shadowing. This doesn't cause the files themselves to be in jeopardy. There are several easy steps you can take to troubleshoot, fix, or (when necessary) work around this problem.

Make sure your own shell isn't what's keeping the filesystem from unmounting.

For a filesystem mounted at sudo fuser -km /home/my_name, running sudo fuser -km /home/my_name to kill all processes accessing the filesystem, as you have done, is usually sufficient to let you unmount it. You reported that when you did this your terminal was closed as a result.

This will happen if your current directory, in the shell, is the mount point you're trying to unmount or one of its subdirectories. (Your shell is killed, and then the terminal program running it sees that the shell has closed and, under most setups, quits automatically as well, much as it does when you run the exit command.)

To fix this, simply navigate outside the mount and attempt to unmount it again. For example, you could change directory to / first:

cd /
sudo umount /home/my_name

Find and close or kill whatever processes are accessing files in the mount.

If you are ever in a situation where don't want to kill processes with fuser, or if you are unable to do so and need to find out what those processes are, you have several options for finding them, including some graphical utilities. I'll briefly illustrate one option here.

You can run lsof on a directory to see what processes are accessing files inside it:

lsof /home/my_name

This works well for mount points. Sometimes lsof is able to obtain better information if you run it as root, though this is often not necessary:

sudo lsof /home/my_name

Output from lsof usually looks something like this:

ek@Io:~$ sudo lsof ~/mnt/old
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
flock   15529   ek  cwd    DIR    7,0     2048 1280 /home/ek/mnt/old
flock   15529   ek    3rW  REG    7,0    21114 1311 /home/ek/mnt/old/md5sum.txt
less    15530   ek  cwd    DIR    7,0     2048 1280 /home/ek/mnt/old
less    15530   ek    3r   REG    7,0    21114 1311 /home/ek/mnt/old/md5sum.txt
less    15530   ek    5r   REG    7,0      231 1325 /home/ek/mnt/old/README.diskdefines
bash    27465   ek  cwd    DIR    7,0     2048 1280 /home/ek/mnt/old

This tells you the files and processes that are being used. In particular, if you have a situation where your shell is what's keeping it from unmounting, and you haven't noticed that this is the case, then lsof will make it plain with output like:

ek@Io:~$ lsof ~/mnt/old
lsof: WARNING: can't stat() tracefs file system /sys/kernel/debug/tracing
      Output information may be incomplete.
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    27465   ek  cwd    DIR    7,0     2048 1472 /home/ek/mnt/old/boot

In the above example, the only thing keeping the filesystem from being unmounted is that I had navigated into a subdirectory of the mount point with my shell (bash), and was still there.

If you need more information on a process, one way is to run ps with its process ID, which given in the second column of lsof's output:

ek@Io:~$ ps 27465
  PID TTY      STAT   TIME COMMAND
27465 pts/3    Ss+    0:04 -bash

You can also kill the process by its PID (kill 27465, but with whatever the appropriate number is in your situation). It's usually best to try this before moving on to the stronger kill -KILL 27465. (The -9 flag works in place if -KILL, too.)

Of course, before using the kill command or similar facilities at all, it's usually better to try closing programs the normal way, especially if there might be important data at stake. For example, it's better to save a document and close a word processor than to quit the word processor with the kill command.

Access the shadowed files through a nonrecursive bind mount.

As Paul says in Is it possible to access files “shadowed” by a mount? on SuperUser, you can access files shadowed by mounts by creating a bind mount and navigating to them there:

sudo mkdir /mnt/root
sudo mount --bind / /mnt/root

Those commands are adapted slightly from Paul's excellent answer to that question. (I encourage you to consult that page for additional details.) You don't have to use /mnt/root, but it's as good a choice as any.

If you do it that way, your entire root filesystem is accessible through /mnt/root.

If you have a separate /home partition then you'll want to make the bind mount bind to that instead:

sudo mkdir /mnt/home
sudo mount --bind /home /mnt/home

This is the same as for /, just with /home.

Reboot.

oldfred's suggestion to reboot the system is a reasonable solution to this problem. You should be able to shut down and reboot the system, if you want. Your data are shadowed by a mount, but should not be at risk of being lost.

Rebooting should be safe but you shouldn't need to reboot to solve this, if you don't want to. If you have trouble with the methods given above and want to get them to work, please comment or (better) edit your post. (Other people reading this with similar problems should consider posting a new question.)

You must log in to answer this question.