From bbb9f075a93e112f5203b1c7bd5dff038200dfea Mon Sep 17 00:00:00 2001 From: Jerry Date: Sun, 2 Apr 2023 11:04:23 +0800 Subject: [PATCH] add package: kernel-modules-hook-hardlinks --- .../10-linux-modules-post.hook | 10 +++++++ .../10-linux-modules-pre.hook | 11 ++++++++ kernel-modules-hook-hardlinks/PKGBUILD | 28 +++++++++++++++++++ kernel-modules-hook-hardlinks/README.md | 12 ++++++++ .../linux-modules-cleanup.conf | 2 ++ .../linux-modules-restore | 11 ++++++++ .../linux-modules-save | 24 ++++++++++++++++ 7 files changed, 98 insertions(+) create mode 100644 kernel-modules-hook-hardlinks/10-linux-modules-post.hook create mode 100644 kernel-modules-hook-hardlinks/10-linux-modules-pre.hook create mode 100644 kernel-modules-hook-hardlinks/PKGBUILD create mode 100644 kernel-modules-hook-hardlinks/README.md create mode 100644 kernel-modules-hook-hardlinks/linux-modules-cleanup.conf create mode 100644 kernel-modules-hook-hardlinks/linux-modules-restore create mode 100644 kernel-modules-hook-hardlinks/linux-modules-save diff --git a/kernel-modules-hook-hardlinks/10-linux-modules-post.hook b/kernel-modules-hook-hardlinks/10-linux-modules-post.hook new file mode 100644 index 0000000..11e71e2 --- /dev/null +++ b/kernel-modules-hook-hardlinks/10-linux-modules-post.hook @@ -0,0 +1,10 @@ +[Trigger] +Operation = Remove +Type = Path +Target = usr/lib/modules/*/vmlinuz + +[Action] +Description = Restoring Linux kernel modules... +When = PostTransaction +Exec = /usr/share/libalpm/scripts/linux-modules-restore +NeedsTargets diff --git a/kernel-modules-hook-hardlinks/10-linux-modules-pre.hook b/kernel-modules-hook-hardlinks/10-linux-modules-pre.hook new file mode 100644 index 0000000..38e4134 --- /dev/null +++ b/kernel-modules-hook-hardlinks/10-linux-modules-pre.hook @@ -0,0 +1,11 @@ +[Trigger] +Operation = Install +Operation = Remove +Type = Path +Target = usr/lib/modules/*/vmlinuz + +[Action] +Description = Saving Linux kernel modules directory (hardlinks)... +When = PreTransaction +Exec = /usr/share/libalpm/scripts/linux-modules-save +NeedsTargets diff --git a/kernel-modules-hook-hardlinks/PKGBUILD b/kernel-modules-hook-hardlinks/PKGBUILD new file mode 100644 index 0000000..2d03844 --- /dev/null +++ b/kernel-modules-hook-hardlinks/PKGBUILD @@ -0,0 +1,28 @@ +# Maintainer: Max Gautier +# Contributor: Artoria Pendragon +_pkgname=kernel-modules-hook +pkgname=${_pkgname}-hardlinks +pkgver=0.2.3 +pkgrel=1 +pkgdesc="Keeps your system fully functional after a kernel upgrade" +arch=('any') +provides=("$_pkgname") +url="https://github.com/VannTen/$_pkgname" +license=('GPL3') +source=("linux-modules-cleanup.conf" + "10-linux-modules-post.hook" + "10-linux-modules-pre.hook" + "linux-modules-restore" + "linux-modules-save" +) +sha256sums=('950f851eba08dac4d0b93ff62b3fb16ddacd4f8ebb98a2435f80bf05f2ea5a29' + 'fc4d53dec520c80fe97dfda65b238c7d678e7ef26aaebffc5b43f924477ea4f4' + '087e5ed70b2fc512d814fbded9e873955b1da5b6bf1e31975174d704ad143156' + '28b995f6ea62899620d43d71e542bfc3432ad3ce90a6582f4ded1173fe8c0837' + '5d9092689d9baa3d8e5ac65ae92d1c201e23c4ef42c99d131f9465c99d6505ef') + +package() { + install -Dm644 'linux-modules-cleanup.conf' "${pkgdir}/usr/lib/tmpfiles.d/linux-modules-cleanup.conf" + install -Dm644 10-linux-modules-{pre,post}.hook -t "${pkgdir}/usr/share/libalpm/hooks/" + install -Dm755 linux-modules-{save,restore} -t "${pkgdir}/usr/share/libalpm/scripts/" +} diff --git a/kernel-modules-hook-hardlinks/README.md b/kernel-modules-hook-hardlinks/README.md new file mode 100644 index 0000000..cf25ee1 --- /dev/null +++ b/kernel-modules-hook-hardlinks/README.md @@ -0,0 +1,12 @@ +# kernel-modules-hook + +This is an alpm hook for Archlinux (or other pacman-based distribution) to keep the kernel modules of the running kernel available during a kernel upgrade. + +It relies on hardlinks and bind mount for saving and restoring, and systemd-tmpfiles for cleanup. + +## Installation + +### Archlinux +[AUR](https://aur.archlinux.org/packages/kernel-modules-hook-hardlinks/) + +Use your AUR helper of choise, or use makepkg. diff --git a/kernel-modules-hook-hardlinks/linux-modules-cleanup.conf b/kernel-modules-hook-hardlinks/linux-modules-cleanup.conf new file mode 100644 index 0000000..1dd8d3b --- /dev/null +++ b/kernel-modules-hook-hardlinks/linux-modules-cleanup.conf @@ -0,0 +1,2 @@ +R! /usr/lib/modules/running-kernel +r /usr/lib/modules/* diff --git a/kernel-modules-hook-hardlinks/linux-modules-restore b/kernel-modules-hook-hardlinks/linux-modules-restore new file mode 100644 index 0000000..baceebf --- /dev/null +++ b/kernel-modules-hook-hardlinks/linux-modules-restore @@ -0,0 +1,11 @@ +#!/bin/bash -e + +kver=$(uname -r) +while read -r line; do + # We only care about the running kernel + if [[ "$line" == usr/lib/modules/$kver/vmlinuz && -d "${line/$kver/running-kernel}" ]];then + mount --mkdir --bind --options ro /usr/lib/modules/{running-kernel,$kver} + # Mounting read-only since the only modification here should be unmounting + # when rebooting or reinstalling the running kernel + fi +done diff --git a/kernel-modules-hook-hardlinks/linux-modules-save b/kernel-modules-hook-hardlinks/linux-modules-save new file mode 100644 index 0000000..8bf2e29 --- /dev/null +++ b/kernel-modules-hook-hardlinks/linux-modules-save @@ -0,0 +1,24 @@ +#!/bin/bash -e + +kver=$(uname -r) +while read -r line; do + # We only care about the running kernel + if [[ "$line" == "usr/lib/modules/$kver/vmlinuz" ]];then + if mountpoint --nofollow --quiet ${line%vmlinuz};then + # Mount point is already present + # This means we already ran that hook during 'remove case' + # + # Remove the mount so we can reinstall the kernel + umount ${line%vmlinuz} + elif [[ -f "$line" && ! -d "/usr/lib/modules/running-kernel/" ]];then + # Kernel install is present and we do not have a copy + # + # This is the removal case, so we save the kernel + mkdir /usr/lib/modules/running-kernel + cp --archive --link /usr/lib/modules/{$kver}/{kernel,modules*} \ + /usr/lib/modules/running-kernel/ + fi + # If we are re-removing the running kernel, (after removing + reinstalling), + # we already have a backup and this hook is a no-op + fi +done