Бинарное ядро

Оригинал там Сборка ядра в Gentoo GNU/Linux отдельно от исходников

Меня всегда озадачивал тот момент что в gentoo, в которой опакечено всё, sys-kernel/*-sources заведует исходниками а собственно самим собранным ядром и его модулями не заведует вообще никто.

Описываю текущее решение. Подробности, порядок работы и прочее читайте в оригинале. Далее привожу скрипты.

Подготовка :

:::BashLexer
# Тут будет сборка
mkdir -p /var/portage/kernel
# А тут env файл
mkdir -p /etc/portage/env/sys-kernel

Теперь сама магия

cat /etc/portage/env/sys-kernel/bentoo-sources
post_src_unpack() {
# Remove "-rXX" from EXTRAVERSION
    sed -i -e 's/\(^EXTRAVERSION = .*\)-r.*$/\1/' "${S}/Makefile"
}

post_pkg_postinst() {
# Specify a directory for temporary kernel files.
KBUILD_DIR="/var/portage/kernel"

[ "${PR}" != "r0" ] && PR="-${PR}" || PR=""
KBUILD_OUTPUT="${KBUILD_DIR}/kernel-${PV}-${PN/-sources/}${PR}"
[ ! -d "${KBUILD_OUTPUT}" ] && mkdir -p "${KBUILD_OUTPUT}"

# If you want to copy the current kernel configuration, uncomment the following line.
#[ -f /proc/config.gz ] && [ ! -f "${KBUILD_OUTPUT}/.config" ] && gzip -cd /proc/config.gz >"${KBUILD_OUTPUT}/.config"
}

Изменения в bashrc

cat /etc/portage/bashrc
if [[ ${PN} =~ (modules|addons)$ ]]; then
  export KBUILD_OUTPUT="/var/tmp/portage/kernel/kernel-$(readlink /usr/src/linux|sed -e 's/^linux-//')"
fi

makekernel.sh

cat /var/portage/kernel/makekernel.sh
#!/bin/bash
#
# Make kernel and modules
# Install into portage
# Build binary package
#

source $(dirname $(readlink -f $0))/lib.sh
prepare

TMPDIR=$(mktemp -u -d -p /tmp/ krn.XXXXXX)
PREFIX="$(echo "${KERNELLINK}"|cut -d '-' -f2)"
PN="${PREFIX}-kernel"
PNFW="kernel-firmware"
PV=$(echo "${KERNELLINK}"|cut -d '-' -f1)
PR=$(echo "${KERNELLINK}"|cut -d '-' -f3)
[ -n "${PR}" ] && PR="-${PR}"
PVR="${PV}${PR}"
PF="${PN}-${PVR}"
PFFW="${PNFW}-${PVR}"

INSTALL="${TMPDIR}/sys-kernel/${PN}/files"
INSTALLFW="${TMPDIR}/sys-kernel/${PNFW}/files/lib"

eval $(portageq envvar -v PKGDIR)

if [ -z "${PKGDIR}" ]; then
    eerror "Variable 'PKGDIR' not set in '/etc/portage/make.conf'"
    exit 1
fi

cd "${KERNEL}"
einfo "Preparing kernel and modules..."
if make bzImage modules; then
    einfo "Install modules..."
    INSTALL_MOD_PATH="${INSTALL}" make modules_install
else
    eerror "Kernel compilation error!"
    exit 1
fi

if [ -d "${INSTALL}/lib/modules" ]; then
    einfo "Prepare environment..."

    mkdir -p "${INSTALLFW}"

    if [ -d "${INSTALL}/lib/firmware" ]; then
    mv -f "${INSTALL}/lib/firmware" "${INSTALLFW}"
    FWDEP=">=sys-kernel/kernel-firmware-${PVR}"
    else
    ewarn "Firmware not found."
    FWDEP=""
    fi

    mkdir -p "${INSTALL}/boot"

    cp "${KBUILD_OUTPUT}/System.map" "${INSTALL}/boot/System.map-${PV}-${PREFIX}"
    cp "${KBUILD_OUTPUT}/arch/x86/boot/bzImage" "${INSTALL}/boot/vmlinuz-${PV}-${PREFIX}"
    rm -f "${INSTALL}/lib/modules/${PV}-${PREFIX}/build"
    rm -f "${INSTALL}/lib/modules/${PV}-${PREFIX}/source"

    mkdir -p "${TMPDIR}/profiles"
    cat > "${TMPDIR}/profiles/repo_name" << EOF
bentoo-sources
EOF

    mkdir -p "${TMPDIR}/metadata"
    cat > "${TMPDIR}/metadata/layout.conf" << EOF
masters = gentoo
eapis-banned = 0 1 2 3
eapis-deprecated = 0 1 2 3 4
sign-commits = true
sign-manifests = false
thin-manifests = true
use-manifests = strict
manifest-hashes = SHA256 SHA512 WHIRLPOOL
update-changelog = false
cache-formats = md5-dict
profile-formats = portage-2
EOF

    cat > "${TMPDIR}/sys-kernel/${PN}/${PF}.ebuild" << EOF
# Copyright 1999-2017 The Bentoo Authors. All rights reserved.
# Distributed under the terms of the GNU General Public License v3 or later
# $Id$

EAPI="6"

inherit mount-boot

DESCRIPTION="Binary ${PVR} kernel and modules"
HOMEPAGE="https://bitbucket.org/redeyeteam/bentoo"
SRC_URI=""

LICENSE="GPL-2"
SLOT="${PV}"
KEYWORDS="amd64 x86"
IUSE=""

RDEPEND="virtual/dev-manager ${FWDEP}"

S="\${WORKDIR}"

pkg_setup() {
    ewarn 'All modules from "/lib/modules/${PV}-${PREFIX}" removed'
    ewarn 'Please, run "module-rebuild rebuild" after installation'
    rm -rf /lib/modules/${PV}-${PREFIX}
}

src_prepare() {
    eapply_user
}

src_install() {
    insinto /
    doins -r "\${FILESDIR}/boot"
    doins -r "\${FILESDIR}/lib"
}

pkg_prerm() {
    mount-boot_mount_boot_partition
}
EOF
    cat > "${TMPDIR}/sys-kernel/${PNFW}/${PFFW}.ebuild" << EOF
# Copyright 1999-2017 The Bentoo Authors. All rights reserved.
# Distributed under the terms of the GNU General Public License v3 or later
# $Id$

EAPI="6"

DESCRIPTION="Binary ${PVR} kernel firmware"
HOMEPAGE="https://bitbucket.org/redeyeteam/bentoo"
SRC_URI=""

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="amd64 x86"
IUSE=""

RDEPEND="!sys-kernel/linux-firmware"

S="\${WORKDIR}"

src_prepare() {
    eapply_user
}

src_install() {
    insinto /
    doins -r "\${FILESDIR}/lib"
}
EOF
    if [ -n "${FWDEP}" ]; then
    einfo "Building kernel firmware..."
    ebuild "${TMPDIR}/sys-kernel/${PNFW}/${PFFW}.ebuild" digest package
    einfo "Install kernel firmware..."
    [ -f "${PKGDIR}/sys-kernel/${PFFW}.tbz2" ] && emerge -1K "=sys-kernel/${PFFW}" || eerror "Kernel firmware not installed"
 fi
    einfo "Building binary kernel..."
    ebuild "${TMPDIR}/sys-kernel/${PN}/${PF}.ebuild" digest package
    einfo "Install binary kernel..."
    [ -f "${PKGDIR}/sys-kernel/${PF}.tbz2" ] && emerge -1K "=sys-kernel/${PF}" || eerror "Kernel not installed"
    rm -rf "${TMPDIR}"
else
    eerror "Kernel compilation error!"
    exit 1
fi

[ -f /boot/grub/grub.cfg ] && grub-mkconfig -o /boot/grub/grub.cfg

makemenuconfig.sh

cat /var/portage/kernel/makemenuconfig.sh
#!/bin/bash
#
# Run 'make menuconfig'
#

source $(dirname $(readlink -f $0))/lib.sh
prepare

cd "${KERNEL}"
make menuconfig

makeoldconfig.sh

cat /var/portage/kernel/makeoldconfig.sh
#!/bin/bash
#
# Run 'make oldconfig'
#

source $(dirname $(readlink -f $0))/lib.sh
prepare

cd "${KERNEL}"
make oldconfig

lib.sh

cat /var/portage/kernel/lib.sh
#!/bin/bash

# Specify a directory for temporary kernel files.
KBUILD_DIR="/var/portage/kernel"

prepare () {
    PORTAGE_BIN_PATH="/usr/lib/portage/python2.7/" || exit 1
    source "${PORTAGE_BIN_PATH}/isolated-functions.sh" || exit 1

    if [ ! -L "/usr/src/linux" ]; then
    eerror "'/usr/src/linux' not a symlink"
    exit 1
    fi

    KERNELLINK=$(readlink /usr/src/linux|sed -e 's/^linux-//')
    KERNEL="/usr/src/linux-${KERNELLINK}"
    export KBUILD_OUTPUT="${KBUILD_DIR}/kernel-${KERNELLINK}"

    if [ -d "${KBUILD_OUTPUT}" ]; then
    if [ ! -f "${KBUILD_OUTPUT}/.config" ]; then
        eerror "Kernel confiruration '${KBUILD_OUTPUT}/.config' not found"
        exit 1
    fi
    else
    eerror "Kernel binary directory '${KBUILD_OUTPUT}' not exist"
    exit 1
    fi
}

Comments