I know that switching from user mode to kernel mode occurs continuously via system calls. My question is if systemd is the exact point during the starting of a linux system where the first foundamental transition from kernel mode to user mode occurs. If this is the point where the operating system says : "From now on ( starting from systemd and all its direct or indirect children ) every process except me that wants to run in kernel mode must do so via system calls".
2 Answers
Linux is just an Operating System Kernel. It does not care what processes it runs. The first process to run is passed as an argument to the kernel when it boots, so the first process to run on Linux is whatever you told it to.
On systems that only run a single application or service, it is quite common to run that particular app or service as the initial process. Microservices written in Go often are deployed like this, for example.
Many microservice containers use tini as their init process.
Some Linux distributions use one of the many implementations of a SysV-style init process such as the sysvinit project or Busybox-init, or improved versions such as runit. Some Linux distributions use a BSD-style rc. Some Linux distributions use newly-developed init systems such as GoboLinux BootScripts, Epoch, Initng, The GNU Shepherd, or s6.
For a while, Ubuntu used Upstart, which was an in-house development of an event-driven init system by the Ubuntu project, inspired by macOS launchd, Solaris SMF, and others. Around the same time, Red Hat developed a similar project called systemd, which Ubuntu (and some other Linux distributions) now also use.
Any of these (and more) can be used by Linux as the init process. It all depends on how you configured your system.
-
tiniis a container init, so it wouldn’t be the first process the kernel runs — just the first process within a given container (I suspect the same comment applies to “microservices written in Go”). It might be worth mentioning too that even with something like Upstart or systemd, the first process is usually not the full-blown daemon, but a script in an initramfs.Stephen Kitt– Stephen Kitt2024-08-19 09:00:02 +00:00Commented Aug 19, 2024 at 9:00 -
@Jörg W Mittag, ok so in the context of Ubuntu is my assumption correct ? "systemd is the exact point during the starting of a linux system where the first foundamental transition from kernel mode to user mode occurs. It is the point where the operating system says : "From now on ( starting from systemd and all its direct or indirect children ) every process except me that wants to run in kernel mode must do so via system calls"Kode1000– Kode10002024-08-19 09:10:45 +00:00Commented Aug 19, 2024 at 9:10
From now on (starting from systemd and all its direct or indirect children) every process except me that wants to run in kernel mode must do so via system calls
In most systems running systemd, that is currently not quite true: the first process run in user space initially runs an init script in an initramfs, not systemd itself. For Debian derivatives, including Ubuntu, the script is part of initramfs-tools-core and you’ll find it installed as /init in your initramfs (use lsinitramfs -l to examine the contents).
As explained in Jörg W Mittag’s answer, the kernel doesn’t care what process it starts, and it can be instructed to start an arbitrary executable it has access to. If none is specified, it defaults to /init when booting from an initramfs, /sbin/init, /etc/init, /bin/init, or /bin/sh otherwise.
Regardless of which executable really starts the userspace boot, care must be taken in general so that pid 1 ends up being a system management daemon of some kind (systemd on systemd-based systems): pid 1 is special in a number of ways. In particular, if pid 1 (in the root namespace) exits, the kernel panics! (That’s why kill -1 sends a signal to all processes except pid 1.) Pid 1 is also used to reparent orphaned processes. So in an initramfs-based system, the initial script will replace itself with the system management daemon, and you’ll end up with systemd (or equivalent) as pid 1 even though it wasn’t the first executable to run.
/sbin/init, plus initramfs might run something else first, IIRC. Plus you can strike the "except me" as the same will apply to all processes.