简体   繁体   中英

reboot within an initrd image

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.

I have found this:

echo b >/proc/sysrq-trigger

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

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 ac 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 */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM