Skip to content

Instantly share code, notes, and snippets.

@bencord0
Created November 10, 2020 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bencord0/e8b96d6cbc56675b0126eb8e08d9455a to your computer and use it in GitHub Desktop.
Save bencord0/e8b96d6cbc56675b0126eb8e08d9455a to your computer and use it in GitHub Desktop.
How to build a kernel
#!/bin/bash
set -ex
KVER="$(make -s -C "${BUILD_DIR}" kernelversion 2>/dev/null)"
BUILD_DIR="/usr/src/build-${KVER}"
SRC_DIR=/usr/src/linux
function copy_config() {
mkdir -vp "${BUILD_DIR}"
if [[ -e /root/config ]]; then
cp -v /root/config "${BUILD_DIR}/.config"
else
zcat /proc/config.gz > "${BUILD_DIR}/.config"
fi
make -C "${SRC_DIR}" O="${BUILD_DIR}" olddefconfig
}
if [[ ! -d /boot/loader ]]; then
mount /boot
fi
copy_config
make -C "${BUILD_DIR}" "-j$(nproc)" olddefconfig modules_prepare scripts
export KBUILD_OUTPUT="${BUILD_DIR}"
env EXTRA_ECONF="--with-spl=${SRC_DIR} --enable-linux-builtin --with-spl-obj=${BUILD_DIR}" \
ebuild "$(portageq get_repo_path / gentoo)/sys-fs/zfs-kmod/zfs-kmod-9999.ebuild" clean configure
(cd /var/tmp/portage/sys-fs/zfs-kmod-9999/work/zfs-kmod-9999/ && ./copy-builtin "${SRC_DIR}")
unset KBUILD_OUTPUT
emerge --usepkg=n sys-fs/zfs
copy_config
make -C "${BUILD_DIR}" "-j$(nproc)"
make -C "${BUILD_DIR}" modules_install
depmod -a
# Create the initramfs
dracut -f "/root/initramfs.img" "${KVER}"
# Bundle boot artifacts together, the offsets are arbitrary and are only used by linuxx64.efi.stub.
objcopy \
--add-section .osrel=/etc/os-release --change-section-vma .osrel=0x20000 \
--add-section .cmdline=/etc/cmdline.txt --change-section-vma .cmdline=0x30000 \
--add-section .linux="${BUILD_DIR}/arch/x86_64/boot/bzImage" --change-section-vma .linux=0x2000000 \
--add-section .initrd=/root/initramfs.img --change-section-vma .initrd=0x3000000 \
/usr/lib/systemd/boot/efi/linuxx64.efi.stub /root/linux.efi
# Sign boot artifact
sbsign \
--key /etc/efikeys/db.key \
--cert /etc/efikeys/db.crt \
--output "/boot/${KVER}".efi \
"/root/linux.efi"
# Reconfigure the bootloader
sed "s/KVER/${KVER}/" /boot/loader/entries/gentoo-template > "/boot/loader/entries/${KVER}.conf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment