3

Why does a repeated bind mount create multiple entries in /proc/mounts?

# md -p /mnt/test-mount/{source,target}
# mount --bind /mnt/test-mount/{source,target}
# grep test-mount /proc/mounts 
/dev/sda3 /mnt/test-mount/target ext4 rw,relatime 0 0
# mount --bind /mnt/test-mount/{source,target}
# grep test-mount /proc/mounts 
/dev/sda3 /mnt/test-mount/target ext4 rw,relatime 0 0
/dev/sda3 /mnt/test-mount/target ext4 rw,relatime 0 0
/dev/sda3 /mnt/test-mount/source ext4 rw,relatime 0 0

After the first bind mount there was one entry mapping the source volume to the mountpoint. After the second invocation, we have one additional entry for the mountpoint plus one for the source prefix.

Likewise, findmnt reports that the source is bind mounted to itself:

# findmnt |grep test-mount
├─/mnt/test-mount/target                       /dev/sda3[/mnt/test-mount/source]                ext4            rw,relatime
│ └─/mnt/test-mount/target                     /dev/sda3[/mnt/test-mount/source]                ext4            rw,relatime
└─/mnt/test-mount/source                       /dev/sda3[/mnt/test-mount/source]                ext4            rw,relatime

After another repetition, I end up with three entries for the source, four for the target:

# mount --bind /mnt/test-mount/{source,target
# findmnt |grep test-mount
├─/mnt/test-mount/target                       /dev/sda3[/mnt/test-mount/source]                ext4            rw,relatime
│ └─/mnt/test-mount/target                     /dev/sda3[/mnt/test-mount/source]                ext4            rw,relatime
│   └─/mnt/test-mount/target                   /dev/sda3[/mnt/test-mount/source]                ext4            rw,relatime
│     └─/mnt/test-mount/target                 /dev/sda3[/mnt/test-mount/source]                ext4            rw,relatime
└─/mnt/test-mount/source                       /dev/sda3[/mnt/test-mount/source]                ext4            rw,relatime
  └─/mnt/test-mount/source                     /dev/sda3[/mnt/test-mount/source]                ext4            rw,relatime
    └─/mnt/test-mount/source                   /dev/sda3[/mnt/test-mount/source]                ext4            rw,relatime

The progression is 1, 3, 7, 15, …, i. e. n = 1 + 2^(n-1), causing the number of additional entries to double with each mount operation.

What’s the reason for this?

  • Works as expected for me (one entry for each bind mount, while your output suddenly shows three entries). I don't get those reverse entries out of nowhere. I notice you're using an alias for mkdir, could you be using other aliases that interfere somehow? – frostschutz Aug 24 '18 at 9:41
  • alias md='mkdir' – phg Aug 24 '18 at 9:44
3

Mount propagation.

This is a specific case of the "mount point explosion problem", which is explained in the "MS_UNBINDABLE example" in mount_namespaces(7)

systemd effectively enables mount propagation by default. For example, this makes it feasible to run a service in a child namespace where /home is blocked off, as per the systemd.exec option ProtectHome=yes. By allowing mounts and unmounts to propagate into the child namespace, it lets eject continue to work properly in the main namespace, and so on.

  • Those are interesting links. With --make-unbindable I the exponential part is indeed gone. It will still create a new mount entry per invocation but that’s no longer critical. – phg Aug 24 '18 at 13:23
  • @sourcejedi Thanks for pointing that out. Trying it again on a ArchLinux/systemd box, I can reproduce it too. That's scary - do it 10 times in a row and the system almost dies. Not sure it's supposed to work this way... – frostschutz Aug 24 '18 at 14:21
  • Never had such a huge /proc/mounts before and reading it with dd yields 50KiB/s ... yikes. – frostschutz Aug 24 '18 at 14:23
  • 1
    @frostschutz how does 20 times sound? github.com/torvalds/linux/commit/d29216842a85 IOW it's known to work this way for a long time, and the only "fix" that's been applied is to limit the maximum number of mounts before you get into too much trouble. – sourcejedi Aug 24 '18 at 14:58
  • Thanks for the link, the --make-rshared was the missing piece of the puzzle for me. Still seems braindead somehow, but who am I to judge :-) – frostschutz Aug 24 '18 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.