Saturday, July 22, 2023

Linux kernel drivers, initrd vs initramfs

The bootloader will load the kernel. This could be GRUB or BCD for windows

 Initrd - meant initial ram disk. Initramfs - initial ram file system.
It detects hawdware and loads drivers. Has all drivers so tjhe kernel image remains small and doesnt cause errors from conflicting drivers, and mount file system (it could be raid, netowork storage etc).
It is big 50MB and contains all drivers. 

initrd - is compressed image. Zstandard compressed data/ ASCII cpio archive
initramfs - uncompressed for older pcs.

It is called initrd in many places although it is a initramfs, particularly in boot loaders, as for them it is just a BLOB. The difference is made by the OS when it boots.

dmesg

systemd-analyze 
systemd-analyze plot > plot.svg

Startup finished in 2.840s (kernel) + 11.378s (userspace) = 14.218s graphical.target reached after 11.364s in userspace

Userspace will be since systemd (init) starts to load services. 
sudo systemctl disable/enable/start/stop NAME_OF_SERVICE

journalctl --disk-usage
journalctl --vacuum-size=50M
journalctl --verify   (verify integrity)


/etc/systemd/journald.conf
SystemMaxUse=50M

Drivers

To use hw (video card, usb device etc) we need 1 or 2 parts depending on the hardware.
1. a device driver that provices software APIs to the kernel to interact with the hardawre and is os kernel specifici - windows/linux, or even different kernel versions, when some major APIs change.

Drivers can be built into the kernel or dynamic loaded. Most are dynamic for the flexibility.
Then there are in tree and out of tree drivers.
If the driver is open source it gets included into the kernel tree and gets re-compiled with every kernel version. If its not open source and its proprietary code, then you have to compile it with every new kernel you install.

Drivers depend on kernel APIs - so if the driver is added to the kernel-tree, when a developer changes the APIs they also update the drivers.
If some older drivers depen on interfaces or APIs that get dropped out the kernel, then they will stop working, and you need to buy new hardware.
Kernel developers work for free and the hardware manufacturers only support old hardware for a few years until they release new hw. None of them will be willing to update the software of old hw.

To some extent this also happens with Windows, as some older hw might only have drivers up to a specific windows kernel versions, lets say only up to windows xp.
But if the driver is proprietary and out the tree, then it will stop working with new kernels - unless someone updates the code, which rerelly happens - so you need to buy new hardware if you want to use the new kernels.

DKMS - Dynamic Kernel Module Support - need to run as root, will automate the rebuild process of external driver modules with every kernel update. 
You need to install it and configure it. --- sudo install dkms

Kernel driver is the one that is bound to the hw.
Kernel modules are different drivers that can be used, if different functionality is required. NVIDIA might provide multiple drivers for different hw accelerated stuff, directx etc.

Modules = driver /usr/lib/modules/6.1.0-15-amd64/

lsmod
modprobe
insmod
rmmod
sudo modinfo  --- module params ex usbcore --- old scheme, new scheme - read first 8bytes or 64bytes to know the speed

Proprietary out-of the tree drivers - Video drivers: 
xserver-xorg-video-intel 
xf86-video-intel 
xf86-video-amdgpu 
nvidia nvidia-utils

sudo apt install xserver-xorg-video-intell

This package provides the driver for the Intel i8xx and i9xx family of chipsets, including i810, i815, i830, i845, i855, i865, i915, i945 and i965 series chips.


2. a firmware - is a piece of code uploaded to the hw chip
In windows the driver includes both of them.
In linux it depends on the open-source license. So in most cases there is a open-source driver included in the kernel -tree and then you might need to get a firmware from the manufacturer.
Most of the time there is a package that it includes it, if not in the main dristo then in a separate repository like nonfree 

In dmesg you can see a driver request a firmware.
Some hw some the firmware written into the chip's memory, and you can make a firmware upgrade, or some hardware use dynamic firmware that is loaded at boot time into the hardware, for ex wifi cards from intel load the firmware dynamic. to install it you use the package iwlwifi - intel wifi lan driver. Even though its called driver it actually contains a firmware
/usr/lib/firmware
The firmware is loaded by udev and sent to the driver - that is a kernel module.
Most of the time kernel modules are drivers. But they can provide other functionality as well, usb stack, network stacks etc.

udev - device manager
DBUS - inter process comunication protocal

***** udevadm monitor
udev rules (1) /lib/udev  --- (2) /etc/udev  --- /usr/lib/firmware

For ttyUSB0 --- sudo usermod -a -G dialout $USER

Edit apt sources and add contrib non-free  
apt search firmware

some example of packages with firmware:
linux-firmware
firmware-linux-free
firmware-linux-nonfree
linux-firmware-nonfree
firmware-iwlwifi

Add driver at debian install time:
Search here:
https://www.debian.org/distrib/packages#search_contents
Put contents in /dev/sdc1/drivers and mount to /lib/firmware
/lib/firmware/iwlwifi-3945-2.ucode

mkdir /lib/firmware
mount /dev/sdc1/drivers /lib/firmware


https://wiki.ubuntu.com/Kernel/Firmware


How to compile a linux kernel


Build kernel
============
sudo apt install gcc flex bison libncurses-dev libssl-dev
make xconfig
make menuconfig
/usr/src/linux/.config


make localyesconfig --- local loaded modules --- built-in kernel
make localmodconfig --- local loaded modules --- built as modules
make allnoconfig --- minimum kernel :)
+ [sata + ata + scsi] (ahci - lspci -k) si fs(ext4)

time make -j4 // j3 =j4
make modules
sudo make modules_install --- /usr/lib/modules/xx.xx.xx/
sudo make install --- /boot/

https://www.kernel.org/  download kernel
You can make it in ramdisk - and copy final back to hdd for further changes.

make mrproper- The make mrproper command cleans up any leftover files from previous kernel builds in your source directory. It also wipes out any . config files in your source directory. If you want to do a new clean build.
Boot a Linux MX image to detect all drivers.
ADD all drivers loaded either built into kernel or as modules.

lsmod > /tmp/mylsmod
 make LSMOD=/tmp/mylsmod localmodconfig
Press / and search for ex USB_SERIAL_CONSOLE  for usb to serial driver - and it shows the path where to find it.

TO disable initrd, disable:
General Setup -> at the bottom - initrd imitramfs support
Need to have added as drivers in kernel ext4 and sata/ahci or nvme drivers

For iwlwifi driver to work when built as driver into the kernel you need  to set:
CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y
Linux Kernel Configuration
└─> Device Drivers
└─> Generic Driver Options
└─> Firmware loader
└─> Force the firmware sysfs fallback mechanism when possible

No rule to make target 'debian/certs/debian-uefi-certs.pem'
scripts/config --disable SYSTEM_TRUSTED_KEYS
scripts/config --disable SYSTEM_REVOCATION_KEYS



Hardware info

lspci -k
sudo dmesg | grep driver --- to view version
journalctl

sudo dmidecode --mb info
sudo dmidecode -t 16 (max) / 17 (modules) - ram
lm-sensors --- temps/fan/voltages --- sensors
lsusb
lsblk
lshw
uname

lscpu    --- cat /proc/cpuinfo
uname -a --- cat /proc/version --- linux distro version
cat /etc/os-release       --- distro - debian 11 bullseye

Check OpenCL drivers

You can run clinfo (sudo apt install clinfo ) and if it says 0 devices

sudo apt install intel-opencl-icd 

No comments:

Post a Comment