1
|
#!/bin/sh
|
2
|
# functions.sh
|
3
|
# Library of functions which pc-sysinstall may call upon
|
4
|
|
5
|
# Function which displays the help-index file
|
6
|
display_help()
|
7
|
{
|
8
|
if [ -e "${PROGDIR}/doc/help-index" ]
|
9
|
then
|
10
|
cat ${PROGDIR}/doc/help-index
|
11
|
else
|
12
|
echo "Error: ${PROGDIR}/doc/help-index not found"
|
13
|
exit 1
|
14
|
fi
|
15
|
};
|
16
|
|
17
|
# Function which displays the help for a specified command
|
18
|
display_command_help()
|
19
|
{
|
20
|
if [ -z "$1" ]
|
21
|
then
|
22
|
echo "Error: No command specified to display help for"
|
23
|
exit 1
|
24
|
fi
|
25
|
|
26
|
if [ -e "${PROGDIR}/doc/help-${1}" ]
|
27
|
then
|
28
|
cat ${PROGDIR}/doc/help-${1}
|
29
|
else
|
30
|
echo "Error: ${PROGDIR}/doc/help-${1} not found"
|
31
|
exit 1
|
32
|
fi
|
33
|
};
|
34
|
|
35
|
# Function to convert bytes to megabytes
|
36
|
convert_byte_to_megabyte()
|
37
|
{
|
38
|
if [ -z "${1}" ]
|
39
|
then
|
40
|
echo "Error: No bytes specified!"
|
41
|
exit 1
|
42
|
fi
|
43
|
|
44
|
expr -e ${1} / 1048576
|
45
|
};
|
46
|
|
47
|
# Function to convert blocks to megabytes
|
48
|
convert_blocks_to_megabyte()
|
49
|
{
|
50
|
if [ -z "${1}" ] ; then
|
51
|
echo "Error: No blocks specified!"
|
52
|
exit 1
|
53
|
fi
|
54
|
|
55
|
expr -e ${1} / 2048
|
56
|
};
|
57
|
|
58
|
# Takes $1 and strips the whitespace out of it, returns VAL
|
59
|
strip_white_space()
|
60
|
{
|
61
|
if [ -z "${1}" ]
|
62
|
then
|
63
|
echo "Error: No value setup to strip whitespace from!"
|
64
|
|
65
|
exit 1
|
66
|
fi
|
67
|
|
68
|
VAL=`echo "$1" | tr -d ' '`
|
69
|
export VAL
|
70
|
};
|
71
|
|
72
|
# Displays an error message and exits with error 1
|
73
|
exit_err()
|
74
|
{
|
75
|
# Echo the message for the users benefit
|
76
|
echo "$1"
|
77
|
|
78
|
# Save this error to the log file
|
79
|
echo "${1}" >>$LOGOUT
|
80
|
|
81
|
# Check if we need to unmount any file-systems after this failure
|
82
|
unmount_all_filesystems_failure
|
83
|
|
84
|
echo "For more details see log file: $LOGOUT"
|
85
|
|
86
|
exit 1
|
87
|
};
|
88
|
|
89
|
# Run-command, don't halt if command exits with non-0
|
90
|
rc_nohalt()
|
91
|
{
|
92
|
CMD="$1"
|
93
|
|
94
|
if [ -z "${CMD}" ]
|
95
|
then
|
96
|
exit_err "Error: missing argument in rc_nohalt()"
|
97
|
fi
|
98
|
|
99
|
echo "Running: ${CMD}" >>${LOGOUT}
|
100
|
${CMD} >>${LOGOUT} 2>>${LOGOUT}
|
101
|
|
102
|
};
|
103
|
|
104
|
# Run-command, halt if command exits with non-0
|
105
|
rc_halt()
|
106
|
{
|
107
|
CMD="$1"
|
108
|
|
109
|
if [ -z "${CMD}" ]
|
110
|
then
|
111
|
exit_err "Error: missing argument in rc_halt()"
|
112
|
fi
|
113
|
|
114
|
echo "Running: ${CMD}" >>${LOGOUT}
|
115
|
${CMD} >>${LOGOUT} 2>>${LOGOUT}
|
116
|
STATUS="$?"
|
117
|
if [ "${STATUS}" != "0" ]
|
118
|
then
|
119
|
exit_err "Error ${STATUS}: ${CMD}"
|
120
|
fi
|
121
|
};
|
122
|
|
123
|
# Run-command w/echo to screen, halt if command exits with non-0
|
124
|
rc_halt_echo()
|
125
|
{
|
126
|
CMD="$1"
|
127
|
|
128
|
if [ -z "${CMD}" ]
|
129
|
then
|
130
|
exit_err "Error: missing argument in rc_halt_echo()"
|
131
|
fi
|
132
|
|
133
|
echo "Running: ${CMD}" >>${LOGOUT}
|
134
|
${CMD} 2>&1 | tee -a ${LOGOUT}
|
135
|
STATUS="$?"
|
136
|
if [ "$STATUS" != "0" ]
|
137
|
then
|
138
|
exit_err "Error ${STATUS}: $CMD"
|
139
|
fi
|
140
|
|
141
|
};
|
142
|
|
143
|
# Run-command w/echo, don't halt if command exits with non-0
|
144
|
rc_nohalt_echo()
|
145
|
{
|
146
|
CMD="$1"
|
147
|
|
148
|
if [ -z "${CMD}" ]
|
149
|
then
|
150
|
exit_err "Error: missing argument in rc_nohalt_echo()"
|
151
|
fi
|
152
|
|
153
|
echo "Running: ${CMD}" >>${LOGOUT}
|
154
|
${CMD} 2>&1 | tee -a ${LOGOUT}
|
155
|
|
156
|
};
|
157
|
|
158
|
# Echo to the screen and to the log
|
159
|
echo_log()
|
160
|
{
|
161
|
STR="$1"
|
162
|
|
163
|
if [ -z "${STR}" ]
|
164
|
then
|
165
|
exit_err "Error: missing argument in echo_log()"
|
166
|
fi
|
167
|
|
168
|
echo "${STR}" | tee -a ${LOGOUT}
|
169
|
};
|
170
|
|
171
|
# Function which uses "fetch" to download a file, and display a progress report
|
172
|
fetch_file()
|
173
|
{
|
174
|
|
175
|
FETCHFILE="$1"
|
176
|
FETCHOUTFILE="$2"
|
177
|
EXITFAILED="$3"
|
178
|
|
179
|
SIZEFILE="${TMPDIR}/.fetchSize"
|
180
|
EXITFILE="${TMPDIR}/.fetchExit"
|
181
|
|
182
|
rm ${SIZEFILE} 2>/dev/null >/dev/null
|
183
|
rm ${FETCHOUTFILE} 2>/dev/null >/dev/null
|
184
|
|
185
|
fetch -s "${FETCHFILE}" >${SIZEFILE}
|
186
|
SIZE="`cat ${SIZEFILE}`"
|
187
|
SIZE="`expr ${SIZE} / 1024`"
|
188
|
echo "FETCH: ${FETCHFILE}"
|
189
|
echo "FETCH: ${FETCHOUTFILE}" >>${LOGOUT}
|
190
|
|
191
|
( fetch -o ${FETCHOUTFILE} "${FETCHFILE}" >/dev/null 2>/dev/null ; echo "$?" > ${EXITFILE} ) &
|
192
|
PID="$!"
|
193
|
while
|
194
|
z=1
|
195
|
do
|
196
|
|
197
|
if [ -e "${FETCHOUTFILE}" ]
|
198
|
then
|
199
|
DSIZE=`du -k ${FETCHOUTFILE} | tr -d '\t' | cut -d '/' -f 1`
|
200
|
echo "SIZE: ${SIZE} DOWNLOADED: ${DSIZE}"
|
201
|
echo "SIZE: ${SIZE} DOWNLOADED: ${DSIZE}" >>${LOGOUT}
|
202
|
fi
|
203
|
|
204
|
# Check if the download is finished
|
205
|
ps -p ${PID} >/dev/null 2>/dev/null
|
206
|
if [ "$?" != "0" ]
|
207
|
then
|
208
|
break;
|
209
|
fi
|
210
|
|
211
|
sleep 2
|
212
|
done
|
213
|
|
214
|
echo "FETCHDONE"
|
215
|
|
216
|
EXIT="`cat ${EXITFILE}`"
|
217
|
if [ "${EXIT}" != "0" -a "$EXITFAILED" = "1" ]
|
218
|
then
|
219
|
exit_err "Error: Failed to download ${FETCHFILE}"
|
220
|
fi
|
221
|
|
222
|
return $EXIT
|
223
|
|
224
|
};
|
225
|
|
226
|
# Function to return a the zpool name for this device
|
227
|
get_zpool_name()
|
228
|
{
|
229
|
DEVICE="$1"
|
230
|
|
231
|
# Set the base name we use for zpools
|
232
|
BASENAME="tank"
|
233
|
|
234
|
if [ ! -d "${TMPDIR}/.zpools" ] ; then
|
235
|
mkdir -p ${TMPDIR}/.zpools
|
236
|
fi
|
237
|
|
238
|
if [ -e "${TMPDIR}/.zpools/${DEVICE}" ] ; then
|
239
|
cat ${TMPDIR}/.zpools/${DEVICE}
|
240
|
return 0
|
241
|
else
|
242
|
# Need to generate a zpool name for this device
|
243
|
NUM=`ls ${TMPDIR}/.zpools/ | wc -l | sed 's| ||g'`
|
244
|
NEWNAME="${BASENAME}${NUM}"
|
245
|
echo "$NEWNAME" >${TMPDIR}/.zpools/${DEVICE}
|
246
|
echo "${NEWNAME}"
|
247
|
return
|
248
|
fi
|
249
|
};
|