diff options
author | Wynn Wolf Arbor | 2020-02-29 19:23:38 +0100 |
---|---|---|
committer | Wynn Wolf Arbor | 2020-02-29 19:36:34 +0100 |
commit | 070a66509cc8c0df7122587944005a0131b210d8 (patch) | |
tree | b67069692b2144a4c090a8c5eaad33430a7cb071 | |
download | kern-0.1.0.tar.gz |
Initial import0.1.0
-rw-r--r-- | LICENSE | 13 | ||||
-rw-r--r-- | Makefile | 6 | ||||
-rwxr-xr-x | kern | 118 |
3 files changed, 137 insertions, 0 deletions
@@ -0,0 +1,13 @@ +Copyright 2018-2019 Wynn Wolf Arbor <wolf@oriole.systems> + +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 @@ -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 |