1
|
#!/bin/sh
|
2
|
#-
|
3
|
# Copyright (c) 2010 iXsystems, Inc. All rights reserved.
|
4
|
#
|
5
|
# Redistribution and use in source and binary forms, with or without
|
6
|
# modification, are permitted provided that the following conditions
|
7
|
# are met:
|
8
|
# 1. Redistributions of source code must retain the above copyright
|
9
|
# notice, this list of conditions and the following disclaimer.
|
10
|
# 2. Redistributions in binary form must reproduce the above copyright
|
11
|
# notice, this list of conditions and the following disclaimer in the
|
12
|
# documentation and/or other materials provided with the distribution.
|
13
|
#
|
14
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
15
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
18
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20
|
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21
|
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23
|
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24
|
# SUCH DAMAGE.
|
25
|
#
|
26
|
# $FreeBSD: src/usr.sbin/pc-sysinstall/backend/functions.sh,v 1.2 2010/06/27 16:46:11 imp Exp $
|
27
|
|
28
|
# functions.sh
|
29
|
# Library of functions which pc-sysinstall may call upon
|
30
|
|
31
|
# Function which displays the help-index file
|
32
|
display_help()
|
33
|
{
|
34
|
if [ -e "${PROGDIR}/doc/help-index" ]
|
35
|
then
|
36
|
cat ${PROGDIR}/doc/help-index
|
37
|
else
|
38
|
echo "Error: ${PROGDIR}/doc/help-index not found"
|
39
|
exit 1
|
40
|
fi
|
41
|
};
|
42
|
|
43
|
# Function which displays the help for a specified command
|
44
|
display_command_help()
|
45
|
{
|
46
|
if [ -z "$1" ]
|
47
|
then
|
48
|
echo "Error: No command specified to display help for"
|
49
|
exit 1
|
50
|
fi
|
51
|
|
52
|
if [ -e "${PROGDIR}/doc/help-${1}" ]
|
53
|
then
|
54
|
cat ${PROGDIR}/doc/help-${1}
|
55
|
else
|
56
|
echo "Error: ${PROGDIR}/doc/help-${1} not found"
|
57
|
exit 1
|
58
|
fi
|
59
|
};
|
60
|
|
61
|
# Function to convert bytes to megabytes
|
62
|
convert_byte_to_megabyte()
|
63
|
{
|
64
|
if [ -z "${1}" ]
|
65
|
then
|
66
|
echo "Error: No bytes specified!"
|
67
|
exit 1
|
68
|
fi
|
69
|
|
70
|
expr -e ${1} / 1048576
|
71
|
};
|
72
|
|
73
|
# Function to convert blocks to megabytes
|
74
|
convert_blocks_to_megabyte()
|
75
|
{
|
76
|
if [ -z "${1}" ] ; then
|
77
|
echo "Error: No blocks specified!"
|
78
|
exit 1
|
79
|
fi
|
80
|
|
81
|
expr -e ${1} / 2048
|
82
|
};
|
83
|
|
84
|
# Takes $1 and strips the whitespace out of it, returns VAL
|
85
|
strip_white_space()
|
86
|
{
|
87
|
if [ -z "${1}" ]
|
88
|
then
|
89
|
echo "Error: No value setup to strip whitespace from!"
|
90
|
|
91
|
exit 1
|
92
|
fi
|
93
|
|
94
|
VAL=`echo "$1" | tr -d ' '`
|
95
|
export VAL
|
96
|
};
|
97
|
|
98
|
# Displays an error message and exits with error 1
|
99
|
exit_err()
|
100
|
{
|
101
|
# Echo the message for the users benefit
|
102
|
echo "$1"
|
103
|
|
104
|
# Save this error to the log file
|
105
|
echo "${1}" >>$LOGOUT
|
106
|
|
107
|
# Check if we need to unmount any file-systems after this failure
|
108
|
unmount_all_filesystems_failure
|
109
|
|
110
|
echo "For more details see log file: $LOGOUT"
|
111
|
|
112
|
exit 1
|
113
|
};
|
114
|
|
115
|
# Run-command, don't halt if command exits with non-0
|
116
|
rc_nohalt()
|
117
|
{
|
118
|
CMD="$1"
|
119
|
|
120
|
if [ -z "${CMD}" ]
|
121
|
then
|
122
|
exit_err "Error: missing argument in rc_nohalt()"
|
123
|
fi
|
124
|
|
125
|
echo "Running: ${CMD}" >>${LOGOUT}
|
126
|
${CMD} >>${LOGOUT} 2>>${LOGOUT}
|
127
|
|
128
|
};
|
129
|
|
130
|
# Run-command, halt if command exits with non-0
|
131
|
rc_halt()
|
132
|
{
|
133
|
CMD="$1"
|
134
|
|
135
|
if [ -z "${CMD}" ]
|
136
|
then
|
137
|
exit_err "Error: missing argument in rc_halt()"
|
138
|
fi
|
139
|
|
140
|
echo "Running: ${CMD}" >>${LOGOUT}
|
141
|
${CMD} >>${LOGOUT} 2>>${LOGOUT}
|
142
|
STATUS="$?"
|
143
|
if [ "${STATUS}" != "0" ]
|
144
|
then
|
145
|
exit_err "Error ${STATUS}: ${CMD}"
|
146
|
fi
|
147
|
};
|
148
|
|
149
|
# Run-command w/echo to screen, halt if command exits with non-0
|
150
|
rc_halt_echo()
|
151
|
{
|
152
|
CMD="$1"
|
153
|
|
154
|
if [ -z "${CMD}" ]
|
155
|
then
|
156
|
exit_err "Error: missing argument in rc_halt_echo()"
|
157
|
fi
|
158
|
|
159
|
echo "Running: ${CMD}" >>${LOGOUT}
|
160
|
${CMD} 2>&1 | tee -a ${LOGOUT}
|
161
|
STATUS="$?"
|
162
|
if [ "$STATUS" != "0" ]
|
163
|
then
|
164
|
exit_err "Error ${STATUS}: $CMD"
|
165
|
fi
|
166
|
|
167
|
};
|
168
|
|
169
|
# Run-command w/echo, don't halt if command exits with non-0
|
170
|
rc_nohalt_echo()
|
171
|
{
|
172
|
CMD="$1"
|
173
|
|
174
|
if [ -z "${CMD}" ]
|
175
|
then
|
176
|
exit_err "Error: missing argument in rc_nohalt_echo()"
|
177
|
fi
|
178
|
|
179
|
echo "Running: ${CMD}" >>${LOGOUT}
|
180
|
${CMD} 2>&1 | tee -a ${LOGOUT}
|
181
|
|
182
|
};
|
183
|
|
184
|
# Echo to the screen and to the log
|
185
|
echo_log()
|
186
|
{
|
187
|
STR="$1"
|
188
|
|
189
|
if [ -z "${STR}" ]
|
190
|
then
|
191
|
exit_err "Error: missing argument in echo_log()"
|
192
|
fi
|
193
|
|
194
|
echo "${STR}" | tee -a ${LOGOUT}
|
195
|
};
|
196
|
|
197
|
# Make sure we have a numeric
|
198
|
is_num() {
|
199
|
expr $1 + 1 2>/dev/null
|
200
|
return $?
|
201
|
}
|
202
|
|
203
|
# Function which uses "fetch" to download a file, and display a progress report
|
204
|
fetch_file()
|
205
|
{
|
206
|
|
207
|
FETCHFILE="$1"
|
208
|
FETCHOUTFILE="$2"
|
209
|
EXITFAILED="$3"
|
210
|
|
211
|
SIZEFILE="${TMPDIR}/.fetchSize"
|
212
|
EXITFILE="${TMPDIR}/.fetchExit"
|
213
|
|
214
|
rm ${SIZEFILE} 2>/dev/null >/dev/null
|
215
|
rm ${FETCHOUTFILE} 2>/dev/null >/dev/null
|
216
|
|
217
|
fetch -s "${FETCHFILE}" >${SIZEFILE}
|
218
|
SIZE="`cat ${SIZEFILE}`"
|
219
|
SIZE="`expr ${SIZE} / 1024`"
|
220
|
echo "FETCH: ${FETCHFILE}"
|
221
|
echo "FETCH: ${FETCHOUTFILE}" >>${LOGOUT}
|
222
|
|
223
|
( fetch -o ${FETCHOUTFILE} "${FETCHFILE}" >/dev/null 2>/dev/null ; echo "$?" > ${EXITFILE} ) &
|
224
|
PID="$!"
|
225
|
while
|
226
|
z=1
|
227
|
do
|
228
|
|
229
|
if [ -e "${FETCHOUTFILE}" ]
|
230
|
then
|
231
|
DSIZE=`du -k ${FETCHOUTFILE} | tr -d '\t' | cut -d '/' -f 1`
|
232
|
if [ $(is_num "$DSIZE") ] ; then
|
233
|
if [ $SIZE -lt $DSIZE ] ; then DSIZE="$SIZE"; fi
|
234
|
echo "SIZE: ${SIZE} DOWNLOADED: ${DSIZE}"
|
235
|
echo "SIZE: ${SIZE} DOWNLOADED: ${DSIZE}" >>${LOGOUT}
|
236
|
fi
|
237
|
fi
|
238
|
|
239
|
# Check if the download is finished
|
240
|
ps -p ${PID} >/dev/null 2>/dev/null
|
241
|
if [ "$?" != "0" ]
|
242
|
then
|
243
|
break;
|
244
|
fi
|
245
|
|
246
|
sleep 2
|
247
|
done
|
248
|
|
249
|
echo "FETCHDONE"
|
250
|
|
251
|
EXIT="`cat ${EXITFILE}`"
|
252
|
if [ "${EXIT}" != "0" -a "$EXITFAILED" = "1" ]
|
253
|
then
|
254
|
exit_err "Error: Failed to download ${FETCHFILE}"
|
255
|
fi
|
256
|
|
257
|
return $EXIT
|
258
|
|
259
|
};
|
260
|
|
261
|
# Function to return a the zpool name for this device
|
262
|
get_zpool_name()
|
263
|
{
|
264
|
DEVICE="$1"
|
265
|
|
266
|
# Set the base name we use for zpools
|
267
|
BASENAME="tank"
|
268
|
|
269
|
if [ ! -d "${TMPDIR}/.zpools" ] ; then
|
270
|
mkdir -p ${TMPDIR}/.zpools
|
271
|
fi
|
272
|
|
273
|
if [ -e "${TMPDIR}/.zpools/${DEVICE}" ] ; then
|
274
|
cat ${TMPDIR}/.zpools/${DEVICE}
|
275
|
return 0
|
276
|
else
|
277
|
# Need to generate a zpool name for this device
|
278
|
NUM=`ls ${TMPDIR}/.zpools/ | wc -l | sed 's| ||g'`
|
279
|
NEWNAME="${BASENAME}${NUM}"
|
280
|
echo "$NEWNAME" >${TMPDIR}/.zpools/${DEVICE}
|
281
|
echo "${NEWNAME}"
|
282
|
return
|
283
|
fi
|
284
|
};
|