fix bug in mt-mapperd
[archipelago] / xseg / launch
1 #!/bin/bash
2 #
3 # Launcher for xseg configurations
4
5 NI_SAVED=${NONINTERACTIVE}
6 export NONINTERACTIVE=1
7 source envsetup
8 export NONINTERACTIVE=${NI_SAVED}
9
10 set +e
11
12 function pretty_print {
13         echo "======================="
14         echo "$1"
15         echo "======================="
16 }
17
18 function fail {
19         echo "${1}"
20         exit 1
21 }
22
23 function parse_config {
24         [ -e ~/.xsegrc ] && source ~/.xsegrc
25
26         [ -z "${HOSTNAME}" ] && HOSTNAME=`hostname`
27         [ -z "${XSEG_HOME}" ] && XSEG_HOME="/root/archip/xseg"
28         [ -z "${MODULES_DIR}" ] && MODULES_DIR="${XSEG_HOME}/lib/kernel"
29         [ -z "${SPEC}" ] && SPEC="segdev:xsegbd:512:1024:12"
30         [ -z "${REQS}" ] && REQS=512
31         [ -z "${PORTS}" ] && PORTS=512
32         [ -z "${IMAGES}" ] && IMAGES="/home/user/archip/xseg/peers/user/foo"
33         [ -z "${MAPS}" ] && MAPS="/home/user/archip/xseg/peers/user/foo"
34         [ -z "${PITHOS}" ] && PITHOS="/srv/pithos/data/blocks"
35         [ -z "${PITHOSMAPS}" ] && PITHOSMAPS="/srv/pithos/data/maps"
36         [ -z "${XSEG_LOGS}" ] && XSEG_LOGS="/srv/archip/logs"
37         [ -z "${DEVICE_PREFIX}" ] && DEVICE_PREFIX="/dev/xsegbd"
38         [ -z "${XSEGBD_SYSFS}" ] && XSEGBD_SYSFS="/sys/bus/xsegbd"
39         [ -z "${CHRDEV_NAME}" ] && CHRDEV_NAME="/dev/`cat /proc/devices | grep segdev | awk '{print $2}'`"
40         [ "${CHRDEV_NAME}" = "/dev/" ] && CHRDEV_NAME="/dev/segdev"
41         [ -z "${CHRDEV_MAJOR}" ] && CHRDEV_MAJOR=`cat /proc/devices | grep segdev | awk '{print $1}'`
42         [ -z "${CHRDEV_MAJOR}" ] && CHRDEV_MAJOR=60
43         [ -z "${NR_OPS}" ] && NR_OPS=128
44         [ -z "${BPORT}" ] && BPORT=0
45         [ -z "${VPORT_START}" ] && VPORT_START=4
46         [ -z "${VPORT_END}" ] && VPORT_END=200
47         [ -z "${MPORT}" ] && MPORT=1
48         [ -z "${MBPORT}" ] && MBPORT=2
49         [ -z "${VTOOL}" ] && VTOOL=3
50 }
51
52
53 function unload_module {
54         rmmod "$1"
55 }
56
57 function unload_all {
58         unload_module "xsegbd"
59         rm "${CHRDEV_NAME}" || true
60         unload_module "xseg_segdev"
61         unload_module "xseg_posix"
62         unload_module "xseg_pthread"
63         unload_module "segdev"
64         unload_module "xseg"
65 }
66
67 function load_module {
68         (lsmod | grep "$1" > /dev/null) || insmod "${MODULES_DIR}/$1.ko" "$2" || exit 1
69 }
70
71 function mk_chardev {
72         ls "${CHRDEV_NAME}" &> /dev/null || \
73         mknod "${CHRDEV_NAME}" c "${CHRDEV_MAJOR}" 0 || exit 1
74 }
75
76 function load_all {
77         load_module "xseg"
78         load_module "segdev"
79         load_module "xseg_posix"
80         load_module "xseg_pthread"
81         load_module "xseg_segdev"
82         mk_chardev
83     "${XSEG_HOME}/peers/user/xseg" "${SPEC}" create || true
84         load_module "xsegbd" "spec=$SPEC"
85 }
86
87 function create_segment {
88         "${XSEG_HOME}/peer/users/xseg" "${SPEC}" create
89 }
90
91 function destroy_segment {
92         "${XSEG_TOOL}" "${SPEC}" destroy
93 }
94
95 function alloc_requests {
96         "${XSEG_TOOL}" "${SPEC}" $1 alloc_requests $2
97 }
98
99 function free_requests {
100         "${XSEG_TOOL}" "${SPEC}" $1 free_requests $2
101 }
102
103 function spawn_filed {
104         pgrep -f "peers/user/filed" || \
105         "${XSEG_HOME}/peers/user/filed" "$IMAGES" -p "$BPORT" -g "${SPEC}" -n \
106         ${NR_OPS}  &> "${XSEG_LOGS}/filed-${HOSTNAME}" &
107 }
108
109 function spawn_pfiled {
110         "${XSEG_HOME}/peers/user/pfiled" -p "$BPORT" -g "${SPEC}" -n \
111         ${NR_OPS} "${PITHOS}" "${IMAGES}" -v 1 &> "${XSEG_LOGS}/pfiled-${HOSTNAME}" &
112 }
113
114 function spawn_pfiledm {
115         "${XSEG_HOME}/peers/user/pfiled" -p "$MBPORT" -g "${SPEC}" -n \
116         ${NR_OPS} "${PITHOSMAPS}" "${MAPS}" -v 1  &> "${XSEG_LOGS}/pfiledm-${HOSTNAME}" &
117 }
118
119 function spawn_sosd {
120         "${XSEG_HOME}/peers/user/mt-sosd" -p "$BPORT" -g "${SPEC}" -n \
121         ${NR_OPS} --pool "blocks" -v 3 &> "${XSEG_LOGS}/sosd-${HOSTNAME}" &
122 }
123
124 function spawn_sosdm {
125         "${XSEG_HOME}/peers/user/mt-sosd" -p "$MBPORT" -g "${SPEC}" -n \
126         ${NR_OPS} --pool "maps" -v 3  &> "${XSEG_LOGS}/sosdm-${HOSTNAME}" &
127 }
128
129 function spawn_vlmcd {
130         pgrep -f "peers/user/st-vlmcd" || \
131         "${XSEG_HOME}/peers/user/st-vlmcd" -t 1 -sp "$VPORT_START" \
132         -ep "$VPORT_END" -bp "$BPORT" -mp "$MPORT" -g \
133         "${SPEC}" -n ${NR_OPS} -v 3 &> "${XSEG_LOGS}/vlmcd-${HOSTNAME}" &
134 #       alloc_requests "$VPORT:0" 128
135 }
136
137 function spawn_mapperdc {
138         echo "\"${XSEG_HOME}/peers/user/mt-mapperd\" -t 1 -sp \"$MPORT\" -ep
139         \"$MPORT\" -bp \"$BPORT\" -mbp \"$MBPORT\" -g \"${SPEC}\" -n ${NR_OPS} -v 3" 
140         pgrep -f "mt-mapperd" || \
141         "${XSEG_HOME}/peers/user/mt-mapperd" -t 1 -sp "$MPORT" -ep "$MPORT"\
142         -bp "$BPORT" -mbp "$MBPORT" -g "${SPEC}"\
143         -n ${NR_OPS} -v 3 &> "${XSEG_LOGS}/mapperd-${HOSTNAME}" & 
144 #       alloc_requests "$MPORT:0" 128
145 }
146
147 function spawn_mapperd {
148         pgrep -f "mapperd.py" || \
149         "${XSEG_HOME}/peers/user/mapperd.py" -p "$MPORT" -b "$BPORT" -g "${SPEC}" \
150         &> "${XSEG_LOGS}/mapperd-${HOSTNAME}" &
151 #       alloc_requests "$MPORT:0" 128
152 }
153
154 # map_volume - Map a volume to an xsegbd device
155 #
156 # @param $1             target/volume name
157 # @param $2             src port
158 # @param $3             dst port
159 function map_volume {
160         echo "$1 $2:$3:${REQS}" > "${XSEGBD_SYSFS}/add" || exit 1
161 }
162
163 # unmap_device - Unmap an xsegbd device/volume
164 #
165 # @param $1             xsegbd device id
166 function unmap_device {
167         echo "$1" > "${XSEGBD_SYSFS}/remove" || exit 1
168 }
169
170 function help {
171         echo "Usage: ./launch [list | <configuration> [start | stop] | insmod | rmmod]"
172 }
173
174 if [ -n "$NONINTERACTIVE" ]; then
175         exit 0
176 fi
177
178 parse_config
179 XSEG_TOOL="${XSEG_HOME}/peers/user/xseg"
180
181 case $1 in
182 list)
183         echo "filed [start|stop]"
184         echo "map [start|stap]"
185         echo " "
186         echo "insmod"
187         echo "rmmod"
188         ;;
189 insmod)
190         load_all
191         mk_chardev
192         ;;
193 rmmod)
194         rm ${CHRDEV_NAME}
195         unload_all
196         ;;
197 config)
198         echo "HOSTNAME=${HOSTNAME}"
199         echo "XSEG_HOME=${XSEG_HOME}"
200         echo "MODULES_DIR=${MODULES_DIR}"
201         echo "SPEC=${SPEC}"
202         echo "REQS=${REQS}"
203         echo "PORTS=${PORTS}"
204         echo "IMAGES=${IMAGES}"
205         echo "PITHOS=${PITHOS}"
206         echo "XSEG_LOGS=${XSEG_LOGS}"
207         echo "DEVICE_PREFIX=${DEVICE_PREFIX}"
208         echo "XSEGBD_SYSFS=${XSEGBD_SYSFS}"
209         echo "CHRDEV_NAME=${CHRDEV_NAME}"
210         echo "CHRDEV_MAJOR=${CHRDEV_MAJOR}"
211         echo "NR_OPS=${NR_OPS}"
212         echo "BPORT=${BPORT}"
213         echo "VPORT=${VPORT}"
214         echo "MPORT=${MPORT}"
215         ;;
216 filed)
217         case $2 in
218         start)
219                 load_all
220                 sleep 0.5
221                 spawn_filed
222                 sleep 0.5
223                 map_volume xsegvol 3 0
224                 ;;
225         stop)
226                 pkill -f peers/user/filed
227                 sleep 0.5
228                 unmap_device 3
229                 unload_all
230                 ;;
231         esac
232         ;;
233 map)
234         case $2 in
235         start)
236                 load_all
237                 sleep 0.5
238                 spawn_pfiled
239                 spawn_mapperd
240                 spawn_vlmcd
241                 ;;
242         stop)
243                 pkill -f peers/user/mt-vlmcd || true
244 #               free_requests "$VPORT:0" 128 || true
245                 pkill -f peers/user/mt-mapperd || true
246 #               free_requests "$MPORT:0" 128 || true
247                 pkill -f peers/user/pfiled || true
248                 sleep 0.5
249                 unload_all
250                 ;;
251         *)
252                 help
253                 ;;
254         esac
255         ;;
256 nomodules)
257         case $2 in
258         start)
259                 spawn_filed
260                 spawn_mapperd
261                 spawn_vlmcd
262                 ;;
263         stop)
264                 pkill -f peers/user/mt-vlmcd || true
265 #               free_requests "$VPORT:0" 128 || true
266                 pkill -f peers/user/mt-mapperd || true
267 #               free_requests "$MPORT:0" 128 || true
268                 pkill -f peers/user/filed || true
269                 ;;
270         *)
271                 help
272                 ;;
273         esac
274         ;;
275 one)
276         case $2 in
277         start)
278                 load_all
279                 sleep 1
280                 spawn_filed 
281                 spawn_mapperdc
282                 spawn_vlmcd 
283                 ;;
284         stop)
285                 pkill -f peers/user/mt-vlmcd || true
286 #               free_requests "$VPORT:0" 128 || true
287                 pkill -f peers/user/mt-mapperd || true
288 #               free_requests "$MPORT:0" 128 || true
289                 pkill -f peers/user/filed || true
290                 sleep 0.5
291                 unload_all
292                 ;;
293         *)
294                 help
295                 ;;
296         esac
297         ;;
298 doublemap)
299         case $2 in
300         start)
301                 load_all
302                 sleep 0.5
303                 spawn_pfiled
304                 spawn_pfiledm
305                 spawn_mapperdc
306                 spawn_vlmcd
307                 ;;
308         stop)
309                 pkill -f peers/user/st-vlmcd || true
310 #               free_requests "$VPORT:0" 128 || true
311                 pkill -f peers/user/mt-mapperd || true
312 #               free_requests "$MPORT:0" 128 || true
313                 pkill -f peers/user/pfiled || true
314                 sleep 0.5
315                 unload_all
316                 ;;
317         *)
318                 help
319                 ;;
320         esac
321         ;;
322 mapperd)
323         case $2 in
324         start)
325                 spawn_mapperdc
326                 ;;
327         stop)
328                 pkill -f peers/user/mt-mapperd || true
329                 ;;
330         *)
331                 help
332                 ;;
333         esac
334         ;;
335 doublesosd)
336         case $2 in
337         start)
338                 load_all
339                 sleep 0.5
340                 spawn_sosd
341                 spawn_sosdm
342                 spawn_mapperdc
343                 spawn_vlmcd
344                 ;;
345         stop)
346                 pkill -f peers/user/st-vlmcd || true
347 #               free_requests "$VPORT:0" 128 || true
348                 pkill -f peers/user/mt-mapperd || true
349 #               free_requests "$MPORT:0" 128 || true
350                 pkill -f peers/user/mt-sosd || true
351                 sleep 0.5
352                 unload_all
353                 ;;
354         *)
355                 help
356                 ;;
357         esac
358         ;;
359 mtmapperd)
360         case $2 in
361         start)
362                 spawn_mapperdc
363                 ;;
364         stop)
365                 pkill -f peers/user/mt-mapperd || true
366                 ;;
367         *)
368                 help
369                 ;;
370         esac
371         ;;
372 *)
373         help
374         ;;
375 esac