Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Kernel Build script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Tony0945
Advocate
Advocate


Joined: 25 Jul 2006
Posts: 4907
Location: Illinois, USA

PostPosted: Mon May 24, 2021 8:02 pm    Post subject: Kernel Build script Reply with quote

I see a lot of posts where some step was omitted or worse, a config was editted with vim or nano.
So I have added comments to my buildscript and am presenting it.
It installs the completed kernel into /boot but only advises the builder to update the boot loader.
I run refind so there is no need to do this. perhaps someone will script a test for grub/stubkernel and the appropriate auto-installation.

I keep my script as /usr/local/sbin/buildmykernell
Code:
#!/bin/bash

#test if running on a bare VT
if [ "$TERM" = linux ]; then
    TERM=xterm     #so make menuconfig will display correctly
fi

NPROC=$( nproc ) # run nproc to get the number of cores

cd /usr/src/linux || { echo "Did you forget 'eselect kernel set' ?" >&2 && exit 255 ; }

# Optional Parameter #1 is the location of the config file used
# if omitted the running kernel's internal configuration is used
if [ "$1" != "" ]; then
    ( cp "$1" .config && echo "Config is $1") || exit 255;
else
    echo "Using present kernel's built-in config"
    zcat /proc/config.gz >.config
fi

#test for backporting nct6775.c for MSI B350 TOMAHAWK ARCTIC motherboard
LONGVER=$(uname -r)
SHORTVER=$(echo -n "$LONGVER" | cut -f1,2 -d'.')
if [ "$SHORTVER" = 4.4 ] || [ "$SHORTVER" = 4.9 ]; then
 cp /usr/src/new_nct6775.c drivers/hwmon/nct6775.c
fi

# At this point we should compare kernel versions and make oldconfig if the base version has updated
make oldconfig
make menuconfig

#Actually build
make -j"${NPROC}" || {  echo "make -j${NPROC} failed"; exit 1; }

#install into /boot
make -j"${NPROC}"  modules_install || { echo "make modules_install failed";  exit 2; }
make -j"${NPROC}" install && echo "Don't forget to update boot loader menu"

# no longer needed but left in as an example
# kver="$(eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}')"
# i.e. kver contains 4.9.9-gentoo

#virtualbox modules, nvidia etc.
echo "Building Out of Kernel modules"
emerge @module-rebuild
echo "Done"

You may want to add bells and whistles like mounting /boot (I don't use a separate /boot)
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 2288
Location: KUUSANKOSKI, Finland

PostPosted: Mon May 24, 2021 8:21 pm    Post subject: Reply with quote

I think your script is also /bin/sh compatible. ;)

Thanks for this. I may use this as basis if I choose to use gentoo-sources again on my server. I went with gentoo-kernel some time ago.
_________________
..: Zucca :..
Gentoo IRC channels no longer reside on freenode but instead on Libera.Chat.
[Quick IRC quide]
Back to top
View user's profile Send private message
Goverp
Veteran
Veteran


Joined: 07 Mar 2007
Posts: 1199

PostPosted: Tue May 25, 2021 9:48 am    Post subject: Re: Kernel Build script Reply with quote

Tony0945 wrote:
Code:
...
# no longer needed but left in as an example
# kver="$(eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}')"
...


FWIW, probably more consistent coding style with the rest is:
Code:
kver=$(make -s kernelrelease)


Out of interest, I found that when making external modules I needed to set environment variable KVER to get the new kernel version rather than the running version (the source defaults to "uname -v"), so there are reasons for:
Code:
export KVER=$(make -s kernelrelease)

_________________
Greybeard
Back to top
View user's profile Send private message
Tony0945
Advocate
Advocate


Joined: 25 Jul 2006
Posts: 4907
Location: Illinois, USA

PostPosted: Tue May 25, 2021 1:27 pm    Post subject: Reply with quote

Code:
Casti ~ # cd /usr/src/linux
Casti linux # make -s kernelrelease
5.10.27-gentoo
Casti linux # uname -v
#7 SMP PREEMPT Mon May 3 17:50:49 CDT 2021
Casti linux # uname -a
Linux Casti 5.10.27-gentoo #7 SMP PREEMPT Mon May 3 17:50:49 CDT 2021 x86_64 AMD Phenom(tm) II X6 1090T Processor AuthenticAMD GNU/Linux

But I think uname refers to the current running kernel not the selected kernel (usually a higher version).
Yes, temporarily selecting a different version.
Code:
Casti ~ # $(eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}')
-bash: 5.10.27-gentoo: command not found
Casti ~ # eselect kernel list
Available kernel symlink targets:
  [1]   linux-4.17.19-gentoo
  [2]   linux-5.4.72-gentoo
  [3]   linux-5.4.92-gentoo
  [4]   linux-5.4.97-gentoo
  [5]   linux-5.10.27-gentoo *
Casti ~ # eselect kernel set 4
Casti ~ # $(eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}')
-bash: 5.4.97-gentoo: command not found
Back to top
View user's profile Send private message
Princess Nell
l33t
l33t


Joined: 15 Apr 2005
Posts: 861

PostPosted: Tue May 25, 2021 9:34 pm    Post subject: Reply with quote

Quote:
Code:

Casti ~ # $(eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}')
-bash: 5.4.97-gentoo: command not found

Make that
Code:

eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}'

The $() construct excutes what is inside it, which in this case is just a version string.
Also:
Quote:
Code:

kver=$(make -s kernelrelease)

This is a tad more lightweight
Code:
cat /usr/src/linux/include/config/kernel.release
Back to top
View user's profile Send private message
Tony0945
Advocate
Advocate


Joined: 25 Jul 2006
Posts: 4907
Location: Illinois, USA

PostPosted: Tue May 25, 2021 9:45 pm    Post subject: Reply with quote

Princess Nell wrote:

Also:
Quote:
Code:

kver=$(make -s kernelrelease)

This is a tad more lightweight
Code:
cat /usr/src/linux/include/config/kernel.release

I'm pretty sure that your way gives the version of the kernel being built.
My (admittedly clumsy) way gives the kernel version that was eslected.
I'm sure my way can be used before any make.As the note says/implies, that was a relic of an earlier version and can be completely erased.
i just keep it in case I need it in the future.


Last edited by Tony0945 on Tue May 25, 2021 9:58 pm; edited 1 time in total
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 2288
Location: KUUSANKOSKI, Finland

PostPosted: Tue May 25, 2021 9:53 pm    Post subject: Reply with quote

Code:
eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}'
... can be replaced with
Code:
readlink /usr/src/linux | cut -d- -f 2-
cut cuts from the beginning until it sees a dash (-), and cuts it too, then prints the rest. So it assumes all after the first dash is the kernel version string.
_________________
..: Zucca :..
Gentoo IRC channels no longer reside on freenode but instead on Libera.Chat.
[Quick IRC quide]
Back to top
View user's profile Send private message
Tony0945
Advocate
Advocate


Joined: 25 Jul 2006
Posts: 4907
Location: Illinois, USA

PostPosted: Tue May 25, 2021 10:03 pm    Post subject: Reply with quote

Zucca wrote:
... can be replaced with
Code:
readlink /usr/src/linux | cut -d- -f 2-

A lot cleaner and clearer. I'm not familiar with "realink" but it's obvious from the name.
I'm using that in the future. If I just want the numeric part, cut can produce that too.
Back to top
View user's profile Send private message
Anard
Tux's lil' helper
Tux's lil' helper


Joined: 01 Oct 2020
Posts: 81

PostPosted: Mon Jul 05, 2021 6:58 am    Post subject: Reply with quote

Nice script, but no need to add the creation of initramfs image ?
Also, in my /etc/portage/make.conf, MAKEOPTS is set to "-j9" while $( nproc ) return 4 on my computer :
Code:
anard@gentoo-imack ~ $ echo $(nproc)
4
anard@gentoo-imack ~ $ lscpu
Architecture :                          x86_64
Mode(s) opératoire(s) des processeurs : 32-bit, 64-bit
Boutisme :                              Little Endian
Tailles des adresses:                   39 bits physical, 48 bits virtual
Processeur(s) :                         4
Liste de processeur(s) en ligne :       0-3
Thread(s) par cœur :                    1
Cœur(s) par socket :                    4
Socket(s) :                             1
Identifiant constructeur :              GenuineIntel
Famille de processeur :                 6
Modèle :                                60
Nom de modèle :                         Intel(R) Core(TM) i5-4460  CPU @ 3.20GHz


What is the best choice ?
_________________
"iMack" : GA-H97M-D3H, Intel i5 4460, 16Go DDR3, Intel HD4600, SSD 256+128Go, HDD 500Go+2To / Clover - macOS Mojave / Gentoo-Xfce
"Portable" : HP Pavilion DV3500, Intel T6400, 4Go, NVidia 9300M, HDD 256Go / Grub2 - Gentoo-Xfce
Back to top
View user's profile Send private message
Tony0945
Advocate
Advocate


Joined: 25 Jul 2006
Posts: 4907
Location: Illinois, USA

PostPosted: Mon Jul 05, 2021 8:33 am    Post subject: Reply with quote

No need to create intramfs unless doing something advanced like encryption or raid.
nproc returns the number of cores. I don't like to push the system too hard.
Adjust to your taste. The main thing is to autodetect your cores.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum