From 741935f97b6e2683aeed24a22b2439d2f54ec4be Mon Sep 17 00:00:00 2001 From: Ross Younger Date: Tue, 10 Dec 2024 23:18:09 +1300 Subject: [PATCH] package: Add Debian postinst script (#13) --- Cargo.toml | 4 ++- debian/config | 12 ++++++++ debian/postinst | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ debian/postrm | 20 +++++++++++++ debian/templates | 16 +++++++++++ 5 files changed, 125 insertions(+), 1 deletion(-) create mode 100755 debian/config create mode 100755 debian/postinst create mode 100755 debian/postrm create mode 100644 debian/templates diff --git a/Cargo.toml b/Cargo.toml index a4e51f3..9a881ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -129,6 +129,8 @@ assets = [ [ "LICENSE", "usr/share/doc/qcp/", "644" ], # gzip -9n < CHANGELOG.md > misc/changelog.gz [ "misc/changelog.gz", "usr/share/doc/qcp/", "644" ], - [ "misc/20-qcp.conf", "etc/sysctl.d/", "644" ], + [ "misc/20-qcp.conf", "etc/sysctl.d/", "644" ], # this is automatically recognised as a conffile [ "misc/qcp.1", "usr/share/man/man1/", "644" ], ] +maintainer-scripts="debian" +depends = "$auto,debconf" diff --git a/debian/config b/debian/config new file mode 100755 index 0000000..6266c26 --- /dev/null +++ b/debian/config @@ -0,0 +1,12 @@ +#!/bin/sh + +set -e + +if [ -e /usr/share/debconf/confmodule ]; then + . /usr/share/debconf/confmodule + db_version 2.0 + db_capb + db_settitle qcp/title +fi + +# We cannot meaningfully preconfigure as postinst checks the filesystem at runtime. diff --git a/debian/postinst b/debian/postinst new file mode 100755 index 0000000..a5b68a0 --- /dev/null +++ b/debian/postinst @@ -0,0 +1,74 @@ +#!/bin/sh + +set -e + +# By Debian policy, packages must not ask unnecessary questions. +# +# Therefore, we examine the persistent sysctl directories to see +# if there are any mentions of the sysctls we want to set. +# If not, we presume that the system has no special requirements +# (this is expected to be the general case). + +SYSCTL_FILE=20-qcp.conf +SYSCTL_PATH=/etc/sysctl.d/${SYSCTL_FILE} + +. /usr/share/debconf/confmodule +db_version 2.0 +db_capb +db_settitle qcp/title + +#when testing this script, this line resets the db: +#db_fset qcp/sysctl_clash seen false + +check_for_clashing_sysctls() { + for DIR in /etc/sysctl.d /usr/lib/sysctl.d; do + if grep -qcr -e net.core.rmem_max -e net.core.wmem_max --exclude "*${SYSCTL_FILE}*" ${DIR}; then + return 0 + fi + done + return 1 +} + +activate_our_sysctls() { + sysctl -w -p ${SYSCTL_PATH} +} + +disable_our_file() { + if [ -e ${SYSCTL_PATH} ]; then + mv -f ${SYSCTL_PATH} ${SYSCTL_PATH}.disabled + fi +} + +try_to_enable_our_file() { + if [ -e ${SYSCTL_PATH}.disabled ]; then + mv -f ${SYSCTL_PATH}.disabled ${SYSCTL_PATH} + fi +} + +alert_sysadmin() { + db_input high qcp/sysctl_clash || true + db_go || true + + db_get qcp/sysctl_clash || true + case "$RET" in + "install and activate now") + try_to_enable_our_file + activate_our_sysctls + ;; + "install but do NOT activate") + try_to_enable_our_file + # do nothing + ;; + "do not install") + # they don't want it, OK + disable_our_file + ;; + esac +} + +if check_for_clashing_sysctls; then + alert_sysadmin +else + # No clashes; proceed quietly. + activate_our_sysctls +fi diff --git a/debian/postrm b/debian/postrm new file mode 100755 index 0000000..d8d3019 --- /dev/null +++ b/debian/postrm @@ -0,0 +1,20 @@ +#!/bin/sh + +set -e + +if [ -e /usr/share/debconf/confmodule ]; then +. /usr/share/debconf/confmodule +db_version 2 + +case "$1" in +purge) + # Remove my changes to the db. + if [ -e /usr/share/debconf/confmodule ]; then + . /usr/share/debconf/confmodule + db_version 2 + db_purge + fi + ;; +esac + +fi \ No newline at end of file diff --git a/debian/templates b/debian/templates new file mode 100644 index 0000000..c6774c8 --- /dev/null +++ b/debian/templates @@ -0,0 +1,16 @@ +Template: qcp/title +Type: title +Description: qcp sysctl files + +Template: qcp/sysctl_clash +Type: select +Choices: install and activate now, install but do NOT activate, do not install +Default: install and activate now +Description: Install sysctl files for qcp? + qcp has detected a possible sysctl clash! + . + qcp would like to install sysctl files to configure the kernel for good performance. + . + The sysctls used by qcp are: net.core.rmem_max net.core.wmem_max + . + sysctl config files may be found in: /etc/sysctl.d /usr/lib/sysctl.d