commit cfa06443da1d392b04fb269c807d5e6c384c24ac Author: zyppe <210hcl@gmail.com> Date: Mon Feb 5 14:59:34 2024 +0800 Initialize for systemd-rpm-macros diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/.systemd-rpm-macros.metadata b/.systemd-rpm-macros.metadata new file mode 100644 index 0000000..e69de29 diff --git a/macros.systemd b/macros.systemd new file mode 100644 index 0000000..49e41dd --- /dev/null +++ b/macros.systemd @@ -0,0 +1,353 @@ +# -*- Mode: rpm-spec; indent-tabs-mode: t -*- */ +# RPM macros for packages installing systemd unit files +# +### +# +# When a package install systemd unit files, it should use the +# following macros: +# +# add %systemd_requires in the specfile +# +# %pre +# %service_add_pre demo.service demo1.service +# +# %post +# %service_add_post demo.service demo1.service +# +# %preun +# %service_del_preun demo.service +# +# %postun +# %service_del_postun demo.service +# %service_del_postun_with_restart demo.service +# %service_del_postun_without_restart demo.service +# +# Note: the upstream variants are also available and are aliases to +# their SUSE counterparts. However for consistency the SUSE macros +# should be preferred unless the package is intended to be portable +# across multiple distributions based on RPM. + +%_systemd_util_dir /usr/lib/systemd +%_unitdir /usr/lib/systemd/system +%_userunitdir /usr/lib/systemd/user +%_presetdir /usr/lib/systemd/system-preset +%_userpresetdir /usr/lib/systemd/user-preset +%_udevhwdbdir /usr/lib/udev/hwdb.d +%_udevrulesdir /usr/lib/udev/rules.d +%_journalcatalogdir /usr/lib/systemd/catalog +%_tmpfilesdir /usr/lib/tmpfiles.d +%_sysusersdir /usr/lib/sysusers.d +%_sysctldir /usr/lib/sysctl.d +%_ntpunitsdir /usr/lib/systemd/ntp-units.d +%_binfmtdir /usr/lib/binfmt.d +%_environmentdir /usr/lib/environment.d +%_modulesloaddir /usr/lib/modules-load.d +%_modprobedir /lib/modprobe.d +%_systemdgeneratordir /usr/lib/systemd/system-generators +%_systemdusergeneratordir /usr/lib/systemd/user-generators +%_systemd_system_env_generator_dir /usr/lib/systemd/system-environment-generators +%_systemd_user_env_generator_dir /usr/lib/systemd/user-environment-generators + +%systemd_requires \ +Requires(pre): systemd \ +Requires(post): systemd \ +Requires(preun): systemd \ +Requires(postun): systemd \ +%{nil} + +# In case you're wondering why "Suggests:" is also used: libzypp +# doesn't understand "OrderWithRequires:" yet, see bsc#1187332 for +# details. +%systemd_ordering \ +OrderWithRequires(pre): systemd \ +OrderWithRequires(post): systemd \ +OrderWithRequires(preun): systemd \ +OrderWithRequires(postun): systemd \ +Suggests: systemd \ +%{nil} + +%_restart_on_update_force() \ +%{warn: '-f' with %%service_del_postun is deprecated, use %%service_del_postun_with_restart instead} \ +(\ + test "$YAST_IS_RUNNING" = instsys && exit 0 \ + %{?*:/usr/bin/systemctl try-restart %{*}} \ +) || : \ +%{nil} + +%_restart_on_update_never() \ +%{warn: '-n' with %%service_del_postun is deprecated, use %%service_del_postun_without_restart instead} \ +%{?*: : # Restart of %{*} skipped} \ +%{nil} + +%_restart_on_update() (\ + test "$YAST_IS_RUNNING" = instsys && exit 0\ + test -f /etc/sysconfig/services -a \\\ + -z "$DISABLE_RESTART_ON_UPDATE" && . /etc/sysconfig/services\ + test "$DISABLE_RESTART_ON_UPDATE" = yes -o \\\ + "$DISABLE_RESTART_ON_UPDATE" = 1 && exit 0\ + %{?*:/usr/bin/systemctl try-restart %{*}}\ + ) || : %{nil} + +%_stop_on_removal_force() \ +%{warn: '-f' with %%service_del_preun is deprecated, please dont use it anymore} \ +( \ + test "$YAST_IS_RUNNING" = instsys && exit 0\ + %{?*:/usr/bin/systemctl stop %{*}}\ +) || : \ + %{nil} + +%_stop_on_removal_never() \ +%{warn: '-n' with %%service_del_preun is unsafe, please dont use it anymore} \ +%{?*: : # Stop of %{*} skipped} \ +%{nil} + +%_stop_on_removal() (\ + test "$YAST_IS_RUNNING" = instsys && exit 0\ + test -f /etc/sysconfig/services -a \\\ + -z "$DISABLE_STOP_ON_REMOVAL" && . /etc/sysconfig/services\ + test "$DISABLE_STOP_ON_REMOVAL" = yes -o \\\ + "$DISABLE_STOP_ON_REMOVAL" = 1 && exit 0\ + %{?*:/usr/bin/systemctl stop %{*}}\ + ) || : %{nil} + +# Figure out when presets need to be applied. This information is only +# recorded during %pre and is actually applied during %post. +# +# Presets might need to be applied during package install but also +# during package update. On update, packages might introduce new +# services but we need to make sure that's not happening during the +# migration of SysV initscripts. On package install, presets might +# have been already applied because of package renaming or split. +# +%service_add_pre() \ +if [ -x /usr/bin/systemctl ]; then \ + test -n "$FIRST_ARG" || FIRST_ARG="$1" \ + [ -d /var/lib/systemd/migrated ] || mkdir -p /var/lib/systemd/migrated || : \ + \ + for service in %{?*} ; do \ + sysv_service=${service%.*} \ + \ + if [ ! -e /usr/lib/systemd/system/$service ] && \ + [ ! -e /etc/init.d/$sysv_service ]; then \ + mkdir -p /run/systemd/rpm/needs-preset \ + touch /run/systemd/rpm/needs-preset/$service \ + \ + elif [ -e /etc/init.d/$sysv_service ] && \ + [ ! -e /var/lib/systemd/migrated/$sysv_service ]; then \ + /usr/sbin/systemd-sysv-convert --save $sysv_service || : \ + mkdir -p /run/systemd/rpm/needs-sysv-convert \ + touch /run/systemd/rpm/needs-sysv-convert/$service \ + fi \ + done \ +fi \ +%{nil} + +# Apply the presets if %pre told us to do so. +# +%service_add_post() \ +if [ -x /usr/bin/systemctl ]; then \ + test -n "$FIRST_ARG" || FIRST_ARG="$1" \ + [ -d /var/lib/systemd/migrated ] || mkdir -p /var/lib/systemd/migrated || : \ + \ + if [ "$YAST_IS_RUNNING" != "instsys" ]; then \ + /usr/bin/systemctl daemon-reload || : \ + fi \ + for service in %{?*} ; do \ + sysv_service=${service%.*} \ + \ + if [ -e /run/systemd/rpm/needs-preset/$service ]; then \ + /usr/bin/systemctl preset $service || : \ + rm "/run/systemd/rpm/needs-preset/$service" || : \ + \ + elif [ -e /run/systemd/rpm/needs-sysv-convert/$service ]; then \ + /usr/sbin/systemd-sysv-convert --apply $sysv_service || : \ + rm "/run/systemd/rpm/needs-sysv-convert/$service" || : \ + touch /var/lib/systemd/migrated/$sysv_service || : \ + fi \ + done \ +fi \ +%{nil} + +# On removal, tell systemd to stop service +# +# Deprecated options, please do not use in new code: +# -f : force service stop on removal (deprecated, will be the default) +# -n : do not stop service on removal (unsafe, do not use) +# +# The default is to check for DISABLE_STOP_ON_REMOVAL environment +# variable if not found use the value read from /etc/sysconfig/services. +# +# NOTE: the default behavior is deprecated and DISABLE_STOP_ON_REMOVAL +# support will be removed soon, please do NOT rely on it anymore. +# +%service_del_preun(fn) \ +test -n "$FIRST_ARG" || FIRST_ARG="$1" \ +if [ "$FIRST_ARG" -eq 0 -a -x /usr/bin/systemctl ]; then \ + # Package removal, not upgrade \ + /usr/bin/systemctl --no-reload disable %{?*} || : \ + %{expand:%%_stop_on_removal%{-f:_force}%{!-f:%{-n:_never}} %{?*}} \ +fi \ +%{nil} + +# On uninstall, tell systemd to reload its unit files. +# On update, tell systemd to reload its unit files but don't restart service. +# +%service_del_postun_without_restart() \ +if [ $1 -eq 0 ]; then \ + # Package removal \ + for service in %{?*} ; do \ + sysv_service="${service%.*}" \ + rm -f "/var/lib/systemd/migrated/$sysv_service" || : \ + done \ +fi \ +if [ -x /usr/bin/systemctl ]; then \ + /usr/bin/systemctl daemon-reload || : \ +fi \ +%{nil} + +# On uninstall, tell systemd to reload its unit files. +# On update, tell systemd to reload its unit files and restart service. +# +# It ignores the content of /etc/sysconfig/services +# +%service_del_postun_with_restart() \ +if [ -x /usr/bin/systemctl ]; then \ + /usr/bin/systemctl daemon-reload || : \ + if [ $1 -ge 1 ]; then \ + # Package upgrade, not uninstall \ + /usr/bin/systemctl try-restart %{?*} || : \ + fi \ +fi \ +%{nil} + +# On uninstall, tell systemd to reload its unit files +# +# Deprecated options, please do not use in new code: +# -f : force restart on update (replaced by %service_del_postun_with_restart) +# -n : don't restart on update (replaced by %service_del_postun_without_restart) +# +# The default is to read DISABLE_RESTART_ON_UPDATE from /etc/sysconfig/services +# +%service_del_postun(fn) \ +test -n "$FIRST_ARG" || FIRST_ARG="$1" \ +%service_del_postun_without_restart %{?*} \ +if [ "$FIRST_ARG" -ge 1 ]; then \ + # Package upgrade, not uninstall \ + if [ -x /usr/bin/systemctl ]; then \ + %{expand:%%_restart_on_update%{-f:_force}%{!-f:%{-n:_never}} %{?*}} \ + fi \ +fi \ +%{nil} + +# +# Upstream variants +# + +%systemd_pre() %{expand::%%service_add_pre %{?**}} +%systemd_post() %{expand::%%service_add_post %{?**}} +%systemd_preun() %{expand::%%service_del_preun %{?**}} +%systemd_postun() %{expand::%%service_del_postun_without_restart %{?**}} +%systemd_postun_with_restart() %{expand::%%service_del_postun %{?**}} + +%systemd_user_pre() \ +if [ -x /usr/bin/systemctl ]; then \ + for service in %{?*} ; do \ + if [ ! -e "/usr/lib/systemd/user/$service" ]; then \ + mkdir -p /run/systemd/rpm/needs-user-preset \ + touch "/run/systemd/rpm/needs-user-preset/$service" \ + fi \ + done \ +fi \ +%{nil} + +%systemd_user_post() \ +if [ -x /usr/bin/systemctl ]; then \ + for service in %{?*} ; do \ + if [ -e "/run/systemd/rpm/needs-user-preset/$service" ]; then \ + /usr/bin/systemctl --global preset "$service" || : \ + rm "/run/systemd/rpm/needs-user-preset/$service" || : \ + fi \ + done \ +fi \ +%{nil} + +%systemd_user_preun() \ +if [ $1 -eq 0 -a -x /usr/bin/systemctl ]; then \ + # Package removal, not upgrade \ + /usr/bin/systemctl --global disable %{?*} || : \ +fi \ +%{nil} + +%systemd_user_postun() %{nil} +%systemd_user_postun_with_restart() %{nil} + +%udev_hwdb_update() \ +[ -x /usr/bin/systemd-hwdb ] && /usr/bin/systemd-hwdb update || : \ +%{nil} + +%udev_rules_update() \ +[ -x /usr/bin/udevadm ] && /usr/bin/udevadm control --reload || : \ +%{nil} + +%journal_catalog_update() \ +[ -x /usr/bin/journalctl ] && /usr/bin/journalctl --update-catalog || : \ +%{nil} + +%tmpfiles_create() \ +[ -z "${TRANSACTIONAL_UPDATE}" -a -x /usr/bin/systemd-tmpfiles ] && \ + /usr/bin/systemd-tmpfiles --create %{?*} || : \ +%{nil} + +# This should be used by package installation scripts which doesn't require +# users or groups to be present before the files installed by the package are +# present on disk and when the sysusers conf files are generated during the +# build of the package hence not easily available before the build of the +# package. +# +# This macro will go away when this will be moved to file triggers. +# +# Example: +# %post +# %sysusers_create %{name}.conf +# %files +# %{_sysusersdir}/%{name}.conf +# +%sysusers_create() \ +[ -x /usr/bin/systemd-sysusers ] && /usr/bin/systemd-sysusers %{?*} || : \ +%{nil} + +%sysusers_create_inline() \ +[ -x /usr/bin/systemd-sysusers ] && /usr/bin/systemd-sysusers - <