Challenge, Easy,  on  Linux

On Linux, every file and directory you can access lives under one directory tree rooted at / (the "single directory hierarchy"). Typically, you use the mount command to attach filesystems from devices (e.g., disks) onto this tree, but Linux can also map an existing part of the tree to another path. That operation is known as a bind mount.

Bind mounts were common with chroot and are still ubiquitous in container tooling (Docker, Podman, Kubernetes). For example, the following functionalities are implemented with bind mounts:

  • Mounting host folders into containers/Pods
  • Sharing data volumes between multiple containers
  • Providing a fast on-disk path that bypasses union filesystems
Bind mount example: /var/log/app is also visible at /mnt/app_logs, so both paths show the same files and directories.

Bind mount example (directory names are arbitrary).

In this challenge, you'll practice using bind mounts. First, mount the existing directory ~/source1 at /opt/target1.

Hint 1: Mount command 💡

Use mount --bind SRC DST to make an existing directory appear at a new location.

Hint 2: Common pitfalls 💡

These are the most frequent causes of bind-mount failures:

  • Mounting is a privileged operation. If you see an error like below, retry with sudo (or as root):
mount: <path>: must be superuser to use mount.
  • The target path must always exist before you attempt mounting. If you run into this error, create the target directory first:
mount: <path>: mount point does not exist.
  • By default, mount SRC DST treats SRC as a block device. If you get an error like below, you likely forgot to add the --bind flag:
mount: <target>: <source> is not a block device.

Next, make the entire contents of ~/source2 available at /opt/target2. This source has sub-mounts inside it, so you'll need to find a way to bring over the whole mount tree, not just the top-level directory:

Hint 3: Recursive bind mounts 💡

If you simply mount --bind SRC DST, only the files and folders that reside in the SRC filesystem itself will be mounted. However, if the SRC has sub-mounts, they won't show up in the DST.

To mount the entire mount tree, you'll need to use a recursive bind mount. Check man 8 mount for how to do this.

Challenge on Linux
Discussion  Discord

Level up your Server Side game — Join 12,000 engineers who receive insightful learning materials straight to their inbox