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.