1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
#!/bin/sh
#-
# Copyright (c) 2010 iXsystems, Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
# Functions which perform the extraction / installation of system to disk
. ${BACKEND}/functions-mountoptical.sh
# Performs the extraction of data to disk from FreeBSD dist files
start_extract_dist()
{
if [ -z "$1" ] ; then exit_err "Called dist extraction with no directory set!"; fi
if [ -z "$INSFILE" ]; then exit_err "Called extraction with no install file set!"; fi
local DDIR="$1"
# Check if we are doing an upgrade, and if so use our exclude list
if [ "${INSTALLMODE}" = "upgrade" ]; then
TAROPTS="-X ${PROGDIR}/conf/exclude-from-upgrade"
else
TAROPTS=""
fi
# Loop though and extract dist files
for di in $INSFILE
do
# Check the MANIFEST see if we have an archive size / count
if [ -e "${DDIR}/MANIFEST" ]; then
count=`grep "^${di}.txz" ${DDIR}/MANIFEST | awk '{print $3}'`
if [ ! -z "$count" ] ; then
echo "INSTALLCOUNT: $count"
fi
fi
echo_log "pc-sysinstall: Starting Extraction (${di})"
tar -xpv -C ${FSMNT} -f ${DDIR}/${di}.txz ${TAROPTS} >&1 2>&1
if [ $? -ne 0 ]; then
exit_err "ERROR: Failed extracting the dist file: $di"
fi
done
# Check if this was a FTP download and clean it up now
if [ "${INSTALLMEDIUM}" = "ftp" ]; then
echo_log "Cleaning up downloaded archives"
rm -rf ${DDIR}
fi
echo_log "pc-sysinstall: Extraction Finished"
}
# Performs the extraction of data to disk from a uzip or tar archive
start_extract_uzip_tar()
{
if [ -z "$INSFILE" ]; then
exit_err "ERROR: Called extraction with no install file set!"
fi
# Check if we have a .count file, and echo it out for a front-end to use in progress bars
if [ -e "${INSFILE}.count" ]; then
echo "INSTALLCOUNT: `cat ${INSFILE}.count`"
fi
# Check if we are doing an upgrade, and if so use our exclude list
if [ "${INSTALLMODE}" = "upgrade" ]; then
TAROPTS="-X ${PROGDIR}/conf/exclude-from-upgrade"
else
TAROPTS=""
fi
echo_log "pc-sysinstall: Starting Extraction"
case ${PACKAGETYPE} in
uzip)
if ! kldstat -v | grep -q "geom_uzip" ; then
exit_err "Kernel module geom_uzip not loaded"
fi
# Start by mounting the uzip image
MDDEVICE=`mdconfig -a -t vnode -o readonly -f ${INSFILE}`
mkdir -p ${FSMNT}.uzip
mount -r /dev/${MDDEVICE}.uzip ${FSMNT}.uzip
if [ $? -ne 0 ]
then
exit_err "ERROR: Failed mounting the ${INSFILE}"
fi
cd ${FSMNT}.uzip
# Copy over all the files now!
tar cvf - . 2>/dev/null | tar -xpv -C ${FSMNT} ${TAROPTS} -f - 2>&1 | tee -a ${FSMNT}/.tar-extract.log
if [ $? -ne 0 ]
then
cd /
echo "TAR failure occurred:" >>${LOGOUT}
cat ${FSMNT}/.tar-extract.log | grep "tar:" >>${LOGOUT}
umount ${FSMNT}.uzip
mdconfig -d -u ${MDDEVICE}
exit_err "ERROR: Failed extracting the tar image"
fi
# All finished, now lets umount and cleanup
cd /
umount ${FSMNT}.uzip
mdconfig -d -u ${MDDEVICE}
;;
tar)
tar -xpv -C ${FSMNT} -f ${INSFILE} ${TAROPTS} >&1 2>&1
if [ $? -ne 0 ]; then
exit_err "ERROR: Failed extracting the tar image"
fi
;;
esac
# Check if this was a FTP download and clean it up now
if [ "${INSTALLMEDIUM}" = "ftp" ]
then
echo_log "Cleaning up downloaded archive"
rm ${INSFILE}
rm ${INSFILE}.count >/dev/null 2>/dev/null
rm ${INSFILE}.md5 >/dev/null 2>/dev/null
fi
echo_log "pc-sysinstall: Extraction Finished"
};
# Performs the extraction of data to disk from a directory with split files
start_extract_split()
{
if [ -z "${INSDIR}" ]
then
exit_err "ERROR: Called extraction with no install directory set!"
fi
echo_log "pc-sysinstall: Starting Extraction"
# Used by install.sh
DESTDIR="${FSMNT}"
export DESTDIR
HERE=`pwd`
DIRS=`ls -d ${INSDIR}/*|grep -Ev '(uzip|kernels|src)'`
for dir in ${DIRS}
do
cd "${dir}"
if [ -f "install.sh" ]
then
echo_log "Extracting" `basename ${dir}`
echo "y" | sh install.sh >/dev/null
if [ $? -ne 0 ]
then
exit_err "ERROR: Failed extracting ${dir}"
fi
else
exit_err "ERROR: ${dir}/install.sh does not exist"
fi
done
cd "${HERE}"
KERNELS=`ls -d ${INSDIR}/*|grep kernels`
cd "${KERNELS}"
if [ -f "install.sh" ]
then
echo_log "Extracting" `basename ${KERNELS}`
echo "y" | sh install.sh generic >/dev/null
if [ $? -ne 0 ]
then
exit_err "ERROR: Failed extracting ${KERNELS}"
fi
rm -rf "${FSMNT}/boot/kernel"
mv "${FSMNT}/boot/GENERIC" "${FSMNT}/boot/kernel"
else
exit_err "ERROR: ${KERNELS}/install.sh does not exist"
fi
cd "${HERE}"
SOURCE=`ls -d ${INSDIR}/*|grep src`
cd "${SOURCE}"
if [ -f "install.sh" ]
then
echo_log "Extracting" `basename ${SOURCE}`
echo "y" | sh install.sh all >/dev/null
if [ $? -ne 0 ]
then
exit_err "ERROR: Failed extracting ${SOURCE}"
fi
else
exit_err "ERROR: ${SOURCE}/install.sh does not exist"
fi
cd "${HERE}"
echo_log "pc-sysinstall: Extraction Finished"
};
# Function which will attempt to fetch the dist file(s) before we start
fetch_dist_file()
{
get_value_from_cfg ftpPath
if [ -z "$VAL" ]
then
exit_err "ERROR: Install medium was set to ftp, but no ftpPath was provided!"
fi
FTPPATH="${VAL}"
# Check if we have a /usr partition to save the download
if [ -d "${FSMNT}/usr" ]
then
DLDIR="${FSMNT}/usr/.fetch.$$"
else
DLDIR="${FSMNT}/.fetch.$$"
fi
mkdir -p ${DLDIR}
# Do the fetch of the dist archive(s) now
for di in $INSFILE
do
fetch_file "${FTPPATH}/${di}.txz" "${DLDIR}/${di}.txz" "1"
done
# Check to see if there is a MANIFEST file for this install
fetch_file "${FTPPATH}/MANIFEST" "${DLDIR}/MANIFEST" "0"
export DLDIR
};
# Function which will attempt to fetch the install file before we start
# the install
fetch_install_file()
{
get_value_from_cfg ftpPath
if [ -z "$VAL" ]
then
exit_err "ERROR: Install medium was set to ftp, but no ftpPath was provided!"
fi
FTPPATH="${VAL}"
# Check if we have a /usr partition to save the download
if [ -d "${FSMNT}/usr" ]
then
OUTFILE="${FSMNT}/usr/.fetch-${INSFILE}"
else
OUTFILE="${FSMNT}/.fetch-${INSFILE}"
fi
# Do the fetch of the archive now
fetch_file "${FTPPATH}/${INSFILE}" "${OUTFILE}" "1"
# Check to see if there is a .count file for this install
fetch_file "${FTPPATH}/${INSFILE}.count" "${OUTFILE}.count" "0"
# Check to see if there is a .md5 file for this install
fetch_file "${FTPPATH}/${INSFILE}.md5" "${OUTFILE}.md5" "0"
# Done fetching, now reset the INSFILE to our downloaded archived
export INSFILE="${OUTFILE}"
};
# Function which will download freebsd install files
fetch_split_files()
{
get_ftpHost
if [ -z "$VAL" ]
then
exit_err "ERROR: Install medium was set to ftp, but no ftpHost was provided!"
fi
FTPHOST="${VAL}"
get_ftpDir
if [ -z "$VAL" ]
then
exit_err "ERROR: Install medium was set to ftp, but no ftpDir was provided!"
fi
FTPDIR="${VAL}"
# Check if we have a /usr partition to save the download
if [ -d "${FSMNT}/usr" ]
then
OUTFILE="${FSMNT}/usr/.fetch-${INSFILE}"
else
OUTFILE="${FSMNT}/.fetch-${INSFILE}"
fi
DIRS="base catpages dict doc info manpages proflibs kernels src"
if [ "${FBSD_ARCH}" = "amd64" ]
then
DIRS="${DIRS} lib32"
fi
for d in ${DIRS}
do
mkdir -p "${OUTFILE}/${d}"
done
NETRC="${OUTFILE}/.netrc"
cat <<EOF >"${NETRC}"
machine ${FTPHOST}
login anonymous
password anonymous
macdef INSTALL
bin
prompt
EOF
for d in ${DIRS}
do
cat <<EOF >>"${NETRC}"
cd ${FTPDIR}/${d}
lcd ${OUTFILE}/${d}
mreget *
EOF
done
cat <<EOF >>"${NETRC}"
bye
EOF
# Fetch the files via ftp
echo "$ INSTALL" | ftp -N "${NETRC}" "${FTPHOST}"
# Done fetching, now reset the INSFILE to our downloaded archived
export INSFILE="${OUTFILE}"
}
# Function which does the rsync download from the server specified in cfg
start_rsync_copy()
{
# Load our rsync config values
get_value_from_cfg rsyncPath
if [ -z "${VAL}" ]; then
exit_err "ERROR: rsyncPath is unset! Please check your config and try again."
fi
export RSYNCPATH="${VAL}"
get_value_from_cfg rsyncHost
if [ -z "${VAL}" ]; then
exit_err "ERROR: rsyncHost is unset! Please check your config and try again."
fi
export RSYNCHOST="${VAL}"
get_value_from_cfg rsyncUser
if [ -z "${VAL}" ]; then
exit_err "ERROR: rsyncUser is unset! Please check your config and try again."
fi
export RSYNCUSER="${VAL}"
get_value_from_cfg rsyncPort
if [ -z "${VAL}" ]; then
exit_err "ERROR: rsyncPort is unset! Please check your config and try again."
fi
export RSYNCPORT="${VAL}"
COUNT=1
while
z=1
do
if [ ${COUNT} -gt ${RSYNCTRIES} ]
then
exit_err "ERROR: Failed rsync command!"
break
fi
rsync -avvzHsR \
--rsync-path="rsync --fake-super" \
-e "ssh -p ${RSYNCPORT}" \
${RSYNCUSER}@${RSYNCHOST}:${RSYNCPATH}/./ ${FSMNT}
if [ $? -ne 0 ]
then
echo "Rsync failed! Tries: ${COUNT}"
else
break
fi
COUNT=$((COUNT+1))
done
};
start_image_install()
{
if [ -z "${IMAGE_FILE}" ]
then
exit_err "ERROR: installMedium set to image but no image file specified!"
fi
# We are ready to start mounting, lets read the config and do it
while read line
do
echo $line | grep -q "^disk0=" 2>/dev/null
if [ $? -eq 0 ]
then
# Found a disk= entry, lets get the disk we are working on
get_value_from_string "${line}"
strip_white_space "$VAL"
DISK="$VAL"
fi
echo $line | grep -q "^commitDiskPart" 2>/dev/null
if [ $? -eq 0 ]
then
# Found our flag to commit this disk setup / lets do sanity check and do it
if [ -n "${DISK}" ]
then
# Write the image
write_image "${IMAGE_FILE}" "${DISK}"
# Increment our disk counter to look for next disk and unset
unset DISK
break
else
exit_err "ERROR: commitDiskPart was called without procceding disk<num>= and partition= entries!!!"
fi
fi
done <${CFGF}
};
# Entrance function, which starts the installation process
init_extraction()
{
# Figure out what file we are using to install from via the config
get_value_from_cfg installFile
if [ -n "${VAL}" ]
then
export INSFILE="${VAL}"
else
# If no installFile specified, try our defaults
if [ "$INSTALLTYPE" = "FreeBSD" ]
then
case $PACKAGETYPE in
uzip) INSFILE="${FBSD_UZIP_FILE}" ;;
tar) INSFILE="${FBSD_TAR_FILE}" ;;
dist)
get_value_from_cfg_with_spaces distFiles
if [ -z "$VAL" ] ; then
exit_err "No dist files specified!"
fi
INSFILE="${VAL}"
;;
split)
INSDIR="${FBSD_BRANCH_DIR}"
# This is to trick opt_mount into not failing
INSFILE="${INSDIR}"
;;
esac
else
case $PACKAGETYPE in
uzip) INSFILE="${UZIP_FILE}" ;;
tar) INSFILE="${TAR_FILE}" ;;
dist)
get_value_from_cfg_with_spaces distFiles
if [ -z "$VAL" ] ; then
exit_err "No dist files specified!"
fi
INSFILE="${VAL}"
;;
esac
fi
export INSFILE
fi
# Lets start by figuring out what medium we are using
case ${INSTALLMEDIUM} in
dvd|usb)
# Lets start by mounting the disk
opt_mount
if [ -n "${INSDIR}" ]
then
INSDIR="${CDMNT}/${INSDIR}" ; export INSDIR
start_extract_split
else
if [ "$PACKAGETYPE" = "dist" ] ; then
start_extract_dist "${CDMNT}/usr/freebsd-dist"
else
INSFILE="${CDMNT}/${INSFILE}" ; export INSFILE
start_extract_uzip_tar
fi
fi
;;
ftp)
case $PACKAGETYPE in
split)
fetch_split_files
INSDIR="${INSFILE}" ; export INSDIR
start_extract_split
;;
dist)
fetch_dist_file
start_extract_dist "$DLDIR"
;;
*)
fetch_install_file
start_extract_uzip_tar
;;
esac
;;
sftp) ;;
rsync) start_rsync_copy ;;
image) start_image_install ;;
local)
get_value_from_cfg localPath
if [ -z "$VAL" ]
then
exit_err "Install medium was set to local, but no localPath was provided!"
fi
LOCALPATH=$VAL
if [ "$PACKAGETYPE" = "dist" ] ; then
INSFILE="${INSFILE}" ; export INSFILE
start_extract_dist "$LOCALPATH"
else
INSFILE="${LOCALPATH}/${INSFILE}" ; export INSFILE
start_extract_uzip_tar
fi
;;
*) exit_err "ERROR: Unknown install medium" ;;
esac
};
|