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