Skip to content

Conversation

shaerpour
Copy link

@shaerpour shaerpour commented Dec 26, 2023

Description

When we install packages with apt, rpm or other package managers, some bash scripts with (prerm, preinst, postrm and postinst) extensions would be execute (if exists) that do some tasks like copy config files, create directories etc before or after unpacking package so the main program can run in the better environment.

Issue

If remove and purge package without stop (and | or) disable it, OS still shows the service is running and can cause issues after rebooting.

Added feature

In this branch, the prerm script is added to prevent those issues. (Built and tested on ubuntu:20.04)

Amirhossein Shaerpour added 2 commits December 26, 2023 16:58
Signed-off-by: ahspw <ahspvirtuallife@gmail.com>
Signed-off-by: ahspw <ahspvirtuallife@gmail.com>
@shaerpour shaerpour force-pushed the apt-prerm-script-implementation branch from 61ef21c to 6e31d48 Compare December 26, 2023 13:29
@shaerpour shaerpour changed the title apt package manager prerm script cpack: add apt package manager prerm script Dec 26, 2023
Comment on lines +6 to +7
systemctl stop fluent-bit.service || true
systemctl disable fluent-bit.service || true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check whether service or systemctl command is effective on the Debian box?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I will test it on Debian 11 and 12 and reply back with results.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests have been successfully done on debian bullseye and bookworm versions.
After Debian 8 systemd is the default init system for debian (Debian Reference) so systemctl would not make any issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds great. So, we shouldn't consider the outdated Debian system. Let's leave as-is.

Copy link

@aminvakil aminvakil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be investigated more whether this is the right choice to do or not, take fail2ban.prerm package which has been downloaded and installed from https://github.com/fail2ban/fail2ban/releases/download/1.0.2/fail2ban_1.0.2-1.upstream1_all.deb on Debian 10 Buster for example:

#!/bin/sh
set -e
# Automatically added by dh_installinit/13.3.4
if [ -x "/etc/init.d/fail2ban" ] && [ "$1" = remove ]; then
        invoke-rc.d fail2ban stop || exit 1
fi
# End automatically added section

# Automatically added by dh_python3
if which py3clean >/dev/null 2>&1; then
        py3clean -p fail2ban 
else
        dpkg -L fail2ban | perl -ne 's,/([^/]*)\.py$,/__pycache__/\1.*, or next; unlink $_ or die $! foreach glob($_)'
        find /usr/lib/python3/dist-packages/ -type d -name __pycache__ -empty -print0 | xargs --null --no-run-if-empty rmdir
fi

# End automatically added section

First it uses /bin/sh instead of /bin/bash and it seems some stuff would be handled by dh_installinit which will be automatically added to prerm files of package.

So maybe it's better to rely on debscripts scripts to run these stuff instead of executing systemctl disable and systemctl stop manually.

@@ -0,0 +1,12 @@
#!/bin/bash

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#!/bin/bash
#!/bin/sh

@aminvakil
Copy link

Also netfilter-persistent.prem on Debian 10 Buster and Debian 11 Bullseye:

Debian 10 Buster:

#!/bin/sh
set -e
# Automatically added by dh_installinit/12.1.1
if [ -x "/etc/init.d/netfilter-persistent" ] && [ "$1" = remove ]; then
        invoke-rc.d netfilter-persistent stop || exit 1
fi
# End automatically added section

Debian 11 Bullseye:

#!/bin/sh
set -e
# Automatically added by dh_installsystemd/13.3.3
if [ -d /run/systemd/system ] && [ "$1" = remove ]; then
        deb-systemd-invoke stop 'netfilter-persistent.service' >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installinit/13.3.3
if [ -x "/etc/init.d/netfilter-persistent" ] && [ "$1" = remove ]; then
        invoke-rc.d --skip-systemd-native netfilter-persistent stop || exit 1
fi
# End automatically added section

This is more suggesting that these stuff should be handled using debhelper itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants