It hasn't always behaved this way, but nowadays I get this inconsistent behaviour. Bind mounts don't copy existing mounts (unless you use --rbind
), but new mounts (and unmounts) get copied automatically. It seems like a bug. What causes this?
# mount --bind / /mnt/tmp
# mount | grep /mnt
/dev/mapper/fedora-root on /mnt/tmp type ext4 (rw,relatime,seclabel,data=ordered)
# mount /var/lib/docker
# mount | grep mnt
/dev/mapper/fedora-root on /mnt/tmp type ext4 (rw,relatime,seclabel,data=ordered)
/dev/mapper/fedora-docker on /mnt/tmp/var/lib/docker type ext4 (rw,relatime,seclabel,data=ordered)
This happens on Fedora Workstation 23. I believe Debian 8 is also affected.
It doesn't happen if I boot bash with no other processes, i.e. init=/bin/bash
. So it doesn't seem to be inherent to the Linux kernel.
It's annoying because this used to be the simplest way to move files from the root filesystem into a new mount point. It would be particularly convenient with SELinux (at least if you use cp
?), because the files would be labeled automatically, avoiding the need for restorecon
or the like.
Best Answer
You should be able to see that new mounts stop being copied, if you run
mount --make-private
on the mount point.The difference when running bash as init, is that the source filesystem has been mounted as private.[*] Whereas booting the full system is effectively running
--make-shared
. You can see the difference by looking atfindmnt -o +PROPAGATION
.Once the root filesystem is mounted as shared, any filesystem mounted directly under it will inherit the same propagation setting.
The root filesystem is being remounted as shared by
systemd
. This feature was added to systemd around 2012. It was discussed on the amazing Arch Linux wiki.https://wiki.archlinux.org/index.php?title=Talk:Systemd&oldid=411350#Systemd_defaults_.2F_to_rshared.2C_gotcha
https://github.com/systemd/systemd/commit/b3ac5f8cb98757416d8660023d6564a7c411f0a0
While you're reading this, I also recommend learning how to safely disassemble a recursive bind mount. Because on shared mounts, mounts and unmounts propagate in both directions :-).
[*] Booting with
init=/bin/bash
, I saw the filesystems mounted as private. Even though I was still booting with Fedora'sdracut
initramfs, which runs systemd internally. I'm not 100% sure what was happening here.