If default Amazon Linux kernel configuration doesn’t have some options enabled in kernel then you can rebuild the kernel with options you need.
If you are not sure how then follow steps below 😉
Contents
- 1 Spawn new Amazon EC2 instance
- 2 Install required tools to build kernel
- 3 Find kernel version of currently loaded kernel
- 4 Find Amazon Linux git repo branch that is associated with currently loaded kernel
- 5 Download Amazon Linux sources
- 6 Clone config of currently loaded kernel.
- 7 Configure kernel
- 8 Building kernel
- 9 Install kernel
- 10 Patch bootloader configuration
- 11 OR update boot loader configuration
- 12 Reboot
Spawn new Amazon EC2 instance
Install required tools to build kernel
sudo yum -y group install "Development Tools"
sudo yum -y install ncurses-devel bison flex elfutils-libelf-devel openssl-devel
Find kernel version of currently loaded kernel
KERNEL_VERSION=$(cut -d ' ' -f 3 < /proc/version)
echo "KERNEL_VERSION=$KERNEL_VERSION"
This variable set in shell will be used later.
Find Amazon Linux git repo branch that is associated with currently loaded kernel
Unfortunately, Amazon doesn’t tag releases, so it will be bleeding edge branch:
KERNEL_AMZN_BRANCH="amazon-$(cut -d ' ' -f 3 < /proc/version | sed -e 's/-.*$//' -e 's/\.[^.]*$//').y/master"
echo "KERNEL_AMZN_BRANCH=$KERNEL_AMZN_BRANCH"
This variable set in shell will be used later.
Download Amazon Linux sources
git clone --single-branch --branch "${KERNEL_AMZN_BRANCH}" \
"https://github.com/amazonlinux/linux.git"
Using --single-branch
saves a lot of time and traffic!!!
Switch to kernel folder:
cd linux
Now you could try to switch to specific commit in the branch to move a bit back from bleeding edge, but you will need to find commit revision on your own. Or you can leave as it is, build it and see if it works without side effects.
Clone config of currently loaded kernel.
cp -v "/boot/config-${KERNEL_VERSION}" ".config"
Configure kernel
Edit “.config` file using sed like below:
# CONFIG_X86_X32 change is just an example
sed -i -e '/CONFIG_X86_X32[ =]/c\CONFIG_X86_X32=y' ".config"
Manual config edits require fixing configuration if it is confusing:
# manual - you will be asked
make oldconfig
# automatic - use defaults
yes "" | make oldconfig
Configure using menuconfig (if needed):
make menuconfig
Building kernel
Build kernel utilizing all available processor cores.
make -j "$(nproc)"
Install kernel
Identify kernel version:
# here we assume bleeding edge, that's why "+" is added
KERNEL_VERSION_NEW="$(make kernelversion)+"
echo "KERNEL_VERSION_NEW=$KERNEL_VERSION_NEW"
This variable set in shell will be used below.
Install kernel modules:
sudo make modules_install
Install kernel config (just for reference):
sudo cp -v ".config" "/boot/config-${KERNEL_VERSION_NEW}"
Install kernel:
sudo cp -v "arch/x86_64/boot/bzImage" "/boot/vmlinuz-${KERNEL_VERSION_NEW}"
Build ram disk using dracut:
sudo dracut --hostonly --kver "${KERNEL_VERSION_NEW}"
Patch bootloader configuration
KERNEL_FILE="/boot/vmlinuz-${KERNEL_VERSION_NEW}"
INITRAMFS_FILE="/boot/initramfs-${KERNEL_VERSION_NEW}.img"
echo "KERNEL_FILE=$KERNEL_FILE"
echo "INITRAMFS_FILE=$INITRAMFS_FILE"
GRUB_CONFIG_SEARCH="/boot/grub/menu.lst /boot/grub/grub.cfg /boot/grub2/grub.cfg"
for GRUB_CONFIG in $GRUB_CONFIG_SEARCH; do
if [ -e "$GRUB_CONFIG" ]; then
sed -i \
-e 's!/boot/\(vmlinuz\|kernel\)-\S\+!'"$KERNEL_FILE"'!g' \
-e 's!/boot/initramfs-\S\+!'"$INITRAMFS_FILE"'!g' \
"$GRUB_CONFIG"
fi
done
OR update boot loader configuration
# if file /boot/grub/grub.cfg exists
grub-mkconfig -o /boot/grub/grub.cfg
# if file /boot/grub2/grub.cfg exists
grub2-mkconfig -o /boot/grub2/grub.cfg
Keep in mind, if you use grub-mkconfig
/ grub2-mkconfig
then your new kernel will be most likely the first that will be booted by grub because it should have higher kernel version number than other available kernels in /boot
. If it is not true, then you can remove vmlinuz-*
and initramfs-*
kernel files for not used kernels and rerun grub-mkconfig
/ grub2-mkconfig
to get your kernel record to be populated first in the menu for sure.
Reboot
Reboot the instance.
If everything will go well, then you will be able to access your instance as before.
If something will go wrong, then take a look in AWS console on System Log and Instance Screenshot. This should give a clue what exactly did not go well.