Skip to content
Snippets Groups Projects
Verified Commit d9c21e90 authored by Jane Rachinger's avatar Jane Rachinger Committed by Clayton Craft
Browse files

systemd/postmarketos-base-systemd: run systemctl on preset changes for already...

systemd/postmarketos-base-systemd: run systemctl on preset changes for already installed units (MR 6120)

Co-authored-by: default avatarClayton Craft <clayton@craftyguy.net>

[ci:skip-build]: already built successfully in CI
parent 267126bc
No related branches found
No related tags found
No related merge requests found
......@@ -33,6 +33,11 @@ depends="
"
replaces="alpine-baselayout postmarketos-base systemd systemd-logind"
replaces_priority=100
install="
$pkgname.post-install
$pkgname.post-upgrade
$pkgname.pre-upgrade
"
_source644="
etc/motd.ansi
......
#!/bin/sh
. /usr/lib/systemd/systemd-apk-macros.sh
system_preset_path="/usr/lib/systemd/system-preset/90-base.preset"
user_preset_path="/usr/lib/systemd/user-preset/90-base.preset"
# $1: path to file (without .old)
remove_old() {
rm "$1.old" 2>/dev/null || true
}
# https://www.freedesktop.org/software/systemd/man/latest/systemd.preset.html
# tldr: preset file format
# - discard line when:
# - empty line
# - first character is # or :
# - expect line to be
# - keyword + whitespace + unit name + [template unit directive]
# - keywords are: 'enable', 'disable', 'ignore'
# - unit name can contain shell-style wild-cards
# - unit name must match to a real file
# - when template unit directives are defined:
# - doesn't match to DefaultInstance
# - template unit gets expanded by directives
# - example: `enable dirsrv@.service foo bar baz` equivalent to:
# `systemctl preset dirsrv@foo.service`
# `systemctl preset dirsrv@bar.service`
# `systemctl preset dirsrv@baz.service`
# $1: path to preset
units_affected_by_preset() {
# https://stackoverflow.com/questions/4198138/printing-everything-except-the-first-field-with-awk
if [ ! -f "$1" ]; then
# If the file doesn't exist, then there's no policy
echo ""
return
fi
# "enable unit.service abc def" becomes "unit.service abc def"
cat "$1" |
busybox awk '$1 == "enable" || $1 == "disable" || $1 == "ignore"' |
busybox sed 's/(\|enable \|disable \|ignore )//' |
uniq |
sort
}
# $1: path to file (without .old)
units_affected_by_old_and_new_preset() {
# step one: get list of services in general
local new="$(units_affected_by_preset "$1")"
local old="$(units_affected_by_preset "$1.old")"
printf "$new\n$old" | sort | uniq
}
# $1: path to file
# $2: entry name
# returns: string oder exitcode
get_policy_for_unit() {
# printf '%q' isn't available in busybox
# so we hope only * as an wildcard is used for now
# FIXME: better shell escaping
local escaped="$(echo "$2" | sed 's/\*/\\\*/')"
if [ ! -f "$1" ]; then
# If the file doesn't exist, then there's no policy
echo ""
return
fi
cat $1 |
grep "$escaped"'$' |
awk '$1 == "enable" || $1 == "disable" || $1 == "ignore" {print $1}'
}
# $1: path to file
# $2: entry name
# returns: exitcode
policy_changed() {
# we don't have to care about the exitcode altough it's faster
# "" != "enable"
local new="$(get_policy_for_unit $1 "$2")"
local old="$(get_policy_for_unit $1.old "$2")"
[ "$new" != "$old" ]
}
# $1: takes system or user
check_and_apply_presets() {
local type="$1"
local preset_path=""
if [ "$type" = user ]; then
preset_path="$user_preset_path"
else
preset_path="$system_preset_path"
type="system"
fi
local entries="$(units_affected_by_old_and_new_preset $preset_path)"
local old_ifs=$IFS
IFS=$'\n'
for entry in $entries; do
if policy_changed "$preset_path" "$entry"; then
echo "unit policy has changed for $entry"
if echo "$entry" | grep -q '*'; then
echo " wildcards are not supported yet, skipping"
else
if [ -f "/usr/lib/systemd/$type/$entry" ]; then
systemd_service_post_install "$type" "$entry"
else
echo " no unit file found, skipping"
fi
fi
fi
done
IFS="$old_ifs"
}
check_and_apply_presets system
check_and_apply_presets user
remove_old "$system_preset_path"
remove_old "$user_preset_path"
postmarketos-base-systemd.post-install
\ No newline at end of file
#!/bin/sh
# NOTE: we don't want to use apk's protected_paths here, as
# creation of .apk-new files can be disabled via `--clean-protected`
# $1: path to file
retain_old() {
if [ -f "$1" ]; then
cp "$1" "$1".old
else
touch "$1.old"
fi
}
retain_old /usr/lib/systemd/system-preset/90-base.preset
retain_old /usr/lib/systemd/user-preset/90-base.preset
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment