Magellan Linux

Annotation of /trunk/include/mtools.minc

Parent Directory Parent Directory | Revision Log Revision Log


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