Switch from raw images to qcow2 format
[strongswan.git] / testing / scripts / function.sh
1 #!/bin/bash
2 # provides some general-purpose script functions
3 #
4 # Copyright (C) 2004 Eric Marchionni, Patrik Rayo
5 # Zuercher Hochschule Winterthur
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 2 of the License, or (at your
10 # option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 # for more details.
16
17 export TERM=xterm
18 RED=$(tput setaf 1)
19 GREEN=$(tput setaf 2)
20 NORMAL=$(tput op)
21
22 # exit with given error message
23 # $1 - error message
24 die() {
25 echo -e "${RED}$1${NORMAL}"
26 exit 1
27 }
28
29 [ -f testing.conf ] || die "Configuration file 'testing.conf' not found"
30 . testing.conf
31
32 # execute command
33 # $1 - command to execute
34 # $2 - whether or not to log command exit status
35 # (0 -> disable exit status logging)
36 execute()
37 {
38 cmd=${1}
39 echo $cmd >>$LOGFILE 2>&1
40 $cmd >>$LOGFILE 2>&1
41 status=$?
42 [ "$2" != 0 ] && log_status $status
43 if [ $status != 0 ]; then
44 echo
45 echo "! Command $cmd failed, exiting (status $status)"
46 echo "! Check why here $LOGFILE"
47 exit 1
48 fi
49 }
50
51 # execute command in chroot
52 # $1 - command to execute
53 execute_chroot()
54 {
55 execute "chroot $LOOPDIR $@"
56 }
57
58 # write green status message to console
59 # $1 - msg
60 echo_ok()
61 {
62 echo -e "${GREEN}$1${NORMAL}"
63 }
64
65 # write red status message to console
66 # $1 - msg
67 echo_failed()
68 {
69 echo -e "${RED}$1${NORMAL}"
70 }
71
72 # log an action
73 # $1 - current action description
74 log_action()
75 {
76 /bin/echo -n "[....] $1 "
77 }
78
79 # log an action status
80 # $1 - exit status of action
81 log_status()
82 {
83 tput hpa 0
84 if [ $1 -eq 0 ]; then
85 /bin/echo -ne "[${GREEN} ok ${NORMAL}"
86 else
87 /bin/echo -ne "[${RED}FAIL${NORMAL}"
88 fi
89 echo
90 }
91
92 # the following two functions are stolen from [1]
93 # [1] - http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
94
95 declare -a on_exit_items
96
97 # perform registered actions on exit
98 on_exit()
99 {
100 for i in "${on_exit_items[@]}"
101 do
102 eval $i >>$LOGFILE 2>&1
103 done
104 on_exit_items=""
105 trap - EXIT
106 }
107
108 # register a command to execute when the calling script terminates. The
109 # registered commands are called in FIFO order.
110 # $* - command to register
111 do_on_exit()
112 {
113 local n=${#on_exit_items[*]}
114 on_exit_items[$n]="$*"
115 if [ $n -eq 0 ]; then
116 trap on_exit EXIT
117 fi
118 }
119
120 # wait for a mount to disappear
121 # $1 - device/image to wait for
122 # $2 - maximum time to wait in seconds, default is 5 seconds
123 graceful_umount()
124 {
125 secs=$2
126 [ ! $secs ] && secs=5
127
128 let steps=$secs*100
129 for i in `seq 1 $steps`
130 do
131 umount $1 >>$LOGFILE 2>&1
132 mount | grep $1 >/dev/null 2>&1
133 [ $? -eq 0 ] || return 0
134 sleep 0.01
135 done
136
137 return 1
138 }
139
140 # load qemu NBD kernel module, if not already loaded
141 load_qemu_nbd()
142 {
143 lsmod | grep ^nbd[[:space:]]* >/dev/null 2>&1
144 if [ $? != 0 ]
145 then
146 log_action "Loading NBD kernel module"
147 execute "modprobe nbd max_part=16"
148 fi
149 }
150
151 #############################################
152 # search and replace strings throughout a
153 # whole directory
154 #
155
156 function searchandreplace {
157
158 SEARCHSTRING="$1"
159 REPLACESTRING="$2"
160 DESTDIR="$3"
161
162 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
163
164
165 ###########################################
166 # search and replace in each found file the
167 # given string
168 #
169
170 for eachfoundfile in `find $DESTDIR -type f`
171 do
172 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
173 done
174
175 }
176
177 #############################################
178 # add a bridge
179 #
180
181 function umlbr_add {
182 brctl addbr "umlbr$1"
183 brctl setfd "umlbr$1" 0
184 brctl setageing "umlbr$1" 3600
185 brctl stp "umlbr$1" off
186 ifconfig "umlbr$1" "$2" netmask "$3" up
187 }
188
189 #############################################
190 # delete a bridge
191 #
192
193 function umlbr_del {
194 ifconfig "umlbr$1" down &> /dev/null 2>&1
195 brctl delbr "umlbr$1" &> /dev/null 2>&1
196 }
197
198 #############################################
199 # add a tap interface to a bridge
200 #
201
202 function umlbr_add_tap {
203 tunctl -t "tap$1_$2" &> /dev/null 2>&1
204 ifconfig "tap$1_$2" 0.0.0.0 promisc up &> /dev/null 2>&1
205 brctl addif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
206 cecho-n "$2.."
207 }
208
209 #############################################
210 # delete a tap interface from a bridge
211 #
212
213 function umlbr_del_tap {
214 ifconfig "umlbr$2" down &> /dev/null 2>&1
215 brctl delif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
216 tunctl -d "tap$1_$2" &> /dev/null 2>&1
217 cecho-n "$2.."
218 }
219