2

Is it possible to umount the partition, whose files/directories are in use?

The underlying files and directories are in memory so un-mounting the partition is technically safe (I guess).

But umount is not allowing me to un-mount it.

(Who is denying this operation: umount or kernel?)

Mat
  • 52,586
SHW
  • 14,786
  • 14
  • 66
  • 101
  • Killing the processes that are tying it up or rebooting the system are the only other methods I've seen that work. – slm Jul 07 '14 at 12:48

3 Answers3

5

You can do a "lazy unmount".

A lazy unmount makes the filesystem unavailable to any new processes that are launched, but any processes which are currently using it will be able to continue using it. Then once those processes which are currently using it are finished, the filesystem will unmount.

To do this, it's simply:

umount -l /mount/point
phemmer
  • 71,831
0

You can attempt a force unmount. I would be cautious when using this as it may not be good while files are in use in the particular partition. The -f option tells Linux to force an unmount operation that might otherwise fail. The command to do this is:

umount -f <partition name>

ryekayo
  • 4,763
  • It didn't work. umount2: Device or resource busy umount: /media/sdb1: device is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) umount2: Device or resource busy – SHW Jul 07 '14 at 11:17
  • Is there any way you can kill the processes before unmounting? – ryekayo Jul 07 '14 at 11:17
  • That's what I don't want to do – SHW Jul 07 '14 at 11:21
  • You can attempt to unmount it to read-only. The command for that is umount -r <partition> – ryekayo Jul 07 '14 at 11:24
  • 1
    @SHW: there's no telling what will happen if you manage to force-unmount that process's work directory. You're much better off killing it, what it would do after that umount is unpredictable. (You could use a nasty hack like here to change the process's working directory, but that's dangerous too.) – Mat Jul 07 '14 at 11:40
0

The underlying files and directories are in memory so un-mounting the partition is technically safe (I guess).

On the contrary, if a file from a filesystem is open, then unmounting the partition is impossible. A file that is open by a process is not actually loaded into memory when the process opens the file (this would be inefficient, and would even make it completely impossible to work with files that are larger than available memory). Instead, when a file is open, its contents are loaded into RAM as needed, and removed from RAM when the kernel finds better uses for that RAM.

As long as a file is open, that file's content cannot be removed from the filesystem where it is stored. If you delete the file (e.g. with the rm command), what this really does is to remove the directory entry for that file; but the data remains on the disk until the file is closed. (You can observe this by watching the used disk space: it only goes down when the file is closed, not when an open file is deleted.)

Similarly, if a file is open, you can't unmount its filesystem, since that would make it impossible to retrieve the file's content when needed.

Linux offers the option of a “lazy unmount”. A lazy unmount of a filesystem is comparable to calling rm on a file: the point at which the filesystem is attached (the mount point) disappears, but the filesystem remains mounted until the last file on it is closed (just like deleting a file detaches it from the directory that contained it but keeps the data on the disk).

All this happens in the kernel. The umount command doesn't do much, apart from parsing /etc/mtab to let you specify either the device or the mount point.

If you want to list the processes that have a file open on a filesystem, use fuser or lsof. To kill all the processes that have a file open on a filesystem, run fuser -k /path/to/mount/point.