diff mbox series

[8/9] UBUNTU: [Packaging] handle the -open variant

Message ID 20220914135455.2685644-9-apw@canonical.com
State New
Headers show
Series [1/9] UBUNTU: [Packaging] fix our dh_clean override | expand

Commit Message

Andy Whitcroft Sept. 14, 2022, 1:54 p.m. UTC
Add support for the -open variant.  This is a version of the Nvidia
kernel driver which is GPL compliant and which therefore we can build
pre-linked.  Adjust to this workflow for the -open variant only.  Final
signed binaries are generated in -signatures.

Signed-off-by: Andy Whitcroft <apw@canonical.com>
---
 .../debian/scripts/gen-rules                  |  12 +-
 .../debian/rules.in                           |  18 +++
 .../debian/scripts/gen-rules                  | 147 +++++++++++++++++-
 .../debian/templates/open.postinst.in         |  20 +++
 .../debian/templates/open.postrm.in           |  26 ++++
 debian/scripts/gen-rules                      |  12 ++
 6 files changed, 232 insertions(+), 3 deletions(-)
 create mode 100755 debian/ancillary/linux-restricted-signatures/debian/templates/open.postinst.in
 create mode 100755 debian/ancillary/linux-restricted-signatures/debian/templates/open.postrm.in
diff mbox series

Patch

diff --git a/debian/ancillary/linux-restricted-generate/debian/scripts/gen-rules b/debian/ancillary/linux-restricted-generate/debian/scripts/gen-rules
index a8b349c..2814d34 100755
--- a/debian/ancillary/linux-restricted-generate/debian/scripts/gen-rules
+++ b/debian/ancillary/linux-restricted-generate/debian/scripts/gen-rules
@@ -136,7 +136,16 @@  EOL
 
 		# debian/rules.gen
 		# XXX: BUILD should help us here.
-		cat - >>"debian/rules.gen" <<EOL
+		if [ "$variant" = "-open" ]; then
+			cat - >>"debian/rules.gen" <<EOL
+
+# $package $version $suffix_minus $suffix_under $suffix_short
+$targets::
+	install -d \$(custom_dir)/${src_abi}-${flavour}/open/nvidia-${suffix_short}
+	cp -rp /lib/modules/${src_abi}-${flavour}/kernel/nvidia-${suffix_short}/*.ko \$(custom_dir)/${src_abi}-${flavour}/open/nvidia-${suffix_short}
+EOL
+		else
+			cat - >>"debian/rules.gen" <<EOL
 
 # $package $version $suffix_minus $suffix_under $suffix_short
 $targets::
@@ -149,6 +158,7 @@  $targets::
 	)
 	rm -rf \$(custom_dir)/${src_abi}-${flavour}/signatures/nvidia-${suffix_short}/bits
 EOL
+		fi
 
 	done <"debian/dkms-versions"
 done <"debian/package.config"
diff --git a/debian/ancillary/linux-restricted-signatures/debian/rules.in b/debian/ancillary/linux-restricted-signatures/debian/rules.in
index 1c22d8e..2cf4725 100755
--- a/debian/ancillary/linux-restricted-signatures/debian/rules.in
+++ b/debian/ancillary/linux-restricted-signatures/debian/rules.in
@@ -10,3 +10,21 @@  override_dh_auto_build:
 
 override_dh_auto_install: nvidia-$(arch)
 	dh_install
+
+override_dh_gencontrol:
+	dh_gencontrol -- $(gencontrol_flags)
+
+gencontrol_flags :=
+
+define install_control =
+	for which in $(5);							\
+	do									\
+		template="debian/templates/$(3).$$which.in";			\
+		script="debian/$(1).$$which";					\
+		sed -e 's/@abiname@/$(src_abi)/g'				\
+		    -e 's/@localversion@/-$(2)/g'				\
+		    -e 's/@image-stem@/$(instfile)/g'				\
+		    -e 's/@nvidia-ver@/$(4)/g'					\
+			<"$$template" >"$$script";				\
+	done
+endef
diff --git a/debian/ancillary/linux-restricted-signatures/debian/scripts/gen-rules b/debian/ancillary/linux-restricted-signatures/debian/scripts/gen-rules
index ed74d70..98aad37 100755
--- a/debian/ancillary/linux-restricted-signatures/debian/scripts/gen-rules
+++ b/debian/ancillary/linux-restricted-signatures/debian/scripts/gen-rules
@@ -29,6 +29,8 @@  src_genr_version=${src_version}
 # linux-restricted-signatures/5.8.0-41.46[+1]
 
 # linux-restricted-modules/5.8.0-41.46[+1]
+src_lrm_package=$(echo "${src_package}" | sed -e 's/-restricted-signatures/-restricted-modules/')
+src_lrm_version=${src_version}
 
 cat - "debian/rules.in" >"debian/rules.gen" <<EOL
 #! /usr/bin/make -f
@@ -100,7 +102,148 @@  Description: Linux kernel signatures for nvidia modules for version ${src_abi}-$
 EOL
 done <"debian/package.config"
 
-cat "debian/control.common" "debian/control.signatures" | sed \
+nvidia_desktop=
+nvidia_server=
+nvidia_ignore=
+while read command arg
+do
+	case "$command" in
+	option)		;;
+	suppress)		nvidia_ignore="$nvidia_ignore $arg"; continue ;;
+	*)		continue ;;
+	esac
+
+	case "$arg" in
+	desktop)	nvidia_desktop=y ;;
+	server)		nvidia_server=y ;;
+	esac
+done <"debian/package.config"
+
+: >"debian/control.open"
+while read command flavour archs
+do
+	case "$command" in
+	build)		;;
+	*)		continue ;;
+	esac
+
+	validate_unique "archs" "$archs"
+
+	while read package version extra
+	do
+		case "$package" in
+		nvidia-graphics-drivers-*-server)
+			[ -z "$nvidia_server" ] && continue
+			;;
+		nvidia-graphics-drivers-*)
+			[ -z "$nvidia_desktop" ] && continue
+			;;
+		*) continue ;;
+		esac
+		case " $nvidia_ignore " in
+		*\ $package\ *)		continue ;;
+		esac
+
+		case " $extra " in
+		*\ signonly\ *)		continue ;;
+		esac
+
+		# If package specifies arch= keys, only build it on
+		# the intersection of kernel flavour arches & the
+		# package arches, otherwise build for all kernel
+		# flavour arches.
+		pkg_archs=
+		variant=
+		for cmd in $extra
+		do
+			case "$cmd" in
+			arch=*)		pkg_archs="$pkg_archs ${cmd#arch=}" ;;
+			variant=*) variant="-${cmd#variant=}" ;;
+			esac
+		done
+
+		if [ "$variant" != "-open" ]; then
+			continue
+		fi
+
+		# No package arches set, build for everything
+		if [ -z "$pkg_archs" ]; then
+			pkg_archs=$archs
+		else
+			# Package arches set, build for intersection
+			validate_unique "pkg_archs" "$pkg_archs"
+			pkg_archs=$(sort <(echo $archs | tr " " "\n") <(echo $pkg_archs | tr " " "\n") | uniq -d | tr "\n" " ")
+			# No intersection, skip
+			if [ -z "$pkg_archs" ]; then
+				continue
+			fi
+		fi
+
+		targets=$(echo "$pkg_archs" | sed -e 's/\</nvidia-/g')
+		suffix_minus=$(echo "$package$variant" | sed -e 's/nvidia-graphics-drivers-//')
+		suffix_under=$(echo "$suffix_minus" | sed -e 's/-/_/g')
+		suffix_short=$(echo "$suffix_minus" | sed -e 's/-server/srv/g')
+		suffix_minus_common=$(echo "$package" | sed -e 's/nvidia-graphics-drivers-//')
+
+		echo "II: build $package for $flavour $pkg_archs"
+
+		cat - >>"debian/control.interlock-up" <<EOL
+ linux-objects-nvidia-${suffix_minus}-${src_abi}-${flavour} (>= ${src_lrm_version}) [${pkg_archs}],
+EOL
+
+		# debian/rules.gen
+		cat - >>"debian/rules.gen" <<EOL
+
+# $package $version $suffix_minus $suffix_under
+dkms_nvidia_${suffix_under}_version="$version"
+nvidia_${suffix_under}_upstream_version=\$(shell echo "\$(dkms_nvidia_${suffix_under}_version)" | cut -d '-' -f 1)
+gencontrol_flags += -Vdkms:nvidia-${suffix_minus}-modules="nvidia-dkms-${suffix_minus} (= \$(dkms_nvidia_${suffix_under}_version))"
+gencontrol_flags += -Vnvk:nvidia-${suffix_minus}="nvidia-kernel-common-${suffix_minus_common} (<= \$(nvidia_${suffix_under}_upstream_version)-1), nvidia-kernel-common-${suffix_minus_common} (>= \$(nvidia_${suffix_under}_upstream_version))"
+test::
+	@echo $package $version $suffix_minus $suffix_under $suffix_short $targets
+$targets::
+	install -d debian/linux-modules-nvidia-${suffix_minus}-\$(src_abi)-${flavour}
+	install -d debian/linux-modules-nvidia-${suffix_minus}-\$(src_abi)-${flavour}/usr/lib/linux/triggers
+	\$(call install_control,linux-modules-nvidia-${suffix_minus}-\$(src_abi)-${flavour},${flavour},open,${suffix_short},postinst postrm); \\
+	stream="${src_genr_version}/${src_abi}-${flavour}/open/nvidia-${suffix_minus}"; \\
+	nvidia=\$\$(basename "\$\$stream"); \\
+	install -d debian/linux-modules-nvidia-${suffix_minus}-\$(src_abi)-${flavour}/lib/modules/kernel/\$\$nvidia; \\
+	for sig in \$\$stream/*.ko.sig; \\
+	do \\
+		ko=\$\$(basename "\$\$sig" ".sig"); \\
+		cat "/lib/modules/${src_abi}-${flavour}/kernel/\$\$nvidia/\$\$ko" "\$\$sig" >"\$\$stream/\$\$ko"; \\
+		echo "\$\$stream/\$\$ko /lib/modules/${src_abi}-${flavour}/kernel/\$\$nvidia" >>debian/linux-modules-nvidia-${suffix_minus}-${src_abi}-${flavour}.install; \\
+	done
+EOL
+
+		# debian/control.d/nvidia
+		cat - >>"debian/control.open" <<EOL
+
+Package: linux-modules-nvidia-${suffix_minus}-${src_abi}-${flavour}
+Build-Profiles: <!stage1>
+Architecture: ${pkg_archs}
+Section: kernel
+Priority: optional
+Built-Using: \${dkms:nvidia-${suffix_minus}-modules}
+Depends:
+ \${misc:Depends},
+ \${shlibs:Depends},
+ linux-image-${src_abi}-${flavour} | linux-image-unsigned-${src_abi}-${flavour},
+ \${nvk:nvidia-${suffix_minus}},
+Description: Linux kernel nvidia modules for version ${src_abi}
+ This package pulls together the Linux kernel nvidia modules for
+ version ${src_abi} with the appropriate signatures.
+ .
+ You likely do not want to install this package directly. Instead, install the
+ one of the linux-modules-nvidia-${suffix_minus}-${flavour}* meta-packages,
+ which will ensure that upgrades work correctly, and that supporting packages are
+ also installed.
+EOL
+
+	done <"debian/dkms-versions"
+done <"debian/package.config"
+
+cat "debian/control.common" "debian/control.signatures" "debian/control.open" | sed \
 	-e "/@BUILD-INTERLOCK@/{"		\
 	-e " r debian/control.interlock-up"	\
 	-e " d"					\
@@ -110,4 +253,4 @@  cat "debian/control.common" "debian/control.signatures" | sed \
 	-e "s/@SERIES@/${src_series}/g"		\
     >"debian/control"
 
-rm -f "debian/control.interlock-up" "debian/control.signatures"
+rm -f "debian/control.interlock-up" "debian/control.signatures" "debian/control.open"
diff --git a/debian/ancillary/linux-restricted-signatures/debian/templates/open.postinst.in b/debian/ancillary/linux-restricted-signatures/debian/templates/open.postinst.in
new file mode 100755
index 0000000..c4a556c
--- /dev/null
+++ b/debian/ancillary/linux-restricted-signatures/debian/templates/open.postinst.in
@@ -0,0 +1,20 @@ 
+#!/bin/sh
+set -e
+
+version=@abiname@@localversion@
+image_path=/boot/@image-stem@-$version
+
+if [ "$1" != configure ]; then
+    exit 0
+fi
+
+depmod -a -F /boot/System.map-$version $version || true
+if [ -d /etc/kernel/postinst.d ]; then
+    cat - >/usr/lib/linux/triggers/$version <<EOF
+DEB_MAINT_PARAMS="$*" run-parts --report --exit-on-error --arg=$version \
+	    --arg="$image_path" /etc/kernel/postinst.d
+EOF
+    dpkg-trigger --no-await linux-update-$version
+fi
+
+exit 0
diff --git a/debian/ancillary/linux-restricted-signatures/debian/templates/open.postrm.in b/debian/ancillary/linux-restricted-signatures/debian/templates/open.postrm.in
new file mode 100755
index 0000000..ae781cd
--- /dev/null
+++ b/debian/ancillary/linux-restricted-signatures/debian/templates/open.postrm.in
@@ -0,0 +1,26 @@ 
+#!/bin/sh
+set -e
+
+version=@abiname@@localversion@
+image_path=/boot/@image-stem@-$version
+
+if [ "$1" != remove ]; then
+    exit 0
+fi
+
+depmod -a -F /boot/System.map-$version $version 2>/dev/null || true
+#
+# See the commentary on extra.postrm.in for why this is disabled.
+#
+#if [ -d /etc/kernel/postinst.d ]; then
+#    # We want to behave as if linux-image (without us) was installed, therefore
+#    # we do not want the postinst support to know we are being removed, claim
+#    # this is an installation event.
+#    cat - >/usr/lib/linux/triggers/$version <<EOF
+#DEB_MAINT_PARAMS="configure" run-parts --report --exit-on-error --arg=$version \
+#	--arg="$image_path" /etc/kernel/postinst.d
+#EOF
+#    dpkg-trigger --no-await linux-update-$version
+#fi
+
+exit 0
diff --git a/debian/scripts/gen-rules b/debian/scripts/gen-rules
index e833fad..9e287b3 100755
--- a/debian/scripts/gen-rules
+++ b/debian/scripts/gen-rules
@@ -156,8 +156,14 @@  test::
 $targets::
 	rm -rf \$(dkms_dir)/build \$(dkms_dir)/source
 	mkdir -p \$(dkms_dir)/build \$(dkms_dir)/source
+EOL
+		if [ "$variant" != "-open" ]; then
+			cat - >>"debian/rules.gen" <<EOL
 	install -d debian/linux-modules-nvidia-${suffix_minus}-\$(src_abi)-${flavour}
 	install -d debian/linux-modules-nvidia-${suffix_minus}-\$(src_abi)-${flavour}/usr/lib/linux/triggers
+EOL
+		fi
+		cat - >>"debian/rules.gen" <<EOL
 	\$(call install_control,linux-modules-nvidia-${suffix_minus}-\$(src_abi)-${flavour},${flavour},nvidia,${suffix_short},postinst postrm prerm config templates)
 	install -d debian/linux-objects-nvidia-${suffix_minus}-\$(src_abi)-${flavour}
 	\$(call build_dkms, \$(src_abi)-${flavour}, linux-objects-nvidia-${suffix_minus}-\$(src_abi)-${flavour}, \$(CURDIR)/debian/linux-objects-nvidia-${suffix_minus}-\$(src_abi)-${flavour}/lib/modules/\$(src_abi)-${flavour}/kernel, "", nvidia-${suffix_short}, pool/restricted/n/nvidia-graphics-drivers-${suffix_minus}/nvidia-kernel-source-${suffix_minus}_\$(dkms_nvidia_${suffix_under}_version)_\$(arch).deb pool/restricted/n/nvidia-graphics-drivers-${suffix_minus}/nvidia-dkms-${suffix_minus}_\$(dkms_nvidia_${suffix_under}_version)_\$(arch).deb)
@@ -199,6 +205,10 @@  Description: Linux kernel nvidia modules for version ${src_abi} (objects)
  one of the linux-modules-nvidia-${suffix_minus}-${flavour}* meta-packages,
  which will ensure that upgrades work correctly, and that supporting packages are
  also installed.
+EOL
+		# debian/control.d/nvidia
+		if [ "$variant" != "-open" ]; then
+			cat - >>"debian/control.d/nvidia" <<EOL
 
 Package: linux-modules-nvidia-${suffix_minus}-${src_abi}-${flavour}
 Build-Profiles: <!stage1>
@@ -222,6 +232,8 @@  Description: Linux kernel nvidia modules for version ${src_abi}
  which will ensure that upgrades work correctly, and that supporting packages are
  also installed.
 EOL
+		fi
+
 		# debian/control.d/migrate-nvidia
 		for cmd in $extra
 		do