Custom Compiled Kernel on CentOS 7

Updated by Alex Fornuto

Contribute on GitHub

View Project | View File | Edit File

Running a custom-compiled Linux kernel is useful if you need to enable or disable certain kernel features that are not available in Linode-supplied or distribution-supplied kernels. For example, some users desire SELinux support, which is not enabled in stock Linode kernels, and may not be enabled in some distribution-supplied kernels.

If you’d rather run a distribution-supplied kernel instead, please follow our guide for Running a Distribution-Supplied Kernel.

Prior to these instructions, follow the steps outlined in our Getting Started guide. Then, log in to your Linode as the root user.

Prepare the System

  1. Update your package repositories and installed packages, install the development tools required for compiling a kernel, and install the ncurses library:

    1
    2
    yum update
    yum install -y ncurses-devel make gcc bc openssl-devel grub2
    
  2. Since some distributions install a pre-compiled kernel package into the /boot/ directory along with their development package, avoid confusion later by removing any existing files there. Warning, this will delete everything within the /boot/ directory without asking for confirmation:

    1
    rm -rf /boot/*
    

Compile and Install the Kernel

Download Kernel Sources

  1. Download the latest 4.x kernel sources from kernel.org. A conventional location to download to is /usr/src/.

    1
    wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.7.tar.xz
    
  2. Expand the archived file and change directories:

    1
    2
    tar -xvf linux-4.7.tar.xz 
    cd linux-4.7
    

Configure the Kernel

The kernel must be properly configured to run within the Linode environment. Some required configuration options may include:

1
2
3
4
5
6
7
8
CONFIG_KVM_GUEST=y
CONFIG_VIRTIO_PCI=y
CONFIG_VIRTIO_PCI_LEGACY=y
CONFIG_BLK_DEV_SD
CONFIG_SCSI_VIRTIO=y
CONFIG_VIRTIO_NET=y
CONFIG_SERIAL_8250=y
CONFIG_SERIAL_8250_CONSOLE=y

We recommend that you start with a kernel configuration (config) from a running Linode kernel. All Linode kernels expose their configuration via /proc/config.gz. For example:

1
2
zcat /proc/config.gz > .config
make oldconfig

make oldconfig prompts the user to answer any new configuration options not present in the old configuration file.

Changes to the kernel’s configuration can be made with the menuconfig command. Enable any additional options, making sure to leave filesystem support (likely ext3 or ext4) compiled into the kernel (not configured as a module). For example, to enable SELinux support, check the option “Security options –> NSA SELinux Support” in the configuration interface.

1
make menuconfig

Once your configuration options are set, exit the configuration interface and answer “y” for yes when asked whether you would like to save the new kernel configuration.

Compile the Kernel

  1. Compile and install the kernel and modules:

    1
    2
    3
    4
    5
    make bzImage
    make modules
    make 
    make install
    make modules_install
    

    If you’re using a Linode with multiple cores, you can use the j option to spawn multiple simultaneous jobs to increase speed. For example:

    1
    make -j2 bzImage
    
  2. Give the kernel a more descriptive name. Modify vmlinuz-4.7-custom in the command below to reflect the kernel version you’ve just compiled:

    1
    mv /boot/vmlinuz /boot/vmlinuz-4.7-custom
    
  3. Create an initrd file. Again, adjust the filename to match the current kernel version:

    1
    mkinitrd /boot/initrd-4.7-custom.img /boot/vmlinuz-4.7-custom
    
  4. Edit /etc/default/grub and add or change the following variables to match. There will be other variables in this file, but we are only concerned with those listed below:

    /etc/default/grub
    1
    2
    3
    4
    5
    GRUB_TIMEOUT=10
    GRUB_DISABLE_LINUX_UUID=true
    GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0,19200n8"
    GRUB_SERIAL_COMMAND="serial --speed=19200 --unit=0 --word=8 --parity=no --stop=1"
    GRUB_TERMINAL="serial console"
    

    Comment or remove any lines starting with GRUB_HIDDEN.

  5. Make the grub directory and build your GRUB configuration file:

    1
    2
    mkdir /boot/grub
    grub2-mkconfig -o /boot/grub/grub.cfg
    

Configure the Linode

  1. In the Linode Dashboard, click Edit next to your Configuration Profile (usually named after the version of Linux installed).

  2. Under Boot Settings, click on the Kernel drop-down menu, and select GRUB2:

    The GRUB2 Option.

  3. Click Save Changes. You can now reboot the Linode. We suggest opening a LISH or GLISH session first, so you can monitor the boot process and troubleshoot if necessary.

Note that if you install an updated kernel, you need to create a new initrd file, and update GRUB.

Congratulations, you’ve booted your Linode using a custom-compiled kernel!

You may need to run cp /boot/grub/unicode.pf2 /boot/grub/fonts/ for the boot menu to properly display in GLISH. Your Linode will still boot, assuming there are no configuration issues, without this command.

This guide is published under a CC BY-ND 4.0 license.