Course contentsShow
← Linux
Lesson 650 of 1,8197. Storage Management and Logical VolumesFree lesson

Filesystem mounting and unmounting safety

Handle busy filesystems and safely eject storage hardware.

In this lesson, you will learn how to identify why a filesystem refuses to unmount and how to safely disconnect your storage hardware without risking data corruption.

The "Device is Busy" Problem

You have likely encountered the frustrating umount: /mnt/data: target is busy error. This happens because the Linux kernel prevents unmounting if any process is actively accessing files, holding an open file descriptor, or has its "current working directory" set within that mount point. Forcibly removing a drive while the kernel is still flushing data to the disk—especially with journaling filesystems like Ext4 or XFS—can lead to metadata inconsistency or partial writes.

Investigating the Culprit

Before you can safely eject a device, you must identify what is holding it hostage. The lsof (List Open Files) command is your best friend here. If you are trying to unmount /mnt/data, run:

lsof +D /mnt/data

This command lists every process currently accessing files inside that directory. You will see the Process ID (PID) and the command name. Once identified, you can gracefully close the application or, if necessary, terminate the process holding the lock.

Safe Unmounting Procedures

Once the filesystem is no longer "busy," you can safely unmount it using umount. If you are working with physical hardware like a USB drive, it is best practice to run the sync command immediately before unmounting. This forces the kernel to flush the write buffers to the physical medium:

sync
umount /mnt/data

If you are dealing with a stuck mount that absolutely refuses to close, you might be tempted to use umount -l (the "lazy" unmount). While this detaches the filesystem from the directory tree immediately, it can still leave pending I/O operations incomplete. Always prioritize identifying and closing the blocking processes first to ensure your filesystem journals remain intact and your data stays consistent.

Key Takeaway: Never pull a drive or forcefully eject a mount if it is "busy"; always use lsof to resolve the conflict and sync to guarantee data integrity before unmounting.