Thursday, February 22, 2024

switch_root PID 1

I am developing an initramfs with a custom init. For dev/test purposes I am spawning a bash shell at the end of the init startup sequence (but before the switch_root would normally happen) so I can work on complex parts of the boot process without doing a lot of build iteration.

I thought I had everything working, and did a test run - "exec switch_root /sysroot /sbin/init". That got me a nice kernel panic, the kind you get when init exits. Which is precisely what happens when you fork /bin/bash at the end of your init and exec switch_root from the forked shell.

The init process is PID 1 and the forked bash shell is... not PID 1. Attempting a switch_root from any process other than one running as PID 1 will not work.

The solution was to exec /bin/bash at the end of init so that the new shell becomes PID 1. Once I am happy with the way things work, I can get rid of the shell call and replace it with exec switch_root /sysroot /sbin/init.

No comments: