ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I've been googling this quite a bit, and can't find anything that works. OK, first, what I want to do:
A. I want to write a little piece of code that will restart LINUX
B. This has to run in the linuxrc script (which is built into the boot.img part of the booting of LINUX).
C. If it is compiled, all the better. So, C would be nice.
What did not work:
1. Using the "reboot" command or source. Because init thread/app has not started yet, there is not rc to take to run level 6 to shut down!
2. Code that calls interupts, because I can't find a way to call interupts in g++ in LINUX as I did years and years ago in DOS with Borland C (with int(0x90,null,null)
If any-one has an idea, or even a direction I can look into, it would be appr.
Distribution: Ubuntu, Debian, Various using VMWare
Why on earth do you want to restart your computer before it has even started
I would say that this will cause major problems, since your computer will be restarted everytime you boot, and you will never be able to change that, because you will never be able to get into your system.
The reason I have for wanting to restart on linuxrc is that I created a very small boot.img along a very small kernel to do a few specific things. It is not needed to load a lot of things, so I insmod what I need, do what I want (some C code), and then would like to restart the PC when it is done. So, this is not on my running OS, but a very specific and applied tool I've created. At the moment I need the user to <CTRL><ALT><DEL>, but would love to have a non-user interaction system.
Thanks, it gives me something to investigate more.
Although, on first glance, it does not seem to be able to do what I want it to do. My kernel boots, and there is no telling what the default OS on the HD is (LINUX, BSD, Windows, Solaris). In the LINUX setup, it works great. So that that is 1/4 of my problem solved!
Thanks for you help. Any more ideas will be appreciated.
OK, quick detail of what is there, and what is not:
and this runs
linuxrc (default start script contained in boot.img)
insmod <lots of stuff, like ext2.ko, etc.>
then it runs
C CODE (my stuff).
So, the LINUX kernel set the environment for to load modules, and startes up linuxrc for me. linuxrc script loads the modules I need to make things accessable like drives that I need. When the logic in my C code are done, I would LOVE the whole bundle to restart itself. At the moment it just prompts the user to please <CTLR><ALT><DEL> to restart his/her machine. And this does do a restart, no errors, not problems.
So, you see there was no need for me to create a RAM disk and load a init and a root OS into just for a simple task.
Areas I've looked at to do the restart script/code:
1. the old DOS interupts. Calling a restart interupt on your PC BIOS will make the PC reboot. I have DOS code (Borland C) for this, but can't get an interupt call on LINUX.
2. Then I thought of maybe forcing a <CTRL><ALT><DEL> key to the system, but still needs interupts.
3. Had a look at the "shutdown" and "reboot" LINUX apps, but they just target init and run levels.
4. Had a look at kexec (as learned on this thread), but I can't 100% say that the OS on the PC is LINUX. It might be BSD, Netware or even Windows.
So I still sit with the question: how do you (with g++/gcc, or any other thing that will run in LINUX, not reliant on init or other threads that are assumed started) under a half started LINUX (thus just kernel) force your PC to just simulate a <CTLR><ALT><DEL> to restart.
shutdown -r -n -t : "now" with -n actually doesn't call init. If the man page can be believed. It does restart the computer fairly quickly on my system. And it seems to work even with init completely gone. So i would hazard a guess its standalone.
I don't think that solution is needed, and at worst a bad idea. I think you have do have init, especially if the three-fingered-salute works. Have you actually checked whether init is running or not? The surest way is to check the cmdline in /proc/1
shutdown does not work, regardless of parameters. It always interacts with init.
As far as usibility and need for this app goes: YES it works, YES I need it. People told Linux Torvolts not to start LINUX, because there was a MINIX or WINDOWS. So, we all have reasons for wanting to do something different.
So, still, the question is put forward: how do I restart a kernel without init running.
If he doesn't have init he doesn't have Linux or any *nix like OS running.
False. By default, linux kernel has an init parameter which is set to /bin/init, or /sbin/init, BUT if you put something like
in your bootloader options to pass to the kernel, the system will boot without init. In fact, many boot floppies and embedded linux systems don't even use init, and opt for the linuxrc system or even just shell scripts or a custom boot program.
Originally Posted by tuxdev
don't think that solution is needed, and at worst a bad idea. I think you have do have init, especially if the three-fingered-salute works.
Again, wrong here. Linux has an interrupt handler for the 3-finger salute. When that happens, linux kills the "init" program, since every other process is a child of that program. Init's sighand will call the proper shutdown routines. Again, if you do the init= line on boot, you'll see. It's much less graceful when the kernel handler doesn't have init to fall back on (nothing is unmounted, disks aren't synced, etc).
In fact, I think the first few linux systems I was running didn't have init. They just ran a custom program which called bash scripts.
As far as shutting down the system, as I said, if you can kill the root process (PID=1) then you shutdown the system. It's not terribly graceful, you should have synced disks, unmounted all filesystems, cleaned up any programs running, and THEN have pid=1 return 0. That will cause the kernel to reboot the system.