# Kernel Development and Testing



## Unee0x (Jun 23, 2020)

I'm just starting kernel development ( custom kernels, drivers, patching and etc... ) and i'd like to find a good tutorial of how to setup and environment for FreeBSD Kernel Development. For instance, executing custom code on a live kernel as well as debugging. Will I actually need two machines? Can I use two VMs or better yet, will a jail or two be better?
Ultimately, my goal is to able to build a custom kernel and change some code


----------



## a6h (Jun 23, 2020)

I assume you already familiar with FreeBSD Books and Articles Online,

FreeBSD Developers' Handbook
FreeBSD Architecture Handbook
...
Then I'll go with DTrace on FreeBSD
and there's also the _Design and Implementation of the FreeBSD Operating System_ book, by *Marshall Kirk McKusick*, ....



Unee0x said:


> Ultimately, my goal is to able to build a custom kernel and change some code


Building and Installing a Custom Kernel does not need 2nd machine, vm or jail.


----------



## Unee0x (Jun 23, 2020)

Ok thanks.
Now what if I want to execute my changes to the new kernel.
What is the safest way to accomplish that without causes damages to my live system?


----------



## a6h (Jun 23, 2020)

Backup.


----------



## Unee0x (Jun 23, 2020)

That was simple enough, I guess I put too much thought into it....
Thanks again


----------



## mast07 (Jun 23, 2020)

Unee0x said:


> Ok thanks.
> Now what if I want to execute my changes to the new kernel.
> What is the safest way to accomplish that without causes damages to my live system?



Bootenvironments (if you are using ZFS)


----------



## Mjölnir (Jun 29, 2020)

Maybe the original question of the OP is answered so easily, once you have gathered some knowledge of FreeBSD (ZFS, sysutils/beadm).  Nevertheless I shout _"me too!"_ and would like to know if there is a _commonly accepted good practice_ of how to do that.  My current solution:

I set up ZFS datasets pool/src/{12.1-REL,12-STABLE,13-CUR}. Move /usr/src/* into /src/12.1-REL, symlink /usr/src to point to /src/12.1-REL, and fill the datasets pool/ROOT/{12-STABLE,13-CUR} as the DESTDIR of `make install-{kernel,world}`.  In these boot environments, the symlink /usr/src needs to be adjusted.
(the STABLE and CURRENT source trees are filled and updated with svn(1))
For own development & patches:
`foreach tree (12.1-REL 12-STABLE 13-CUR)
foreach? mount -t unionfs -o noatime -o below /src/$tree ~/Projects/FreeBSD/src/$tree
foreach? end`
and in /etc/fstab (EDIT: added _late_ mount option):

```
/src/13-CUR /home/paul/Projects/FreeBSD/src/13-CUR unionfs rw,late,below,noatime 0 0
/src/12-STABLE /home/paul/Projects/FreeBSD/src/12-STABLE unionfs rw,late,below,noatime 0 0
/src/12.1-REL /home/paul/Projects/FreeBSD/src/12.1-REL unionfs rw,late,below,noatime 0 0
```

This way I can switch between my RELEASE (used with pkg-software) and the two STABLE & CURRENT images with beadm(8), to test on _"bare metal"_
Should I decide I want some special options for ports or dive into helping the _KDE on FreeBSD_ team, I'll clone the STABLE & CURRENT images.
Comments are welcome, the are many much more experienced people around here.


----------



## Lamia (Jun 30, 2020)

Unee0x said:


> Can I use two VMs or better yet, will a jail or two be better?


Giving this some thought, you might use Buildbot to regularly build kernel/world. You can set it up for various architectures or kernel confs.


----------



## Mjölnir (Jun 30, 2020)

Lamia said:


> Giving this some thought, you might use Buildbot to regularly build kernel/world. You can set it up for various architectures or kernel confs.


Found this _buildbot_ in the packages, never heard of it.  Since it's python-based and unfortunately Python has a long history of security issues: is it safe to use?  Can this be used on top of _pouriere_ or _synth_ or is it an alternative only for _kernel_ & _world_?  It has a package _grid-view_.  Does that mean I can set up a _grid_ of build hosts a là _distcc_?  Personally, I have much more sympathy for Python than other script languages e.g. Java, for it's clean design.


----------



## Lamia (Jun 30, 2020)

mjollnir said:


> Found this _buildbot_ in the packages, never heard of it.  Since it's pathon-based and unfortunately Python has a long history of security issues: is it safe to use?  Can this be used on top of _pouriere_ or _synth_ or is it an alternative only for _kernel_ & _world_?  It has a package _grid-view_.  Does that mean I can set up a _grid_ of build hosts a là _distcc_?  Personally, I have much more sympathy for Python than other script languages e.g. Java, for it's clean design.


Yes to all. It is not an alternative. You can reuse as you desire.


----------



## Aeterna (Jul 1, 2020)

Nothing special is needed to build and boot several FreeBSD kernels. I have two custom kernels and default FreeBSD kernel that I can boot interchangeably.
FreeBSD has a nice feature that backs up last (working) kernel, so in the case that custom kernel fails to boot, it is possible to boot generic. 
To install more than one kernel just use
KERNCONF=TEST to build
and
KERNCONF=TEST KODIR=/boot/kernel.test to install.
then edit lader.conf and add default kernel to boot.
However during boot process you can select whichever kernel you want as long as you know the name.


----------

