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: head/usr.sbin/pc-sysinstall/backend/functions.sh 235005 2012-05-04 15:31:35Z jpaetzel $
|
26# $FreeBSD: head/usr.sbin/pc-sysinstall/backend/functions.sh 235453 2012-05-14 18:06:51Z jpaetzel $ |
27 28# functions.sh 29# Library of functions which pc-sysinstall may call upon 30 31# Function which displays the help-index file 32display_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 44display_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 62convert_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 74convert_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 85strip_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 export VAL=`echo "$1" | tr -d ' '` 95}; 96 97# Displays an error message and exits with error 1 98exit_err() 99{ 100 # Echo the message for the users benefit 101 echo "EXITERROR: $1" 102 103 # Save this error to the log file 104 echo "EXITERROR: ${1}" >>$LOGOUT 105 106 # Check if we need to unmount any file-systems after this failure 107 unmount_all_filesystems_failure 108 109 echo "For more details see log file: $LOGOUT" 110 111 exit 1 112}; 113 114# Run-command, don't halt if command exits with non-0 115rc_nohalt() 116{ 117 CMD="$1" 118 119 if [ -z "${CMD}" ] 120 then 121 exit_err "Error: missing argument in rc_nohalt()" 122 fi 123 124 echo "Running: ${CMD}" >>${LOGOUT} 125 ${CMD} >>${LOGOUT} 2>>${LOGOUT} 126 127}; 128 129# Run-command, halt if command exits with non-0 130rc_halt() 131{ 132 CMD="$1" 133 134 if [ -z "${CMD}" ] 135 then 136 exit_err "Error: missing argument in rc_halt()" 137 fi 138 139 echo "Running: ${CMD}" >>${LOGOUT} 140 eval ${CMD} >>${LOGOUT} 2>>${LOGOUT} 141 STATUS="$?" 142 if [ "${STATUS}" != "0" ] 143 then 144 exit_err "Error ${STATUS}: ${CMD}" 145 fi 146}; 147 148# Run-command w/echo to screen, halt if command exits with non-0 149rc_halt_echo() 150{ 151 CMD="$1" 152 153 if [ -z "${CMD}" ] 154 then 155 exit_err "Error: missing argument in rc_halt_echo()" 156 fi 157 158 echo "Running: ${CMD}" >>${LOGOUT} 159 ${CMD} 2>&1 | tee -a ${LOGOUT} 160 STATUS="$?" 161 if [ "$STATUS" != "0" ] 162 then 163 exit_err "Error ${STATUS}: $CMD" 164 fi 165 166}; 167 168# Run-command w/echo, don't halt if command exits with non-0 169rc_nohalt_echo() 170{ 171 CMD="$1" 172 173 if [ -z "${CMD}" ] 174 then 175 exit_err "Error: missing argument in rc_nohalt_echo()" 176 fi 177 178 echo "Running: ${CMD}" >>${LOGOUT} 179 ${CMD} 2>&1 | tee -a ${LOGOUT} 180 181}; 182 183# Echo to the screen and to the log 184echo_log() 185{ 186 STR="$1" 187 188 if [ -z "${STR}" ] 189 then 190 exit_err "Error: missing argument in echo_log()" 191 fi 192 193 echo "${STR}" | tee -a ${LOGOUT} 194}; 195 196# Make sure we have a numeric 197is_num() 198{ 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 204fetch_file() 205{ 206 207 FETCHFILE="$1" 208 FETCHOUTFILE="$2" 209 EXITFAILED="$3" 210
|
211 SIZEFILE="${TMPDIR}/.fetchSize"
|
211 EXITFILE="${TMPDIR}/.fetchExit" 212
|
214 rm ${SIZEFILE} 2>/dev/null >/dev/null
|
213 rm ${FETCHOUTFILE} 2>/dev/null >/dev/null 214
|
217 fetch -s "${FETCHFILE}" >${SIZEFILE}
218 SIZE="`cat ${SIZEFILE}`"
219 SIZE=$((SIZE/1024))
|
215 SIZE=$(( `fetch -s "${FETCHFILE}"` / 1024 )) |
216 echo "FETCH: ${FETCHFILE}" 217 echo "FETCH: ${FETCHOUTFILE}" >>${LOGOUT} 218 219 ( fetch -o ${FETCHOUTFILE} "${FETCHFILE}" >/dev/null 2>/dev/null ; echo "$?" > ${EXITFILE} ) & 220 PID="$!" 221 while 222 z=1 223 do 224 225 if [ -e "${FETCHOUTFILE}" ] 226 then 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} 232 fi 233 fi 234 235 # Check if the download is finished 236 ps -p ${PID} >/dev/null 2>/dev/null 237 if [ $? -ne 0 ] 238 then 239 break; 240 fi 241 242 sleep 2 243 done 244 245 echo "FETCHDONE" 246 247 EXIT="`cat ${EXITFILE}`" 248 if [ "${EXIT}" != "0" -a "$EXITFAILED" = "1" ] 249 then 250 exit_err "Error: Failed to download ${FETCHFILE}" 251 fi 252 253 return $EXIT 254 255}; 256 257# Function to return a the zpool name for this device 258get_zpool_name() 259{ 260 DEVICE="$1" 261 262 # Set the base name we use for zpools 263 BASENAME="tank" 264 265 if [ ! -d "${TMPDIR}/.zpools" ] ; then 266 mkdir -p ${TMPDIR}/.zpools 267 fi 268 269 if [ -e "${TMPDIR}/.zpools/${DEVICE}" ] ; then 270 cat ${TMPDIR}/.zpools/${DEVICE} 271 return 0 272 else 273 # Need to generate a zpool name for this device 274 NUM=`ls ${TMPDIR}/.zpools/ | wc -l | sed 's| ||g'` 275 276 # Is it used in another zpool?
|
281 while
282 z=1
|
277 while : |
278 do 279 NEWNAME="${BASENAME}${NUM}"
|
285 zpool import | grep -q "${NEWNAME}"
286 if [ $? -ne 0 ] ; then break ; fi
|
280 zpool import | grep -qw "${NEWNAME}" && break |
281 NUM=$((NUM+1)) 282 done 283 284 # Now save the new tank name 285 mkdir -p ${TMPDIR}/.zpools/`dirname $DEVICE` 286 echo "$NEWNAME" >${TMPDIR}/.zpools/${DEVICE} 287 echo "${NEWNAME}" 288 return 0 289 fi 290}; 291 292iscompressed() 293{ 294 local FILE 295 local RES 296 297 FILE="$1" 298 RES=1 299 300 if echo "${FILE}" | \ 301 grep -qiE '\.(Z|lzo|lzw|lzma|gz|bz2|xz|zip)$' 2>&1 302 then 303 RES=0 304 fi 305 306 return ${RES} 307} 308 309get_compression_type() 310{ 311 local FILE 312 local SUFFIX 313 314 FILE="$1" 315 SUFFIX=`echo "${FILE}" | sed -E 's|^(.+)\.(.+)$|\2|'` 316 317 VAL="" 318 SUFFIX=`echo "${SUFFIX}" | tr A-Z a-z` 319 case "${SUFFIX}" in 320 z) VAL="lzw" ;; 321 lzo) VAL="lzo" ;; 322 lzw) VAL="lzw" ;; 323 lzma) VAL="lzma" ;; 324 gz) VAL="gzip" ;; 325 bz2) VAL="bzip2" ;; 326 xz) VAL="xz" ;; 327 zip) VAL="zip" ;; 328 esac 329 330 export VAL 331} 332 333write_image() 334{ 335 local DEVICE_FILE 336 337 IMAGE_FILE="$1" 338 DEVICE_FILE="$2" 339 340 if [ -z "${IMAGE_FILE}" ] 341 then 342 exit_err "ERROR: Image file not specified!" 343 fi 344 345 if [ -z "${DEVICE_FILE}" ] 346 then 347 exit_err "ERROR: Device file not specified!" 348 fi 349 350 if [ ! -f "${IMAGE_FILE}" ] 351 then 352 exit_err "ERROR: '${IMAGE_FILE}' does not exist!" 353 fi 354 355 DEVICE_FILE="${DEVICE_FILE#/dev/}" 356 DEVICE_FILE="/dev/${DEVICE_FILE}" 357 358 if [ ! -c "${DEVICE_FILE}" ] 359 then 360 exit_err "ERROR: '${DEVICE_FILE}' is not a character device!" 361 fi 362 363 if iscompressed "${IMAGE_FILE}" 364 then 365 local COMPRESSION 366 367 get_compression_type "${IMAGE_FILE}" 368 COMPRESSION="${VAL}" 369 370 case "${COMPRESSION}" in 371 lzw) 372 rc_halt "uncompress ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 373 IMAGE_FILE="${IMAGE_FILE%.Z}" 374 ;; 375 376 lzo) 377 rc_halt "lzop -d $IMAGE_{FILE} -c | dd of=${DEVICE_FILE}" 378 IMAGE_FILE="${IMAGE_FILE%.lzo}" 379 ;; 380 381 lzma) 382 rc_halt "lzma -d ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 383 IMAGE_FILE="${IMAGE_FILE%.lzma}" 384 ;; 385 386 gzip) 387 rc_halt "gunzip ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 388 IMAGE_FILE="${IMAGE_FILE%.gz}" 389 ;; 390 391 bzip2) 392 rc_halt "bunzip2 ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 393 IMAGE_FILE="${IMAGE_FILE%.bz2}" 394 ;; 395 396 xz) 397 rc_halt "xz -d ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 398 IMAGE_FILE="${IMAGE_FILE%.xz}" 399 ;; 400 401 zip) 402 rc_halt "unzip ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 403 IMAGE_FILE="${IMAGE_FILE%.zip}" 404 ;; 405 406 *) 407 exit_err "ERROR: ${COMPRESSION} compression is not supported" 408 ;; 409 esac 410 411 else 412 rc_halt "dd if=${IMAGE_FILE} of=${DEVICE_FILE}" 413 414 fi 415}; 416 417# Setup and install on a new disk / partition 418install_fresh() 419{ 420 # Lets start setting up the disk slices now 421 setup_disk_slice 422 423 if [ -z "${ROOTIMAGE}" ] 424 then 425 426 # Disk setup complete, now lets parse WORKINGSLICES and setup the bsdlabels 427 setup_disk_label 428 429 # Now we've setup the bsdlabels, lets go ahead and run newfs / zfs 430 # to setup the filesystems 431 setup_filesystems 432 433 # Lets mount the partitions now 434 mount_all_filesystems 435 436 # We are ready to begin extraction, lets start now 437 init_extraction 438 439 # Check if we have any optional modules to load 440 install_components 441 442 # Check if we have any packages to install 443 install_packages 444 445 # Do any localization in configuration 446 run_localize 447 448 # Save any networking config on the installed system 449 save_networking_install 450 451 # Now add any users 452 setup_users 453 454 # Do any last cleanup / setup before unmounting 455 run_final_cleanup 456 457 # Now run any commands specified 458 run_commands 459 460 # Unmount and finish up 461 unmount_all_filesystems 462 fi 463 464 echo_log "Installation finished!" 465}; 466 467# Extract the system to a pre-mounted directory 468install_extractonly() 469{ 470 # We are ready to begin extraction, lets start now 471 init_extraction 472 473 # Check if we have any optional modules to load 474 install_components 475 476 # Check if we have any packages to install 477 install_packages 478 479 # Do any localization in configuration 480 run_localize 481 482 # Save any networking config on the installed system 483 save_networking_install 484 485 # Now add any users 486 setup_users 487 488 # Now run any commands specified 489 run_commands 490 491 # Set a hostname on the install system 492 setup_hostname 493 494 # Set the root_pw if it is specified 495 set_root_pw 496 497 echo_log "Installation finished!" 498}; 499 500install_image() 501{ 502 # We are ready to begin extraction, lets start now 503 init_extraction 504 505 echo_log "Installation finished!" 506}; 507 508install_upgrade() 509{ 510 # We're going to do an upgrade, skip all the disk setup 511 # and start by mounting the target drive/slices 512 mount_upgrade 513 514 # Start the extraction process 515 init_extraction 516 517 # Do any localization in configuration 518 run_localize 519 520 # Now run any commands specified 521 run_commands 522 523 # Merge any old configuration files 524 merge_old_configs 525 526 # Check if we have any optional modules to load 527 install_components 528 529 # Check if we have any packages to install 530 install_packages 531 532 # All finished, unmount the file-systems 533 unmount_upgrade 534 535 echo_log "Upgrade finished!" 536};
|