-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-systemd-helper.sh
127 lines (112 loc) · 3.18 KB
/
11-systemd-helper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash
# SystemdEnable
# [--no-alias|--no-wanted-by]
# [--name CUSTOM_NAME]
# [--type system|user]
# [--from-file|package] unit
#
# Enable a systemd unit file.
#
# Caveat: This will not process Also directives, as it might in theory require
# handling files from other packages. In addition you might not want to install
# both this unit and the Also unit.
#
# --no-alias and --no-wanted-by can be used to disable installing those types of
# links. This is useful if you want to just use socket and dbus activation and
# don't want the unit to start on boot.
#
# --name is to be used for parameterised units ("[email protected]"), to provide the
# parameter.
#
# --type defaults to system but can be used to override and install default user
# units in /etc/systemd/user.
#
# --from-file is used when unit file is installed by aconfmgr instead of pulled
# from a package. In this case the package name MUST be skipped. Otherwise it is
# REQUIRED.
function SystemdEnable() {
local type=system
local do_alias=1 do_wanted_by=1 from_file=0
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--no-alias) do_alias=0 ;;
--no-wanted-by) do_wanted_by=0 ;;
--from-file) from_file=1 ;;
--name)
local name_override=$2
shift 1
;;
--type)
type=$2
shift 1
;;
*)
break
;;
esac
shift 1
done
if [[ $from_file -eq 0 ]]; then
[[ $# -ne 2 ]] && FatalError "Expected 2 arguments, got $#."
local pkg="$1"
local unit="$2"
else
[[ $# -ne 1 ]] && FatalError "Expected 1 argument, got $#."
local unit="$1"
fi
if [[ "$type" != "system" && "$type" != "user" ]]; then
FatalError "Unkown type ${type}"
fi
local filename="${unit##*/}"
# Find the unit, either from package data or already added to the output
# directory
if [[ $from_file -eq 0 ]]; then
local unit_source="$tmp_dir/systemd_helpers/$pkg/$filename"
if [[ ! -f "$unit_source" ]]; then
mkdir -p "$tmp_dir/systemd_helpers/$pkg"
AconfGetPackageOriginalFile "$pkg" "$unit" > "$unit_source"
fi
else
local unit_source="$output_dir/files/$unit"
fi
[[ ! -f "$unit_source" ]] && FatalError "$unit_source not found"
local target
local oIFS="$IFS"
# Process WantedBy lines (if enabled)
if [[ $do_wanted_by -eq 1 ]]; then
local name="${name_override:-${filename}}"
local -a wantedby
if grep -q WantedBy= "$unit_source"; then
IFS=$' \n\t'
wantedby=( $(grep -E '^WantedBy=' "$unit_source" | cut -d= -f2) )
IFS="$oIFS"
for target in "${wantedby[@]}"; do
CreateLink "/etc/systemd/${type}/${target}.wants/${name}" "${unit}"
done
fi
fi
# Process Alias lines (if enabled)
if [[ $do_alias -eq 1 ]]; then
local -a aliases
if grep -q Alias= "$unit_source"; then
IFS=$' \n\t'
aliases=( $(grep -E '^Alias=' "$unit_source" | cut -d= -f2) )
IFS="$oIFS"
for target in "${aliases[@]}"; do
CreateLink "/etc/systemd/${type}/${target}" "${unit}"
done
fi
fi
}
# SystemdMask unit-name [type]
#
# Mask a unit. Defaults to masking system units
function SystemdMask() {
local unit="$1"
local type="${2:-system}"
if [[ "$type" != "system" && "$type" != "user" ]]; then
FatalError "Unkown type ${type}"
fi
CreateLink "/etc/systemd/${type}/${unit}" /dev/null
}