Compiling/Installing my own kernel

Summary: Is there anything I need to be careful about if I’m installing a kernel outside of the package manager?

Longer version:

Hi all,

5.12 merge window is closing soon-ish, and I’m thinking about compiling it myself. It’s supposed to deliver some important changes for Lenovo laptops which I want to try ASAP, also my town is still under lockdown and I’m looking for something fun to do :slight_smile:

I’m not going to ask for help on how to compile it, there is plenty of documentation online and I think I got it covered. What I’m worried about is breaking my Manjaro setup when the same kernel arrives in the package manager. Is this something I need to be concerned about?

can you have a look with

sudo mhw-kernel -l
sudo mhw-kernel -li

Something to be concerned about is recent news on data loss due to buggy code slipped into the aforementioned merge window. Allow it to settle down a bit, maybe rc2 or 3 will be more mature and safe.

I heard about this issue on Phoronix, looks serious. I expect that minds smarter than I are already on it :slight_smile:

My concern is more about breaking any logic in Manjaro or package manager by installing a new kernel manually.

So don’t. Grab a PKGBUILD for a recent Manjaro kernel, and adapt it for the 5.12 version.

So don’t. Grab a PKGBUILD for a recent Manjaro kernel, and adapt it for the 5.12 version.

I have no idea how to do that. Never packaged for Manjaro before. Any links you can send my way?

This is mine:

# Based on the file created for Arch Linux by:
# Tobias Powalowski <>
# Thomas Baechler <>

# Maintainer: Philip Müller (x86_64) <>
# Maintainer: Jonathon Fernyhough (i686) <>
# Contributor: Helmut Stult <helmut[at]manjaro[dot]org>
# Contributor: Vladimir Yerilov <openmindead[at]gmail[dot]com>

pkgname=('linux512' 'linux512-headers')
validpgpkeys=('ABAF11C65A2970B130ABE3C479BE3E4300411886'  # Linus Torvalds
'647F28654894E3BD457199BE38DBBDC86092693E'  # Greg Kroah-Hartman
'8218F88849AAC522E94CF470A5E9288C4FA415FA'  # Jan Alexander Steffens (heftig)
        # the main kernel config files
        'config' 'config.anbox'
        # ARCH Patches
        # MANJARO Patches
        # Lenovo + AMD

pkgver() {
  printf '%s' "${_pkgver}"

prepare() {

  #mv "${srcdir}/linux-${_commit}" "${srcdir}/linux-${_basekernel}"
  mv "${srcdir}/linux" "${srcdir}/linux-${_basekernel}"
  cd "${srcdir}/linux-${_basekernel}"
  #git pull
  #git checkout ${_commit}

  # add upstream patch
  #patch -p1 -i "${srcdir}/patch-${pkgver}"

  local src
  for src in "${source[@]}"; do
      [[ $src = *.patch ]] || continue
      msg2 "Applying patch: $src..."
      patch -Np1 < "../$src"

  msg2 "add config.anbox to config"
  cat "${srcdir}/config" > ./.config
  cat "${srcdir}/config.anbox" >> ./.config

  #msg2 "Rebuilding local signing key..."
  #cd ../../certs-local

  #msg2 "Updating kernel config with new key..."
  #./ "${srcdir}/linux-${_basekernel}/.config"
  #cd "${srcdir}/linux-${_basekernel}"

  if [ "${_kernelname}" != "" ]; then
    sed -i "s|CONFIG_LOCALVERSION=.*|CONFIG_LOCALVERSION=\"${_kernelname}\"|g" ./.config

  msg "set extraversion to pkgrel"
  sed -ri "s|^(EXTRAVERSION =).*|\1 -${pkgrel}|" Makefile

  # set patchlevel to 12
  sed -ri "s|^(PATCHLEVEL =).*|\1 12|" Makefile

  msg "don't run depmod on 'make install'"
  # We'll do this ourselves in packaging
  sed -i '2iexit 0' scripts/

  msg "get kernel version"
  make prepare

  # load configuration
  # Configure the kernel. Replace the line below with one of your choice.
  #make menuconfig # CLI menu for configuration
  #make nconfig # new CLI menu for configuration
  #make xconfig # X-based configuration
  #make oldconfig # using old config from previous kernel version
  # ... or manually edit .config

  msg "rewrite configuration"
  yes "" | make LSMOD=$HOME/.config/modprobed.db localmodconfig >/dev/null
  #yes "" | make config >/dev/null

build() {
  cd "${srcdir}/linux-${_basekernel}"

  msg "build!"
  make ${MAKEFLAGS} LOCALVERSION= bzImage modules
  #make ${MAKEFLAGS} CC=clang HOSTCC=clang LOCALVERSION= bzImage modules

package_linux512() {
  pkgdesc="The ${pkgbase/linux/Linux} kernel and modules"
  depends=('coreutils' 'linux-firmware' 'kmod' 'mkinitcpio>=27')
  optdepends=('crda: to set the correct wireless channels of your country')

  cd "${srcdir}/linux-${_basekernel}"


  # get kernel version
  _kernver="$(make LOCALVERSION= kernelrelease)"

  mkdir -p "${pkgdir}"/{boot,usr/lib/modules}
  make LOCALVERSION= INSTALL_MOD_PATH="${pkgdir}/usr" INSTALL_MOD_STRIP=1 modules_install

  # systemd expects to find the kernel here to allow hibernation
  cp arch/$KARCH/boot/bzImage "${pkgdir}/usr/lib/modules/${_kernver}/vmlinuz"

  # Used by mkinitcpio to name the kernel
  echo "${pkgbase}" | install -Dm644 /dev/stdin "${pkgdir}/usr/lib/modules/${_kernver}/pkgbase"
  echo "${_basekernel}-${CARCH}" | install -Dm644 /dev/stdin "${pkgdir}/usr/lib/modules/${_kernver}/kernelbase"

  # add kernel version
  echo "${pkgver}-${pkgrel}-MANJARO x64" > "${pkgdir}/boot/${pkgbase}-${CARCH}.kver"

  # make room for external modules
  local _extramodules="extramodules-${_basekernel}${_kernelname:--MANJARO}"
  ln -s "../${_extramodules}" "${pkgdir}/usr/lib/modules/${_kernver}/extramodules"

  # add real version for building modules and running depmod from hook
  echo "${_kernver}" |
    install -Dm644 /dev/stdin "${pkgdir}/usr/lib/modules/${_extramodules}/version"

  # remove build and source links
  rm "${pkgdir}"/usr/lib/modules/${_kernver}/{source,build}

  # now we call depmod...
  depmod -b "${pkgdir}/usr" -F "${_kernver}"

  # add vmlinux
  install -Dt "${pkgdir}/usr/lib/modules/${_kernver}/build" -m644 vmlinux

package_linux512-headers() {
  pkgdesc="Header files and scripts for building modules for ${pkgbase/linux/Linux} kernel"
  depends=('gawk' 'python' 'libelf')

  cd "${srcdir}/linux-${_basekernel}"
  local _builddir="${pkgdir}/usr/lib/modules/${_kernver}/build"

  install -Dt "${_builddir}" -m644 Makefile .config Module.symvers
  install -Dt "${_builddir}/kernel" -m644 kernel/Makefile

  mkdir "${_builddir}/.tmp_versions"

  cp -t "${_builddir}" -a include scripts

  install -Dt "${_builddir}/arch/${KARCH}" -m644 "arch/${KARCH}/Makefile"
  install -Dt "${_builddir}/arch/${KARCH}/kernel" -m644 "arch/${KARCH}/kernel/asm-offsets.s"
  #install -Dt "${_builddir}/arch/${KARCH}/kernel" -m644 "arch/${KARCH}/kernel/macros.s"

  cp -t "${_builddir}/arch/${KARCH}" -a "arch/${KARCH}/include"

  install -Dt "${_builddir}/drivers/md" -m644 drivers/md/*.h
  install -Dt "${_builddir}/net/mac80211" -m644 net/mac80211/*.h

  install -Dt "${_builddir}/drivers/media/i2c" -m644 drivers/media/i2c/msp3400-driver.h

  install -Dt "${_builddir}/drivers/media/usb/dvb-usb" -m644 drivers/media/usb/dvb-usb/*.h
  install -Dt "${_builddir}/drivers/media/dvb-frontends" -m644 drivers/media/dvb-frontends/*.h
  install -Dt "${_builddir}/drivers/media/tuners" -m644 drivers/media/tuners/*.h

  # add xfs and shmem for aufs building
  mkdir -p "${_builddir}"/{fs/xfs,mm}

  # copy in Kconfig files
  find . -name Kconfig\* -exec install -Dm644 {} "${_builddir}/{}" \;

  # add objtool for external module building and enabled VALIDATION_STACK option
  install -Dt "${_builddir}/tools/objtool" tools/objtool/objtool

  # remove unneeded architectures
  local _arch
  for _arch in "${_builddir}"/arch/*/; do
    [[ ${_arch} == */x86/ ]] && continue
    rm -r "${_arch}"

  # remove files already in linux-docs package
  rm -r "${_builddir}/Documentation"

  # Fix permissions
  chmod -R u=rwX,go=rX "${_builddir}"

  # strip scripts directory
  local _binary _strip
  while read -rd '' _binary; do
    case "$(file -bi "${_binary}")" in
      *application/x-sharedlib*)  _strip="${STRIP_SHARED}"   ;; # Libraries (.so)
      *application/x-archive*)    _strip="${STRIP_STATIC}"   ;; # Libraries (.a)
      *application/x-executable*) _strip="${STRIP_BINARIES}" ;; # Binaries
      *) continue ;;
    /usr/bin/strip ${_strip} "${_binary}"
  done < <(find "${_builddir}/scripts" -type f -perm -u+w -print0 2>/dev/null)

   # Out-of-tree module signing
   # This is run in the kernel source / build directory
   #msg2 "Local Signing certs for out-of-tree modules..."


   #mkdir -p ${certs_local_dst}
   #rsync -a $certs_local_src/{current,$key_dir,$signer} $certs_local_dst/

   # DKMS tools
   #mkdir -p $dkms_dst

   #rsync -a $dkms_src/{kernel-sign.conf,} $dkms_dst/

Linux git repo files should be in /home/%username/Git/linux.

What you need to do is:

  • mkdir ~/Git && cd ~/Git
  • git clone && cd linux511
  • nano PKGBUILD_512 and then paste the contents of the above, press Ctrl+S and then Ctrl+X
  • cd .. && git clone && cd linux511
  • updpkgsums PKGBUILD_512
  • makepkg -Ccf -p PKGBUILD_512