1 #!/bin/ksh93 -p
   2 #
   3 # CDDL HEADER START
   4 #
   5 # The contents of this file are subject to the terms of the
   6 # Common Development and Distribution License (the "License").
   7 # You may not use this file except in compliance with the License.
   8 #
   9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10 # or http://www.opensolaris.org/os/licensing.
  11 # See the License for the specific language governing permissions
  12 # and limitations under the License.
  13 #
  14 # When distributing Covered Code, include this CDDL HEADER in each
  15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16 # If applicable, add the following below this CDDL HEADER, with the
  17 # fields enclosed by brackets "[]" replaced with your own identifying
  18 # information: Portions Copyright [yyyy] [name of copyright owner]
  19 #
  20 # CDDL HEADER END
  21 #
  22 #
  23 # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24 # Use is subject to license terms.
  25 #
  26 
  27 #
  28 # mkbootmedia - create bootable Solaris ISO image
  29 #
  30 
  31 readonly PROG=$0
  32 MKISOFS=/usr/bin/mkisofs
  33 ELTORITO=boot/grub/stage2_eltorito      # relative to $MEDIA_ROOT
  34 SPARCBOOT=boot/hsfs.bootblock
  35 CAT=/usr/bin/cat
  36 CP=/usr/bin/cp
  37 RM=/usr/bin/rm
  38 UNAME=/usr/bin/uname
  39 MACH=`$UNAME -p`
  40 BOOTBLOCK=
  41 GREP=/usr/bin/grep
  42 
  43 # for gettext
  44 TEXTDOMAIN=SUNW_OST_OSCMD
  45 export TEXTDOMAIN
  46 
  47 
  48 function usage
  49 {
  50         gettext "Usage:\n${PROG##*/} [-v] [-l <label>] <media-root> <iso>\n"
  51         gettext "Options:\n  -l <label>\n        Label/volume name of the ISO image.\n"
  52         gettext "  -v\n        Verbose.  Multiple -v options increase verbosity.\n"
  53         echo;
  54 }
  55 
  56 
  57 #
  58 # Main
  59 #
  60 LABEL=
  61 VERBOSITY=0
  62 
  63 while getopts ':l:v' opt
  64 do
  65         case $opt in
  66         l)      LABEL=$OPTARG
  67                 ;;
  68         v)      (( VERBOSITY += 1 ))
  69                 ;;
  70         :)      gettext "Option -$OPTARG missing argument.\n"
  71                 usage
  72                 exit 1
  73                 ;;
  74         *)      gettext "Option -$OPTARG invalid.\n"
  75                 usage
  76                 exit 2
  77                 ;;
  78         esac
  79 done
  80 shift 'OPTIND - 1'
  81 
  82 if (( $# != 2 ))
  83 then
  84         usage
  85         exit 1
  86 fi
  87 
  88 MEDIA_ROOT=$1
  89 ISOIMAGE=$2
  90 
  91 if [ ! -z `echo $ISOIMAGE | $GREP "^/tmp"` ]; then
  92         gettext "ISO images will not be created on /tmp.\nPlease choose a different output location.\n"
  93         exit 3
  94 fi
  95 
  96 # Verify $MEDIA_ROOT is a Solaris install media (Solaris 10 Update 1 or later)
  97 if [[ ! -d $(echo "$MEDIA_ROOT"/Solaris*/Tools/Boot) ]]; then
  98         gettext "$MEDIA_ROOT is not Solaris install media.\n"
  99         exit 1
 100 fi
 101 
 102 # If no label specified use the Solaris_* version under $MEDIA_ROOT
 103 if [[ -z "$LABEL" ]]; then
 104         LABEL=$(echo "$MEDIA_ROOT"/Solaris*)
 105         LABEL=${LABEL##*/}
 106 fi
 107 
 108 # If $ISOIMAGE exists, verify it's writable.
 109 if [[ -e "$ISOIMAGE" && ! -w "$ISOIMAGE" ]]; then
 110         gettext "$ISOIMAGE exists but is not writable.\n"
 111         exit 1
 112 fi
 113 
 114 # If we're on an x86/x64 system, we need to have the El Torito file
 115 # modified with some boot information (-boot-info-table option).
 116 # If the image isn't writable, we can't continue
 117 # UltraSPARC systems (sun4u, sun4v etc) don't use El Torito
 118 if [[ "$MACH" = "i386" && ! -w "$MEDIA_ROOT/$ELTORITO" ]]; then
 119         gettext "$MEDIA_ROOT/$ELTORITO is not writable.\n"
 120         exit 1
 121 fi
 122 
 123 # Check that we've got mkisofs installed 
 124 if [[ ! -f "$MKISOFS" || ! -x "$MKISOFS" ]]; then
 125     gettext "Cannot find $f\n"
 126     exit 1
 127 fi
 128 
 129 
 130 # Determine mkisofs' verbose flag depending on $VERBOSITY.
 131 case $VERBOSITY in
 132 0)      VERBOSE_FLAG=-quiet
 133         ;;
 134 1)      VERBOSE_FLAG=                   # mkisofs' default verboseness
 135         ;;
 136 *)      VERBOSE_FLAG=
 137         i=$VERBOSITY
 138         while ((i > 0))
 139         do
 140                 VERBOSE_FLAG="-v $VERBOSE_FLAG"
 141                 (( i -= 1 ))
 142         done
 143         ;;
 144 esac
 145 
 146 # Since mkisofs below will modify the file $ELTORITO in-place, save a copy
 147 # of it first.  Use trap to restore it when this script exits (including
 148 # when user hits control-C).
 149 
 150 if [[ "$MACH" = "i386" ]]
 151 then
 152         BOOTBLOCK=$MEDIA_ROOT/$ELTORITO
 153         ELTORITO_SAVE=/tmp/${ELTORITO##*/}.$$
 154         $CP "$MEDIA_ROOT/$ELTORITO" "$ELTORITO_SAVE" || exit 1
 155         trap '"$CP" "$ELTORITO_SAVE" "$MEDIA_ROOT/$ELTORITO" 2>/dev/null;
 156                 "$RM" -f "$ELTORITO_SAVE"' EXIT
 157 else
 158         # sun4u/sun4u1/sun4v et al
 159         BOOTBLOCK=$MEDIA_ROOT/$SPARCBOOT
 160         SPARCBOOT_SAVE=/tmp/hsfs.bootblock.$$
 161         $CP "$MEDIA_ROOT/$SPARCBOOT" "$SPARCBOOT_SAVE" || exit 1
 162         trap '"$CP" "$MEDIA_ROOT/$SPARCBOOT" "$SPARCBOOT_SAVE" 2>/dev/null;
 163                 "$RM" -f $SPARCBOOT_SAVE"' EXIT
 164 fi
 165 
 166 # Call mkisofs to do the actual work.
 167 # Note: the "-log-file >(cat -u >&2)" and "2>/dev/null" below is a trick
 168 #       to filter out mkisofs's warning message about being non-conforming
 169 #       to ISO-9660.
 170 # We do some funky architecture-specific stuff here so that we can
 171 # actually create a bootable media image for UltraSPARC systems
 172 
 173 sparc_ISOARGS="-G $BOOTBLOCK -B ... -joliet-long -R -U"
 174 i386_ISOARGS="-b boot/grub/stage2_eltorito -boot-info-table "
 175 i386_ISOARGS="$i386_ISOARGS -boot-load-size 4 -c .catalog -d -N "
 176 i386_ISOARGS="$i386_ISOARGS -no-emul-boot -r -relaxed-filenames"
 177 if [[ "$MACH" = "i386" ]]
 178 then
 179         ISOARGS=$i386_ISOARGS
 180 else
 181         ISOARGS=$sparc_ISOARGS
 182 fi
 183 
 184 $MKISOFS -o "$ISOIMAGE" \
 185         -allow-leading-dots \
 186         $ISOARGS \
 187         -l -ldots \
 188         -R -J \
 189         -V "$ISOLABEL" \
 190         $VERBOSE_FLAG \
 191         -log-file >($CAT -u >&2) \
 192         "$MEDIA_ROOT" 2>/dev/null