[SRU][canonical-kernel-snaps/main][PATCH 1/1] nvidia-hooks: add pc-kernel configure hook
Aaron Jauregui
aaron.jauregui at canonical.com
Tue Jun 17 04:05:22 UTC 2025
BugLink: https://bugs.launchpad.net/bugs/2114786
Add a configure hook to the pc-kernel snap to be able to switch between
installed graphics drivers on a system. Configure hook installs
components based off the nvidia-stream snap variable by pulling
nvidia-<nvidia-stream>-ko and nvidia-<nvidia-stream>-server. Setting
nvidia-stream to nouveau triggers a special case where only the
nouveau-ko component is installed. If components are already on the
system they are "loaded" into the graphics dir by rerunning their
install hook if one exists.
To operate the configure hook, in this case to the 550-erd driver:
snap set pc-kernel nvidia-stream=550-erd
Alternatively, to the nouveau driver:
snap set pc-kernel nvidia-stream=nouveau
And so on.
EOL components are also handled by this hook. Installed components are
checked for an "eol" file after being installed. If one is found, the
component number specified inside the eol file is installed. This
behaviour is also triggered if an existing eol component is loaded.
Signed-off-by: Aaron Jauregui <aaron.jauregui at canonical.com>
---
hooks/module/install.module | 4 ++--
hooks/module/post-refresh.module | 4 ++--
hooks/pc-kernel/configure.pc-kernel | 37 +++++++++++++++++++++++++++++
3 files changed, 41 insertions(+), 4 deletions(-)
create mode 100755 hooks/pc-kernel/configure.pc-kernel
diff --git a/hooks/module/install.module b/hooks/module/install.module
index 81695bc..f1e6490 100755
--- a/hooks/module/install.module
+++ b/hooks/module/install.module
@@ -20,5 +20,5 @@ fi
rm -rf "$dest"
mkdir -p "$dest"
-find "$SNAP_COMPONENT" -name '*.ko' -exec cp '{}' "$dest" \;
-find "$SNAP_COMPONENT" -name '*.ko.zst' -exec cp '{}' "$dest" \;
+find -L "$SNAP_COMPONENT" -name '*.ko' -exec cp '{}' "$dest" \;
+find -L "$SNAP_COMPONENT" -name '*.ko.zst' -exec cp '{}' "$dest" \;
diff --git a/hooks/module/post-refresh.module b/hooks/module/post-refresh.module
index 81695bc..f1e6490 100755
--- a/hooks/module/post-refresh.module
+++ b/hooks/module/post-refresh.module
@@ -20,5 +20,5 @@ fi
rm -rf "$dest"
mkdir -p "$dest"
-find "$SNAP_COMPONENT" -name '*.ko' -exec cp '{}' "$dest" \;
-find "$SNAP_COMPONENT" -name '*.ko.zst' -exec cp '{}' "$dest" \;
+find -L "$SNAP_COMPONENT" -name '*.ko' -exec cp '{}' "$dest" \;
+find -L "$SNAP_COMPONENT" -name '*.ko.zst' -exec cp '{}' "$dest" \;
diff --git a/hooks/pc-kernel/configure.pc-kernel b/hooks/pc-kernel/configure.pc-kernel
new file mode 100755
index 0000000..dff3c80
--- /dev/null
+++ b/hooks/pc-kernel/configure.pc-kernel
@@ -0,0 +1,37 @@
+#!/bin/bash
+# Configuration hook for selecting nvidia driver version
+
+nvidia_ver="$(snapctl get nvidia-stream)"
+
+# Do not install any components by default
+[ -z "$nvidia_ver" ] && exit 0
+
+set -eux
+
+compdir="/snap/$SNAP_NAME/components/$SNAP_REVISION"
+
+install_component () {
+
+ # only install component if it does not exist, otherwise rerun install hook
+ if [ ! -d "$compdir/$1" ]; then
+ snapctl install "+$1"
+ elif [ -f "$compdir/$1/meta/hooks/install" ]; then
+ comprev=$(basename "$(readlink "$compdir/$1")")
+ SNAP_COMPONENT_REVISION="$comprev" SNAP_COMPONENT_NAME="$SNAP_NAME+$1" SNAP_COMPONENT="$compdir/$1" "$compdir/$1/meta/hooks/install"
+ fi
+}
+
+
+if [ "$nvidia_ver" = "nouveau" ]; then
+ install_component "nouveau-ko"
+else
+ install_component "nvidia-$nvidia_ver-ko"
+ # check for eol
+ eol_file="$compdir/nvidia-$nvidia_ver-ko/eol"
+ if [ -f "$eol_file" ]; then
+ install_component "nvidia-$(cat "$eol_file")-ko"
+ install_component "nvidia-$(cat "$eol_file")-user"
+ else
+ install_component "nvidia-$nvidia_ver-user"
+ fi
+fi
--
2.43.0
More information about the kernel-team
mailing list