Skip to content

Instantly share code, notes, and snippets.

@szocsbarni
Last active June 14, 2023 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szocsbarni/5bcfa1403781f94affa92c79e21b5fdf to your computer and use it in GitHub Desktop.
Save szocsbarni/5bcfa1403781f94affa92c79e21b5fdf to your computer and use it in GitHub Desktop.
Enable kernel level networking

Kernel level network

The following guide enables networking on kernel level. The solution is using the initramfs-tools and is tested on Debian 11. The main list of steps:

  1. backup current initramfs image
  2. include network driver in initramfs
  3. tell initramfs when to bring up the network
  4. rebuild initramfs image
  5. update kernel parameters

Backup current initramfs

Locate the latest initrd image inside the /boot/ folder and back it up via:

sudo cp -aRf /boot/initrd.img-5.10.0-23-amd64 /boot/initrd.img-5.10.0-23-amd64.bak

If something goes wrong in the steps later, this backup initramfs image can be used anytime. To specify this backup image to be loaded instead of any other, press e on the GRUB menu before boot and go to line starting with initrd and change the name of the image to the name of backup image and press Ctrl+x to boot.

Network driver in initramfs

First we need to locate the network driver, this can be done via lspci -v and on the output look for the Ethernet controller section, Kernel modules entry's value. Example output:

27:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8125 2.5GbE Controller (rev 05)
	Subsystem: Micro-Star International Co., Ltd. [MSI] RTL8125 2.5GbE Controller
	Flags: bus master, fast devsel, latency 0, IRQ 36
	I/O ports at e000 [size=256]
	Memory at fc900000 (64-bit, non-prefetchable) [size=64K]
	Memory at fc910000 (64-bit, non-prefetchable) [size=16K]
	Capabilities: <access denied>
	Kernel driver in use: r8169
	Kernel modules: r8169

The located module then needs to be included in the initramfs image, this can be done by first including the located module on the following configuration file: sudo nano /etc/initramfs-tools/modules After changes the file should look like this:

# List of modules that you want to include in your initramfs.
# They will be loaded at boot time in the order below.
#
# Syntax:  module_name [args ...]
#
# You must run update-initramfs(8) to effect this change.
#
# Examples:
#
# raid1
# sd_mod

# network interface driver
r8169

Network activation

To tell initramfs when to activate networking, hooks are used, read more about them here. The below solution activates the network before the file systems are mounted.

Create the following file: sudo nano /etc/initramfs-tools/scripts/init-premount/init_network.sh

With the content below:

#!/bin/sh
PREREQ=""
prereqs()
{
     echo "$PREREQ"
}

case $1 in
prereqs)
     prereqs
     exit 0
     ;;
esac

. /scripts/functions
configure_networking


Add execution rights: sudo chmod +x /etc/initramfs-tools/scripts/init-premount/init_network.sh

Rebuild initramfs image

Can be done via: sudo update-initramfs -u -k all

Update kernel parameters

Additional kernel parameter needs to supplied, so that the network configuration scripts knows what strategy to use to acquire an IP. This can be done via adding the ip=dhcp paramter to the /etc/default/grub file, the line starting with GRUB_CMDLINE_LINUX_DEFAULT=.

sudo nano /etc/default/grub

The resulting file looking like this:

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="ip=dhcp quiet amdgpu.ppfeaturemask=0xffffffff"
GRUB_CMDLINE_LINUX=""

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"

GRUB_ENABLE_CRYPTODISK=y

Then rebuild grub via the command below and test:

sudo update-grub

Done.

Debug

To be able to inspect the available resource in runtime during boot, a terminal can be spawn in the initramfs system. This can be done by editing the grub config manually before boot:

  • press e on the GRUB menu before boot and go to line starting with linux and append break=mount and press Ctrl+x. The boot process then will stop before reaching the mount target and terminal will be spawn. Read more about kernel targets here
  • use for ex. dmesg to checkout eventual driver conflicts, read more about debug options here
  • once finished, use exit command to resume the boot process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment