2

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.

1

/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).

  • Please give an example of this mount! – George Udosen Aug 6 '17 at 23:45
  • @george You mean mount -t proc none /proc? – Hauke Laging Aug 7 '17 at 1: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 7 '17 at 5: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 7 '17 at 5:24
  • @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 7 '17 at 5:34

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.