6 # The contents of this file are subject to the terms of the
7 # Common Development and Distribution License (the "License").
8 # You may not use this file except in compliance with the License.
10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 # or http://www.opensolaris.org/os/licensing.
12 # See the License for the specific language governing permissions
13 # and limitations under the License.
15 # When distributing Covered Code, include this CDDL HEADER in each
16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 # If applicable, add the following below this CDDL HEADER, with the
18 # fields enclosed by brackets "[]" replaced with your own identifying
19 # information: Portions Copyright [yyyy] [name of copyright owner]
25 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
26 # Use is subject to license terms.
30 # Generate USB image from iso
31 # Originally from slim_install, modified by Toomas Soome.
34 # Solaris needs /usr/xpg4/bin/ because the tools in /usr/bin are not
36 export PATH
=/usr
/xpg
4/bin
:/bin
:/usr
/bin
:/usr
/sbin
38 #######################################################################
40 # Print the usage message.
44 #######################################################################
48 print
-u2 "${progname} iso_file usb_image tmpdir"
49 print
-u2 "iso_file : The path to an existing iso file."
50 print
-u2 "usb_image : The path to usb image to be created."
51 print
-u2 "tmpdir : Temporary directy used during usb image " \
54 print
-u2 "If tmpdir does not exist it will be created. If " \
55 "it or subdirectories under tmpdir can not be created " \
56 " an error is generated and this script exits."
62 #######################################################################
64 # This function attempst to clean up any resources this script
65 # could generate. Depending on where in the script this function
66 # is involked some resouces may not be there to cleanup, but
67 # that will not adversely effect anything.
69 # This function is not defined using the function keyword
70 # to avoid an exit loop.
75 #######################################################################
83 # unmounting, and uninstalling the lofi'ed devices and
84 # cleanup temporary files.
87 umount
"${usb_path}" || true
88 umount
"${iso_path}" || true
90 lofiadm
"${usb_file}" && \
91 lofiadm
-d "${usb_file}"
93 lofiadm
"${iso_file}" && \
94 lofiadm
-d "${iso_file}"
96 if [[ -d "${iso_path}" ]] ; then
100 if [[ -d "${usb_path}" ]] ; then
105 # If the tmpdir did not exist this script could
106 # have created it, so remove it here.
108 if ! ${tmpdir_existed} ; then
115 #######################################################################
117 # The error_handler for this script. Will cleanup the usb image
118 # that could have been partially created. Then invoke cleanup
119 # to clean up any temporary resouces.
121 # This function is not defined using the function keyword
122 # to avoid an exit loop.
127 #######################################################################
133 print
-u2 "\nError:\n"
134 print
-u2 -r -- "${progname}: $*"
138 # If an error was encountered while attempting to create
139 # the new usb image file don't leave a possibley partially
140 # constructed one around.
142 if [[ -f "${usb_file}" ]] ; then
143 rm -rf "${usb_file}" > /dev
/null
2>&1
150 #######################################################################
154 # iso_file : The path to an existing iso file.
155 # usb_image : The path to usb image to be created.
156 # tmpdir : Temporary directroy used during usb image creation.
158 # If tmpdir does not exist it will be created. If it can not be
159 # created an error is generated and this script exits.
161 # This script must be run as root"
164 # Set up error handling.
165 # Confirm input arguments.
166 # Create temporary directories.
167 # Mount up the existing ISO image file.
168 # Compute the size for the new USB image.
169 # Create and mount an empty new USB image.
170 # Copy the contents of the ISO file to the new USB image.
171 # Remove GRUB entries from the USB image which apply only to ISO.
172 # Set the file protections for the new USB image.
178 #######################################################################
179 typeset
-r progname
="$0"
180 typeset
-r iso_file
="$1"
181 typeset
-r tmpdir
="$3"
182 typeset tmpdir_existed
=true
183 typeset
-r iso_path
="${tmpdir}/iso"
185 typeset
-r usb_file
="$2"
186 typeset
-r usb_path
="${tmpdir}/usb"
196 # Confirm input arguments.
198 if [ $
(id
-u) != 0 ]; then
199 print
-u2 "Error:\nYou must run this script as root"
204 if [ $# -ne 3 ] ; then
205 print
-u2 "Error:\nImproper arguments"
211 # Set up error handling.
212 # Use set -o errexit to trap errors. However, where possible,
213 # explicitly check command return status for errors.
215 trap "error_handler Error or interrupt encountered. Exiting" ERR INT
219 # Create temporary directories.
221 [[ ! -d $tmpdir ]] && tmpdir_existed
=false
222 mkdir
-p "${iso_path}"
223 if [[ ! -d "${iso_path}" ]] ; then
224 error_handler
"Unable to create or access tmpdir ${iso_path}"
227 mkdir
-p "${usb_path}"
228 if [[ ! -d "${usb_path}" ]] ; then
229 error_handler
"Unable to create or access tmpdir ${usb_path}"
233 # Mount up the existing ISO image file.
235 isodev
="$(lofiadm -a "${iso_file}")" || \
236 error_handler
"Failed to lofiadm ${iso_file}"
238 mount
-F hsfs
"${isodev}" "${iso_path}" || \
239 error_handler
"Failed to mount ${isodev} on ${iso_path}"
243 # Compute the size in MB for the new USB image.
244 # Use ISO file size + 20% to account for smaller block size on UFS
247 usb_size
=$
(stat
-f '%z' "${iso_file}")
248 usb_size
=$
((usb_size
* 12/10 / (1024*1024)))
251 # Create and mount an empty new USB image.
253 mkfile
-n ${usb_size}M
"${usb_file}" || \
254 error_handler
"Failed to create file ${usb_file}"
256 devs
="$(lofiadm -la "${usb_file}")" || \
257 error_handler
"Failed to lofiadm file ${usb_file}"
260 # Set rdevs by replacing dsk with rdsk in devs
262 rdevs
=/dev
/rdsk
${devs#/dev/dsk}
265 # for newfs and installboot
266 rs0devs
=${rdevs%p0}s0
267 # for prtvtoc | fmthard
268 rs2devs
=${rdevs%p0}s2
271 # create Solaris2 partition
274 prtvtoc
"${rs2devs}" | nawk
'
275 /^[^\*]/ { r = $1; for(n = 1; n <= NF; n++) vtoc[r,n] = $n }
280 vtoc[0,4] = vtoc[8,6] + 1;
281 vtoc[0,5] = vtoc[2,6] - vtoc[8,6];
282 vtoc[0,6] = vtoc[2,6];
283 printf("\t%d\t%d\t%02d\t%d\t%d\t%d\n",
284 vtoc[0,1], vtoc[0,2], vtoc[0,3], vtoc[0,4], vtoc[0,5], vtoc[0,6]);
285 printf("\t%d\t%d\t%02d\t%d\t%d\t%d\n",
286 vtoc[2,1], vtoc[2,2], vtoc[2,3], vtoc[2,4], vtoc[2,5], vtoc[2,6]);
287 printf("\t%d\t%d\t%02d\t%d\t%d\t%d\n",
288 vtoc[8,1], vtoc[8,2], vtoc[8,3], vtoc[8,4], vtoc[8,5], vtoc[8,6]);
289 }' | fmthard
-s- "${rs2devs}"
291 # newfs doesn't ask questions if stdin isn't a tty.
292 newfs
"${rs0devs}" </dev
/null || \
293 error_handler
"Failed to construct the UFS file system ${rs0devs}"
295 mount
-o nologging
"${s0devs}" "${usb_path}" || \
296 error_handler
"Failed to mount construct the UFS file system ${rs0devs}"
299 # Copy the contents of the ISO file to the new USB image.
301 print
"Copying ISO contents to USB image..."
302 (cd "${iso_path}"; find .
-print |
cpio -pmudV "${usb_path}")
307 installboot
-mf "${usb_path}/boot/pmbr" "${usb_path}/boot/gptzfsboot" \
311 # Set the file protections for the new USB image.
313 chmod 644 "${usb_file}"
316 # unmounting, and uninstalling the lofi'ed devices
320 printf "=== %s completed at %s\n\n" "$0" "$(date)"