The Linux kernel refused to perform an operation because the target device or resource was already in use by another process.

Common Causes and Fixes

1. Mounted Filesystem Still in Use

This is the most frequent culprit. You’re trying to unmount a filesystem, but a process is still accessing files or directories within it.

Diagnosis:

sudo lsof | grep /path/to/mountpoint

Replace /path/to/mountpoint with the actual directory where the filesystem is mounted. This command lists all open files and the processes that have them open.

Fix: Identify the process ID (PID) from the lsof output and terminate it.

sudo kill -9 <PID>

Then, attempt to unmount again:

sudo umount /path/to/mountpoint

This works because kill -9 forcefully terminates the process, releasing its hold on the filesystem resources.

Alternative Fix (if kill doesn’t work): If the process is a zombie or unkillable, you might need to reboot the system. This is a last resort as it interrupts all running services.

2. Block Device in Use (e.g., USB drive, partition)

You’re trying to format, partition, or unmount a block device (like /dev/sdb1 or a USB drive), but it’s actively being used.

Diagnosis:

sudo fuser -m /dev/sdb1

Replace /dev/sdb1 with the device node you’re trying to access. This command shows the PIDs of processes using the specified block device.

Fix: Similar to the filesystem issue, kill the identified processes:

sudo kill -9 <PID>

If fuser reports no processes but you still get the error, the kernel might have stale information. Rebooting is often the quickest way to clear this state.

3. Swap Device or File in Use

You’re trying to disable or reformat a swap partition or file that’s currently active.

Diagnosis:

sudo swapon --show

This command lists all active swap devices and files. If the one you’re targeting is listed, it’s in use.

Fix: First, disable the swap:

sudo swapoff /dev/your_swap_partition_or_file

If swapoff fails, it means a process is still actively using swap. You’ll need to find and kill that process as described in the "Mounted Filesystem" section, or reboot. Once disabled, you can proceed with reformatting or re-purposing the swap space.

4. Network Port Already Bound

A service or application is trying to bind to a network port (e.g., 80 for HTTP, 443 for HTTPS), but another process is already listening on it.

Diagnosis:

sudo netstat -tulnp | grep :<port_number>

Replace <port_number> with the specific port (e.g., :80). This shows which process (by PID) is using that port.

Fix: Identify the PID and stop the conflicting service or application.

sudo systemctl stop <service_name>

or

sudo kill -9 <PID>

This releases the port, allowing your new application to bind to it.

5. Device in Use by Kernel Module

A hardware device is being used by a loaded kernel module, preventing you from accessing it directly (e.g., trying to dd to a disk that’s mounted or has active partitions).

Diagnosis:

lsmod | grep <module_name>

You’ll need to guess or know the likely module name associated with the device. For storage devices, it’s often sd_mod or nvme. For network interfaces, it might be e1000e or r8169.

Fix: Unload the module:

sudo rmmod <module_name>

Caution: Unloading critical modules can destabilize your system. Ensure you know what you’re doing. Often, the easier fix is to unmount any filesystems associated with the device and ensure no user-space processes are accessing it before attempting direct device operations.

6. Device Mapper Target Busy

This can happen with LVM logical volumes or device mapper targets if they are still part of an active configuration (e.g., multipath, RAID).

Diagnosis:

sudo dmsetup ls

This lists active device mapper devices. If the device you’re targeting is listed, it’s managed by dmsetup.

Fix: You’ll need to deactivate the device mapper target. For LVM, this often involves deactivating the volume group or logical volume.

sudo lvchange -an /dev/vgname/lvname

or

sudo dmsetup remove <device_name>

The exact command depends on how the device mapper target was set up. This works by telling the kernel to stop managing the device through the device mapper, freeing it up.

The next error you’ll likely encounter is a "Permission denied" error if the user or service attempting the operation doesn’t have the necessary privileges.

Want structured learning?

Take the full Linux & Systems Programming course →