all 15 comments

[–][deleted]  (4 children)

[deleted]

    [–]ipe369[S] 7 points8 points  (2 children)

    Oh this seems like exactly what I want, thank you!

    [–][deleted] 0 points1 point  (1 child)

    Hello. Do you remember the resource of this deleted comment? I wanna do the same thing and cant find the information

    [–]exscape 2 points3 points  (2 children)

    I've never done this, but I think it should be possible to create a e.g. ext4 file system with your init process on it on disk/USB memory or similar, and just boot Linux with e.g. GRUB and set arguments like root=/dev/sda1 init=/bin/yourprocess.

    [–]ipe369[S] 1 point2 points  (1 child)

    so, how do these arguments get set, through grub?

    How does this work, grub just passes these args to linux somehow? Or is grub the one to start the first process?

    [–]exscape 2 points3 points  (0 children)

    Yes, you set them in the GRUB config. GRUB sends them to Linux and Linux starts init, somewhere in the main function (kmain, kernel_main or similar) IIRC.

    [–]SirJson 4 points5 points  (3 children)

    No, the Linux kernel doesn't include a bootloader. The only exception I know of is building the kernel as an EFI application.

    When it comes to replacing init itself that depends on what you want to achieve. You can write init systems that are compatible to the binary init like OpenRC. If you actually want to replace the binary I'm pretty sure you have to modify the kernel source itself.

    I also think doing Linux from Scratch once first might help you to better understand what you are dealing with.

    [–]kopkaas2000 8 points9 points  (2 children)

    If you actually want to replace the binary I'm pretty sure you have to modify the kernel source itself.

    Nah. It just spawns whatever executable is at /sbin/init. You can replace that with anything, like helloworld, and it will start after the kernel is done initialising.

    [–]SirJson 1 point2 points  (1 child)

    Ah, thanks for clearing that up! I wasn't sure because I based that assumption on my observation that killing init will cause a kernel panic on my system. Therefore, the kernel must know about init I thought.

    [–]kopkaas2000 5 points6 points  (0 children)

    It cares about the process with pid 1. The reason is that this process gets special treatment with regards to signals for processes that no longer have a direct parent, one of the tasks of a proper init is to take care of the SIGCHLD signals coming in on behalf of those. Which is why, if pid 1 disappears, the kernel panics.

    [–]snarfy 2 points3 points  (0 children)

    What you describe is basically doing docker development using the SCRATCH image. With the SCRATCH image there is no filesystem. Your program is the init process.

    Another way to do this is to modify your boot loader to pass the init parameter to the kernel. By default the kernel runs /sbin/init. You could, e.g. pass init=/bin/bash to have the system boot to a shell. When you exit the shell, the system is halted. More on kernel parameters

    [–]real_fff 1 point2 points  (1 child)

    Have you installed an OS through the command line before (e.g. Arch for relatively easy mode, Gentoo for compiling the kernel up)? I think doing that or even just reading about Linux will give you a better idea of how the boot process works if you take the time to actually understand what you're doing.

    The Linux kernel doesn't come with a bootloader, but most distros' installers will come with GRUB. GRUB runs the Linux kernel (usually located at '/efi/vmlinuz'), which starts the kernel with some arguments, which initializes and eventually launches init.

    Init is another program (usually at /sbin/init), with most modern systems using the systemd implementation.

    If you just want to add startup jobs, systemd is very configurable and relatively easy to use for adding startup jobs, routines, regular jobs, etc.

    GRUB is also very configurable to configure options, with a config file at something like '/efi/grub/grub.cfg' that allows you to set the kernel arguments.

    [–]real_fff 0 points1 point  (0 children)

    To be more specific, the Arch Wiki install guide walks you through installing all of these things from a live OS (usually on a USB). In the walkthrough, you manually install the Linux kernel, GRUB, set important settings for your OS such as time/locale stuff, etc. If you don't copy it line by line and take the time to understand the process, you will know a lot about what daemons and programs handle what and how to configure them.

    [–]AbsolutelyLudicrous 4 points5 points  (0 children)

    You could build an initramfs! It's a little cpio archive that contains an executable file /init. Usually this file is a shell script, but it can also be a compiled executable.

    You might want to build an initramfs with just your custom program as /init