Linux – Purpose of Bind Mounting /sys with rbind

bind-mountfilesystemsgentoomount

I was going through the Gentoo Handbook in preparation for installing Gentoo on my system.

In the Chrooting section, these commands are given:

mount --rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --make-rslave /mnt/gentoo/dev

These are the doubts I have regarding this section:

  1. I'm no Linux expert but based on a little digging I did, I found out that a bind mount

    takes an existing directory tree and replicates it under a different point. The directories and files in the bind mount are the same as the original. Any modification on one side is immediately reflected on the other side, since the two views show the same data.

    What I don't understand is, how does making a bind mount in /mnt/gentoo/sys help? The original directory tree is in the installation medium which will be removed. So any changes made in the medium will not be reflected here. What is the purpose of using this command instead of

    mount -t sysfs sysfs /mnt/gentoo/sys 
    
  2. What is the purpose of using rbind? Assuming that the purpose of the bind mounting was only to create a /sys file on the /mnt/gentoo and not make use of bind mount's reflective feature, why use of rbind? Again, as far as I know, rbind is used when there are mount points under the original directory being bind mounted. But there aren't any mount points under /sys are there?

Best Answer

1.) /sys is not a real on-disk filesystem: it is a representation of and a means to access kernel internal state in the form of a virtual filesystem. It is entirely RAM-based and there is no point in storing the contents of /sys on disk.

In a certain sense you might say that /sys is regenerated from scratch each time the kernel boots and hardware is detected; in another sense you might say that things in the /sys filesystem don't actually have a permanent existence at all, and are only generated on demand, whenever you attempt to access them, based on the actual kernel state they're supposed to represent.

While you're in the process of installing Gentoo, the new installation does not yet have its own kernel running, so the new installation cannot have its own separate /sys yet. But the installer environment has its own /sys, and making the bind mount makes the "system under construction" borrow the /sys filesystem tree of the installation environment. This makes certain tasks in the installation be exactly the same as when upgrading an existing system, and so the same scripts can be used for both cases: in an upgrade, they are used as-is, but during an installation they just need to be run chrooted to /mnt/gentoo.

2.) Under /sys, there may or may not be debugfs mounted as /sys/kernel/debug, efivarfs UEFI variable store pseudo-filesystem as /sys/firmware/efi/efivars, and a potentially several RAM-based filesystems for the management of various control groups under /sys/fs/cgroup/*.

Under /dev there can be at least /dev/pts, /dev/shm, /dev/hugepages and/or /dev/mqueue, all various special-purpose RAM-based filesystems.

So using rbind will clearly simplify things.

Related Question