Annotation of /trunk/include/mtools.minc
Parent Directory | Revision Log
Revision 63 -
(hide annotations)
(download)
Wed Oct 9 12:23:06 2013 UTC (10 years, 7 months ago) by niro
File size: 8383 byte(s)
Wed Oct 9 12:23:06 2013 UTC (10 years, 7 months ago) by niro
File size: 8383 byte(s)
-fix broken --root handling of systemctl via chroot solution
1 | niro | 18 | # $Id$ |
2 | niro | 1 | |
3 | # adds services to runlevels and starts them | ||
4 | # mstartservice service {/path/to/service_exec} | ||
5 | mstartservice() | ||
6 | { | ||
7 | local service="$1" | ||
8 | local service_exec="$2" | ||
9 | |||
10 | niro | 18 | # only run if sysV init was found |
11 | if [[ ! -f /etc/rc.d/init.d/functions ]] | ||
12 | then | ||
13 | echo "sysV init not found, not adding service ${service} to runlevels!" | ||
14 | return | ||
15 | fi | ||
16 | |||
17 | niro | 34 | # don't run if feature !autosvc was set |
18 | if mqueryfeature "!autosvc" | ||
19 | then | ||
20 | echo "!autosvc detected; auto management of services disabled." | ||
21 | return | ||
22 | fi | ||
23 | |||
24 | niro | 20 | [[ -z ${service_exec} ]] && service_exec="$(which ${service})" |
25 | niro | 1 | |
26 | # add service to default runlevels | ||
27 | niro | 8 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding ${service} to default runlevels ..." |
28 | niro | 1 | [[ -x ${MROOT}/etc/rc.d/init.d/${service} ]] && rc-config add ${service} &> /dev/null |
29 | |||
30 | # do not start services on bootstrap or MROOT!=/ | ||
31 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
32 | then | ||
33 | # start service | ||
34 | if [[ -n $(pidof ${service_exec}) ]] | ||
35 | then | ||
36 | # restart service | ||
37 | /etc/init.d/${service} restart | ||
38 | else | ||
39 | # start service | ||
40 | /etc/init.d/${service} start | ||
41 | fi | ||
42 | fi | ||
43 | } | ||
44 | |||
45 | # removes services from runlevels and stops them | ||
46 | # mstopservice service {/path/to/service_exec} | ||
47 | mstopservice() | ||
48 | { | ||
49 | local service="$1" | ||
50 | local service_exec="$2" | ||
51 | |||
52 | niro | 18 | # only run if sysV init was found |
53 | if [[ ! -f /etc/rc.d/init.d/functions ]] | ||
54 | then | ||
55 | echo "sysV init not found, not adding service ${service} to runlevels!" | ||
56 | return | ||
57 | fi | ||
58 | |||
59 | niro | 34 | # don't run if feature !autosvc was set |
60 | if mqueryfeature "!autosvc" | ||
61 | then | ||
62 | echo "!autosvc detected; auto management of services disabled." | ||
63 | return | ||
64 | fi | ||
65 | niro | 1 | |
66 | niro | 34 | if [[ -z ${service_exec} ]] |
67 | then | ||
68 | service_exec="$(which ${service})" | ||
69 | elif [[ x$(basename ${service_exec}) = x${service_exec} ]] | ||
70 | then | ||
71 | # expand full path | ||
72 | service_exec="$(which ${service_exec})" | ||
73 | fi | ||
74 | |||
75 | niro | 1 | # only stop the service if ${service_exec} does not exist |
76 | niro | 34 | [[ -x ${MROOT}/${service_exec} ]] && return |
77 | niro | 1 | |
78 | # del services from runlevel regardless if they exist or not | ||
79 | niro | 8 | echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing ${service} from default runlevels ..." |
80 | niro | 1 | rc-config del ${service} &> /dev/null |
81 | |||
82 | # do not stop services on bootstrap or MROOT!=/ | ||
83 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
84 | then | ||
85 | # stop service if running | ||
86 | if [[ -n $(pidof ${service_exec}) ]] | ||
87 | then | ||
88 | killall -15 ${service_exec} &> /dev/null | ||
89 | sleep 1 | ||
90 | killall -9 ${service_exec} &> /dev/null | ||
91 | fi | ||
92 | fi | ||
93 | niro | 4 | } |
94 | |||
95 | # reloads a service if already running | ||
96 | # mreloadservice service {/path/to/service_exec} | ||
97 | mreloadservice() | ||
98 | { | ||
99 | local service="$1" | ||
100 | local service_exec="$2" | ||
101 | |||
102 | niro | 18 | # only run if sysV init was found |
103 | if [[ ! -f /etc/rc.d/init.d/functions ]] | ||
104 | then | ||
105 | echo "sysV init not found, not adding service ${service} to runlevels!" | ||
106 | return | ||
107 | fi | ||
108 | |||
109 | niro | 34 | # don't run if feature !autosvc was set |
110 | if mqueryfeature "!autosvc" | ||
111 | then | ||
112 | echo "!autosvc detected; auto management of services disabled." | ||
113 | return | ||
114 | fi | ||
115 | |||
116 | niro | 20 | [[ -z ${service_exec} ]] && service_exec="$(which ${service})" |
117 | niro | 4 | |
118 | # do not stop services on bootstrap or MROOT!=/ | ||
119 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
120 | then | ||
121 | # only reload the service if running | ||
122 | if [[ -n $(pidof ${service_exec}) ]] | ||
123 | then | ||
124 | /etc/init.d/${service} reload | ||
125 | fi | ||
126 | fi | ||
127 | } | ||
128 | niro | 16 | |
129 | # adds systemd units to runlevels and starts them | ||
130 | # mstartunit service {/path/to/service_exec} | ||
131 | mstartunit() | ||
132 | { | ||
133 | local service="$1" | ||
134 | local service_exec="$2" | ||
135 | niro | 32 | local opts |
136 | niro | 63 | local chroot |
137 | niro | 16 | |
138 | # only run if systemd was found | ||
139 | niro | 43 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] |
140 | niro | 16 | then |
141 | echo "systemd not found, not adding unit ${service} to runlevels!" | ||
142 | return | ||
143 | fi | ||
144 | |||
145 | niro | 34 | # don't run if feature !autosvc was set |
146 | if mqueryfeature "!autosvc" | ||
147 | then | ||
148 | echo "!autosvc detected; auto management of services disabled." | ||
149 | return | ||
150 | fi | ||
151 | |||
152 | niro | 17 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
153 | niro | 16 | then |
154 | niro | 63 | # symlinks root path too, not exactly what we want |
155 | #opts="--root ${MROOT}" | ||
156 | if [ -x $(type -P systemd-nspawn) ] | ||
157 | then | ||
158 | chroot="$(type -P systemd-nspawn)" | ||
159 | else | ||
160 | chroot="chroot" | ||
161 | fi | ||
162 | niro | 16 | fi |
163 | |||
164 | niro | 20 | if [[ -z ${service_exec} ]] |
165 | then | ||
166 | case ${service} in | ||
167 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
168 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
169 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
170 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
171 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
172 | esac | ||
173 | fi | ||
174 | niro | 16 | |
175 | # add service to default runlevels | ||
176 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding unit ${service} to default runlevels ..." | ||
177 | # reload daemon to honor changed unit files | ||
178 | niro | 63 | if [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
179 | then | ||
180 | systemctl ${opts} daemon-reload | ||
181 | fi | ||
182 | ${chroot} systemctl ${opts} enable ${service} | ||
183 | niro | 16 | |
184 | # do not start services on bootstrap or MROOT!=/ | ||
185 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
186 | then | ||
187 | niro | 21 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} starting unit ${service} ..." |
188 | niro | 16 | # start or restart the service |
189 | niro | 22 | # dont use try-restart, works only with active services! |
190 | systemctl restart ${service} | ||
191 | niro | 16 | fi |
192 | } | ||
193 | |||
194 | # removes systemd units from runlevels and stops them | ||
195 | # mstopunit service {/path/to/service_exec} | ||
196 | mstopunit() | ||
197 | { | ||
198 | local service="$1" | ||
199 | local service_exec="$2" | ||
200 | niro | 32 | local opts |
201 | niro | 16 | |
202 | # only run if systemd was found | ||
203 | niro | 43 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] |
204 | niro | 16 | then |
205 | echo "systemd not found, not removing unit ${service} from runlevels!" | ||
206 | return | ||
207 | fi | ||
208 | |||
209 | niro | 34 | # don't run if feature !autosvc was set |
210 | if mqueryfeature "!autosvc" | ||
211 | then | ||
212 | echo "!autosvc detected; auto management of services disabled." | ||
213 | return | ||
214 | fi | ||
215 | |||
216 | niro | 17 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
217 | niro | 16 | then |
218 | niro | 32 | opts="--root ${MROOT}" |
219 | niro | 16 | fi |
220 | |||
221 | niro | 20 | if [[ -z ${service_exec} ]] |
222 | then | ||
223 | case ${service} in | ||
224 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
225 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
226 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
227 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
228 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
229 | esac | ||
230 | niro | 34 | elif [[ x$(basename ${service_exec}) = x${service_exec} ]] |
231 | then | ||
232 | # expand full path | ||
233 | service_exec="$(which ${service_exec})" | ||
234 | niro | 20 | fi |
235 | niro | 16 | |
236 | # only stop the service if ${service_exec} does not exist | ||
237 | niro | 34 | [[ -x ${MROOT}/${service_exec} ]] && return |
238 | niro | 16 | |
239 | # del services from runlevel regardless if they exist or not | ||
240 | echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing unit ${service} from default runlevels ..." | ||
241 | # reload daemon to honor changed unit files | ||
242 | niro | 32 | systemctl ${opts} daemon-reload |
243 | systemctl ${opts} disable ${service} | ||
244 | niro | 16 | |
245 | # do not stop services on bootstrap or MROOT!=/ | ||
246 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
247 | then | ||
248 | niro | 21 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} stopping unit ${service} ..." |
249 | niro | 16 | systemctl stop ${service} |
250 | # try harder | ||
251 | if [[ -n $(pidof ${service_exec}) ]] | ||
252 | then | ||
253 | killall -15 ${service_exec} &> /dev/null | ||
254 | sleep 1 | ||
255 | killall -9 ${service_exec} &> /dev/null | ||
256 | fi | ||
257 | fi | ||
258 | } | ||
259 | |||
260 | # reloads a systemd unit if already running | ||
261 | # mreloadunit service {/path/to/service_exec} | ||
262 | mreloadunit() | ||
263 | { | ||
264 | local service="$1" | ||
265 | local service_exec="$2" | ||
266 | niro | 32 | local opts |
267 | niro | 16 | |
268 | # only run if systemd was found | ||
269 | niro | 43 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] |
270 | niro | 16 | then |
271 | echo "systemd not found, not removing unit ${service} from runlevels!" | ||
272 | return | ||
273 | fi | ||
274 | |||
275 | niro | 34 | # don't run if feature !autosvc was set |
276 | if mqueryfeature "!autosvc" | ||
277 | then | ||
278 | echo "!autosvc detected; auto management of services disabled." | ||
279 | return | ||
280 | fi | ||
281 | |||
282 | niro | 17 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
283 | niro | 16 | then |
284 | niro | 32 | opts="--root ${MROOT}" |
285 | niro | 16 | fi |
286 | |||
287 | niro | 20 | if [[ -z ${service_exec} ]] |
288 | then | ||
289 | case ${service} in | ||
290 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
291 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
292 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
293 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
294 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
295 | esac | ||
296 | fi | ||
297 | niro | 16 | |
298 | niro | 28 | # reload daemon to honor changed unit files |
299 | niro | 32 | systemctl ${opts} daemon-reload |
300 | niro | 28 | |
301 | niro | 16 | # do not stop services on bootstrap or MROOT!=/ |
302 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
303 | then | ||
304 | # only reload the service if running | ||
305 | systemctl reload-or-try-restart ${service} | ||
306 | fi | ||
307 | } |