Success in building a Kernel Snap in snapcraft 2.8.4


Wow! Talk about a crazy week. And it couldn’t have been better. After many days trying to get a working kernel in Snappy Ubuntu Core with customized Kernel Modules built against the kernel into a single snap bundle was easy is like saying giving a cat a bath is easy (I did that today as well, waiting for the kernel to finish building…)

Anyways, this blog post is about how I managed to get it working using snapcraft 2.8.4, running on Ubuntu 16.04, with kernel 4.4.0-21-generic from the Ubuntu kernel repository, and building the modules and then depmod’ing them and making sure they get put into the final snap.

First thing I did, download snapcraft. Easy to do now that it is in the main repository for Ubuntu 16.04. All you do is sudo apt update then sudo apt install snapcraft -y. Once that is complete, then you can run snapcraft init and you have a blank snapcraft.yaml file waiting for you to tell it what you are going to build. Below is an example that I used for my kernel snap:

name: custom-kernel
 version: 4.4.6
 summary:  custom kernel
 description:  custom kernel for Snappy
 type: kernel
 parts:
    kernel:
      plugin: kernel
      source: .
      kconfigfile: 4.4-config
      kconfigs:
        - CONFIG_SQUASHFS=m
      kernel-initrd-modules:
        - squashfs
        - ahci

I then ran sudo apt source linux-image-`uname -r` to get the latest kernel source that is running on Xenial. After that, I copied my snapcraft.yaml file to the location I downloaded the kernel source files to and copied the default kernel config file from /boot/config-4.4.0-21-generic to the kernel source directory and renamed it to 4.4-config. I then ran snapcraft build and after two hours, and giving my cat a bath, it was done. I was then able to build my custom kernel modules against these headers so that I knew they would have the same symbols for it. All I did was modify my makefile so that instead of it looking in /lib/modules/`uname -r`/build for my modules, I pointed it to /home/wililupy/linux-4.4.0/parts/kernel/build and modified the kernel version to 4.4.6. I then ran make all and it built my *.ko files I needed.

I then copied the *ko files to /home/wililupy/linux-4.4.0/parts/install/lib/4.4.6/extra (I had to create the extra directory mkdir -p extra) and then ran depmod -b ~wililupy/linux-4.4.0/parts/install 4.4.6 to update the modules symbols for the compiled kernel. I then ran  snapcraft snap. It then built my kernel.snap file. I then ran ubuntu-device-flash, which you will need to download from here. Run sudo ./ubuntu-device-flash core 16 --channel=edge --kernel=custom-kernel_4.4.6_amd64.snap --gadget=canonical-pc --os=ubuntu-core -o custom-snappy.imgand you will have your custom image that you are now ready to install on your device.

Since I work heavily in the Whitebox switching area, I use ONIE to install my software on whitebox switches. Read up on how this works from their Github or if you want, ask me in the comments below.

Happy Hacking!!

[ayssocial_buttons id=”2″]


7 responses to “Success in building a Kernel Snap in snapcraft 2.8.4”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.