Annotation of /branches/magellan-next/include/mtools.minc
Parent Directory | Revision Log
Revision 19 -
(hide annotations)
(download)
Thu Jun 2 21:21:16 2011 UTC (12 years, 11 months ago) by niro
File size: 7049 byte(s)
Thu Jun 2 21:21:16 2011 UTC (12 years, 11 months ago) by niro
File size: 7049 byte(s)
-try to determinate service_exec from unit name
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 | 19 | if [[ -z ${service_exec} ]] |
18 | then | ||
19 | case ${service} in | ||
20 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
21 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
22 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
23 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
24 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
25 | esac | ||
26 | fi | ||
27 | niro | 1 | |
28 | # add service to default runlevels | ||
29 | niro | 8 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding ${service} to default runlevels ..." |
30 | niro | 1 | [[ -x ${MROOT}/etc/rc.d/init.d/${service} ]] && rc-config add ${service} &> /dev/null |
31 | |||
32 | # do not start services on bootstrap or MROOT!=/ | ||
33 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
34 | then | ||
35 | # start service | ||
36 | if [[ -n $(pidof ${service_exec}) ]] | ||
37 | then | ||
38 | # restart service | ||
39 | /etc/init.d/${service} restart | ||
40 | else | ||
41 | # start service | ||
42 | /etc/init.d/${service} start | ||
43 | fi | ||
44 | fi | ||
45 | } | ||
46 | |||
47 | # removes services from runlevels and stops them | ||
48 | # mstopservice service {/path/to/service_exec} | ||
49 | mstopservice() | ||
50 | { | ||
51 | local service="$1" | ||
52 | local service_exec="$2" | ||
53 | |||
54 | niro | 18 | # only run if sysV init was found |
55 | if [[ ! -f /etc/rc.d/init.d/functions ]] | ||
56 | then | ||
57 | echo "sysV init not found, not adding service ${service} to runlevels!" | ||
58 | return | ||
59 | fi | ||
60 | |||
61 | niro | 19 | if [[ -z ${service_exec} ]] |
62 | then | ||
63 | case ${service} in | ||
64 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
65 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
66 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
67 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
68 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
69 | esac | ||
70 | fi | ||
71 | niro | 1 | |
72 | # only stop the service if ${service_exec} does not exist | ||
73 | [[ -f ${MROOT}/${service_exec} ]] && return | ||
74 | |||
75 | # del services from runlevel regardless if they exist or not | ||
76 | niro | 8 | echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing ${service} from default runlevels ..." |
77 | niro | 1 | rc-config del ${service} &> /dev/null |
78 | |||
79 | # do not stop services on bootstrap or MROOT!=/ | ||
80 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
81 | then | ||
82 | # stop service if running | ||
83 | if [[ -n $(pidof ${service_exec}) ]] | ||
84 | then | ||
85 | killall -15 ${service_exec} &> /dev/null | ||
86 | sleep 1 | ||
87 | killall -9 ${service_exec} &> /dev/null | ||
88 | fi | ||
89 | fi | ||
90 | niro | 4 | } |
91 | |||
92 | # reloads a service if already running | ||
93 | # mreloadservice service {/path/to/service_exec} | ||
94 | mreloadservice() | ||
95 | { | ||
96 | local service="$1" | ||
97 | local service_exec="$2" | ||
98 | |||
99 | niro | 18 | # only run if sysV init was found |
100 | if [[ ! -f /etc/rc.d/init.d/functions ]] | ||
101 | then | ||
102 | echo "sysV init not found, not adding service ${service} to runlevels!" | ||
103 | return | ||
104 | fi | ||
105 | |||
106 | niro | 19 | if [[ -z ${service_exec} ]] |
107 | then | ||
108 | case ${service} in | ||
109 | *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; | ||
110 | *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; | ||
111 | *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; | ||
112 | *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; | ||
113 | *) service_exec="$(which ${service} 2> /dev/null)" ;; | ||
114 | esac | ||
115 | fi | ||
116 | niro | 4 | |
117 | # do not stop services on bootstrap or MROOT!=/ | ||
118 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
119 | then | ||
120 | # only reload the service if running | ||
121 | if [[ -n $(pidof ${service_exec}) ]] | ||
122 | then | ||
123 | /etc/init.d/${service} reload | ||
124 | fi | ||
125 | fi | ||
126 | } | ||
127 | niro | 16 | |
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 | |||
136 | # only run if systemd was found | ||
137 | if [[ ! -x /bin/systemctl ]] | ||
138 | then | ||
139 | echo "systemd not found, not adding unit ${service} to runlevels!" | ||
140 | return | ||
141 | fi | ||
142 | |||
143 | # we do not support $MROOT atm | ||
144 | niro | 17 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
145 | niro | 16 | then |
146 | echo "\$MROOT is set, doing nothing!" | ||
147 | echo "Please add unit ${service} manually to the runlevels!" | ||
148 | echo "Run 'systemctl enable ${service}' in your chroot later on." | ||
149 | return | ||
150 | fi | ||
151 | |||
152 | [[ -z ${service_exec} ]] && service_exec="$(which ${service})" | ||
153 | |||
154 | # add service to default runlevels | ||
155 | echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding unit ${service} to default runlevels ..." | ||
156 | # reload daemon to honor changed unit files | ||
157 | systemctl daemon-reload | ||
158 | systemctl enable ${service} | ||
159 | |||
160 | # do not start services on bootstrap or MROOT!=/ | ||
161 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
162 | then | ||
163 | # start or restart the service | ||
164 | systemctl try-restart ${service} | ||
165 | fi | ||
166 | } | ||
167 | |||
168 | # removes systemd units from runlevels and stops them | ||
169 | # mstopunit service {/path/to/service_exec} | ||
170 | mstopunit() | ||
171 | { | ||
172 | local service="$1" | ||
173 | local service_exec="$2" | ||
174 | |||
175 | # only run if systemd was found | ||
176 | if [[ ! -x /bin/systemctl ]] | ||
177 | then | ||
178 | echo "systemd not found, not removing unit ${service} from runlevels!" | ||
179 | return | ||
180 | fi | ||
181 | |||
182 | # we do not support $MROOT atm | ||
183 | niro | 17 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
184 | niro | 16 | then |
185 | echo "\$MROOT is set, doing nothing!" | ||
186 | echo "Please remove unit ${service} manually from the runlevels!" | ||
187 | echo "Run 'systemctl disable ${service}' in your chroot later on." | ||
188 | return | ||
189 | fi | ||
190 | |||
191 | [[ -z ${service_exec} ]] && service_exec="$(which ${service} 2> /dev/null)" | ||
192 | |||
193 | # only stop the service if ${service_exec} does not exist | ||
194 | [[ -f ${MROOT}/${service_exec} ]] && return | ||
195 | |||
196 | # del services from runlevel regardless if they exist or not | ||
197 | echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing unit ${service} from default runlevels ..." | ||
198 | # reload daemon to honor changed unit files | ||
199 | systemctl daemon-reload | ||
200 | systemctl disable ${service} | ||
201 | |||
202 | # do not stop services on bootstrap or MROOT!=/ | ||
203 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
204 | then | ||
205 | systemctl stop ${service} | ||
206 | # try harder | ||
207 | if [[ -n $(pidof ${service_exec}) ]] | ||
208 | then | ||
209 | killall -15 ${service_exec} &> /dev/null | ||
210 | sleep 1 | ||
211 | killall -9 ${service_exec} &> /dev/null | ||
212 | fi | ||
213 | fi | ||
214 | } | ||
215 | |||
216 | # reloads a systemd unit if already running | ||
217 | # mreloadunit service {/path/to/service_exec} | ||
218 | mreloadunit() | ||
219 | { | ||
220 | local service="$1" | ||
221 | local service_exec="$2" | ||
222 | |||
223 | # only run if systemd was found | ||
224 | if [[ ! -x /bin/systemctl ]] | ||
225 | then | ||
226 | echo "systemd not found, not removing unit ${service} from runlevels!" | ||
227 | return | ||
228 | fi | ||
229 | |||
230 | # we do not support $MROOT atm | ||
231 | niro | 17 | if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] |
232 | niro | 16 | then |
233 | echo "\$MROOT is set, doing nothing!" | ||
234 | echo "Please remove unit ${service} manually from the runlevels!" | ||
235 | echo "Run 'systemctl disable ${service}' in your chroot later on." | ||
236 | return | ||
237 | fi | ||
238 | |||
239 | [[ -z ${service_exec} ]] && service_exec="$(which ${service} 2> /dev/null)" | ||
240 | |||
241 | # do not stop services on bootstrap or MROOT!=/ | ||
242 | if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] | ||
243 | then | ||
244 | # only reload the service if running | ||
245 | systemctl reload-or-try-restart ${service} | ||
246 | fi | ||
247 | } |