Skip to Content

How to fix a broken bind mount error when using Linux and snap applications

  • The article explains a problem that can occur when using Linux and snap applications, where a bind mount becomes invalid and shows an empty directory or an error message.
  • The article provides several ways to fix the problem, such as reconnecting the original directory tree, unlinking the keys with keyctl, or using the lazy unmount option.

If you use Linux, you may have used the bind mount feature to create a mirror of a directory tree in another location. This can be useful for various purposes, such as making files accessible to a snap application, sharing files between users, or creating backups. However, sometimes a bind mount can break and cause unexpected behavior, such as showing empty directories or refusing to unmount. In this article, I will explain what causes a broken bind mount, how to reproduce it, and how to fix it. I will also answer some frequently asked questions related to this topic.

How to fix a broken bind mount error when using Linux and snap applications

What Causes a Broken Bind Mount?

A bind mount is a special type of mount that creates a new mount point that refers to an existing directory tree. For example, if you have a directory /path/to/real/files that contains some files, you can create a bind mount /home/me/MuseScore3Development/Scores that points to the same directory tree. This way, any changes made in either location will be reflected in the other.

To create a bind mount, you can use the mount command with the –bind or -o bind option, or you can add an entry in the /etc/fstab file with the bind option. For example:

sudo mount --bind /path/to/real/files /home/me/MuseScore3Development/Scores

or

/path/to/real/files /home/me/MuseScore3Development/Scores none defaults,bind 0 0

A broken bind mount occurs when the original directory tree is deleted or moved, but the bind mount still exists. This can happen for various reasons, such as:

  • The original directory tree is on a removable device that is unplugged or ejected.
  • The original directory tree is on a network share that is disconnected or unavailable.
  • The original directory tree is renamed or moved by another process or user.
  • The original directory tree is deleted by mistake or by a malicious program.

When this happens, the bind mount becomes invalid and may show an empty directory or an error message. For example, if you have a bind mount /home/me/MuseScore3Development/Scores that points to /path/to/real/files, and the latter is deleted, you may see something like this:

ls /home/me/MuseScore3Development/Scores
ls: cannot access '/home/me/MuseScore3Development/Scores': No such file or directory

or

ls /home/me/MuseScore3Development/Scores
total 0

Furthermore, if you try to unmount the bind mount, you may encounter another error message like this:

sudo umount /home/me/MuseScore3Development/Scores
Could not unlink the key(s) from your keyring.
Please use `keyctl unlink` if you wish to remove the key(s).
Proceeding with umount.
umount: /home/me/MuseScore3Development/Scores: not mounted.

This error message is related to the kernel keyring feature, which is used by some applications (such as snap) to store and access encryption keys or other sensitive data. When you create a bind mount for a snap application, the application may add some keys to your keyring that are associated with the bind mount. When you try to unmount the bind mount, the application may fail to remove the keys from your keyring, resulting in this error message.

How to Reproduce a Broken Bind Mount?

To reproduce a broken bind mount, you need to have a Linux system with a snap application installed (such as MuseScore), and a removable device (such as a USB flash drive) that contains some files. Here are the steps to reproduce the problem:

  1. Plug in your removable device and find out its mount point (such as /media/me/usb).
  2. Create a directory on your home folder for the bind mount (such as /home/me/MuseScore3Development/Scores).
  3. Create a bind mount from your removable device to your home folder using the mount command or the /etc/fstab file. For example:
     sudo mount --bind /media/me/usb /home/me/MuseScore3Development/Scores
    
  4. Verify that the bind mount works by listing the files in both locations. You should see the same files in both directories.
  5. Unplug your removable device without unmounting it first. This will delete the original directory tree and break the bind mount.
  6. Try to list the files in your home folder again. You should see an empty directory or an error message.
  7. Try to unmount the bind mount using the umount command. You should see another error message related to keyctl.

How to Fix a Broken Bind Mount?

There are several possible ways to fix a broken bind mount, depending on your situation and preferences. Here are some of them:

Method 1: Reconnect the original directory tree

If the original directory tree was on a removable device or a network share, you can try to reconnect it and make sure it has the same mount point as before. This may restore the bind mount and allow you to access the files or unmount it normally.

Method 2: Use the keyctl command to unlink the keys

If the original directory tree is gone and you see an error message related to keyctl, you can try to use the keyctl command to unlink the keys from your keyring. This may allow you to unmount the bind mount without errors. To do this, you need to find out the key ID or description that is associated with the bind mount. You can use the keyctl list command to list all the keys in your keyring, or the keyctl search command to search for a specific key by description. For example:

  keyctl list @u
  3 keys in keyring:
  123456789: --alswrv   1000  1000 user: snap.musescore.mscore
  234567890: --alswrv   1000  1000 user: snap.musescore.mscore.instance
  345678901: --alswrv   1000  1000 user: snap.musescore.mscore.mnt

In this example, the last key is related to the bind mount for MuseScore. You can use its ID (345678901) or its description (snap.musescore.mscore.mnt) to unlink it from your keyring using the keyctl unlink command. For example:

  sudo keyctl unlink 345678901 @u

or

  sudo keyctl unlink snap.musescore.mscore.mnt @u

After unlinking the key, you can try to unmount the bind mount again using the umount command. You should not see any error messages this time.

Method 3: Use the lazy unmount option

If none of the above methods work, you can try to use the lazy unmount option (-l) with the umount command. This will detach the bind mount from the file system hierarchy and clean up all references to it as soon as it is not busy anymore. For example:

  sudo umount -l /home/me/MuseScore3Development/Scores

This may not work if the bind mount is still in use by some process or application, so you may need to close or kill them first.

Frequently Asked Questions

Question: What is a snap application and why does it need a bind mount?

Answer: A snap application is a software package that contains all the dependencies and libraries needed to run it on any Linux system. Snap applications are isolated from the rest of the system and run in a sandboxed environment for security and compatibility reasons. However, some snap applications may need to access files outside their sandbox, such as user documents or configuration files. To do this, they may use a bind mount to create a mirror of an external directory tree inside their sandbox.

Question: How can I prevent a broken bind mount from happening?

Answer: The best way to prevent a broken bind mount from happening is to always unmount it properly before deleting or moving the original directory tree. You can use the umount command or remove the entry from the /etc/fstab file and reboot your system. This will ensure that there are no dangling references or keys left behind.

Question: How can I list all the bind mounts on my system?

Answer: You can use the findmnt command with the -o option to list all the mounts on your system and filter them by type. For example:

    findmnt -o SOURCE,TARGET,FSTYPE,OPTIONS -t none

This will show all the mounts that have none as their file system type, which usually indicates a bind mount.

Conclusion

A broken bind mount is a common problem that can occur when using Linux and snap applications. It can cause confusion and frustration when trying to access or unmount your files. However, there are some ways to fix it, such as reconnecting the original directory tree, unlinking the keys with keyctl, or using the lazy unmount option. Hopefully, this article has helped you understand and solve this problem.

If you found this article helpful, please share it with your friends and colleagues who may also encounter this problem. If you have any questions or comments, please leave them below. Thank you for reading!

Disclaimer: The content of this article is for informational purposes only and does not constitute professional advice. The author is not affiliated with Linux, Snap, or any of the applications mentioned in this article. The author is not responsible for any damages or losses that may result from following the instructions or suggestions in this article. The author does not guarantee the accuracy, completeness, or timeliness of the information in this article. The reader should always exercise caution and consult a qualified professional before making any decisions based on the information in this article.