From 070a66509cc8c0df7122587944005a0131b210d8 Mon Sep 17 00:00:00 2001 From: Wynn Wolf Arbor Date: Sat, 29 Feb 2020 19:23:38 +0100 Subject: Initial import --- LICENSE | 13 +++++++ Makefile | 6 ++++ kern | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100755 kern diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..efa24df --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright 2018-2019 Wynn Wolf Arbor + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8a3008a --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +PREFIX ?= /usr/local + +install: kern + install -D -m 755 -t '${DESTDIR}${PREFIX}/bin' kern + +.PHONY: install diff --git a/kern b/kern new file mode 100755 index 0000000..23822e1 --- /dev/null +++ b/kern @@ -0,0 +1,118 @@ +#!/bin/sh + +set -e + +SRCDIR=/usr/src +KERNFRAG=${KERNFRAG:-/etc/kernfrag} + +errx() { + printf "kern: %s\n" "$@" >&2 + exit 1 +} + +has_boot_mount() { + awk '{print $2}' /etc/fstab | grep -q ^/boot$ +} + +get_latest_kernel() { + eselect --brief kernel list | tail -n1 +} + +get_current_kernel() { + basename "$(eselect --brief kernel show)" +} + +set_kernel() { + kern=${1:-$(get_latest_kernel)} + current=$(get_current_kernel) + kerndir=$SRCDIR/$kern + + if test ! -d "$kerndir"; then + errx "No such file or directory: $kerndir" + fi + + if test "$kern" = "$current"; then + errx "Latest kernel already set: $kern" + fi + + printf "Setting active kernel: %s\n" "$kern" + + sudo eselect kernel set "$kern" + sudo chown -R "$(id -un)":"$(id -gn)" "$kerndir" +} + +config_kernel() { + host=${1:-$(hostname)} + + printf "Starting merge for %s...\n\n" "$host" + + awk -v prefix="$KERNFRAG/fragments/" '{print prefix $0}' \ + "$KERNFRAG/templates/base" "$KERNFRAG/templates/$host" | xargs scripts/kconfig/merge_config.sh +} + +diff_kernel() { + diff=${1:-/proc/config.gz} + nvim -d .config "$diff" +} + +build_kernel() { + make -j"$(nproc)" +} + +install_kernel() { + if has_boot_mount; then + sudo mount /boot + fi + sudo make install modules_install +} + +post_install_kernel() { + sudo emerge --ask=n @module-rebuild +} + +clean_kernel() { + sudo eclean-kernel -An3 +} + +test_diff() { + dt=$(mktemp) + zcat /proc/config.gz | sed -nE '/^(# )?CONFIG_.*/p' > "$dt" + sed -nE '/^(# )?CONFIG_.*/p' .config | diff -q "$dt" - + diffr=$? + rm -f "$dt" + if test $diffr -ne 0; then + diff_kernel + printf "Configs differ, exiting.\n" + fi + return $diffr +} + +all() { + set_kernel && + cd /usr/src/linux && + config_kernel && + test_diff && + build_kernel && + install_kernel && + post_install_kernel && + clean_kernel +} + +if test $# -eq 0; then + all + exit 0 +fi + +cmd=$1 +shift + +case $cmd in + set) set_kernel "$@";; + config) config_kernel "$@";; + diff) diff_kernel "$@";; + build) build_kernel;; + install) install_kernel;; + postinst) post_install_kernel;; + clean) clean_kernel;; + all) all;; +esac -- cgit v1.2.3-2-gb3c3