# How to build a kernel for Fatdog

This is an example for building/compiling a kernel for Fatdog from scratch. It assumes you already know your way around Fatdog and Linux in general.
This however is neither a tutorial nor a comprehensive instructions to build the kernel which ships with Fatdog. This is only an example, and it covers only the minimal steps you need to do create, and package, the most basic kernel that is capable of booting Fatdog. There are additional steps not explained here (e.g. building aufs utilities, or external / third party kernel modules) because they only cause distraction and do not add to the pedagogical value of this example. Building an additional external kernel module for example is covered here.
In this example, we will build the 4.19.319 kernel. At the time of writing, 4.19.319 is the most recent version of the 4.19 LTS (long-term-supported) kernel. It is also the kernel line with the longest longevitiy, which makes it an ideal example as this kernel line would probably continue long after this document is written. Nevertheless, the same principles are applicable for all kernels, either older or newer. The only thing to be careful is to adjust the aufs patches to use(different kernel versons may have different patches to use).

## Preparations

1. The most important step of the process: Obtain the known-good kernel configuration file from the kernel version nearest to the one that you want to build.
The kernel configuration file is known as .config file, or in Puppy's terms, the DOTconfig.
In Fatdog, this file can be obtained in two ways:
a) from the running kernel, by running the command zcat /proc/config.gz > DOTconfig
b) from kernel-source-X.Y.Z.sfs; this file is located in /usr/src/linux-X.Y.Z/.config
For this example I will take the .config from kernel-source-4.19.92.sfs (from here: http://distro.ibiblio.org/fatdog/sfs/800/)
2. Obtain the known-good firmware collection for a kernel version which is nearest to the one that you're building.
This is normally located in kernel-modules.sfs, located in /lib/firmware. If you're being lazy, then just download one of the tarballs from here: https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git, but that file will be huge (linux-firmware-20240811.tar.gz for example is 555 MB) as it will include firmware from all kernel versions back to version 2.6 onwards. The point of using existing firmware collection is to include only the firmware that you need, to make the size smaller.
For this example I will use the firmware from kernel-modules.sfs-4.19.92 (from here: http://distro.ibiblio.org/fatdog/kernels/800/)
3. Have a Linux partition ready. Building kernel requires a lot of space which probably won't fit into your RAM, or your savefile/savefolder, so do it in an external Linux partition (it has to be a Linux partition, not FAT32, NTFS or anything like that).
For this example, I will use /mnt/sdc1 as the location (/mnt/sdc1 is the mountpoint of /dev/sdc1, which is my external USB drive containing an ext4 partition).
The rest of the steps will be shown one line at a time, and without commentaries except when it is absolutely necessary (comments are prefixed by #).

## Steps

    cd /mnt/sdc1

    # get and unpack kernel sources
    wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.19.319.tar.xz
    tar -xvf linux-4.19.319.tar.xz
    cd linux-4.19.319

    # get aufs and apply aufs patches
    git clone https://github.com/sfjro/aufs4-standalone
    cd aufs4-standalone
    git checkout aufs4.19.63+
    cp -r Documentation/ fs/ ..
    cp include/uapi/linux/aufs_type.h ../include/uapi/linux/
    cd ..
    patch -Np1 -i aufs4-standalone/aufs4-kbuild.patch
    patch -Np1 -i aufs4-standalone/aufs4-base.patch
    patch -Np1 -i aufs4-standalone/aufs4-mmap.patch
    patch -Np1 -i aufs4-standalone/aufs4-loopback.patch
    patch -Np1 -i aufs4-standalone/vfs-ino.patch
    patch -Np1 -i aufs4-standalone/tmpfs-idr.patch
    patch -Np1 -i aufs4-standalone/proc_mounts.patch

    # get .config
    mount /path/to/kernel-source-4.19.92.sfs /mnt/data
    cp /mnt/data/usr/src/linux-4.19.92/.config .config
    umount /mnt/data

    # apply .config
    yes "" | make oldconfig
    # if you want to, you can adjust your kernel configuration by running:
    # make menuconfig

    # go for coffee
    make -j4 bzImage modules

    # install and prepare modules
    make modules_install INSTALL_MOD_PATH=modules
    depmod -a -b modules 4.19.319

    # create kernel-source links for dkms
    ln -s /usr/src/linux-4.19.319 modules/lib/modules/4.19.319/build
    ln -s /usr/src/linux-4.19.319 modules/lib/modules/4.19.319/source

    # copy firmware
    mkdir -p modules/lib/firmware
    mount /path/to/kernel-modules.sfs-4.19.92 /mnt/data
    cp -a /mnt/data/lib/firmware/* modules/lib/firmware
    umount /mnt/data

    # package kernel-modules.sfs and vmlinuz
    mksquashfs modules kernel-modules.sfs -noappend -comp xz
    cp arch/x86/boot/bzImage vmlinuz

The vmlinuz and kernel-modules.sfs will be in your kernel build directory. In this example, it will be in /mnt/sdc1/linux-4.19.319.
This document was originally posted The Puppy Linux Forum.