0
# Mount the root filesystem.
1 .mount -o ro /dev/sda1 /mnt/root
# Boot the real thing.
2 .exec switch_root /mnt/root /sbin/init

This is a piece of code from /init file of initramfs as mentioned in gentoo. here the rootfs mounted to /mnt/root and later switch_root is performed with /mnt/root as newroot and /sbin/init as destination.

After line 1 is executed the contents of /dev/sda1 is mounted at /mnt/root. i tried,

sudo mount -o ro /dev/sda1 /mnt/temp and got this message,

mount: /mnt/temp: /dev/sda1 already mounted on /boot/efi.

so I tried, cd /boot/efi and it stated

cd: command not found

so I tried ./efi in the /boot dir but now i got this msg,

bash: ./efi: Is a directory

then i tried to view the contents of /sbin/init as:

cd /sbin/init but now it says,

bash: cd: /sbin/init: Not a directory.

i am confused .how come /dev/sda1 and /mnt/root are directories but all of a sudden /sbin/init is an executable ? shouldn't that be a directory as well ?

6
  • Why should it be a directory just because some of the others are? – ilkkachu Aug 26 '20 at 17:21
  • Also, getting cd: command not found from a shell sounds curious, especially since later the cd worked. I'd guess you did something odd when entering that command. – ilkkachu Aug 26 '20 at 17:23
  • 1
    /sbin/init isn't the destination, it's the program to execute as the new init. – Shadur Aug 26 '20 at 17:30
  • correct me if i am wrong, switch_root /mnt/root /sbin/init switch_root here should move the contents of /mnt/root to /sbin/init and make /sbin/init as the new root filesystem. if the contents are moved then whatever is present in /mnt/root will be present in /sbin/init right ? so if /mnt/root is a directory then /sbin/init must also be a directory right ? – ferb Aug 26 '20 at 17:31
  • 1
    No, it shouldn't, because that's not what switch_root does at all. If you'll look at the manpage, it explicitly says what it does. – Shadur Aug 26 '20 at 17:34
2

The confusion here arises because you've misunderstood what switch_root does.

From the manpage:

NAME
       switch_root - switch to another filesystem as the root of the mount tree

SYNOPSIS
       switch_root [-hV]

       switch_root newroot init [arg...]

DESCRIPTION
       switch_root  moves  already  mounted  /proc,  /dev,  
       /sys  and  /run to newroot and makes newroot the new root
       filesystem and starts init process.

       WARNING: switch_root removes recursively all 
       files and directories on the current root filesystem.

What this means, in layman's terms, is that the directory specified as the first argument (in your case, /mnt/root becomes the new root filesystem (it becomes /), the various virtual filesystem trees are mounted, and the program specified as the second argument is executed as the new init.

More importantly, it looks like those instructions are wrong and don't apply to whatever setup you have, because in your case /dev/sda1 is where your EFI filesystem is mounted.

I strongly recommend you not try to execute this until you know what you're doing and why.

2

The man page says:

SWITCH_ROOT(8)                               System Administration                                                   > SWITCH_ROOT(8)

NAME
      switch_root - switch to another filesystem as the root of the
      mount tree

SYNOPSIS
      switch_root newroot init [arg...]

DESCRIPTION
      switch_root moves already mounted /proc, /dev, /sys and /run
      to newroot and makes newroot the new root filesystem and
      starts init process.

So when you run switch_root /mnt/root /sbin/init, you are using making /mnt/root/ as /, copying a few system directories, and then executing /sbin/init.

On my debian system /sbin/init is a symbolic link:

$ ls -la /sbin/init
lrwxrwxrwx 1 root root 20 Aug 17 22:28 /sbin/init -> /lib/systemd/systemd

It's linked to /lib/systemd/systemd which is an executable:

$ ls -la /lib/systemd/systemd
-rwxr-xr-x 1 root root 1649088 Aug 17 22:28 /lib/systemd/systemd

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.