Walkthrough: How to build your own customised LinuxKit kernel?

 

“..Its Time to Talk about Bring Your Own Components (BYOC) now..”

The Moby Project  is gaining momentum day by day. If you are System Builder looking out for building your own container based systems, Moby is for you. You have freedom to choose from the library of components derived from Docker or you can elect to “bring your own components” packaged as containers with mix-&match options among all of the components to create your own customised container system. Moby as a tool promises to convert Docker from a monolithic engine into a toolkit of reusable components and these individual components results in the building blocks for custom solutions.

Moby_1

I was a speaker at Docker Bangalore Meetup last weekend and talked about “Introduction to LinuxKit”. I walked through the need of LinuxKit for immutable infrastructure, platform it supports and finally demoed PWM(Play with Moby). I spent considerable amount of time talking about “How to Build Your Own LinuxKit OS” and showcased them how to build it using a single YAML file. One of the interesting question raised was – ” How shall I build Ubuntu or Debian based OS as our infrastructure runs most of these distributions?” I promised them to write a blog post which is easy to follow and help them build their own customised kernel with LinuxKit.

A Brief about LinuxKit Kernel..

LinuxKit Kernel images are distributed as hub images as shown below:

Screen Shot 2017-07-09 at 7.25.10 AM

 

It contains the kernel, kernel modules, kernel config file, and optionally, kernel headers to compile kernel modules against. It is important to note that LinuxKit kernels are based on the latest stable releases.Each kernel image is tagged with the full kernel version (e.g.,linuxkit/kernel:4.10.x) and with the full kernel version plus the hash of the files it was created from.

LinuxKit offers the ability to build bootable Linux images with kernels from various distributions like CentOS, Debian, Ubuntu. As of today, LinuxKit offers a choice of the following kernels:

Moby uses YAML file as an input to build LinuxKit OS image. By default, the YAML file uses the default linuxkit/kernel:<version> as an official image as shown below in the first 3 lines.

Screen Shot 2017-07-09 at 7.34.01 AM

 

Let us see how to build our own distribution Linux Kernel(Ubuntu) through the below steps:

Under LinuxKit project repository, there is a kernel build directory and holds essential Dockerfile, configuration files and patches available to build your own Kernel for LinuxKit OS.

Screen Shot 2017-07-09 at 7.46.57 AM

If you want to include your own new patch, you can fork the repository and get the patch added under the right patch directory(shown above). For example, if you need to add patch to 4.11.x release, you can add it under patch-4.11.x directory as shown below:

Screen Shot 2017-07-09 at 7.57.35 AM

Below is the snippet of kernel_config-4.11.x file. The few lines below shows that the new built kernel will be pushed to Dockerhub under ORG/kernel:<version>.

Screen Shot 2017-07-09 at 8.33.36 AM

Next, all you need to do is run the below command to build your own customised kernel with required patches and modules loaded:

make build_4.11.x ORG=ajeetraina

PLEASE NOTE – ORG should be replaced by your Dockerhub ORG (it’s basically Dockerhub ID). If you are planning to raise PR for your patch to be included, you can use LinuxKit ORG and then get it approved.

 

Screen Shot 2017-07-09 at 7.52.21 AM

This will take sometime to create a local kernel image called ajeetraina/kernel:4.11.8-8bcfec8e1f86bab7a642082aa383696c182732f5 assuming you haven’t committed you local changes.

Screen Shot 2017-07-09 at 9.24.57 AM

 

You can then use this kernel in your YAML file as:

kernel:
   image: "ajeetraina/kernel:4.11.9-2fd6982c78b66dbdef84ca55935f8f2ab3d2d3e6"

Screen Shot 2017-07-09 at 9.45.42 AM

 

Run the Moby tool to build up this new kernel:

moby build linuxkit.yml

Screen Shot 2017-07-09 at 9.46.57 AM

 

How to Build Ubuntu based Kernel Image

If you are looking out for building distribution specific Kernel images, then this is the right section for you to refer. Docker Team has done a great job in putting it altogether under linuxkit/scripts/kernel directory as shown below:

Screen Shot 2017-07-09 at 8.12.28 AM

Let us look inside Dockerfile.deb:

Screen Shot 2017-07-09 at 8.50.35 AM

The script ubuntu.sh picks up Dockerfile.deb to build and push the kernel-ubuntu image as shown below in the snippet:

Screen Shot 2017-07-09 at 8.53.07 AM

All you need to do is execute the script:

sh ubuntu.sh

It will take sometime and you should see the below output:

Screen Shot 2017-07-09 at 8.56.10 AM

Now you can leverage this kernel into YAML file as shown below in the output:

Screen Shot 2017-07-09 at 10.01.27 AM

Let us pick up linuxkit/linuxkit.yml and add the above kernel entry to build LinuxKit OS running Docker containers as a service.

Screen Shot 2017-07-09 at 10.31.12 AM

 

Use the moby build command to build LinuxKit OS image based on Ubuntu kernel:

moby build linuxkit.yml

Screen Shot 2017-07-09 at 10.36.47 AM

 

 

Next, execute the linuxkit run command to boot up LinuxKit.

linuxkit run linuxkit

Screen Shot 2017-07-09 at 10.40.18 AM

 

Let us verify if the services are up and running:

Screen Shot 2017-07-09 at 10.48.50 AM

It’s easy to enter into one of service container as shown below:

Screen Shot 2017-07-09 at 10.52.40 AM

Did you find this blog helpful?  Feel free to share your experience. Get in touch @ajeetsraina

If you are looking out for contribution/discussion, join me at Docker Community Slack Channel.

Know more what’s happening in LinuxKit project by visiting this link.