switch_root and /etc/inittab

Porter, Jeremy porter.188 at buckeyemail.osu.edu
Mon Jun 14 14:35:23 UTC 2021


Thanks Ján!

I'm not sure I completely understand though. Doesn't "exec" make it run as PID 1?

I suppose I need to start /dev/console before switching the root if I'm going to use that in the command to redirect there after switch_root.

In my inittab

::sysinit:/bin/busybox mount -t devpts devpts /dev/pts
::sysinit:/bin/busybox mount -t tmpfs tmpfs /dev/shm
::sysinit:/bin/busybox mount -t proc proc /proc
::sysinit:/bin/busybox mount -t sysfs sysfs /sys
::sysinit:/bin/busybox mount -t tmpfs tmpfs /tmp
::sysinit:/bin/busybox --install -s
::sysinit:/bin/busybox echo /sbin/mdev > /proc/sys/kernel/hotplug
::sysinit:/bin/busybox mdev -s
::sysinit:/bin/busybox mount -t xfs /dev/vda /mnt
/dev/console::sysinit:-/bin/ash
::sysinit:/bin/init.sh
::respawn:-/bin/ash

In my init.sh script

#/bin/bash
echo "Swtiching root"
exec /sbin/switch_root -c /dev/console /mnt /sbin/init
echo "Switched root"

This does not switch root either.

I don't quite understand the boot flow. Can you walk through what needs to happen here?

Thanks!

Jeremy
________________________________
From: Ján Sáreník <jajomojo at gmail.com>
Sent: Sunday, June 13, 2021 1:41 AM
To: Porter, Jeremy <porter.188 at buckeyemail.osu.edu>; busybox at busybox.net <busybox at busybox.net>
Subject: Re: switch_root and /etc/inittab

Hi Jeremy!

On Fri, Jun 11, 2021 at 2:39 PM Porter, Jeremy
<porter.188 at buckeyemail.osu.edu> wrote:
>
>
> I'm working on a RISC-V Linux system that uses busybox and switch_root. My goal is to build and include a tool chain (GDB, GCC, etc) into this RISC-V Linux. I figured I could create a non-volatile image and attach that in QEMU, which works fine. What is not working is the "switch_root" portion. I know it needs to run at PID 1 and to use "exec". But that doesn't cut it. It runs, but doesn't switch root. I'll post my inittab files below.
>
> This is the inittab on the initramfs:
>
> ::sysinit:/bin/busybox mount -t devpts devpts /dev/pts
> ::sysinit:/bin/busybox mount -t tmpfs tmpfs /dev/shm
> ::sysinit:/bin/busybox mount -t proc proc /proc
> ::sysinit:/bin/busybox mount -t sysfs sysfs /sys
> ::sysinit:/bin/busybox mount -t tmpfs tmpfs /tmp
> ::sysinit:/bin/busybox --install -s
> ::sysinit:/bin/busybox echo /sbin/mdev > /proc/sys/kernel/hotplug
> ::sysinit:/bin/busybox mdev -s
> ::sysinit:/bin/busybox mount -t xfs /dev/vda /mnt
> /dev/console::sysinit:-/bin/ash

Instead of running the init straight, there is possibility of using an
initial shell script (e.g. init=/my/script.sh in bootloader) and then
this script would contain the lines from inittab (quoted above)
including execing switch_root in the end.

So the boot would look like this (all PID 1 here):
  1. /my/script.sh, execs switch_root /new/root /bin/init
  2. /bin/init (on new root, will read /etc/inittab on new root)

The reason it did not work for you is that init itself was run as PID
1 and it is not a shell so it does not have any `exec ...` syntax
(yet, I mean the busybox init).

> When I run switch_root it executes and just hangs with no console so I added the line:
>
> ::respawn:-/bin/ash
>
> which now returns to console like I want. However, it didn't switch_root after I run:
>
> exec switch_root -c /dev/console /mnt /sbini/init
>
> I am still on the initramfs and I still have the new root on /mnt
>
> I also noticed that switch_root runs this /etc/inittab no matter what -- even if I delete it -- when I run switch_root. I guess I was expected the new file system's inittab to take over and I could just have a few lines to run. The primary function would be to get back to the console with ::respawn:-/bin/ash.
>
> What am I missing?

It can not work in the shell run by that respawn line because that
spawned shell is not PID 1.

Hoping this helps,
best regards, Ján
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20210614/e9d66088/attachment.html>


More information about the busybox mailing list