2

I booted my system using in grub :

linux /boot/vmlinuz-4.5.0 root=/dev

initrd /boot/initrd-4.5.0

I set root to dev to specifically not get it to boot correctly. Of course it falls back to the busybox stuffs; I've seen around the net that you can use mount and somehow start init. I searched around and found switch_root under /sbin and tried to use it, but to no success. How does one change the root and start init with a PID of 1? Also, my init is System V and everything is on the same partition.

2

You need to mount your root into which you will switch. I assume you already know how to do that. Basically it's just mkdir /newroot; mount -r /dev/something /newroot

Then you need to replace your currently running fallback shell. It's running with PID 1 (you can verify that with echo $$), and target init needs to get this number again. So you need an exec shell builtin to be used.

switch_root accepts at least two arguments: root directory to be switched, and init executable file name to run after switch.

So you just replace your fallback shell:

exec switch_root /newroot /sbin/init, and /sbin/init gains PID 1 again after changing root to mounted /newroot and it can continue further initialization.

  • The pid of the shell is somewhere in the upper 100s. why isn't the shell 1? How can I fix that? – N. Morris Apr 29 '16 at 20:30
  • This means that something just spawns a dedicated shell to you, but continues running. Please see ps output to find out who is running with pid 1. – user140866 Apr 30 '16 at 2:51
  • Echo $$ is 175 which is sh in ps and 1 is /bin/sh /init in ps. Is that because when you exit the shell the kernel doesn't panic and just goes back into another shell? Is the switch root method impossible because of this? – N. Morris May 2 '16 at 0:50
  • Yes, you need to make your shell be pid 1 (if you exit it, kernel will panic). Otherwise it's not possible to repace pid 1 with init which is to be executed from switched root. Although switch_root just remove everything from current / within borders of one file system (usually temporary memory storage, like initrd), it then moves / to switched root and replaces itself with init from there. If this init gets pid other than 1, it become useless. – user140866 May 2 '16 at 1:13

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.