#!/usr/bin/bash # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License (the "License"). # You may not use this file except in compliance with the License. # # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at usr/src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # Copyright 2007 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # # Script to install Belenix onto a Bootable LiveUSB # By Anil Gulecha # # 04.05.2008 v2.0 # enhancements by Bernd Schemmer (Bernd.Schemmer@gmx.de): # - added the parameter "-t targetslice" # - added the parameter "-s targetslice_size" # - added the parameter "-V" # - added code to grow the filesystem on the USB device # - added code to umount all slices from the USB device # - added code to remove all other slices (only if the script creates the # targetslice) # - added some general functions and messages # - added more error checking code # - added trap handler # # -------------------- constants # __TRUE=0 __FALSE=1 # script version # scriptversion="v2.0 03.05.2008" # name of this script without the path # scriptname="${0##*/}" # boot image name # bootimage="Milax" # the script should also work with belenix but that was not tested # bootimage="Belenix" # -------------------- variables # # marker for invalid parameters # invalid_parameter_found=${__FALSE} # default values for the parameter # img="" targetslice="" targetslice_size="" # mountpoint # mnt=/tmp/usb.$$ # -------------------- functions # usage: usage # function usage { cat <&2 } # usage: cleanup # function cleanup { # check for temporary mounts mount | grep "^${mnt} " >/dev/null && umount "${mnt}" 2>/dev/null # check for temporary directories [ -d "${mnt}" ] && rmdir "${mnt}" 2>/dev/null } # usage: die returncode ["message"] # function die { typeset thisrc=$1 if [ $# -gt 1 ] ; then shift if [ ${thisrc} = 0 ] ; then logMsg "$*" else logError "$*" fi fi cleanup exit ${thisrc} } # usage: trap_handler trapnumber # function trap_handler { logMsg "" logError "Trap $1 catched" die 200 } # -------------------- main function logMsg "${scriptname} ${scriptversion} - copy ${bootimage} to a USB device" # install trap handler # trap "trap_handler 1" 1 trap "trap_handler 2" 2 trap "trap_handler 3" 3 trap "trap_handler 15" 15 # process the script options # while getopts ":i:t:s:hV" cur_switch ; do if [ "${cur_switch}" = ":" ] ; then cur_switch=${OPTARG} OPTARG="" fi case ${cur_switch} in "h" ) usage die 1 ;; "i" ) img="${OPTARG}" ;; "t" ) targetslice="${OPTARG}" ;; "s" ) targetslice_size="${OPTARG}" ;; "V" ) die 0 ;; \? ) logError "Unknown parameter found: \"${OPTARG}\" " invalid_parameter_found=${__TRUE} break ;; * ) logError "Unknown parameter found: \"${cur_switch}\"" invalid_parameter_found=${__TRUE} break ;; esac done # support old syntax also # shift $(( OPTIND - 1 )) if [ $# -gt 1 ] ; then logError "To many arguments: \"$*\" " invalid_parameter_found=${__TRUE} else [ $# -ne 0 ] && img="$*" fi if [ "${targetslice}"x != ""x -a "${targetslice_size}"x != ""x ] ; then logError "Use either the parameter -t or -s but not both" invalid_parameter_found=${__TRUE} fi # check the parameter for the target slice size # case "${targetslice_size}" in all | max | min | *e | *b | *c | *mb | *gb | "" ) : these are the known values ;; * ) logError "The value for the parameter -s is invalid: \"${targetslice_size}\"" invalid_parameter_found=${__TRUE} ;; esac [ ${invalid_parameter_found} = ${__TRUE} ] && die 5 "Invalid parameter found; exiting" [ "${img}"x = ""x ] && die 6 "Mandatory parameter for the usbimage file is missing." [ ! -f $img ] && [ ! -c $img ] && die 1 "The usbimage file \"$img\" does not exist." [ $( id -u ) != 0 ] && die 7 "You must be root to execute this script" if [ "${targetslice}"x != ""x ] ; then # use an existing slice while true ; do slice_found=${__TRUE} if [ -c "${targetslice}" ] ; then s0cdev="${targetslice}" s0bdev="/dev/dsk/${targetslice##*/}" break fi if [ -b "${targetslice}" ] ; then s0bdev="${targetslice}" s0cdev="/dev/rdsk/${targetslice##*/}" break fi if [ -c "/dev/rdsk/${targetslice}" ] ; then s0cdev="/dev/rdsk/${targetslice}" s0bdev="/dev/dsk/${targetslice}" break fi if [ -b "/dev/dsk/${targetslice}" ] ; then s0cdev="/dev/rdsk/${targetslice}" s0bdev="/dev/dsk/${targetslice}" break fi slice_found=${__FALSE} break done [ ${slice_found} = ${__FALSE} ] && die 15 "Slice \"${targetslice}\" not found. Exiting ..." logMsg "Installing ${bootimage} to the existing slice \"${s0bdev}\" " else # create the necessary slice logMsg "Searching USB devices ..." let i=0 #nawk script to o/p the details of plugged in USB drives rmformat 2>/dev/null | nawk 'BEGIN { FS = ":"; lnode=0; physdev=""; node = ""; devname = ""; bus = ""; size = 0; bustype = "USB"; } { if (lnode == 1 && match($1, "Logical Node")) { if (match(bus, bustype)) printf("%s\t%s\t%s\t%s\n", physdev, node, size, devname); node = $2; } else { if (match($1, "Logical Node")) { lnode = 1; node = $2; } else if (match($1, "Bus")) { bus=$2 } else if (match($1, "Connected Device")) { devname = $2; } else if (match($1, "Size")) { size = $2; } else if (match($1, "Physical Node")) { physdev=$2 } } } END { if (lnode == 1) { if (match(bus, bustype)) printf("%s\t%s\t%s\t%s\n", physdev, node, size, devname); } }' >/tmp/ulst while read p l s m d; do phys[$i]=$p log[$i]=$l size[$i]=$s mult[$i]=$m desc[$i]=$d let i=$i+1 done /dev/null <