reboot within an initrd image
Asked Answered
V

2

6

I am looking for a method to restart/reset my linux system from within an init-bottom script*. At the time my script is executed the system is found under /root and I have access to a busybox.

But the "reboot" command which is part of my busybox does not work. Is there any other possibility?


My system is booted normally with an initramfs image and my script is eventually causing an update process. The new systemd which comes with debian irritates this. But with a power reset everything is fine.

Vittorio answered 27/2, 2015 at 18:37 Comment(2)
This would be better at unix.stackexchange.comWashbasin
Can I move this topic or do I have to recreate it there? (unix.stackexchange.com)Vittorio
S
7

I have found this:

echo b >/proc/sysrq-trigger

(it's like pressing CTRL+ALT+DEL)

Stringpiece answered 4/10, 2015 at 7:21 Comment(0)
P
1

If you -are- init (the PID of your process/script is 0), then starting the busybox reboot program won't work since it tries to signal init (which is not started) to reboot.

Instead, as PID 0, you should do what init would do. This is call the correct kernel API for the reboot. See Man reboot(2) for details.

Assuming you are running a c program or something, one would do:

#include <unistd.h>
#include <sys/reboot.h>

void main() { reboot(0x1234567); }

This is much better than executing the sysrq trigger which will act more like a panic restart than a clean restart.

As a final note, busybox's init actually forks a process to do the reboot for it. This is because the reboot systemcall actually also exists the program, and the system should never run without an init process (which will also panic the kernel). Hence in this case, you would do something like:

pid_t pid;
pid = vfork();
if (pid == 0) { /* child */
    reboot(0x1234567);
    _exit(EXIT_SUCCESS);
}
while (1); /* Parent (init) waits */
Pacifically answered 15/5, 2017 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.