functions.sh revision 220059
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 220059 2011-03-27 16:57:54Z 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 "$1" 102 103 # Save this error to the log file 104 echo "${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" 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 [ $? -ne 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 262get_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}; 285 286iscompressed() 287{ 288 local FILE 289 local RES 290 291 FILE="$1" 292 RES=1 293 294 if echo "${FILE}" | \ 295 grep -qiE '\.(Z|lzo|lzw|lzma|gz|bz2|xz|zip)$' 2>&1 296 then 297 RES=0 298 fi 299 300 return ${RES} 301} 302 303get_compression_type() 304{ 305 local FILE 306 local SUFFIX 307 308 FILE="$1" 309 SUFFIX=`echo "${FILE}" | sed -E 's|^(.+)\.(.+)$|\2|'` 310 311 VAL="" 312 SUFFIX=`echo "${SUFFIX}" | tr A-Z a-z` 313 case "${SUFFIX}" in 314 z) VAL="lzw" ;; 315 lzo) VAL="lzo" ;; 316 lzw) VAL="lzw" ;; 317 lzma) VAL="lzma" ;; 318 gz) VAL="gzip" ;; 319 bz2) VAL="bzip2" ;; 320 xz) VAL="xz" ;; 321 zip) VAL="zip" ;; 322 esac 323 324 export VAL 325} 326 327write_image() 328{ 329 local DEVICE_FILE 330 331 IMAGE_FILE="$1" 332 DEVICE_FILE="$2" 333 334 if [ -z "${IMAGE_FILE}" ] 335 then 336 exit_err "ERROR: Image file not specified!" 337 fi 338 339 if [ -z "${DEVICE_FILE}" ] 340 then 341 exit_err "ERROR: Device file not specified!" 342 fi 343 344 if [ ! -f "${IMAGE_FILE}" ] 345 then 346 exit_err "ERROR: '${IMAGE_FILE}' does not exist!" 347 fi 348 349 DEVICE_FILE="${DEVICE_FILE#/dev/}" 350 DEVICE_FILE="/dev/${DEVICE_FILE}" 351 352 if [ ! -c "${DEVICE_FILE}" ] 353 then 354 exit_err "ERROR: '${DEVICE_FILE}' is not a character device!" 355 fi 356 357 if iscompressed "${IMAGE_FILE}" 358 then 359 local COMPRESSION 360 361 get_compression_type "${IMAGE_FILE}" 362 COMPRESSION="${VAL}" 363 364 case "${COMPRESSION}" in 365 lzw) 366 rc_halt "uncompress ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 367 IMAGE_FILE="${IMAGE_FILE%.Z}" 368 ;; 369 370 lzo) 371 rc_halt "lzop -d $IMAGE_{FILE} -c | dd of=${DEVICE_FILE}" 372 IMAGE_FILE="${IMAGE_FILE%.lzo}" 373 ;; 374 375 lzma) 376 rc_halt "lzma -d ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 377 IMAGE_FILE="${IMAGE_FILE%.lzma}" 378 ;; 379 380 gzip) 381 rc_halt "gunzip ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 382 IMAGE_FILE="${IMAGE_FILE%.gz}" 383 ;; 384 385 bzip2) 386 rc_halt "bunzip2 ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 387 IMAGE_FILE="${IMAGE_FILE%.bz2}" 388 ;; 389 390 xz) 391 rc_halt "xz -d ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 392 IMAGE_FILE="${IMAGE_FILE%.xz}" 393 ;; 394 395 zip) 396 rc_halt "unzip ${IMAGE_FILE} -c | dd of=${DEVICE_FILE}" 397 IMAGE_FILE="${IMAGE_FILE%.zip}" 398 ;; 399 400 *) 401 exit_err "ERROR: ${COMPRESSION} compression is not supported" 402 ;; 403 esac 404 405 else 406 rc_halt "dd if=${IMAGE_FILE} of=${DEVICE_FILE}" 407 408 fi 409}; 410 411# Setup and install on a new disk / partition 412install_fresh() 413{ 414 # Lets start setting up the disk slices now 415 setup_disk_slice 416 417 if [ -z "${ROOTIMAGE}" ] 418 then 419 420 # Disk setup complete, now lets parse WORKINGSLICES and setup the bsdlabels 421 setup_disk_label 422 423 # Now we've setup the bsdlabels, lets go ahead and run newfs / zfs 424 # to setup the filesystems 425 setup_filesystems 426 427 # Lets mount the partitions now 428 mount_all_filesystems 429 430 # We are ready to begin extraction, lets start now 431 init_extraction 432 433 # Check if we have any optional modules to load 434 install_components 435 436 # Check if we have any packages to install 437 install_packages 438 439 # Do any localization in configuration 440 run_localize 441 442 # Save any networking config on the installed system 443 save_networking_install 444 445 # Now add any users 446 setup_users 447 448 # Now run any commands specified 449 run_commands 450 451 # Do any last cleanup / setup before unmounting 452 run_final_cleanup 453 454 # Unmount and finish up 455 unmount_all_filesystems 456 fi 457 458 echo_log "Installation finished!" 459}; 460 461# Extract the system to a pre-mounted directory 462install_extractonly() 463{ 464 # We are ready to begin extraction, lets start now 465 init_extraction 466 467 # Check if we have any optional modules to load 468 install_components 469 470 # Check if we have any packages to install 471 install_packages 472 473 # Do any localization in configuration 474 run_localize 475 476 # Save any networking config on the installed system 477 save_networking_install 478 479 # Now add any users 480 setup_users 481 482 # Now run any commands specified 483 run_commands 484 485 # Set a hostname on the install system 486 setup_hostname 487 488 # Set the root_pw if it is specified 489 set_root_pw 490 491 echo_log "Installation finished!" 492}; 493 494install_image() 495{ 496 # We are ready to begin extraction, lets start now 497 init_extraction 498 499 echo_log "Installation finished!" 500}; 501 502install_upgrade() 503{ 504 # We're going to do an upgrade, skip all the disk setup 505 # and start by mounting the target drive/slices 506 mount_upgrade 507 508 # Start the extraction process 509 init_extraction 510 511 # Do any localization in configuration 512 run_localize 513 514 # Now run any commands specified 515 run_commands 516 517 # Merge any old configuration files 518 merge_old_configs 519 520 # Check if we have any optional modules to load 521 install_components 522 523 # Check if we have any packages to install 524 install_packages 525 526 # All finished, unmount the file-systems 527 unmount_upgrade 528 529 echo_log "Upgrade finished!" 530}; 531