3 # Copyright (c) 2010 iXsystems, Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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
29 # Library of functions which pc-sysinstall may call upon
31 # Function which displays the help-index file
34 if [ -e "${PROGDIR}/doc/help-index" ]
36 cat ${PROGDIR}/doc/help-index
38 echo "Error: ${PROGDIR}/doc/help-index not found"
43 # Function which displays the help for a specified command
44 display_command_help()
48 echo "Error: No command specified to display help for"
52 if [ -e "${PROGDIR}/doc/help-${1}" ]
54 cat ${PROGDIR}/doc/help-${1}
56 echo "Error: ${PROGDIR}/doc/help-${1} not found"
61 # Function to convert bytes to megabytes
62 convert_byte_to_megabyte()
66 echo "Error: No bytes specified!"
70 expr -e ${1} / 1048576
73 # Function to convert blocks to megabytes
74 convert_blocks_to_megabyte()
76 if [ -z "${1}" ] ; then
77 echo "Error: No blocks specified!"
84 # Takes $1 and strips the whitespace out of it, returns VAL
89 echo "Error: No value setup to strip whitespace from!"
94 export VAL=`echo "$1" | tr -d ' '`
97 # Displays an error message and exits with error 1
100 # Echo the message for the users benefit
103 # Save this error to the log file
104 echo "EXITERROR: ${1}" >>$LOGOUT
106 # Check if we need to unmount any file-systems after this failure
107 unmount_all_filesystems_failure
109 echo "For more details see log file: $LOGOUT"
114 # Run-command, don't halt if command exits with non-0
121 exit_err "Error: missing argument in rc_nohalt()"
124 echo "Running: ${CMD}" >>${LOGOUT}
125 ${CMD} >>${LOGOUT} 2>>${LOGOUT}
129 # Run-command, halt if command exits with non-0
136 exit_err "Error: missing argument in rc_halt()"
139 echo "Running: ${CMD}" >>${LOGOUT}
140 eval ${CMD} >>${LOGOUT} 2>>${LOGOUT}
142 if [ "${STATUS}" != "0" ]
144 exit_err "Error ${STATUS}: ${CMD}"
148 # Run-command w/echo to screen, halt if command exits with non-0
155 exit_err "Error: missing argument in rc_halt_echo()"
158 echo "Running: ${CMD}" >>${LOGOUT}
159 ${CMD} 2>&1 | tee -a ${LOGOUT}
161 if [ "$STATUS" != "0" ]
163 exit_err "Error ${STATUS}: $CMD"
168 # Run-command w/echo, don't halt if command exits with non-0
175 exit_err "Error: missing argument in rc_nohalt_echo()"
178 echo "Running: ${CMD}" >>${LOGOUT}
179 ${CMD} 2>&1 | tee -a ${LOGOUT}
183 # Echo to the screen and to the log
190 exit_err "Error: missing argument in echo_log()"
193 echo "${STR}" | tee -a ${LOGOUT}
196 # Make sure we have a numeric
199 expr $1 + 1 2>/dev/null
203 # Function which uses "fetch" to download a file, and display a progress report
211 EXITFILE="${TMPDIR}/.fetchExit"
213 rm ${FETCHOUTFILE} 2>/dev/null >/dev/null
215 SIZE=$(( `fetch -s "${FETCHFILE}"` / 1024 ))
216 echo "FETCH: ${FETCHFILE}"
217 echo "FETCH: ${FETCHOUTFILE}" >>${LOGOUT}
219 ( fetch -o ${FETCHOUTFILE} "${FETCHFILE}" >/dev/null 2>/dev/null ; echo "$?" > ${EXITFILE} ) &
225 if [ -e "${FETCHOUTFILE}" ]
227 DSIZE=`du -k ${FETCHOUTFILE} | tr -d '\t' | cut -d '/' -f 1`
228 if [ $(is_num "$DSIZE") ] ; then
229 if [ $SIZE -lt $DSIZE ] ; then DSIZE="$SIZE"; fi
230 echo "SIZE: ${SIZE} DOWNLOADED: ${DSIZE}"
231 echo "SIZE: ${SIZE} DOWNLOADED: ${DSIZE}" >>${LOGOUT}
235 # Check if the download is finished
236 ps -p ${PID} >/dev/null 2>/dev/null
247 EXIT="`cat ${EXITFILE}`"
248 if [ "${EXIT}" != "0" -a "$EXITFAILED" = "1" ]
250 exit_err "Error: Failed to download ${FETCHFILE}"
257 # Function to return a the zpool name for this device
262 # Set the base name we use for zpools
265 if [ ! -d "${TMPDIR}/.zpools" ] ; then
266 mkdir -p ${TMPDIR}/.zpools
269 if [ -e "${TMPDIR}/.zpools/${DEVICE}" ] ; then
270 cat ${TMPDIR}/.zpools/${DEVICE}
273 # Need to generate a zpool name for this device
274 NUM=`ls ${TMPDIR}/.zpools/ | wc -l | sed 's| ||g'`
276 # Is it used in another zpool?
279 NEWNAME="${BASENAME}${NUM}"
280 zpool list | grep -qw "${NEWNAME}"
282 zpool import | grep -qw "${NEWNAME}"
284 if [ $chk1 -eq 1 -a $chk2 -eq 1 ] ; then break ; fi
288 # Now save the new tank name
289 mkdir -p ${TMPDIR}/.zpools/`dirname $DEVICE`
290 echo "$NEWNAME" >${TMPDIR}/.zpools/${DEVICE}
304 if echo "${FILE}" | \
305 grep -qiE '\.(Z|lzo|lzw|lzma|gz|bz2|xz|zip)$' 2>&1
313 get_compression_type()
319 SUFFIX=`echo "${FILE}" | sed -E 's|^(.+)\.(.+)$|\2|'`
322 SUFFIX=`echo "${SUFFIX}" | tr A-Z a-z`
344 if [ -z "${IMAGE_FILE}" ]
346 exit_err "ERROR: Image file not specified!"
349 if [ -z "${DEVICE_FILE}" ]
351 exit_err "ERROR: Device file not specified!"
354 if [ ! -f "${IMAGE_FILE}" ]
356 exit_err "ERROR: '${IMAGE_FILE}' does not exist!"
359 DEVICE_FILE="${DEVICE_FILE#/dev/}"
360 DEVICE_FILE="/dev/${DEVICE_FILE}"
362 if [ ! -c "${DEVICE_FILE}" ]
364 exit_err "ERROR: '${DEVICE_FILE}' is not a character device!"
367 if iscompressed "${IMAGE_FILE}"
371 get_compression_type "${IMAGE_FILE}"
374 case "${COMPRESSION}" in
376 rc_halt "uncompress ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}"
377 IMAGE_FILE="${IMAGE_FILE%.Z}"
381 rc_halt "lzop -d $IMAGE_{FILE} -c | dd of=${DEVICE_FILE}"
382 IMAGE_FILE="${IMAGE_FILE%.lzo}"
386 rc_halt "lzma -d ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}"
387 IMAGE_FILE="${IMAGE_FILE%.lzma}"
391 rc_halt "gunzip ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}"
392 IMAGE_FILE="${IMAGE_FILE%.gz}"
396 rc_halt "bunzip2 ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}"
397 IMAGE_FILE="${IMAGE_FILE%.bz2}"
401 rc_halt "xz -d ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}"
402 IMAGE_FILE="${IMAGE_FILE%.xz}"
406 rc_halt "unzip ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}"
407 IMAGE_FILE="${IMAGE_FILE%.zip}"
411 exit_err "ERROR: ${COMPRESSION} compression is not supported"
416 rc_halt "dd if=${IMAGE_FILE} of=${DEVICE_FILE}"
421 # Setup and install on a new disk / partition
424 # Lets start setting up the disk slices now
427 if [ -z "${ROOTIMAGE}" ]
430 # Disk setup complete, now lets parse WORKINGSLICES and setup the bsdlabels
433 # Now we've setup the bsdlabels, lets go ahead and run newfs / zfs
434 # to setup the filesystems
437 # Lets mount the partitions now
438 mount_all_filesystems
440 # We are ready to begin extraction, lets start now
443 # Check if we have any optional modules to load
446 # Check if we have any packages to install
449 # Do any localization in configuration
452 # Save any networking config on the installed system
453 save_networking_install
458 # Do any last cleanup / setup before unmounting
461 # Now run any commands specified
464 # Unmount and finish up
465 unmount_all_filesystems
468 echo_log "Installation finished!"
471 # Extract the system to a pre-mounted directory
472 install_extractonly()
474 # We are ready to begin extraction, lets start now
477 # Check if we have any optional modules to load
480 # Check if we have any packages to install
483 # Do any localization in configuration
486 # Save any networking config on the installed system
487 save_networking_install
492 # Now run any commands specified
495 # Set a hostname on the install system
498 # Set the root_pw if it is specified
501 echo_log "Installation finished!"
506 # We are ready to begin extraction, lets start now
509 echo_log "Installation finished!"
514 # We're going to do an upgrade, skip all the disk setup
515 # and start by mounting the target drive/slices
518 # Start the extraction process
521 # Do any localization in configuration
524 # Now run any commands specified
527 # Merge any old configuration files
530 # Check if we have any optional modules to load
533 # Check if we have any packages to install
536 # All finished, unmount the file-systems
539 echo_log "Upgrade finished!"