22

Trying to make my generic Ubuntu to real time Ubuntu by modifying the kernel by patching / installing RT Linux but couldn't find a straight setup. Can someone help with the steps?

Abhay Nayak
  • 1,069
  • 1
  • 11
  • 25

5 Answers5

21

There's no straight forward documentation for this, but after some tests I was able to resolve it with the following steps. Pls note that these steps could change depending on when you're reading this, upto date as of 2018-19-20-21.

Step 0 - Make a working directory

Make a working directory

#Move to working directory
mkdir ~/kernel && cd ~/kernel

Step 1 - Download kernel and patch

Go to https://www.kernel.org/pub/linux/kernel/ and download a desired version of kernel to ~/kernel. Similarly, go to https://www.kernel.org/pub/linux/kernel/projects/rt/ and download the RT patch with same version as the downloaded kernel version. The kernel and patch I used were linux-4.9.115.tar.gz and patch-4.9.155-rt93.patch.gz.

Step 2 - Unzip the kernel

tar -xzvf linux-4.9.115.tar.gz

Step 3 - Patch the kernel

#Move to kernel source directory
cd linux-4.9.115
gzip -cd ../patch-4.9.115-rt93.patch.gz | patch -p1 --verbose

Step 4 - Enable realtime processing This step requires libncurses-dev

sudo apt-get install libncurses-dev libssl-dev

The next command launches a graphical menu in the terminal to generate the config file.

make menuconfig

Go to the location and make the changes accordingly

##Graphical Menu##

Processor type and features ---> [Enter]
Preemption Model (Voluntary Kernel Preemption (Desktop)) [Enter]
Fully Preemptible Kernel (RT) [Enter] #Select

[Esc][Esc]

Kernel hacking --> [Enter]
Memory Debugging [Enter]
Check for stack overflows #Already deselected - do not select


[Esc][Esc]


[Right Arrow][Right Arrow]

<Save> [Enter]

.config

<Okay> [Enter]

<Exit> [Enter]


[Esc][Esc]


[Right Arrow]
<Exit> [Enter]

Step 5 - Compile the kernel

make -j20
sudo make modules_install -j20
sudo make install -j20

Step 6 - Verify and update Verify that initrd.img-4.9.115-rt93, vmlinuz-4.9.115-rt93, and config-4.9.115-rt93 are generated in /boot directory and update the grub.

cd /boot
ls
sudo update-grub

Verify that there is a menuentry containing the text "menuentry 'Ubuntu, with Linux 4.9.115-rt93'" in /boot/grub/grub.cfg file

To change default kernel in grub, edit the GRUB_DEFAULT value in /etc/default/grub to your desired kernel.

NOTE: 0 is the 1st menuentry

7 - Reboot and verify

sudo reboot

Once the system reboots, open the terminal and use uname -a to check the kernel version, it should look like the following

Linux abhay-home 4.9.115-rt93 #1 SMP PREEMPT RT Mon May 13 03:32:57 EDT 2019 x86_64 x86_64 x86_64 GNU/Linux

Note: "SMP PREEMPT RT" validates that your system is running real time kernel.

Abhay Nayak
  • 1,069
  • 1
  • 11
  • 25
  • 1
    I did everything mentioned, changed the default grub to 6, but after booting up uname -a returns old kernel, – Salman Azmat Jul 18 '19 at 19:31
  • The value 6 might be wrong in the list of menu entries, however you can install the latest kernel version, linux by default picks up the latest kernel version. If your default kernel is 4.15.0 then download kernel 4.16.0 in step 1. If you want the same version then an unconventional way would be to go in the /boot/grub/grub.cfg and find the menu entry for your RT kernel, cut and paste it before the first menuentry. This is not advisable and can mess up with grub boot loader if not done carefully but I've tried this and it works. Hope this helps! Cheers :) – Abhay Nayak Jul 19 '19 at 13:39
  • One can also build debian packages with `make -j<#cores> deb-pkg` and then install them with `sudo dpkg -i linux-*.deb`. This is nice if you want to apply the kernel to several systems. – Wolfson Nov 13 '20 at 18:06
  • Hi Guys, i'm getting error when i execute ( sudo make modules_install -j20 ) it says arch/x86/Makefile:148 CONFIG_X86_X32 enabled but no binutilils support. ! that variable is at line 683: in my .conf file . i tried disabling by changing =N and =n still enabled !!! should i use m ? how can i get through.! any help please .. Also what is the difference between apt get install linux-rt (https://howtoinstall.co/en/linux-rt) and these list of steps. ? i'm ok to apply rt patch on existing kernel. – surya kiran May 02 '22 at 16:22
10

Here’s for Ubuntu 19.10 and above and I patched Linux 5.4.5 rt kernel patch because Linux 5.3 -- the base of Ubuntu 19.10 -- has no rt kernel patch.

0. Make a working directory

# Make dir and move to working directory
$ mkdir ~/kernel && cd ~/kernel

1. Download kernel and patch

Download kernel and rt patch from https://www.kernel.org/. You can get these below:

(kernel) https://www.kernel.org/pub/linux/kernel/
(rt patch) https://www.kernel.org/pub/linux/kernel/projects/rt/

Note that the version of rt patch and kernel should be same. I used linux-5.4.5.tar.gz and patch-5.4.5-rt3.patch.gz.

If you download these on the ~/kernel, skip below and move on to step 2.

# Move these zip file to ~/kernel
$ cd ~/Download
$ mv linux-5.4.5.tar.gz patch-5.4.5-rt3.patch.gz ~/kernel/.

2. Extract kernel sources and patch rt kernel

# Extract kernel sources
$ cd ~/kernel
$ tar xvzf linux-5.4.5.tar.gz

# Patch rt kernel
$ cd linux-5.4.5
$ gzip -cd ../patch-5.4.5-rt3.patch.gz | patch -p1 --verbose {}

3. Install required packages

For using menuconfig GUI, libncurses-dev is required. flex and bison will be needed when you compile the kernel.

# For using gui
$ sudo apt install libncurses-dev libssl-dev

# For compiling kernel
$ sudo apt install flex bison

4. Configure kernel for RT

$ make menuconfig

and enter the menuconfig GUI.

# Make preemptible kernel setup
General setup ---> [Enter]
Preemption Model (Voluntary Kernel Preemption (Desktop)) [Enter]
Fully Preemptible Kernel (RT) [Enter] #Select

# Select <SAVE> and <EXIT>
# Check .config file is made properly

Note that there’s no Check for stack overflows option on GUI configuration anymore. You can check it by searching “overflow”. Type / and overflow on Graphical Menu.

5. Compile the kernel

$ make -j20
$ sudo make modules_install -j20
$ sudo make install -j20

6. Make kernel images lighter

As @mrRo8o7 said earlier, big initrd image can occur kernel panic. So you can resolve this problem by:

# Strip unneeded symbols of object files
$ cd /lib/modules/5.4.5-rt3  # or your new kernel
$ sudo find . -name *.ko -exec strip --strip-unneeded {} +

# Change the compression format
$ sudo vi /etc/initramfs-tools/initramfs.conf
# Modify COMPRESS=lz4 to COMPRESS=xz (line 53)

COMPRESS=xz 

[:wq]

then update initramfs

$ sudo update-initramfs -u

7. Verify and update grub

Verify that directory and update the grub.

# Make sure that initrd.img-5.4.5-rt3, vmlinuz-5.4.5-rt3, and config-5.4.5-rt3 are generated in /boot
$ cd /boot
$ ls

# Update grub
$ sudo update-grub

8. Reboot and verify

$ sudo reboot

# After the reboot
$ uname -a

then you can check your new kernel version

Linux [PROMPT] 5.4.5-rt3 …
Aiden Yeomin Nam
  • 315
  • 5
  • 16
  • Following these exact instructions with those file versions, I get a lot of patch failures- "patching file {} ... 4 out of 4 hunks FAILED - saving rejects to file {}.rej Hmm...The next patch would create the file {}, which already exists! Assume -R? [n] Apply anyway? [n]" - is that normal? What to answer? – DavidJ Feb 25 '20 at 19:43
  • 3
    It seems to work correctly if the "{}" after "--verbose" in step 2 is omitted. – DavidJ Feb 25 '20 at 19:59
4

After installing the new kernel (like @Abhay Nayak posted), I got into a kernel panic. The problem was that the initrd image was too big. I solved that with:

Step 1 - Strip the kernel modules

cd /lib/modules/<new_kernel>
find . -name *.ko -exec strip --strip-unneeded {} +

Step 2 - Change the initramfs compression

Edit file /etc/initramfs-tools/initramfs.conf:

COMPRESS=xz

Step 3 - Update initramfs

sudo update-initramfs -u
sudo update-grub2
mrRo8o7
  • 41
  • 5
2

The PREEMPT_RT patch has slowly been streamed to the Linux mainline and from Ubuntu 22.04 onward it can be activated with a few simple commands as described here.


For prior versions of Ubuntu note that you can also download Debian packages for certain PREEMPT_RT kernels from the official Debian packages web page. If you do not care about the precise kernel version I would prefer that over re-compiling the kernel yourself! After downloading the Debian file you can install it from the download directory with

$ sudo dpkg -i linux-image-rt-amd64_5.10.106-1_amd64.deb
$ sudo apt-get install -f

where linux-image-rt-amd64_5.10.106-1_amd64.deb clearly depends on the version that you downloaded. Reboot into the freshly installed kernel (verify that the current kernel name indeed contains rt with $ uname -r) and you should be ready to go!


In case you really want/have to compile the kernel you might have to set the following kernel flags in the .config after $ make oldconfig (or $ make menuconfig)

CONFIG_SYSTEM_TRUSTED_KEYS=""
CONFIG_SYSTEM_REVOCATION_KEYS=""

else the compilation process might fail with a cryptic error message following the steps given by the other commentators.

I have recently written a more detailed guide on the required steps here. In the process I have also written two scripts that allow you to install it from existing Debian packages or compile the kernel yourself with a graphic user interface. Have a look at the Github repository for this.

2b-t
  • 2,414
  • 1
  • 10
  • 18
0

1 Create working directory

mkdir ~/kernel && cd ~/kernel

2 Download kernel source files and real-time patch files

wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.209.tar.gz 
wget https://mirrors.edge.kernel.org/pub/linux/kernel/projects/rt/5.4/patch-5.4.209-rt77.patch.xz

3 Unpack source files

tar -xzf linux-5.4.209.tar.gz&
xz -d patch-5.4.209-rt77.patch.xz
# wait until tar -xzf linux-5.4.209.tar.gz job finish
cd linux-5.4.209
patch -p1 <../patch-5.4.209-rt77.patch

4 Configure build options

cp /boot/config-5.15.0-43-generic .config
# install dependency
sudo apt update
sudo apt install make gcc libncurses-dev libssl-dev flex libelf-dev bison
# install menu GUI to set configuration option "The RT option"
make menuconfig

# In Graphic menu to set up as follows:
# General setup  —>
# Preemption Model (Voluntary Kernel Preemption (Desktop))  —>
# ( ) Fully Preemptible Kernel (Real-Time) # select
# then SAVE and EXIT

# disable some options to update config file
scripts/config --disable SYSTEM_TRUSTED_KEYS --disable SYSTEM_REVOCATION_KEYS --disable CONFIG_DEBUG_INFO_BTF

5 Build the kernel

sudo make 
# do a check as follows, there should not be any error then we succeed to build. 
sudo make -s >/dev/null

6 Install the kernel modules

sudo make modules_install install

7 Configure bootloader option

sudo vim /etc/default/grub
# to set GRUB_TIMEOUT=-1
# which means it will stop until you choose specific kernel when boot.

8 Reboot and verify

reboot
# choose your real-time kernel, like 5.4.209-rt77
# After you log in,
# print out your real-time kernel name
uname -r
# show scheduler options, it should differ from the generic-linux-kernel's.
cat /sys/block/sda/queue/scheduler
Hogan Chou
  • 11
  • 2