3

I have seen numerous examples online that while generating the initramfs image manually, or installing grub to a partition, or repairing a broken install what you do is chroot into the OS from a Live CD.

Now the concept of chroot, in an in of itself is not difficult to understand, it just changes the root to whatever directory we specify and starts a shell with that root. We can also configure the environment variables as needed.

What confuses me is the preparation that goes on before the actual chroot is executed, specifically the mount of the virtual file systems.

Take this example-:

First we mount the / root partition -:

$ mount -t ext4 /dev/sda5 /mnt/ubuntu

Then we mount the virtual file systems -:

$ mount -t proc none /mnt/ubuntu/proc
$ mount -o bind /dev /mnt/ubuntu/dev
$ mount -o bind /sys /mnt/ubuntu/sys

This is what confuses me. These virtual file systems are of the LiveCD, right ? How can they be expected to work with the OS that I am going to chroot into ? They belong to a different OS.

For example in this answer to a question I previously asked, the reason given that why commands like update-initramfs need the above mounts is because it needs info about the OS before it can generate the kernel image. So how is that happening over here ? I am mounting the filesystems from the LiveCD and not the OS for which I am building the kernel image, so it will use the info of the LiveCD and not of the targeted OS. So its like generating the kernel image for the LiveCD. How is that desirable ? (Correct me if I am wrong)

Also why are they needed ? And why bind mount them instead of just mounting them ?

After the above steps typically in the examples that I have seen so far the actual chroot command is executed.

$ chroot /mnt

So far I have not found any clear explanation for my questions above. Hoping someone can explain it in layman's terms.

ng.newbie
  • 1,175

1 Answers1

2

/proc and /sys are the interface to the running kernel; they are not really related to the installed OS. /dev is the same anyway.

You need these directories for most programs. But you do not need to prepare them before the chroot. You can chroot into the pure root volume and mount the rest from there (like the OS would when it starts).

Hauke Laging
  • 90,279
  • Please give an example of this mount! – George Udosen Aug 06 '17 at 23:45
  • @george You mean mount -t proc none /proc? – Hauke Laging Aug 07 '17 at 01:20
  • @HaukeLaging Yes so what is the reasoning behind mounting them before the chroot. Also why bind mount instead of just a simple mount ? – ng.newbie Aug 07 '17 at 05:18
  • @HaukeLaging You can chroot into the pure root volume and mount the rest from there ? What do you mean by this ? You mean by mounting the /proc and /sys of the OS that I am chrooting into ? These are virtual file systems, so if the OS is not running these directories should be blank. Also if I could do that why do most people prefer mounting the /proc and /sys of the LiveOS ? – ng.newbie Aug 07 '17 at 05:24
  • 1
    @ng.newbie I meant that you can either first mount /proc and then chroot or first chroot and then mount /proc. These virtual file systems refer to the running kernel and not to the Linux binaries or config files on the disk (that's why they are virtual); there is always a running kernel so they should never be empty (except during the boot phase). You get the same result because you mount the exactly same proc file system in both cases. – Hauke Laging Aug 07 '17 at 05:34
  • @HaukeLaging are you telling me that if the LiveCD a linux distro and kernel version different than that of the actual system I will still get the same result ? I understand that the filesystem may be same but the contents should not be. Then how is it possible that the results will be same ? – ng.newbie Aug 07 '17 at 06:23
  • @ng.newbie There may be differences between the /proc filesystemsof the live OS and the installed OS but that is not what my answer is about. There are no differences between a /proc that is mounted before and one that is mounted after the chroot. – Hauke Laging Aug 07 '17 at 06:27
  • @HaukeLaging thanks for the These virtual file systems refer to the running kernel and not to the Linux binaries or config files on the disk, and I meant the You can chroot into the pure root volume and mount the rest from there (like the OS would when it starts). I now understand the need to bind /proc and others :). Great explanation, please add it to your answer – George Udosen Aug 07 '17 at 07:04