Linux – Fix ‘Device or Resource Busy’ Error When Moving Directories

centosfilesystemslinux

I want to replace /home with a symlink to my nfs-mounted home dirs.

Only root is logged in, /home is not a separate filesystem, lsof shows no locks, selinux is permissive. What am I missing?

I'm logged in directly as root via ssh:

[root@usil01-sql01 /]# uname -a
Linux usil01-sql01 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

[root@usil01-sql01 /]# w
 15:30:33 up  1:41,  1 user,  load average: 0.00, 0.02, 0.22
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/2    10.50.11.114     15:13    1.00s  0.19s  0.01s w

[root@usil01-sql01 /]# lsof | grep /home

[root@usil01-sql01 /]# lsof +D /home

[root@usil01-sql01 /]# df -h /home
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        63G  4.1G   56G   7% /

[root@usil01-sql01 /]# mount | grep -w /
/dev/sda2 on / type ext4 (rw,relatime,seclabel,data=ordered)

[root@usil01-sql01 /]# ls -lFd /home
drwxr-xr-x. 3 root root 4096 Mar  7 13:36 /home/

[root@usil01-sql01 /]# getenforce
Permissive

[root@usil01-sql01 /]# mv /home /home-old
mv: cannot move "/home" to "/home-old": Device or resource busy

What else can I check?

More system info:

[root@usil01-sql01 /]# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 836.6G  0 disk 
|-sda1   8:1    0 768.6G  0 part /storage
|-sda2   8:2    0    64G  0 part /
`-sda3   8:3    0     4G  0 part [SWAP]
sr0     11:0    1  1024M  0 rom  

[root@usil01-sql01 /]# blkid
/dev/sda2: UUID="5ba6a429-4c65-4023-82b4-3673bfcf6a88" TYPE="ext4" 
/dev/sda3: UUID="b5eb680f-8789-43b2-9f7e-c52570b0eb73" TYPE="swap" 
/dev/sda1: UUID="cb22d57d-4a5b-4963-a990-890abe0c56dc" TYPE="ext4" 

Best Answer

mv: cannot move "/home" to "/home-old": Device or resource busy

The only "use"[*] I can think of, which holds the name of a file from changing, is a mount point.

What else can I check?

I am not certain, but perhaps this could happen if the mount still exists in another mount namespace. Because it's not getting unmounts propagated from the root namespace, for some reason? Or looking at the result on my system, maybe systemd services with ProtectHome?

$ grep -h home /proc/*/task/*/mountinfo | sort -u
121 89 0:22 /systemd/inaccessible/dir /home ro,nosuid,nodev shared:142 master:24 - tmpfs tmpfs rw,seclabel,mode=755
275 243 253:2 / /home ro,relatime shared:218 master:33 - ext4 /dev/mapper/alan_dell_2016-home rw,seclabel,data=ordered
321 288 253:2 / /home rw,relatime shared:262 master:33 - ext4 /dev/mapper/alan_dell_2016-home rw,seclabel,data=ordered
84 64 253:2 / /home rw,relatime shared:33 - ext4 /dev/mapper/alan_dell_2016-home rw,seclabel,data=ordered
85 46 253:2 / /home rw,relatime master:33 - ext4 /dev/mapper/alan_dell_2016-home rw,seclabel,data=ordered

Note this issue - unable to rename /home despite it not showing as a mount point (in the current namespace) - should be fixed in Linux kernel version 3.18+.

https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/commit/?h=linux-3.18.y&id=8ed936b5671bfb33d89bc60bdcc7cf0470ba52fe


how to find out namespace of a particular process?

lsns might be useful if you can install it. More possible commands:

List mount namespaces:

# readlink /proc/*/task/*/ns/mnt | sort -u

Identify root mount namespace:

# readlink /proc/1/ns/mnt

Find processes with a given mount namespace

# readlink /proc/*/task/*/ns/mnt | grep 4026531840

Inspect the namespace of a given process:

# cat /proc/1/task/1/mountinfo

[*] EBUSY The rename fails because oldpath or newpath is a directory that is in use by some process (perhaps as current working directory, or as root directory, or because it was open for reading) or is in use by the system (for example as mount point), while the system considers this an error. (Note that there is no require‐ ment to return EBUSY in such cases—there is nothing wrong with doing the rename anyway—but it is allowed to return EBUSY if the system cannot otherwise handle such situations.)

Related Question