Fix an issue with parted on partitions without filesystems
[bootimage-initfs.git] / etc / library.sh
1 #!/bin/ash
2
3 eval `stty size 2>/dev/null | (read L C; echo LINES=${L:-24} COLUMNS=${C:-80})`
4
5 PRODUCT_NAME="Bootimage"
6 PRODUCT_VERSION="0.05"
7
8
9 ANSI_GREEN="\033[1;32m"
10 ANSI_RED="\033[1;31m"
11 ANSI_BLUE="\033[1;34m"
12 ANSI_PURPLE="\033[1;35m"
13 ANSI_LEFT="\033[${COLUMNS}G\033[10D"
14 ANSI_DONE="\033[0m"
15
16 PRINT_OK=`echo -n ${ANSI_LEFT}${ANSI_GREEN} [ OK ] ${ANSI_DONE}`
17 PRINT_FAIL=`echo -n ${ANSI_LEFT}${ANSI_RED} [ FAIL ] ${ANSI_DONE}`
18
19 if [ -e /tmp/servers.conf ]
20 then
21         . /tmp/servers.conf
22 fi
23
24 if [ -e /tmp/hardware.conf ]
25 then
26         . /tmp/hardware.conf
27 fi
28
29 if [ -e /tmp/breakin.conf ]
30 then
31         . /tmp/breakin.conf
32 fi
33
34 if [ -e /tmp/network.dhcp ]
35 then
36         . /tmp/network.dhcp
37 fi
38
39
40 msg() {
41         ARG1=$1
42         ARG2=$2
43
44         if [ "$ARG1" = "-n" ]
45         then
46                 echo -en ${ARG2} 
47         else 
48                 echo -e ${ARG1} 
49         fi
50 }
51
52 fail_msg() {
53         MSG=$1
54
55         # echo wget a error message to the server
56         msg "  ${PRINT_FAIL}"
57 }
58
59 ok_msg() {
60         MSG=$1
61
62         # echo wget a error message to the server
63         msg "  ${PRINT_OK}"
64 }
65
66 ok_or_fail() {
67         RETURN=$1
68
69         if [ "$RETURN" = "0" ]
70         then
71                 ok_msg
72         else
73                 fail_msg
74         fi
75 }
76
77 header() {
78         MSG=$1
79
80         msg -n "${ANSI_GREEN}"
81         msg "================================="
82         msg -n "${ANSI_BLUE}"
83         msg " ${MSG}"
84         msg -n "${ANSI_GREEN}"
85         msg "================================="
86         msg -n ${ANSI_DONE}
87
88 }
89
90 fatal_error() {
91         MSG=$1
92
93         msg ""
94         msg -n "${ANSI_RED}"
95         msg "========================================================"
96         msg "           Fatal Error - halting ${PRODUCT_NAME} ${PRODUCT_VERSION}"
97         msg "========================================================"
98         msg -n "${ANSI_BLUE}"
99         msg " ${MSG}"
100         msg -n "${ANSI_RED}"
101         msg "========================================================"
102         msg -n ${ANSI_DONE}
103         msg ""
104         exit 1
105 }
106
107 modprobe_module() {
108
109         MODULE_NAME=${1}
110
111         msg -n "Trying to load ${ANSI_BLUE}${MODULE_NAME}${ANSI_DONE}"
112         /sbin/modprobe "${MODULE_NAME}"  >> /tmp/stdout.log 2>> /tmp/stderr.log
113         if [ "$?" = "1" ];
114         then
115                 fail_msg
116                 return 1
117         else
118                 ok_msg
119                 return 0
120         fi
121 }
122
123 load_module() {
124
125         MODULE_PATH=${1}
126         MODULE_NAME=`basename ${MODULE_PATH}`
127         MODULE_NAME=${MODULE_NAME%.ko}
128
129         msg -n "Trying to load ${MODULE_NAME}"
130         /sbin/modprobe "${MODULE_NAME}"  >> /tmp/stdout.log 2>> /tmp/stderr.log
131         if [ "$?" = "1" ];
132         then
133                 fail_msg
134                 return 1
135         else
136                 ok_msg
137                 return 0
138         fi
139 }
140
141 post_file_to_server() {
142
143         MSG=${1}
144         URIPATH=${2}
145         FILENAME=${3}
146
147         if [ "${BREAKIN_ID}" -lt 0 ]
148         then
149                 return 1
150         fi
151
152         msg -n "$MSG -> ${HTTP_SERVER}:${HTTP_PORT}"
153
154         URL="http://${HTTP_SERVER}:${HTTP_PORT}/${URIPATH}/${NET_MACADDR}/${BREAKIN_ID}"
155
156         wget --server-response -O /tmp/post.out --post-file=${FILENAME} --timeout=10 \
157                 --tries=3 ${URL} >> /tmp/stdout.log 2>> /tmp/stderr.log
158
159         if [ "$?" = 0 ]
160         then
161                 ok_msg  
162                 return 0
163         else
164                 fail_msg
165                 return 1
166         fi
167 }
168
169 breakin_start() {
170
171         msg -n "Notifing server that we've started the breakin process"
172         URL="http://${HTTP_SERVER}:${HTTP_PORT}/cgi-bin/breakin/start/${NET_MACADDR}"
173
174         wget --server-response -O /tmp/post.out --timeout=10 \
175                 --output-document=/tmp/breakin.conf \
176                 --tries=3 ${URL} 2>> /tmp/stderr.log
177
178         . /tmp/breakin.conf
179
180         if [ "$?" = 0 ]
181         then
182                 ok_msg  
183                 return 0
184         else
185                 fail_msg
186                 return 1
187         fi
188 }
189
190 breakin_stop() {
191
192         msg -n "Notifing server that we're stoping the breakin process"
193         URL="http://${HTTP_SERVER}:${HTTP_PORT}/cgi-bin/breakin/stop/${NET_MACADDR}/${BREAKIN_ID}"
194
195         wget --server-response -O /tmp/post.out --timeout=10 \
196                 --tries=3 ${URL} >> /tmp/stdout.log 2>> /tmp/stderr.log
197
198         if [ "$?" = 0 ]
199         then
200                 ok_msg  
201                 return 0
202         else
203                 fail_msg
204                 return 1
205         fi
206 }
207
208 breakin_update() {
209
210         if [ "${BREAKIN_ID}" -lt 0 ]
211         then
212                 return 0
213         fi
214
215         LOADAVG=0
216         TEMPS=""
217
218         if [ -e /proc/loadavg ]
219         then
220                 LOADAVG=`cat /proc/loadavg | awk '{print $1}'`
221         fi
222
223         if [ "${SENSOR_ENABLE}" != "" ]
224         then
225                 TEMPS=""
226                 if [ -e "/etc/sensors.conf" ]
227                 then
228                         for temp_i in `sensors | grep "^CPU[0-9] Temp" | cut -d":" -f2 | awk '{print $1 $2}' 2> /dev/null`
229                         do
230                                 TEMPS="${TEMPS} ${temp_i}"      
231                         done
232                 fi
233         fi
234
235         echo "LOADAVG=${LOADAVG}" > /tmp/update.dat
236         echo "TEMPERATURE=${TEMPS}" >> /tmp/update.dat
237
238         URL="http://${HTTP_SERVER}:${HTTP_PORT}/cgi-bin/breakin/update/${NET_MACADDR}/${BREAKIN_ID}"
239         wget --server-response -O /tmp/post.out --post-file=/tmp/update.dat --timeout=10 \
240                 --tries=3 ${URL} >> /tmp/stdout.log 2>> /tmp/stderr.log
241
242         if [ "$?" = 0 ]
243         then
244                 return 0
245         else
246                 return 1
247         fi
248 }
249
250
251
252
253
254 log_burnin_error() {
255         MSG=$1
256
257         # create a file to notify the LED process there is an error
258         touch /tmp/error_led
259         echo "${MSG}" >> /var/log/breakin.log
260 }