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.
mount -t proc none /proc
? – Hauke Laging Aug 07 '17 at 01:20You 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/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/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/proc
and others :). Great explanation, please add it to your answer – George Udosen Aug 07 '17 at 07:04