Skip to content

Instantly share code, notes, and snippets.

@Frederick888
Last active May 10, 2025 12:18
Show Gist options
  • Save Frederick888/8738f8652348f9f205cc5c6636bd3766 to your computer and use it in GitHub Desktop.
Save Frederick888/8738f8652348f9f205cc5c6636bd3766 to your computer and use it in GitHub Desktop.
zfs-utils kernel version guard
From da74e58cb74903153c290b4e33c62ce8c301102b Mon Sep 17 00:00:00 2001
From: Frederick Zhang <[email protected]>
Date: Sat, 10 May 2025 21:39:14 +1000
Subject: [PATCH] Add example Pacman hook that guards against incompatible
kernels
Signed-off-by: Frederick Zhang <[email protected]>
---
00-zfs-dkms-guard.hook | 18 ++++++++++++++++++
PKGBUILD | 16 +++++++++++++---
zfs_dkms_guard.sh | 36 ++++++++++++++++++++++++++++++++++++
3 files changed, 67 insertions(+), 3 deletions(-)
create mode 100644 00-zfs-dkms-guard.hook
create mode 100755 zfs_dkms_guard.sh
diff --git a/00-zfs-dkms-guard.hook b/00-zfs-dkms-guard.hook
new file mode 100644
index 0000000..fc3ea06
--- /dev/null
+++ b/00-zfs-dkms-guard.hook
@@ -0,0 +1,18 @@
+[Trigger]
+Operation = Install
+Operation = Upgrade
+Type = Path
+# target the LTS kernel
+Target = usr/lib/modules/*-lts/vmlinuz
+# ...or the mainline one
+# Target = !usr/lib/modules/*-rt*arch*/vmlinuz
+# Target = usr/lib/modules/*-arch*/vmlinuz
+# ...or all of them
+# Target = usr/lib/modules/*/vmlinuz
+
+[Action]
+Description = Avoid ZFS-incompatible LTS kernels
+When = PreTransaction
+Exec = /usr/local/bin/zfs_dkms_guard.sh
+AbortOnFail
+NeedsTargets
diff --git a/PKGBUILD b/PKGBUILD
index 22dbd81..267455e 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -13,21 +13,27 @@ optdepends=('python: for arcstat/arc_summary/dbufstat')
source=("https://github.com/zfsonlinux/zfs/releases/download/zfs-${pkgver}/zfs-${pkgver}.tar.gz"{,.asc}
"zfs-node-permission.conf"
"zfs.initcpio.install"
"zfs.initcpio.hook"
- "zfs.initcpio.zfsencryptssh.install")
+ "zfs.initcpio.zfsencryptssh.install"
+ "00-zfs-dkms-guard.hook"
+ "zfs_dkms_guard.sh")
sha256sums=('80da628a9543ec3355bf410617450e167706948ceb287541455a1b8d87b8758a'
'SKIP'
'7ad45fd291aa582639725f14d88d7da5bd3d427012b25bddbe917ca6d1a07c1a'
'2f09c742287f4738c7c09a9669f8055cd63d3b9474cd1f6d9447152d11a1b913'
'15b5acea44225b4364ec6472a08d3d48666d241fe84c142e1171cd3b78a5584f'
- 'ac9ed396465e26fa6896762c52a93eb7aaf8af6d7b2c69bd826d219ff821b2c9')
+ 'ac9ed396465e26fa6896762c52a93eb7aaf8af6d7b2c69bd826d219ff821b2c9'
+ 'ac9359baa0bc9fa8118f678a9060f19b88468c9d8f94441e8251a0ab58116efa'
+ '2ceae0f6dd331ad5a412ec3b0b104155a70adacf7935fbb9d04273d4c1b4e97b')
b2sums=('8a89c62cbbeaf410db4011821cdd9959abef1782be7427b81ac47565407384fa3a381bef041dae73e97c2b2cefca62933180851901b3b1b86974ed33ad178a61'
'SKIP'
'7eb3408b1354a4dd504000739101afc7ec0aed1afcdfa029552bf6989e9a8cd4a95b3d3563b3fb7902afa30a80fb01a3f5a2d5af82f9c734c48b5cc23aac25ca'
'cb774227f157573f960bdb345e5b014c043a573c987d37a1db027b852d77a5eda1ee699612e1d8f4a2770897624889f1a3808116a171cc4c796a95e3caa43012'
'779c864611249c3f21d1864508d60cfe5e0f5541d74fb3093c6bdfa56be2c76f386ac1690d363beaee491c5132f5f6dbc01553aa408cda579ebca74b0e0fd1d0'
- 'fcd871d72c62a7c99d6cf29cb40a4751bfc08238ff39e8c9440d119754e92ded4705414710db86e99d044011f3524e54c778bda94696dde2c06b3289da6628d0')
+ 'fcd871d72c62a7c99d6cf29cb40a4751bfc08238ff39e8c9440d119754e92ded4705414710db86e99d044011f3524e54c778bda94696dde2c06b3289da6628d0'
+ '81662406db5c7e6616d2a2cfffe8a51255b533734536a09b2b828dc24ba2977579fa42bd85fdc6c8befce7881ae10da343c9225c78c689c30d65a1ba83ca1d19'
+ '828e922f6eb4a85ef3281c6b0d1a5aeb376d112da4b2835eb287ce66a66c154bac6df8bc8f5da691c7417116a4265e79d8da14f81d96fb565a569a4d16372b13')
validpgpkeys=('4F3BA9AB6D1F8D683DC2DFB56AD860EED4598027' # Tony Hutter (GPG key for signing ZFS releases) <[email protected]>
'C33DF142657ED1F7C328A2960AB9E991C6AF658B') # Brian Behlendorf <[email protected]>
backup=('etc/default/zfs'
'etc/zfs/zed.d/zed.rc')
@@ -88,5 +94,9 @@ package() {
install -D -m644 "${srcdir}"/zfs.initcpio.hook "${pkgdir}"/usr/lib/initcpio/hooks/zfs
install -D -m644 "${srcdir}"/zfs.initcpio.install "${pkgdir}"/usr/lib/initcpio/install/zfs
install -D -m644 "${srcdir}"/zfs.initcpio.zfsencryptssh.install "${pkgdir}"/usr/lib/initcpio/install/zfsencryptssh
+
+ mkdir -p "${pkgdir}"/usr/share/zfs/libalpm/hooks
+ install -m644 "${srcdir}"/00-zfs-dkms-guard.hook "${pkgdir}"/usr/share/zfs/libalpm/hooks/00-zfs-dkms-guard.hook
+ install -m755 "${srcdir}"/zfs_dkms_guard.sh "${pkgdir}"/usr/share/zfs/libalpm/zfs_dkms_guard.sh
}
diff --git a/zfs_dkms_guard.sh b/zfs_dkms_guard.sh
new file mode 100755
index 0000000..3b4e96f
--- /dev/null
+++ b/zfs_dkms_guard.sh
@@ -0,0 +1,36 @@
+#!/usr/bin/env bash
+
+set -eo pipefail
+shopt -s inherit_errexit
+
+vercomp() {
+ readarray -td. one <<<"$1"
+ readarray -td. two <<<"$2"
+ for i in 0 1; do
+ if [[ "${one[$i]}" -lt "${two[$i]}" ]]; then
+ printf -- '-1'
+ return
+ fi
+ if [[ "${one[$i]}" -gt "${two[$i]}" ]]; then
+ printf '1'
+ return
+ fi
+ done
+ printf '0'
+}
+
+zfs_meta_file="$(grep '/META\b' < <(pacman -Qql zfs-dkms))"
+zfs_linux_min="$(awk '/Linux-Minimum/ { print $2 }' "$zfs_meta_file")"
+zfs_linux_max="$(awk '/Linux-Maximum/ { print $2 }' "$zfs_meta_file")"
+
+while read -r target_path; do
+ kernel_version="$(sed -E 's|.*lib/modules/([[:digit:]]+\.[[:digit:]]+).*|\1|' <<<"$target_path")"
+ if [[ "$(vercomp "$kernel_version" "$zfs_linux_min")" -lt 0 ]]; then
+ printf 'Kernel version %s is below ZFS minimum compatible version %s!\n' "$kernel_version" "$zfs_linux_min" >&2
+ exit 1
+ fi
+ if [[ "$(vercomp "$kernel_version" "$zfs_linux_max")" -gt 0 ]]; then
+ printf 'Kernel version %s is above ZFS maximum compatible version %s!\n' "$kernel_version" "$zfs_linux_max" >&2
+ exit 1
+ fi
+done
--
2.49.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment