add support for double (map, blocks) blocker in mapper
[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:16:1024:12"
30         [ -z "${REQS}" ] && REQS=512
31         [ -z "${PORTS}" ] && PORTS=16
32         [ -z "${IMAGES}" ] && IMAGES="/home/user/archip/xseg/peers/user/foo"
33         [ -z "${MAPS}" ] && IMAGES="/home/user/archip/xseg/peers/user/foo"
34         [ -z "${PITHOS}" ] && PITHOS="/srv/pithos/data/blocks"
35         [ -z "${PITHOSMAPS}" ] && PITHOS="/srv/pithos/data/maps"
36         [ -z "${XSEG_LOGS}" ] && XSEG_LOGS="/home/user/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=16
44         [ -z "${BPORT}" ] && BPORT=0
45         [ -z "${VPORT}" ] && VPORT=2
46         [ -z "${MPORT}" ] && MPORT=1
47         [ -z "${MBPORT}" ] && MBPORT=3
48         [ -z "${VTOOL}" ] && VTOOL=15
49 }
50
51
52 function unload_module {
53         rmmod "$1"
54 }
55
56 function unload_all {
57         unload_module "xsegbd"
58         rm "${CHRDEV_NAME}" || true
59         unload_module "xseg_segdev"
60         unload_module "xseg_posix"
61         unload_module "xseg_pthread"
62         unload_module "segdev"
63         unload_module "xseg"
64 }
65
66 function load_module {
67         (lsmod | grep "$1" > /dev/null) || insmod "${MODULES_DIR}/$1.ko" "$2" || exit 1
68 }
69
70 function mk_chardev {
71         ls "${CHRDEV_NAME}" &> /dev/null || \
72         mknod "${CHRDEV_NAME}" c "${CHRDEV_MAJOR}" 0 || exit 1
73 }
74
75 function load_all {
76         load_module "xseg"
77         load_module "segdev"
78         load_module "xseg_posix"
79         load_module "xseg_pthread"
80         load_module "xseg_segdev"
81         mk_chardev
82     "${XSEG_HOME}/peers/user/xseg" "${SPEC}" create || true
83         load_module "xsegbd" "spec=$SPEC"
84 }
85
86 function create_segment {
87         "${XSEG_HOME}/peer/users/xseg" "${SPEC}" create
88 }
89
90 function destroy_segment {
91         "${XSEG_TOOL}" "${SPEC}" destroy
92 }
93
94 function alloc_requests {
95         "${XSEG_TOOL}" "${SPEC}" $1 alloc_requests $2
96 }
97
98 function free_requests {
99         "${XSEG_TOOL}" "${SPEC}" $1 free_requests $2
100 }
101
102 function spawn_filed {
103         pgrep -f "peers/user/filed" || \
104         "${XSEG_HOME}/peers/user/filed" "$IMAGES" -p "$BPORT" -g "${SPEC}" -n \
105         ${NR_OPS}  &> "${XSEG_LOGS}/filed-${HOSTNAME}" &
106 }
107
108 function spawn_pfiled {
109         pgrep -f "peers/user/pfiled" || \
110         "${XSEG_HOME}/peers/user/pfiled" -p "$BPORT" -g "${SPEC}" -n \
111         ${NR_OPS} "${PITHOS}" "${IMAGES}" &> "${XSEG_LOGS}/pfiled-${HOSTNAME}" &
112 }
113 function spawn_pfiledm {
114         pgrep -f "peers/user/pfiled" || \
115         "${XSEG_HOME}/peers/user/pfiled" -p "$MBPORT" -g "${SPEC}" -n \
116         ${NR_OPS} "${PITHOSMAPS}" "${MAPS}" &> "${XSEG_LOGS}/pfiledm-${HOSTNAME}" &
117
118 function spawn_vlmcd {
119         pgrep -f "peers/user/mt-vlmcd" || \
120         "${XSEG_HOME}/peers/user/mt-vlmcd" -t 1 -p "$VPORT" -bp "$BPORT" -mp "$MPORT" -g \
121         "${SPEC}" -n ${NR_OPS} &> "${XSEG_LOGS}/vlmcd-${HOSTNAME}" &
122 #       alloc_requests "$VPORT:0" 128
123 }
124
125 function spawn_mapperdc {
126         pgrep -f "mt-mapperd" || \
127         "${XSEG_HOME}/peers/user/mt-mapperd" -t 1 -p "$MPORT" -bp "$BPORT"  -g "${SPEC}"\
128         &> "${XSEG_LOGS}/mapperd-${HOSTNAME}" & 
129 #       alloc_requests "$MPORT:0" 128
130 }
131
132 function spawn_mapperd {
133         pgrep -f "mapperd.py" || \
134         "${XSEG_HOME}/peers/user/mapperd.py" -p "$MPORT" -b "$BPORT" -g "${SPEC}" \
135         &> "${XSEG_LOGS}/mapperd-${HOSTNAME}" &
136 #       alloc_requests "$MPORT:0" 128
137 }
138
139 # map_volume - Map a volume to an xsegbd device
140 #
141 # @param $1             target/volume name
142 # @param $2             src port
143 # @param $3             dst port
144 function map_volume {
145         echo "$1 $2:$3:${REQS}" > "${XSEGBD_SYSFS}/add" || exit 1
146 }
147
148 # unmap_device - Unmap an xsegbd device/volume
149 #
150 # @param $1             xsegbd device id
151 function unmap_device {
152         echo "$1" > "${XSEGBD_SYSFS}/remove" || exit 1
153 }
154
155 function help {
156         echo "Usage: ./launch [list | <configuration> [start | stop] | insmod | rmmod]"
157 }
158
159 if [ -n "$NONINTERACTIVE" ]; then
160         exit 0
161 fi
162
163 parse_config
164 XSEG_TOOL="${XSEG_HOME}/peers/user/xseg"
165
166 case $1 in
167 list)
168         echo "filed [start|stop]"
169         echo "map [start|stap]"
170         echo " "
171         echo "insmod"
172         echo "rmmod"
173         ;;
174 insmod)
175         load_all
176         mk_chardev
177         ;;
178 rmmod)
179         rm ${CHRDEV_NAME}
180         unload_all
181         ;;
182 config)
183         echo "HOSTNAME=${HOSTNAME}"
184         echo "XSEG_HOME=${XSEG_HOME}"
185         echo "MODULES_DIR=${MODULES_DIR}"
186         echo "SPEC=${SPEC}"
187         echo "REQS=${REQS}"
188         echo "PORTS=${PORTS}"
189         echo "IMAGES=${IMAGES}"
190         echo "PITHOS=${PITHOS}"
191         echo "XSEG_LOGS=${XSEG_LOGS}"
192         echo "DEVICE_PREFIX=${DEVICE_PREFIX}"
193         echo "XSEGBD_SYSFS=${XSEGBD_SYSFS}"
194         echo "CHRDEV_NAME=${CHRDEV_NAME}"
195         echo "CHRDEV_MAJOR=${CHRDEV_MAJOR}"
196         echo "NR_OPS=${NR_OPS}"
197         echo "BPORT=${BPORT}"
198         echo "VPORT=${VPORT}"
199         echo "MPORT=${MPORT}"
200         ;;
201 filed)
202         case $2 in
203         start)
204                 load_all
205                 sleep 0.5
206                 spawn_filed
207                 sleep 0.5
208                 map_volume xsegvol 3 0
209                 ;;
210         stop)
211                 pkill -f peers/user/filed
212                 sleep 0.5
213                 unmap_device 3
214                 unload_all
215                 ;;
216         esac
217         ;;
218 map)
219         case $2 in
220         start)
221                 load_all
222                 sleep 0.5
223                 spawn_pfiled
224                 spawn_mapperd
225                 spawn_vlmcd
226                 ;;
227         stop)
228                 pkill -f peers/user/mt-vlmcd || true
229 #               free_requests "$VPORT:0" 128 || true
230                 pkill -f peers/user/mt-mapperd || true
231 #               free_requests "$MPORT:0" 128 || true
232                 pkill -f peers/user/pfiled || true
233                 sleep 0.5
234                 unload_all
235                 ;;
236         *)
237                 help
238                 ;;
239         esac
240         ;;
241 nomodules)
242         case $2 in
243         start)
244                 spawn_filed
245                 spawn_mapperd
246                 spawn_vlmcd
247                 ;;
248         stop)
249                 pkill -f peers/user/mt-vlmcd || true
250 #               free_requests "$VPORT:0" 128 || true
251                 pkill -f peers/user/mt-mapperd || true
252 #               free_requests "$MPORT:0" 128 || true
253                 pkill -f peers/user/filed || true
254                 ;;
255         *)
256                 help
257                 ;;
258         esac
259         ;;
260 one)
261         case $2 in
262         start)
263                 load_all
264                 sleep 1
265                 spawn_filed 
266                 spawn_mapperdc
267                 spawn_vlmcd 
268                 ;;
269         stop)
270                 pkill -f peers/user/mt-vlmcd || true
271 #               free_requests "$VPORT:0" 128 || true
272                 pkill -f peers/user/mt-mapperd || true
273 #               free_requests "$MPORT:0" 128 || true
274                 pkill -f peers/user/filed || true
275                 sleep 0.5
276                 unload_all
277                 ;;
278         *)
279                 help
280                 ;;
281         esac
282         ;;
283 doublemap)
284         case $2 in
285         start)
286                 load_all
287                 sleep 0.5
288                 spawn_pfiled
289                 spawn_pfiledm
290                 spawn_mapperd
291                 spawn_vlmcd
292                 ;;
293         stop)
294                 pkill -f peers/user/mt-vlmcd || true
295 #               free_requests "$VPORT:0" 128 || true
296                 pkill -f peers/user/mt-mapperd || true
297 #               free_requests "$MPORT:0" 128 || true
298                 pkill -f peers/user/pfiled || true
299                 sleep 0.5
300                 unload_all
301                 ;;
302         *)
303                 help
304                 ;;
305         esac
306         ;;
307 *)
308         help
309         ;;
310 esac