Contents of /trunk/include/mtools.minc
Parent Directory | Revision Log
Revision 63 -
(show annotations)
(download)
Wed Oct 9 12:23:06 2013 UTC (10 years, 6 months ago) by niro
File size: 8383 byte(s)
Wed Oct 9 12:23:06 2013 UTC (10 years, 6 months ago) by niro
File size: 8383 byte(s)
-fix broken --root handling of systemctl via chroot solution
1 | # $Id$ |
2 | |
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 | # 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 | # 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 | [[ -z ${service_exec} ]] && service_exec="$(which ${service})" |
25 | |
26 | # add service to default runlevels |
27 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding ${service} to default runlevels ..." |
28 | [[ -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 | # 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 | # 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 | |
66 | 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 | # only stop the service if ${service_exec} does not exist |
76 | [[ -x ${MROOT}/${service_exec} ]] && return |
77 | |
78 | # del services from runlevel regardless if they exist or not |
79 | echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing ${service} from default runlevels ..." |
80 | 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 | } |
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 | # 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 | # 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 | [[ -z ${service_exec} ]] && service_exec="$(which ${service})" |
117 | |
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 | |
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 | local opts |
136 | local chroot |
137 | |
138 | # only run if systemd was found |
139 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] |
140 | then |
141 | echo "systemd not found, not adding unit ${service} to runlevels!" |
142 | return |
143 | fi |
144 | |
145 | # 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 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
153 | then |
154 | # 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 | fi |
163 | |
164 | 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 | |
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 | if [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
179 | then |
180 | systemctl ${opts} daemon-reload |
181 | fi |
182 | ${chroot} systemctl ${opts} enable ${service} |
183 | |
184 | # do not start services on bootstrap or MROOT!=/ |
185 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
186 | then |
187 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} starting unit ${service} ..." |
188 | # start or restart the service |
189 | # dont use try-restart, works only with active services! |
190 | systemctl restart ${service} |
191 | 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 | local opts |
201 | |
202 | # only run if systemd was found |
203 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] |
204 | then |
205 | echo "systemd not found, not removing unit ${service} from runlevels!" |
206 | return |
207 | fi |
208 | |
209 | # 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 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
217 | then |
218 | opts="--root ${MROOT}" |
219 | fi |
220 | |
221 | 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 | elif [[ x$(basename ${service_exec}) = x${service_exec} ]] |
231 | then |
232 | # expand full path |
233 | service_exec="$(which ${service_exec})" |
234 | fi |
235 | |
236 | # only stop the service if ${service_exec} does not exist |
237 | [[ -x ${MROOT}/${service_exec} ]] && return |
238 | |
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 | systemctl ${opts} daemon-reload |
243 | systemctl ${opts} disable ${service} |
244 | |
245 | # do not stop services on bootstrap or MROOT!=/ |
246 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
247 | then |
248 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} stopping unit ${service} ..." |
249 | 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 | local opts |
267 | |
268 | # only run if systemd was found |
269 | if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]] |
270 | then |
271 | echo "systemd not found, not removing unit ${service} from runlevels!" |
272 | return |
273 | fi |
274 | |
275 | # 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 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
283 | then |
284 | opts="--root ${MROOT}" |
285 | fi |
286 | |
287 | 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 | |
298 | # reload daemon to honor changed unit files |
299 | systemctl ${opts} daemon-reload |
300 | |
301 | # 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 | } |