I am kind of a newbie to learning the Linux Kernel. I know that Linux uses the /dev/console device file/driver to print boot messages by printk while Linux booting up. As answered here: https://unix.stackexchange.com/a/485198/590780
"On Linux, /dev/console is used to show messages during startup (and shutdown)."
However, there are a couple of aspects that confuse me.
The one is: If the console is a device driver from Linux, in order console to be able to be used, the Linux kernel should be already loaded or in the process of loading into the memory right?
How are the detecting CPU etc. logs printed at the very beginning of the Linux booting even before the console driver is initiated? How can the console be used at the beginning of the kernel loading? It is buffered or anything similar, then it is flushed?
The second one: Another thing that confuses me after finishing the kernel booting. When the kernel is initialized and if there is no /sbin/init program then the kernel will panic! At this point, the console is still open because I can still see the console outputs. So according to a definition that I saw (https://www.qnx.com/developers/docs/6.4.0/neutrino/user_guide/cmdline.html):
"The display adapter, the screen, and the system keyboard are collectively referred to as the physical console, which is controlled by a console driver."
From my understanding, the console driver listens keyboard too! (Correct me if I am wrong)
Why cannot I write/print after the kernel is panic even when the console is opened? Is it opened in O_RDONLY mode or something else?
The third one: The second one leads me to the third one. I guess this one from the historical point of view, if I am not mistaken earlier virtual consoles consisted of a keyboard and a display. When you press a key, the key is displayed on the screen/display that char right? Or like a teleprinter.
Why do I need to write to the console if I have already opened the /dev/console. Let me give an example to express better.
Let's say I opened the /dev/console by using some syscalls:
sys_open(SYS_open, "/dev/console", O_RDWR, ...);
Why should I write to the console if the console driver listens to the keyboard?
sys_write(SYS_write, "/dev/console", ...);
Can someone please explain these points? And I am open to any kind of reading suggestions.
/dev/console
drivers involved. There is one system driver during Kernel boot, this is usually provided bygrub
. After kernel startup there is a modular driver loaded above. Some information is inlinux-source/Documentation/driver-api/console.rst
. Note thatgrub
(and in follow-up the kernel) can also redirect the console to seral interfaces e.g. – stoney Nov 09 '23 at 10:01