Fix peer termination.
[archipelago] / xseg / launch
1 #!/bin/bash
2 #
3 # Launcher for xseg configurations
4
5 # Copyright 2012 GRNET S.A. All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or
8 # without modification, are permitted provided that the following
9 # conditions are met:
10 #
11 #   1. Redistributions of source code must retain the above
12 #      copyright notice, this list of conditions and the following
13 #      disclaimer.
14 #   2. Redistributions in binary form must reproduce the above
15 #      copyright notice, this list of conditions and the following
16 #      disclaimer in the documentation and/or other materials
17 #      provided with the distribution.
18 #
19 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
20 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
23 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 #
32 # The views and conclusions contained in the software and
33 # documentation are those of the authors and should not be
34 # interpreted as representing official policies, either expressed
35 # or implied, of GRNET S.A.
36 #
37
38
39 NI_SAVED=${NONINTERACTIVE}
40 export NONINTERACTIVE=1
41 source envsetup
42 export NONINTERACTIVE=${NI_SAVED}
43
44 set +e
45
46 function pretty_print {
47         echo "======================="
48         echo "$1"
49         echo "======================="
50 }
51
52 function fail {
53         echo "${1}"
54         exit 1
55 }
56
57 function parse_config {
58         [ -e ~/.xsegrc ] && source ~/.xsegrc
59
60         [ -z "${HOSTNAME}" ] && HOSTNAME=`hostname`
61         [ -z "${XSEG_HOME}" ] && XSEG_HOME="/root/archip/xseg"
62         [ -z "${MODULES_DIR}" ] && MODULES_DIR="${XSEG_HOME}/lib/kernel"
63         [ -z "${SPEC}" ] && SPEC="segdev:xsegbd:512:1024:12"
64         [ -z "${REQS}" ] && REQS=512
65         [ -z "${PORTS}" ] && PORTS=512
66         [ -z "${IMAGES}" ] && IMAGES="/home/user/archip/xseg/peers/user/foo"
67         [ -z "${MAPS}" ] && MAPS="/home/user/archip/xseg/peers/user/foo"
68         [ -z "${PITHOS}" ] && PITHOS="/srv/pithos/data/blocks"
69         [ -z "${PITHOSMAPS}" ] && PITHOSMAPS="/srv/pithos/data/maps"
70         [ -z "${XSEG_LOGS}" ] && XSEG_LOGS="/srv/archip/logs"
71         [ -z "${DEVICE_PREFIX}" ] && DEVICE_PREFIX="/dev/xsegbd"
72         [ -z "${XSEGBD_SYSFS}" ] && XSEGBD_SYSFS="/sys/bus/xsegbd"
73         [ -z "${CHRDEV_NAME}" ] && CHRDEV_NAME="/dev/`cat /proc/devices | grep segdev | awk '{print $2}'`"
74         [ "${CHRDEV_NAME}" = "/dev/" ] && CHRDEV_NAME="/dev/segdev"
75         [ -z "${CHRDEV_MAJOR}" ] && CHRDEV_MAJOR=`cat /proc/devices | grep segdev | awk '{print $1}'`
76         [ -z "${CHRDEV_MAJOR}" ] && CHRDEV_MAJOR=60
77         [ -z "${NR_OPS}" ] && NR_OPS=128
78         [ -z "${BPORT}" ] && BPORT=0
79         [ -z "${VPORT_START}" ] && VPORT_START=4
80         [ -z "${VPORT_END}" ] && VPORT_END=200
81         [ -z "${MPORT}" ] && MPORT=1
82         [ -z "${MBPORT}" ] && MBPORT=2
83         [ -z "${VTOOL}" ] && VTOOL=3
84 }
85
86
87 function unload_module {
88         rmmod "$1"
89 }
90
91 function unload_all {
92         unload_module "xsegbd"
93         rm "${CHRDEV_NAME}" || true
94         unload_module "xseg_segdev"
95         unload_module "xseg_posix"
96         unload_module "xseg_pthread"
97         unload_module "segdev"
98         unload_module "xseg"
99 }
100
101 function load_module {
102         (lsmod | grep "$1" > /dev/null) || insmod "${MODULES_DIR}/$1.ko" "$2" || exit 1
103 }
104
105 function mk_chardev {
106         ls "${CHRDEV_NAME}" &> /dev/null || \
107         mknod "${CHRDEV_NAME}" c "${CHRDEV_MAJOR}" 0 || exit 1
108 }
109
110 function load_all {
111         load_module "xseg"
112         load_module "segdev"
113         load_module "xseg_posix"
114         load_module "xseg_pthread"
115         load_module "xseg_segdev"
116         mk_chardev
117     "${XSEG_HOME}/peers/user/xseg" "${SPEC}" create || true
118         load_module "xsegbd" "spec=$SPEC"
119 }
120
121 function create_segment {
122         "${XSEG_HOME}/peer/users/xseg" "${SPEC}" create
123 }
124
125 function destroy_segment {
126         "${XSEG_TOOL}" "${SPEC}" destroy
127 }
128
129 function alloc_requests {
130         "${XSEG_TOOL}" "${SPEC}" $1 alloc_requests $2
131 }
132
133 function free_requests {
134         "${XSEG_TOOL}" "${SPEC}" $1 free_requests $2
135 }
136
137 function spawn_filed {
138         pgrep -f "peers/user/filed" || \
139         "${XSEG_HOME}/peers/user/filed" "$IMAGES" -p "$BPORT" -g "${SPEC}" -n \
140         ${NR_OPS}  &> "${XSEG_LOGS}/filed-${HOSTNAME}" &
141 }
142
143 function spawn_pfiled {
144         "${XSEG_HOME}/peers/user/pfiled" -p "$BPORT" -g "${SPEC}" -n \
145         ${NR_OPS} "${PITHOS}" "${IMAGES}" -v 1 &> "${XSEG_LOGS}/pfiled-${HOSTNAME}" &
146 }
147
148 function spawn_pfiledm {
149         "${XSEG_HOME}/peers/user/pfiled" -p "$MBPORT" -g "${SPEC}" -n \
150         ${NR_OPS} "${PITHOSMAPS}" "${MAPS}" -v 1  &> "${XSEG_LOGS}/pfiledm-${HOSTNAME}" &
151 }
152
153 function spawn_sosd {
154         "${XSEG_HOME}/peers/user/mt-sosd" -p "$BPORT" -g "${SPEC}" -n \
155         ${NR_OPS} --pool "blocks" -v 3 &> "${XSEG_LOGS}/sosd-${HOSTNAME}" &
156 }
157
158 function spawn_sosdm {
159         "${XSEG_HOME}/peers/user/mt-sosd" -p "$MBPORT" -g "${SPEC}" -n \
160         ${NR_OPS} --pool "maps" -v 3  &> "${XSEG_LOGS}/sosdm-${HOSTNAME}" &
161 }
162
163 function spawn_vlmcd {
164         pgrep -f "peers/user/st-vlmcd" || \
165         "${XSEG_HOME}/peers/user/st-vlmcd" -t 1 -sp "$VPORT_START" \
166         -ep "$VPORT_END" -bp "$BPORT" -mp "$MPORT" -g \
167         "${SPEC}" -n ${NR_OPS} -v 3 &> "${XSEG_LOGS}/vlmcd-${HOSTNAME}" &
168 #       alloc_requests "$VPORT:0" 128
169 }
170
171 function spawn_mapperdc {
172         echo "\"${XSEG_HOME}/peers/user/mt-mapperd\" -t 1 -sp \"$MPORT\" -ep
173         \"$MPORT\" -bp \"$BPORT\" -mbp \"$MBPORT\" -g \"${SPEC}\" -n ${NR_OPS} -v 3" 
174         pgrep -f "mt-mapperd" || \
175         "${XSEG_HOME}/peers/user/mt-mapperd" -t 1 -sp "$MPORT" -ep "$MPORT"\
176         -bp "$BPORT" -mbp "$MBPORT" -g "${SPEC}"\
177         -n ${NR_OPS} -v 3 &> "${XSEG_LOGS}/mapperd-${HOSTNAME}" & 
178 #       alloc_requests "$MPORT:0" 128
179 }
180
181 function spawn_mapperd {
182         pgrep -f "mapperd.py" || \
183         "${XSEG_HOME}/peers/user/mapperd.py" -p "$MPORT" -b "$BPORT" -g "${SPEC}" \
184         &> "${XSEG_LOGS}/mapperd-${HOSTNAME}" &
185 #       alloc_requests "$MPORT:0" 128
186 }
187
188 # map_volume - Map a volume to an xsegbd device
189 #
190 # @param $1             target/volume name
191 # @param $2             src port
192 # @param $3             dst port
193 function map_volume {
194         echo "$1 $2:$3:${REQS}" > "${XSEGBD_SYSFS}/add" || exit 1
195 }
196
197 # unmap_device - Unmap an xsegbd device/volume
198 #
199 # @param $1             xsegbd device id
200 function unmap_device {
201         echo "$1" > "${XSEGBD_SYSFS}/remove" || exit 1
202 }
203
204 function help {
205         echo "Usage: ./launch [list | <configuration> [start | stop] | insmod | rmmod]"
206 }
207
208 if [ -n "$NONINTERACTIVE" ]; then
209         exit 0
210 fi
211
212 parse_config
213 XSEG_TOOL="${XSEG_HOME}/peers/user/xseg"
214
215 case $1 in
216 list)
217         echo "filed [start|stop]"
218         echo "map [start|stap]"
219         echo " "
220         echo "insmod"
221         echo "rmmod"
222         ;;
223 insmod)
224         load_all
225         mk_chardev
226         ;;
227 rmmod)
228         rm ${CHRDEV_NAME}
229         unload_all
230         ;;
231 config)
232         echo "HOSTNAME=${HOSTNAME}"
233         echo "XSEG_HOME=${XSEG_HOME}"
234         echo "MODULES_DIR=${MODULES_DIR}"
235         echo "SPEC=${SPEC}"
236         echo "REQS=${REQS}"
237         echo "PORTS=${PORTS}"
238         echo "IMAGES=${IMAGES}"
239         echo "PITHOS=${PITHOS}"
240         echo "XSEG_LOGS=${XSEG_LOGS}"
241         echo "DEVICE_PREFIX=${DEVICE_PREFIX}"
242         echo "XSEGBD_SYSFS=${XSEGBD_SYSFS}"
243         echo "CHRDEV_NAME=${CHRDEV_NAME}"
244         echo "CHRDEV_MAJOR=${CHRDEV_MAJOR}"
245         echo "NR_OPS=${NR_OPS}"
246         echo "BPORT=${BPORT}"
247         echo "VPORT=${VPORT}"
248         echo "MPORT=${MPORT}"
249         ;;
250 filed)
251         case $2 in
252         start)
253                 load_all
254                 sleep 0.5
255                 spawn_filed
256                 sleep 0.5
257                 map_volume xsegvol 3 0
258                 ;;
259         stop)
260                 pkill -f peers/user/filed
261                 sleep 0.5
262                 unmap_device 3
263                 unload_all
264                 ;;
265         esac
266         ;;
267 map)
268         case $2 in
269         start)
270                 load_all
271                 sleep 0.5
272                 spawn_pfiled
273                 spawn_mapperd
274                 spawn_vlmcd
275                 ;;
276         stop)
277                 pkill -f peers/user/mt-vlmcd || true
278 #               free_requests "$VPORT:0" 128 || true
279                 pkill -f peers/user/mt-mapperd || true
280 #               free_requests "$MPORT:0" 128 || true
281                 pkill -f peers/user/pfiled || true
282                 sleep 0.5
283                 unload_all
284                 ;;
285         *)
286                 help
287                 ;;
288         esac
289         ;;
290 nomodules)
291         case $2 in
292         start)
293                 spawn_filed
294                 spawn_mapperd
295                 spawn_vlmcd
296                 ;;
297         stop)
298                 pkill -f peers/user/mt-vlmcd || true
299 #               free_requests "$VPORT:0" 128 || true
300                 pkill -f peers/user/mt-mapperd || true
301 #               free_requests "$MPORT:0" 128 || true
302                 pkill -f peers/user/filed || true
303                 ;;
304         *)
305                 help
306                 ;;
307         esac
308         ;;
309 one)
310         case $2 in
311         start)
312                 load_all
313                 sleep 1
314                 spawn_filed 
315                 spawn_mapperdc
316                 spawn_vlmcd 
317                 ;;
318         stop)
319                 pkill -f peers/user/mt-vlmcd || true
320 #               free_requests "$VPORT:0" 128 || true
321                 pkill -f peers/user/mt-mapperd || true
322 #               free_requests "$MPORT:0" 128 || true
323                 pkill -f peers/user/filed || true
324                 sleep 0.5
325                 unload_all
326                 ;;
327         *)
328                 help
329                 ;;
330         esac
331         ;;
332 doublemap)
333         case $2 in
334         start)
335                 load_all
336                 sleep 0.5
337                 spawn_pfiled
338                 spawn_pfiledm
339                 spawn_mapperdc
340                 spawn_vlmcd
341                 ;;
342         stop)
343                 pkill -f peers/user/st-vlmcd || true
344 #               free_requests "$VPORT:0" 128 || true
345                 pkill -f peers/user/mt-mapperd || true
346 #               free_requests "$MPORT:0" 128 || true
347                 pkill -f peers/user/pfiled || true
348                 sleep 0.5
349                 unload_all
350                 ;;
351         *)
352                 help
353                 ;;
354         esac
355         ;;
356 mapperd)
357         case $2 in
358         start)
359                 spawn_mapperdc
360                 ;;
361         stop)
362                 pkill -f peers/user/mt-mapperd || true
363                 ;;
364         *)
365                 help
366                 ;;
367         esac
368         ;;
369 doublesosd)
370         case $2 in
371         start)
372                 load_all
373                 sleep 0.5
374                 spawn_sosd
375                 spawn_sosdm
376                 spawn_mapperdc
377                 spawn_vlmcd
378                 ;;
379         stop)
380                 pkill -f peers/user/st-vlmcd || true
381 #               free_requests "$VPORT:0" 128 || true
382                 pkill -f peers/user/mt-mapperd || true
383 #               free_requests "$MPORT:0" 128 || true
384                 pkill -f peers/user/mt-sosd || true
385                 sleep 0.5
386                 unload_all
387                 ;;
388         *)
389                 help
390                 ;;
391         esac
392         ;;
393 mtmapperd)
394         case $2 in
395         start)
396                 spawn_mapperdc
397                 ;;
398         stop)
399                 pkill -f peers/user/mt-mapperd || true
400                 ;;
401         *)
402                 help
403                 ;;
404         esac
405         ;;
406 *)
407         help
408         ;;
409 esac