How do you force a CIFS connection to unmount

Viewed 223092

I have a CIFS share mounted on a Linux machine. The CIFS server is down, or the internet connection is down, and anything that touches the CIFS mount now takes several minutes to timeout, and is unkillable while you wait. I can't even run ls in my home directory because there is a symlink pointing inside the CIFS mount and ls tries to follow it to decide what color it should be. If I try to umount it (even with -fl), the umount process hangs just like ls does. Not even sudo kill -9 can kill it. How can I force the kernel to unmount?

12 Answers

I use lazy unmount: umount -l (that's a lowercase L)

Lazy unmount. Detach the filesystem from the filesystem hierarchy now, and cleanup all references to the filesystem as soon as it is not busy anymore. (Requires kernel 2.4.11 or later.)

There's a -f option to umount that you can try:

umount -f /mnt/fileshare

Are you specifying the '-t cifs' option to mount? Also make sure you're not specifying the 'hard' option to mount.

You may also want to consider fusesmb, since the filesystem will be running in userspace you can kill it just like any other process.

Try umount -f /mnt/share. Works OK with NFS, never tried with cifs.

Also, take a look at autofs, it will mount the share only when accessed, and will unmount it afterworlds.

There is a good tutorial at www.howtoforge.net

I experienced very different results regarding unmounting a dead cifs mount and found several tricks to bypass the problem temporarily.

Let's start with the mountpoint command. It can be useful to analyze the status of a mount:

mountpoint /mnt/smb_share

Usually it returns is a mountpoint or / is not a mountpoint.

But it can even return:

  • No such device
  • Transport endpoint is not connected
  • <nothing / stale>

For every result expect of is not a mountpoint there is a chance of unmounting.

You could try the usual way:

umount /mnt/smb_share

or force mode:

umount /mnt/smb_share -f

But often the force does not help. It simply returns the same nasty device is busy message.

Then the only option is to use the lazy mode:

umount /mnt/smb_share -l

BUT: This does not unmount anything. It only "moves" the mount to the root of the system, which can be seen as follows:

# lsof | grep mount | grep cwd
mount.cif  3125             root  cwd   unknown                                          / (stat: No such device)
mount.cif  3150             root  cwd   unknown                                          / (stat: No such device)

It is even noted in the documentation:

Lazy unmount. Detach the filesystem from the file hierarchy now, and clean up all references to this filesystem as soon as it is not busy anymore.

Now if you are unlucky, it will stay there forever. Even killing the process probably does not help:

kill -9 $pid

But why is this a problem? Because mount /mnt/smb_share does not work until the lazy unmounted path is really cleaned up by the Linux Kernel. And this is even mentioned in the documentation of umount. "lazy" should only be used to avoid a long shutdown / reboot times:

A system reboot would be expected in near future if you’re going to use this option for network filesystem or local filesystem with submounts. The recommended use-case for umount -l is to prevent hangs on shutdown due to an unreachable network share where a normal umount will hang due to a downed server or a network partition. Remounts of the share will not be possible.

Workarounds

Use a different SMB version

If you still have hopes that the lazy unmounted path will ever be not busy anymore and cleaned up by the Linux Kernel or you can't reboot at the moment, then you are maybe lucky and your SMB server supports different protocol versions. By that we can use the following trick:

Lets say you mounted your share as follows:

mount.cifs //smb.server/share /mnt/smb_share -o username=smb_user,password=smb_pw

By that Linux automatically tries the maximum support SMB protocol version. Maybe 3.1. Now, you can force this version and it won't mount as expected:

mount.cifs //smb.server/share /mnt/smb_share -o username=smb_user,password=smb_pw,vers=3.1

But then simply try a different version:

mount.cifs //smb.server/share /mnt/smb_share -o username=smb_user,password=smb_pw,vers=3.0

or maybe 2.1:

mount.cifs //smb.server/share /mnt/smb_share -o username=smb_user,password=smb_pw,vers=2.1

Change the IP of the SMB server

If you are able to change the IP address or add a second IP to your SMB server, you can use this to mount the same server.

Dirty: Forward the traffic

Lets say the SMB server has the IP address 10.0.0.1 and the mount is really dead. Then create this iptables rule:

iptables -t nat -A OUTPUT -d 10.0.0.250 -j DNAT --to-destination 10.0.0.1

Now change your mount rule accordingly, so it mounts the samba server through IP 10.0.0.250 instead of 10.0.0.1 and voila, its mounted without server reboot. Dirty, but it works. PS This rule does not survive a reboot, so you should mount the SMB server manually and leave the /etc/fstab as usual.

More debugging

If you want to check if samba connection itself is theoretically working, you could try to list all SMB shares of the server through SMB3 as follows:

smbclient //smb.server -U "smb_user" -m SMB3 -L

or to view the content of a share with SMB1:

smbclient //smb.server -U "smb_user" -m NT1 -c ls
Related