xref: /onnv-gate/usr/src/cmd/itutools/mkbootmedia.ksh (revision 10661:789e162d8de6)
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
31readonly PROG=$0
32MKISOFS=/usr/bin/mkisofs
33ELTORITO=boot/grub/stage2_eltorito	# relative to $MEDIA_ROOT
34SPARCBOOT=boot/hsfs.bootblock
35CAT=/usr/bin/cat
36CP=/usr/bin/cp
37RM=/usr/bin/rm
38UNAME=/usr/bin/uname
39MACH=`$UNAME -p`
40BOOTBLOCK=
41GREP=/usr/bin/grep
42
43# for gettext
44TEXTDOMAIN=SUNW_OST_OSCMD
45export TEXTDOMAIN
46
47
48function 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#
60LABEL=
61VERBOSITY=0
62
63while getopts ':l:v' opt
64do
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
79done
80shift 'OPTIND - 1'
81
82if (( $# != 2 ))
83then
84	usage
85	exit 1
86fi
87
88MEDIA_ROOT=$1
89ISOIMAGE=$2
90
91if [ ! -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
94fi
95
96# Verify $MEDIA_ROOT is a Solaris install media (Solaris 10 Update 1 or later)
97if [[ ! -d $(echo "$MEDIA_ROOT"/Solaris*/Tools/Boot) ]]; then
98	gettext "$MEDIA_ROOT is not Solaris install media.\n"
99	exit 1
100fi
101
102# If no label specified use the Solaris_* version under $MEDIA_ROOT
103if [[ -z "$LABEL" ]]; then
104	LABEL=$(echo "$MEDIA_ROOT"/Solaris*)
105	LABEL=${LABEL##*/}
106fi
107
108# If $ISOIMAGE exists, verify it's writable.
109if [[ -e "$ISOIMAGE" && ! -w "$ISOIMAGE" ]]; then
110	gettext "$ISOIMAGE exists but is not writable.\n"
111	exit 1
112fi
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
118if [[ "$MACH" = "i386" && ! -w "$MEDIA_ROOT/$ELTORITO" ]]; then
119	gettext "$MEDIA_ROOT/$ELTORITO is not writable.\n"
120	exit 1
121fi
122
123# Check that we've got mkisofs installed
124if [[ ! -f "$MKISOFS" || ! -x "$MKISOFS" ]]; then
125    gettext "Cannot find $f\n"
126    exit 1
127fi
128
129
130# Determine mkisofs' verbose flag depending on $VERBOSITY.
131case $VERBOSITY in
1320)	VERBOSE_FLAG=-quiet
133	;;
1341)	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	;;
144esac
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
150if [[ "$MACH" = "i386" ]]
151then
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
157else
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
164fi
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
173sparc_ISOARGS="-G $BOOTBLOCK -B ... -joliet-long -R -U"
174i386_ISOARGS="-b boot/grub/stage2_eltorito -boot-info-table "
175i386_ISOARGS="$i386_ISOARGS -boot-load-size 4 -c .catalog -d -N "
176i386_ISOARGS="$i386_ISOARGS -no-emul-boot -r -relaxed-filenames"
177if [[ "$MACH" = "i386" ]]
178then
179	ISOARGS=$i386_ISOARGS
180else
181	ISOARGS=$sparc_ISOARGS
182fi
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
193