Annotation of /mage/trunk/include/systemd.minc
Parent Directory | Revision Log
Revision 5212 -
(hide annotations)
(download)
Mon Dec 16 10:16:23 2013 UTC (10 years, 9 months ago) by niro
File size: 5152 byte(s)
Mon Dec 16 10:16:23 2013 UTC (10 years, 9 months ago) by niro
File size: 5152 byte(s)
-sync with upstream
1 | niro | 5212 | # $Id$ |
2 | |||
3 | # adds systemd units to runlevels and starts them | ||
4 | # mstartunit service {/path/to/service_exec} | ||
5 | mstartunit() | ||
6 | { | ||
7 | local service="$1" | ||
8 | local service_exec="$2" | ||
9 | local opts | ||
10 | local chroot | ||
11 | |||
12 | # only run if systemd was found | ||
13 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] | ||
14 | then | ||
15 | echo "systemd not found, not adding unit ${service} to runlevels!" | ||
16 | return | ||
17 | fi | ||
18 | |||
19 | # don't run if feature !autosvc was set | ||
20 | if mqueryfeature "!autosvc" | ||
21 | then | ||
22 | echo "!autosvc detected; auto management of services disabled." | ||
23 | return | ||
24 | fi | ||
25 | |||
26 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] | ||
27 | then | ||
28 | # symlinks root path too, not exactly what we want | ||
29 | #opts="--root ${MROOT}" | ||
30 | if [ -x $(type -P systemd-nspawn) ] | ||
31 | then | ||
32 | chroot="$(type -P systemd-nspawn)" | ||
33 | else | ||
34 | chroot="chroot" | ||
35 | fi | ||
36 | fi | ||
37 | |||
38 | if [[ -z ${service_exec} ]] | ||
39 | then | ||
40 | case ${service} in | ||
41 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
42 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
43 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
44 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
45 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
46 | esac | ||
47 | fi | ||
48 | |||
49 | # add service to default runlevels | ||
50 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding unit ${service} to default runlevels ..." | ||
51 | # reload daemon to honor changed unit files | ||
52 | if [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
53 | then | ||
54 | systemctl ${opts} daemon-reload | ||
55 | fi | ||
56 | ${chroot} systemctl ${opts} enable ${service} | ||
57 | |||
58 | # do not start services on bootstrap or MROOT!=/ | ||
59 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
60 | then | ||
61 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} starting unit ${service} ..." | ||
62 | # start or restart the service | ||
63 | # dont use try-restart, works only with active services! | ||
64 | systemctl restart ${service} | ||
65 | fi | ||
66 | } | ||
67 | |||
68 | # removes systemd units from runlevels and stops them | ||
69 | # mstopunit service {/path/to/service_exec} | ||
70 | mstopunit() | ||
71 | { | ||
72 | local service="$1" | ||
73 | local service_exec="$2" | ||
74 | local opts | ||
75 | |||
76 | # only run if systemd was found | ||
77 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] | ||
78 | then | ||
79 | echo "systemd not found, not removing unit ${service} from runlevels!" | ||
80 | return | ||
81 | fi | ||
82 | |||
83 | # don't run if feature !autosvc was set | ||
84 | if mqueryfeature "!autosvc" | ||
85 | then | ||
86 | echo "!autosvc detected; auto management of services disabled." | ||
87 | return | ||
88 | fi | ||
89 | |||
90 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] | ||
91 | then | ||
92 | opts="--root ${MROOT}" | ||
93 | fi | ||
94 | |||
95 | if [[ -z ${service_exec} ]] | ||
96 | then | ||
97 | case ${service} in | ||
98 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
99 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
100 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
101 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
102 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
103 | esac | ||
104 | elif [[ x$(basename ${service_exec}) = x${service_exec} ]] | ||
105 | then | ||
106 | # expand full path | ||
107 | service_exec="$(which ${service_exec})" | ||
108 | fi | ||
109 | |||
110 | # only stop the service if ${service_exec} does not exist | ||
111 | [[ -x ${MROOT}/${service_exec} ]] && return | ||
112 | |||
113 | # del services from runlevel regardless if they exist or not | ||
114 | echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing unit ${service} from default runlevels ..." | ||
115 | # reload daemon to honor changed unit files | ||
116 | systemctl ${opts} daemon-reload | ||
117 | systemctl ${opts} disable ${service} | ||
118 | |||
119 | # do not stop services on bootstrap or MROOT!=/ | ||
120 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
121 | then | ||
122 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} stopping unit ${service} ..." | ||
123 | systemctl stop ${service} | ||
124 | # try harder | ||
125 | if [[ -n $(pidof ${service_exec}) ]] | ||
126 | then | ||
127 | killall -15 ${service_exec} &> /dev/null | ||
128 | sleep 1 | ||
129 | killall -9 ${service_exec} &> /dev/null | ||
130 | fi | ||
131 | fi | ||
132 | } | ||
133 | |||
134 | # reloads a systemd unit if already running | ||
135 | # mreloadunit service {/path/to/service_exec} | ||
136 | mreloadunit() | ||
137 | { | ||
138 | local service="$1" | ||
139 | local service_exec="$2" | ||
140 | local opts | ||
141 | |||
142 | # only run if systemd was found | ||
143 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] | ||
144 | then | ||
145 | echo "systemd not found, not removing unit ${service} from runlevels!" | ||
146 | return | ||
147 | fi | ||
148 | |||
149 | # don't run if feature !autosvc was set | ||
150 | if mqueryfeature "!autosvc" | ||
151 | then | ||
152 | echo "!autosvc detected; auto management of services disabled." | ||
153 | return | ||
154 | fi | ||
155 | |||
156 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] | ||
157 | then | ||
158 | opts="--root ${MROOT}" | ||
159 | fi | ||
160 | |||
161 | if [[ -z ${service_exec} ]] | ||
162 | then | ||
163 | case ${service} in | ||
164 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
165 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
166 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
167 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
168 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
169 | esac | ||
170 | fi | ||
171 | |||
172 | # reload daemon to honor changed unit files | ||
173 | systemctl ${opts} daemon-reload | ||
174 | |||
175 | # do not stop services on bootstrap or MROOT!=/ | ||
176 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
177 | then | ||
178 | # only reload the service if running | ||
179 | systemctl reload-or-try-restart ${service} | ||
180 | fi | ||
181 | } |