1 |
# $Header: /magellan-cvs/mage/include/mtools.minc,v 1.3 2008/02/10 12:13:56 niro Exp $ |
# $Id$ |
2 |
|
|
3 |
# adds services to runlevels and starts them |
# adds services to runlevels and starts them |
4 |
# mstartservice service {/path/to/service_exec} |
# mstartservice service {/path/to/service_exec} |
7 |
local service="$1" |
local service="$1" |
8 |
local service_exec="$2" |
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})" |
[[ -z ${service_exec} ]] && service_exec="$(which ${service})" |
25 |
|
|
26 |
# add service to default runlevels |
# add service to default runlevels |
49 |
local service="$1" |
local service="$1" |
50 |
local service_exec="$2" |
local service_exec="$2" |
51 |
|
|
52 |
[[ -z ${service_exec} ]] && service_exec="$(which ${service} 2> /dev/null)" |
# 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 |
# only stop the service if ${service_exec} does not exist |
76 |
[[ -f ${MROOT}/${service_exec} ]] && return |
[[ -x ${MROOT}/${service_exec} ]] && return |
77 |
|
|
78 |
# del services from runlevel regardless if they exist or not |
# del services from runlevel regardless if they exist or not |
79 |
echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing ${service} from default runlevels ..." |
echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing ${service} from default runlevels ..." |
99 |
local service="$1" |
local service="$1" |
100 |
local service_exec="$2" |
local service_exec="$2" |
101 |
|
|
102 |
[[ -z ${service_exec} ]] && service_exec="$(which ${service} 2> /dev/null)" |
# 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!=/ |
# do not stop services on bootstrap or MROOT!=/ |
119 |
if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
126 |
fi |
fi |
127 |
} |
} |
128 |
|
|
|
|
|
129 |
# adds systemd units to runlevels and starts them |
# adds systemd units to runlevels and starts them |
130 |
# mstartunit service {/path/to/service_exec} |
# mstartunit service {/path/to/service_exec} |
131 |
mstartunit() |
mstartunit() |
132 |
{ |
{ |
133 |
local service="$1" |
local service="$1" |
134 |
local service_exec="$2" |
local service_exec="$2" |
135 |
|
local opts |
136 |
|
|
137 |
# only run if systemd was found |
# only run if systemd was found |
138 |
if [[ ! -x /bin/systemctl ]] |
if [[ ! -x /bin/systemctl ]] |
141 |
return |
return |
142 |
fi |
fi |
143 |
|
|
144 |
# we do not support $MROOT atm |
# don't run if feature !autosvc was set |
145 |
if [[ ${MROOT} != / ]] || [[ ! -z ${MROOT} ]] |
if mqueryfeature "!autosvc" |
146 |
then |
then |
147 |
echo "\$MROOT is set, doing nothing!" |
echo "!autosvc detected; auto management of services disabled." |
|
echo "Please add unit ${service} manually to the runlevels!" |
|
|
echo "Run 'systemctl enable ${service}' in your chroot later on." |
|
148 |
return |
return |
149 |
fi |
fi |
150 |
|
|
151 |
[[ -z ${service_exec} ]] && service_exec="$(which ${service})" |
if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
152 |
|
then |
153 |
|
opts="--root ${MROOT}" |
154 |
|
fi |
155 |
|
|
156 |
|
if [[ -z ${service_exec} ]] |
157 |
|
then |
158 |
|
case ${service} in |
159 |
|
*.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; |
160 |
|
*.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; |
161 |
|
*.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; |
162 |
|
*.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; |
163 |
|
*) service_exec="$(which ${service} 2> /dev/null)" ;; |
164 |
|
esac |
165 |
|
fi |
166 |
|
|
167 |
# add service to default runlevels |
# add service to default runlevels |
168 |
echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding unit ${service} to default runlevels ..." |
echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding unit ${service} to default runlevels ..." |
169 |
# reload daemon to honor changed unit files |
# reload daemon to honor changed unit files |
170 |
systemctl daemon-reload |
systemctl ${opts} daemon-reload |
171 |
systemctl enable ${service} |
systemctl ${opts} enable ${service} |
172 |
|
|
173 |
# do not start services on bootstrap or MROOT!=/ |
# do not start services on bootstrap or MROOT!=/ |
174 |
if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
175 |
then |
then |
176 |
|
echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} starting unit ${service} ..." |
177 |
# start or restart the service |
# start or restart the service |
178 |
systemctl try-restart ${service} |
# dont use try-restart, works only with active services! |
179 |
|
systemctl restart ${service} |
180 |
fi |
fi |
181 |
} |
} |
182 |
|
|
186 |
{ |
{ |
187 |
local service="$1" |
local service="$1" |
188 |
local service_exec="$2" |
local service_exec="$2" |
189 |
|
local opts |
190 |
|
|
191 |
# only run if systemd was found |
# only run if systemd was found |
192 |
if [[ ! -x /bin/systemctl ]] |
if [[ ! -x /bin/systemctl ]] |
195 |
return |
return |
196 |
fi |
fi |
197 |
|
|
198 |
# we do not support $MROOT atm |
# don't run if feature !autosvc was set |
199 |
if [[ ${MROOT} != / ]] || [[ ! -z ${MROOT} ]] |
if mqueryfeature "!autosvc" |
200 |
then |
then |
201 |
echo "\$MROOT is set, doing nothing!" |
echo "!autosvc detected; auto management of services disabled." |
|
echo "Please remove unit ${service} manually from the runlevels!" |
|
|
echo "Run 'systemctl disable ${service}' in your chroot later on." |
|
202 |
return |
return |
203 |
fi |
fi |
204 |
|
|
205 |
[[ -z ${service_exec} ]] && service_exec="$(which ${service} 2> /dev/null)" |
if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
206 |
|
then |
207 |
|
opts="--root ${MROOT}" |
208 |
|
fi |
209 |
|
|
210 |
|
if [[ -z ${service_exec} ]] |
211 |
|
then |
212 |
|
case ${service} in |
213 |
|
*.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; |
214 |
|
*.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; |
215 |
|
*.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; |
216 |
|
*.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; |
217 |
|
*) service_exec="$(which ${service} 2> /dev/null)" ;; |
218 |
|
esac |
219 |
|
elif [[ x$(basename ${service_exec}) = x${service_exec} ]] |
220 |
|
then |
221 |
|
# expand full path |
222 |
|
service_exec="$(which ${service_exec})" |
223 |
|
fi |
224 |
|
|
225 |
# only stop the service if ${service_exec} does not exist |
# only stop the service if ${service_exec} does not exist |
226 |
[[ -f ${MROOT}/${service_exec} ]] && return |
[[ -x ${MROOT}/${service_exec} ]] && return |
227 |
|
|
228 |
# del services from runlevel regardless if they exist or not |
# del services from runlevel regardless if they exist or not |
229 |
echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing unit ${service} from default runlevels ..." |
echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing unit ${service} from default runlevels ..." |
230 |
# reload daemon to honor changed unit files |
# reload daemon to honor changed unit files |
231 |
systemctl daemon-reload |
systemctl ${opts} daemon-reload |
232 |
systemctl disable ${service} |
systemctl ${opts} disable ${service} |
233 |
|
|
234 |
# do not stop services on bootstrap or MROOT!=/ |
# do not stop services on bootstrap or MROOT!=/ |
235 |
if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
236 |
then |
then |
237 |
|
echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} stopping unit ${service} ..." |
238 |
systemctl stop ${service} |
systemctl stop ${service} |
239 |
# try harder |
# try harder |
240 |
if [[ -n $(pidof ${service_exec}) ]] |
if [[ -n $(pidof ${service_exec}) ]] |
252 |
{ |
{ |
253 |
local service="$1" |
local service="$1" |
254 |
local service_exec="$2" |
local service_exec="$2" |
255 |
|
local opts |
256 |
|
|
257 |
# only run if systemd was found |
# only run if systemd was found |
258 |
if [[ ! -x /bin/systemctl ]] |
if [[ ! -x /bin/systemctl ]] |
261 |
return |
return |
262 |
fi |
fi |
263 |
|
|
264 |
# we do not support $MROOT atm |
# don't run if feature !autosvc was set |
265 |
if [[ ${MROOT} != / ]] || [[ ! -z ${MROOT} ]] |
if mqueryfeature "!autosvc" |
266 |
then |
then |
267 |
echo "\$MROOT is set, doing nothing!" |
echo "!autosvc detected; auto management of services disabled." |
|
echo "Please remove unit ${service} manually from the runlevels!" |
|
|
echo "Run 'systemctl disable ${service}' in your chroot later on." |
|
268 |
return |
return |
269 |
fi |
fi |
270 |
|
|
271 |
[[ -z ${service_exec} ]] && service_exec="$(which ${service} 2> /dev/null)" |
if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
272 |
|
then |
273 |
|
opts="--root ${MROOT}" |
274 |
|
fi |
275 |
|
|
276 |
|
if [[ -z ${service_exec} ]] |
277 |
|
then |
278 |
|
case ${service} in |
279 |
|
*.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; |
280 |
|
*.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; |
281 |
|
*.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; |
282 |
|
*.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; |
283 |
|
*) service_exec="$(which ${service} 2> /dev/null)" ;; |
284 |
|
esac |
285 |
|
fi |
286 |
|
|
287 |
|
# reload daemon to honor changed unit files |
288 |
|
systemctl ${opts} daemon-reload |
289 |
|
|
290 |
# do not stop services on bootstrap or MROOT!=/ |
# do not stop services on bootstrap or MROOT!=/ |
291 |
if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |
if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] |