Skip to content

Instantly share code, notes, and snippets.

@robbat2
Created January 5, 2018 22:49
Show Gist options
  • Select an option

  • Save robbat2/3d4fbe17a3503f87cfa46cf0070237cb to your computer and use it in GitHub Desktop.

Select an option

Save robbat2/3d4fbe17a3503f87cfa46cf0070237cb to your computer and use it in GitHub Desktop.
# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$
# Description:
# This ebuild uses genkernel to build tarballs of a kernel.
# - It installs NOTHING on it's own.
# - As a side-effect: It explicitly copies the tarballs from $T to a target
# directory.
EAPI=5
inherit flag-o-matic
INFRA_SUFFIX="infra37"
KERNEL_SOURCES="gentoo-sources"
KERNEL_NAME="gentoo"
KERNEL_PV="$PV"
case $PR in
r0) KERNEL_REVISION="" ;;
*) KERNEL_REVISION="$PR" ;;
esac
KERNEL_PVR="${KERNEL_PV}${KERNEL_REVISION:+-}${KERNEL_REVISION}"
KERNEL_PF="${KERNEL_SOURCES}-${KERNEL_PVR}"
# Where to install the kernel binary packages to
BUILD_DIR="/tmp/"
DESCRIPTION="Package to build kernel + initramfs for Gentoo infra boxes"
HOMEPAGE="http://wiki.gentoo.org/wiki/Project:Infrastructure"
IUSE=""
LICENSE="GPL-2"
SLOT="0"
KEYWORDS="amd64 x86"
DEPEND="
sys-apps/fakeroot
sys-fs/lvm2
>=sys-kernel/genkernel-3.5.2.3
>=sys-apps/kmod-23[zlib,lzma,tools]
=sys-kernel/${KERNEL_PF}"
RDEPEND=""
S="${WORKDIR}"
setup_vars() {
use amd64 && KARCH="x86_64"
use x86 && KARCH="x86"
export KARCH
# Inputs
export KERNEL_DIR="linux-${KERNEL_PV}-${KERNEL_NAME}${KERNEL_REVISION:+-}${KERNEL_REVISION}"
export KERNEL_CONFIG="${FILESDIR}"/${KERNEL_PF}-${KARCH}-${INFRA_SUFFIX}.config
# Output naming
export BINPKG_PVR="${PVR}-${INFRA_SUFFIX}"
export BINPKG_KERNEL="${PN/-sources/}-kernel-${KARCH}-${BINPKG_PVR}.tbz2"
export BINPKG_MODULES="${PN/-sources/}-modules-${KARCH}-${BINPKG_PVR}.tbz2"
}
pkg_setup() {
setup_vars
[ -d /usr/src/${KERNEL_DIR} ] || die "kernel dir /usr/src/${KERNEL_DIR} missing"
# Not legal to access FILESDIR in pkg_setup?
#[ -f "${KERNEL_CONFIG}" ] || die "${KERNEL_CONFIG} missing"
# we need to be using flags that will result in binaries working on all infra systems
strip-flags
filter-flags -march=* -mtune=* -mcpu=* -frecord-gcc-switches
use amd64 && append-flags -march=x86-64 -mtune=generic
use x86 && append-flags -march=pentium3 -mtune=generic
}
src_unpack() {
[ -f "${KERNEL_CONFIG}" ] || die "${KERNEL_CONFIG} missing"
mkdir -p "${T}"/{cache,tmp,kernel-output}
}
src_prepare() {
setup_vars
# copy the kernel sources, this is potentially large, but nothing we can do.
# if it's dirty, the build WILL fail
# symlinks do not work either
mkdir -p "${S}"/usr/src
ksrc="/usr/src/${KERNEL_DIR}"
EXPERT_USER_MY_KERNSRC_DIR_IS_CLEAN=${EXPERT_USER_MY_KERNSRC_DIR_IS_CLEAN:-1}
if [ "${EXPERT_USER_MY_KERNSRC_DIR_IS_CLEAN}" = "1" ]; then
ewarn "Assuming that your kernel source directory is clean, your build WILL fail if this not the case"
find "${ksrc}" \( -name .config -o -name '*.o' -o -name '*.cmd' \) -print -quit |grep -sq . && \
die "Your kernel source tree is not clean!"
ln -sfT "${ksrc}" "${S}${ksrc}" || die "Failed to symlink"
else
cp -a "${ksrc}" "${S}${ksrc}" || die
cd "${S}${ksrc}"
_ARCH="$ARCH"
unset ARCH
emake mrproper || die "Failed to cleanup"
export ARCH=$_ARCH
fi
}
# This deliberately runs a very sterile genkernel
# that IGNORES the system /etc/genkernel.conf
# so that we get more reproducable builds
# almost all the options are easy with this except GK_SHARE
# fakeroot is here because genkernel uses mknod still
# which fails as non-root
genkernel_sterile() {
_DISTDIR="${DISTDIR}"
# the parsing of --config seems to be broken in v3.4.44.2
#--config="${emptyconfig}" \
emptyconfig="${T}"/empty
touch "${emptyconfig}"
CMD_GK_CONFIG="${emptyconfig}" \
GK_SHARE="${ROOT}"/usr/share/genkernel \
DISTDIR="${ROOT}"//usr/share/genkernel/distfiles \
CFLAGS="${CFLAGS}" \
CXXFLAGS="${CXXFLAGS}" \
fakeroot genkernel \
--loglevel=1 \
--no-menuconfig \
--no-gconfig \
--no-xconfig \
--no-save-config \
--oldconfig \
--no-clean \
--no-mrproper \
--no-symlink \
--no-mountboot \
--no-lvm \
--no-mdadm \
--no-dmraid \
--no-multipath \
--no-iscsi \
--no-disklabel \
--no-luks \
--no-gpg \
--no-busybox \
--no-postclear \
--no-install \
--no-zfs \
--no-btrfs \
--no-keymap \
--no-e2fsprogs \
--no-unionfs \
--no-netboot \
--no-firmware-install \
--no-firmware \
--compress-initramfs \
--ramdisk-modules \
--no-debug-cleanup \
"$@"
}
src_compile() {
setup_vars
addpredict "/etc/kernels"
addpredict "/dev"
# call genkernel to build the kernel + initramfs
genkernel_sterile \
--loglevel=5 \
--logfile="${T}"/genkernel.log \
--cachedir="${T}"/cache \
--tempdir="${T}"/tmp \
\
--makeopts="${MAKEOPTS}" \
--kerneldir="/usr/src/${KERNEL_DIR}" \
--kernel-outputdir="${T}/kernel-output" \
--kernel-config="${KERNEL_CONFIG}" \
--module-prefix="${T}" \
\
--lvm \
--disklabel \
--busybox \
--e2fsprogs \
--virtio \
--mdadm --mdadm-config="${FILESDIR}/mdadm.conf-1.0" \
\
--minkernpackage="${T}"/${BINPKG_KERNEL} \
--modulespackage="${T}"/${BINPKG_MODULES} \
\
all \
|| die "genkernel failed"
}
src_install() {
: # This leaves NOTHING in "${D}", by design!
}
pkg_preinst() {
# Cannot do this in src_install, as we WANT to write to somewhere otherwise sandboxed
setup_vars
# copy the built kernel + initramfs
mkdir -p "${BUILD_DIR}"
cp -f "${T}"/${BINPKG_KERNEL} "${BUILD_DIR}" || die "Failed to copy kernel package"
cp -f "${T}"/${BINPKG_MODULES} "${BUILD_DIR}" || die "Failed to copy module package"
einfo "${BINPKG_KERNEL} and ${BINPKG_MODULES} are in ${BUILD_DIR}"
# mirror the packages
# scp ...
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment