Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
How to safely compile kernel myself install and use it.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Installing Gentoo
View previous topic :: View next topic  
Author Message
rupeshforu3
n00b
n00b


Joined: 19 Aug 2024
Posts: 59

PostPosted: Fri Feb 21, 2025 9:29 am    Post subject: How to safely compile kernel myself install and use it. Reply with quote

Hi I am Rupesh from India and I have brought a new system with AMD Ryzen zen3 5500GT and Asus prime b450 motherboard. I installed Linux Operating System five months back updating, and using it without any problem. I thought to compile kernel source code optimized for my hardware and succeeded but I have some doubts.

I want to compile kernel source code to remove unnecessary blotware in the kernel and to improve performance.

I have read a number of tutorials and tried or experimented to compile kernel and learnt a number of lessons.

I tried to compile kernel according to what I learnt but still there may be any mistakes. As you are experts can you try to examine the steps I followed and give suggestions.

The main steps I followed are as follows

1)  Downloaded and installed kernel source code package.

2) Extracted the source code to my home directory.

3) Changed the ownership of the extracted directory recursively through the following command.
Code:

chown -R $USER:$USER linux


4) Entered the source directory and issued the following command to clean any existing configuration.
Code:

make mrproper


5) I entered the following lines to my bash profile so that I need not enter again and again in terminal emulator.
Code:

export KCPFLAGS=" -march=znver3 -O2 pipe"
export KCLAGS=" -march=znver3 -O2 pipe"
export CFLAGS=" -march=znver3 -O2 pipe"
export CPFLAGS=" -march=znver3 -O2 pipe"


6) After that I have created a configuration file which consists of only the modules loaded by old kernel through the following command.
Code:

make localmodconfig


7) After that I entered the following command.
Code:

make xconfig


8) In this gui configuration I done the following changes.
*I selected amdgpu option to compile as module.
*I have added the string  Rupesh to LocalVersion
*I have selected append Local_Version to version to yes
*I have disabled support for other processors like intel etc except AMD.
*I have saved and exited the configuration.

9) After that I issued the following command to make the kernel and modules.
Code:

make bzImage modules


10) After that I issued the following command to install modules
Code:

make modules_install


11) After that I have tried to install kernel to /boot through the following command.
Code:

make install.


12) After that I have updated my grub configuration through the following command.
Code:

grub-mkconfig -o /boot/grub/grub.cfg


13) After that I have installed the grub boot loader through the following command.
Code:

grub install /dev/nvmen101


I think that there is nothing better than "make localmodconfig" to create configuration file.

Previously I tried to compile mainline kernel which is downloaded from kernel.org but I thought distribution kernel source code is more efficient so I tried to compile distribution kernel.

Finally I am asking is it safe to compile kernel myself or it is recommended to use default kernel provided by distribution.

Kindly try to clarify my doubts.
_________________
Regards,
Rupesh.
Back to top
View user's profile Send private message
Banana
Moderator
Moderator


Joined: 21 May 2004
Posts: 1901
Location: Germany

PostPosted: Fri Feb 21, 2025 11:12 am    Post subject: Reply with quote

Quote:
Finally I am asking is it safe to compile kernel myself or it is recommended to use default kernel provided by distribution.


Save is not an absolute thing, it depends on your preferences. Distribution kernels, either -bin or sources, do have additional patches added which the distribution think are good and needed.

So, yes you can do it the way to do it, but do not expect that your way is better. It is diferent. There are no absolute ways or configs which would result in the best possible.
_________________
Forum Guidelines

PFL - Portage file list - find which package a file or command belongs to.
My delta-labs.org snippets do expire
Back to top
View user's profile Send private message
Goverp
Advocate
Advocate


Joined: 07 Mar 2007
Posts: 2207

PostPosted: Fri Feb 21, 2025 11:14 am    Post subject: Reply with quote

Rupesh,
In essence, the steps you list seem correct. I think you will have a problem because the device drivers for your disks or NVMe or whatever will be modules, and therefore not available when you first boot this kernel.
Have you tried booting your new kernel, and if so, did it work? If not, what messages did you get?

Quote:
1) Downloaded and installed kernel source code package.
- which package?

Quote:
11) After that I have tried to install kernel to /boot through the following command.-
you should see your kernel in /boot, with a name "vmlinuz-x.y.z-rupesh" for whichever kernel version x.y.z you downloaded.
You should also see that in grub.cfg, if your "grub-mkconfig" worked correctly.

Quote:
Previously I tried to compile mainline kernel which is downloaded from kernel.org but I thought distribution kernel source code is more efficient so I tried to compile distribution kernel.

If "mainline kernel" means one from kernel.org, and "distribution kernel" is the package sys-kernel/gentoo-sources, the difference it not of efficiency. AFAIR gentoo-sources are basically the same as kernel.org but a little bit older (to allow ~arch users to find the bugs!), perhaps with patches need in response to bugs found by Gentoo users, and with patches to allow configuring OpenRc vs Systemd and to ensure some basic security features are selected.

You might find the "kmake" script I use (see my page in the wiki) useful.
_________________
Greybeard
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 54893
Location: 56N 3W

PostPosted: Fri Feb 21, 2025 11:45 am    Post subject: Reply with quote

rupeshforu3,

Safe in what respect?

Secure ... it could be better. Start reading here and here

Don't use
Code:
export KCPFLAGS=" -march=znver3 -O2 pipe"
export KCLAGS=" -march=znver3 -O2 pipe"
export CFLAGS=" -march=znver3 -O2 pipe"
export CPFLAGS=" -march=znver3 -O2 pipe"
Choose your CPU in the list in the configuration tool.
If it breaks when you pass your flags to the kernel build system, nobody will have seen the problem before.

USE=experimental when you emerge gentoo-sources will get you a patch that adds more CPUs to the list in in the kernel.
They are better tested than doing your own thing.

What happens when you get a panic on boot?
You will - don't expect it to work first time. :)
For easy recovery, you keep a few extra known working kernels in the boot menu.

Your own kernel will not be any faster, at least not measurably, than the distribution kernel.
It can have a smaller attack surface, as it is not required to boot everywhere and take up less HDD space.

The kernels job is to arbitrate among contenders for resources, then get out of the way, so that the code you want to run gets on the CPU.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
rupeshforu3
n00b
n00b


Joined: 19 Aug 2024
Posts: 59

PostPosted: Fri Feb 21, 2025 11:52 am    Post subject: Reply with quote

The kernel source package I have installed is

sys-kernel/zen-sources

Kernel version is 6.12.10

At the time of configuration I found the following error

Nvme_common not found.

Another issue is as I have entered the command "make localmodconfig" I thought everything will go fine but the configuration related to windows file systems like exFAT, NTFS etc. are not enabled.

After booting into the latest kernel I tried to mount exFAT partition directly through gui file manager but I received error as this partition support is not present in the kernel.

Similarly I think few settings are not present in the kernel configuration which are necessary to system work properly.
_________________
Regards,
Rupesh.
Back to top
View user's profile Send private message
sublogic
Guru
Guru


Joined: 21 Mar 2022
Posts: 314
Location: Pennsylvania, USA

PostPosted: Sat Feb 22, 2025 3:08 am    Post subject: Re: How to safely compile kernel myself install and use it. Reply with quote

rupeshforu3 wrote:
Code:
make localmodconfig

I would recommend:
  1. make localyesconfig
  2. make xconfig and change the graphics and networking drivers back to modules
#1 ensures that your storage and filesystem drivers are builtin, not modules, thereby avoiding the issue pointed out by Goverp earlier.
#2 is for drivers that need external firmware; they are not needed in early boot, will initialize after the root partition is mounted, and will load what they need from /lib/firmware .

If you omit #2, you will probably be restricted to text consoles, and have no networking.

Quote:
Another issue is as I have entered the command "make localmodconfig" I thought everything will go fine but the configuration related to windows file systems like exFAT, NTFS etc. are not enabled.
Because the drivers were not in use when you ran localmodconfig. You should have mounted some exFAT or NTFS partitions before starting. Now, go back in xconfig and add the drivers (as modules). Also add USB storage, CDROM, etc. Rebuild, reinstall and reboot.

(Always keep a known good kernel as backup !)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Installing Gentoo 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