10Sstevel@tonic-gate#!/bin/ksh -p
20Sstevel@tonic-gate#
30Sstevel@tonic-gate# CDDL HEADER START
40Sstevel@tonic-gate#
50Sstevel@tonic-gate# The contents of this file are subject to the terms of the
62334Ssetje# Common Development and Distribution License (the "License").
72334Ssetje# You may not use this file except in compliance with the License.
80Sstevel@tonic-gate#
90Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate# See the License for the specific language governing permissions
120Sstevel@tonic-gate# and limitations under the License.
130Sstevel@tonic-gate#
140Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate#
200Sstevel@tonic-gate# CDDL HEADER END
210Sstevel@tonic-gate#
220Sstevel@tonic-gate
232334Ssetje# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate# Use is subject to license terms.
250Sstevel@tonic-gate
262334Ssetje# ident	"%Z%%M%	%I%	%E% SMI"
27174Sjg
280Sstevel@tonic-gateformat=ufs
290Sstevel@tonic-gateALT_ROOT=
300Sstevel@tonic-gateNO_AMD64=
310Sstevel@tonic-gate
320Sstevel@tonic-gateBOOT_ARCHIVE=platform/i86pc/boot_archive
330Sstevel@tonic-gate
342334Ssetjeexport PATH=$PATH:/usr/sbin:/usr/bin:/sbin
350Sstevel@tonic-gate
360Sstevel@tonic-gate#
370Sstevel@tonic-gate# Parse options
380Sstevel@tonic-gate#
390Sstevel@tonic-gatewhile getopts R: OPT 2> /dev/null
400Sstevel@tonic-gatedo
410Sstevel@tonic-gate        case $OPT in
420Sstevel@tonic-gate        R)      ALT_ROOT="$OPTARG"
430Sstevel@tonic-gate		if [ "$ALT_ROOT" != "/" ]; then
442334Ssetje			echo "Creating ram disk for $ALT_ROOT"
450Sstevel@tonic-gate		fi
460Sstevel@tonic-gate		;;
470Sstevel@tonic-gate        ?)      echo Usage: ${0##*/}: [-R \<root\>]
480Sstevel@tonic-gate		exit ;;
490Sstevel@tonic-gate        esac
500Sstevel@tonic-gatedone
510Sstevel@tonic-gate
520Sstevel@tonic-gateif [ -x /usr/bin/mkisofs -o -x /tmp/bfubin/mkisofs ] ; then
530Sstevel@tonic-gate	format=isofs
540Sstevel@tonic-gatefi
550Sstevel@tonic-gate
56621Svikram#
57621Svikram# mkisofs on s8 doesn't support functionality used by GRUB boot.
58621Svikram# Use ufs format for boot archive instead.
59621Svikram#
60621Svikramrelease=`uname -r`
61621Svikramif [ "$release" = "5.8" ]; then
62621Svikram   format=ufs
63621Svikramfi
64621Svikram
650Sstevel@tonic-gateshift `expr $OPTIND - 1`
660Sstevel@tonic-gate
670Sstevel@tonic-gateif [ $# -eq 1 ]; then
682334Ssetje	ALT_ROOT="$1"
692334Ssetje	echo "Creating ram disk for $ALT_ROOT"
700Sstevel@tonic-gatefi
710Sstevel@tonic-gate
722334Ssetjefunction cleanup
732334Ssetje{
742334Ssetje	umount -f "$rdmnt" 2>/dev/null
752334Ssetje	lofiadm -d "$rdfile" 2>/dev/null
762334Ssetje	rm -fr "$rddir" 2> /dev/null
770Sstevel@tonic-gate}
780Sstevel@tonic-gate
792334Ssetjefunction getsize
802334Ssetje{
810Sstevel@tonic-gate	# Estimate image size, add %10 overhead for ufs stuff
820Sstevel@tonic-gate	total_size=0
830Sstevel@tonic-gate	for file in $filelist
840Sstevel@tonic-gate	do
852334Ssetje		if [ -e "$ALT_ROOT/$file" ] ; then
862334Ssetje			du -sk "$ALT_ROOT/$file" | read size name
87820Ssetje			(( total_size += size ))
88820Ssetje		fi
890Sstevel@tonic-gate	done
900Sstevel@tonic-gate	(( total_size += total_size * 10 / 100 ))
910Sstevel@tonic-gate}
920Sstevel@tonic-gate
930Sstevel@tonic-gatefunction create_ufs
940Sstevel@tonic-gate{
950Sstevel@tonic-gate	# should we exclude amd64 binaries?
960Sstevel@tonic-gate	[ $is_amd64 -eq 0 ] && NO_AMD64="-name amd64 -prune"
970Sstevel@tonic-gate
980Sstevel@tonic-gate
992334Ssetje	mkfile ${total_size}k "$rdfile"
1002334Ssetje	lofidev=`lofiadm -a "$rdfile"`
1012334Ssetje	newfs $lofidev < /dev/null 2> /dev/null
1022334Ssetje	mkdir "$rdmnt"
1030Sstevel@tonic-gate	mount -F mntfs mnttab /etc/mnttab > /dev/null 2>&1
1042334Ssetje	mount -o nologging $lofidev "$rdmnt"
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate	# do the actual copy
1072334Ssetje	cd "/$ALT_ROOT"
1082334Ssetje
1092334Ssetje	find $filelist -print $NO_AMD64 2> /dev/null | \
1102334Ssetje	     cpio -pdum "$rdmnt" 2> /dev/null
1112334Ssetje	umount "$rdmnt"
1122334Ssetje	lofiadm -d "$rdfile"
1132334Ssetje	rmdir "$rdmnt"
1142334Ssetje
1152334Ssetje	# Check if gzip exists in /usr/bin, so we only try to run gzip
1162334Ssetje	# on systems that have gzip. Then run gzip out of the patch to
1172334Ssetje	# pick it up from bfubin or something like that if needed.
1182334Ssetje	#
1192334Ssetje	if [ -x /usr/bin/gzip ] ; then
1202334Ssetje		gzip -c "$rdfile" > "$ALT_ROOT/$BOOT_ARCHIVE-new"
1212334Ssetje	else
1222334Ssetje		cat "$rdfile" > "$ALT_ROOT/$BOOT_ARCHIVE-new"
1232334Ssetje	fi
1240Sstevel@tonic-gate}
1250Sstevel@tonic-gate
1260Sstevel@tonic-gatefunction create_isofs
1270Sstevel@tonic-gate{
1280Sstevel@tonic-gate	# should we exclude amd64 binaries?
1290Sstevel@tonic-gate	[ $is_amd64 = 0 ] && NO_AMD64="-m amd64"
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate	# create image directory seed with graft points
1322334Ssetje	mkdir "$rdmnt"
1330Sstevel@tonic-gate	files=
1342334Ssetje	isocmd="mkisofs -quiet -graft-points -dlrDJN -relaxed-filenames $NO_AMD64"
1350Sstevel@tonic-gate	for path in $filelist
1360Sstevel@tonic-gate	do
1372334Ssetje		if [ -d "$ALT_ROOT/$path" ]; then
1382334Ssetje			isocmd="$isocmd $path/=\"$ALT_ROOT/$path\""
1392334Ssetje			mkdir -p "$rdmnt/$path"
1402334Ssetje		elif [ -f "$ALT_ROOT/$path" ]; then
1410Sstevel@tonic-gate			files="$files $path"
1420Sstevel@tonic-gate		fi
1430Sstevel@tonic-gate	done
1442334Ssetje	cd "/$ALT_ROOT"
1452334Ssetje	find $files 2> /dev/null | cpio -pdum "$rdmnt" 2> /dev/null
1462334Ssetje	isocmd="$isocmd \"$rdmnt\""
1472334Ssetje	rm -f "$errlog"
1482334Ssetje
1492334Ssetje	# Check if gzip exists in /usr/bin, so we only try to run gzip
1502334Ssetje	# on systems that have gzip. Then run gzip out of the patch to
1512334Ssetje	# pick it up from bfubin or something like that if needed.
1522334Ssetje	#
1532334Ssetje	if [ -x /usr/bin/gzip ] ; then
1542334Ssetje		ksh -c "$isocmd" 2> "$errlog" | \
1552334Ssetje		    gzip > "$ALT_ROOT/$BOOT_ARCHIVE-new"
1562334Ssetje	else
1572334Ssetje		ksh -c "$isocmd" 2> "$errlog" > "$ALT_ROOT/$BOOT_ARCHIVE-new"
1582334Ssetje	fi
1592334Ssetje
1602334Ssetje	if [ -s "$errlog" ]; then
1612334Ssetje		grep Error: "$errlog" >/dev/null 2>&1
162174Sjg		if [ $? -eq 0 ]; then
1632334Ssetje			grep Error: "$errlog"
1642334Ssetje			rm -f "$ALT_ROOT/$BOOT_ARCHIVE-new"
165174Sjg		fi
166174Sjg	fi
1672334Ssetje	rm -f "$errlog"
1680Sstevel@tonic-gate}
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate#
1710Sstevel@tonic-gate# get filelist
1720Sstevel@tonic-gate#
173*2345Ssetjefilelist=`cat "$ALT_ROOT/boot/solaris/filelist.ramdisk"`
1742334Ssetjeif [ -f "$ALT_ROOT/etc/boot/solaris/filelist.ramdisk" ]; then
175*2345Ssetje	filelistI=`cat "$ALT_ROOT/etc/boot/solaris/filelist.ramdisk"`
1760Sstevel@tonic-gatefi
177*2345Ssetjefilelist=`echo $filelist $filelistI | sort -u`
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate#
1800Sstevel@tonic-gate# decide if cpu is amd64 capable
1810Sstevel@tonic-gate#
1820Sstevel@tonic-gateprtconf -v /devices | grep CPU_not_amd64 > /dev/null 2>&1
1830Sstevel@tonic-gateis_amd64=$?
1840Sstevel@tonic-gate
1852334Ssetjescratch=tmp
1862334Ssetje
1872334Ssetjeif [ $format = ufs ] ; then
1882334Ssetje	# calculate image size
1892334Ssetje	getsize
1902334Ssetje
1912334Ssetje	# check to see if there is sufficient space in tmpfs
1922334Ssetje	#
1932334Ssetje	tmp_free=`df -b /tmp | tail -1 | awk '{ printf ($2) }'`
1942334Ssetje	(( tmp_free = tmp_free / 2 ))
1952334Ssetje
1962334Ssetje	if [ $total_size -gt $tmp_free  ] ; then
1972334Ssetje		# assumes we have enough scratch space on $ALT_ROOT
1982334Ssetje        	scratch="$ALT_ROOT"
1992334Ssetje	fi
2002334Ssetjefi
2012334Ssetje
2022334Ssetjerddir="/$scratch/create_ramdisk.$$.tmp"
2032334Ssetjerdfile="$rddir/rd.file"
2042334Ssetjerdmnt="$rddir/rd.mount"
2052334Ssetjeerrlog="$rddir/rd.errlog"
2062334Ssetje
2072334Ssetje# make directory for temp files safely
2082334Ssetjerm -rf "$rddir"
2092334Ssetjemkdir "$rddir"
2102334Ssetje
2112334Ssetje# Clean up upon exit.
2122334Ssetjetrap 'cleanup' EXIT
2132334Ssetje
2142334Ssetjeecho "updating $ALT_ROOT/$BOOT_ARCHIVE...this may take a minute"
2150Sstevel@tonic-gate
2160Sstevel@tonic-gateif [ $format = "ufs" ]; then
2170Sstevel@tonic-gate	create_ufs
2180Sstevel@tonic-gateelse
2190Sstevel@tonic-gate	create_isofs
2200Sstevel@tonic-gatefi
2210Sstevel@tonic-gate
2222334Ssetje# sanity check the archive before moving it into place
2232334Ssetje# the file type check also establishes that the file exists at all
2242334Ssetje#
2252334SsetjeARCHIVE_SIZE=`du -k "$ALT_ROOT/$BOOT_ARCHIVE-new" | cut -f 1`
2262334Ssetjefile "$ALT_ROOT/$BOOT_ARCHIVE-new" | grep gzip > /dev/null
2272334Ssetje
2282334Ssetjeif [ $? = 1 ] && [ -x /usr/bin/gzip ] || [ $ARCHIVE_SIZE -lt 5000 ]; then
2292334Ssetje	echo "update of $ALT_ROOT/$BOOT_ARCHIVE failed"
2302334Ssetje	rm -rf "$rddir"
231174Sjg	exit 1
232174Sjgfi
233174Sjg
2340Sstevel@tonic-gate#
2350Sstevel@tonic-gate# For the diskless case, hardlink archive to /boot to make it
2360Sstevel@tonic-gate# visible via tftp. /boot is lofs mounted under /tftpboot/<hostname>.
2370Sstevel@tonic-gate# NOTE: this script must work on both client and server
2380Sstevel@tonic-gate#
2392334Ssetjegrep "[	 ]/[	 ]*nfs[	 ]" "$ALT_ROOT/etc/vfstab" > /dev/null
2400Sstevel@tonic-gateif [ $? = 0 ]; then
2412334Ssetje	mv "$ALT_ROOT/$BOOT_ARCHIVE-new" "$ALT_ROOT/$BOOT_ARCHIVE"
2422334Ssetje	rm -f "$ALT_ROOT/boot/boot_archive"
2432334Ssetje	ln "$ALT_ROOT/$BOOT_ARCHIVE" "$ALT_ROOT/boot/boot_archive"
2442334Ssetje	rm -rf "$rddir"
2450Sstevel@tonic-gate	exit
2460Sstevel@tonic-gatefi
2470Sstevel@tonic-gate
2482334Ssetjelockfs -f "/$ALT_ROOT"
2492334Ssetjemv "$ALT_ROOT/$BOOT_ARCHIVE-new" "$ALT_ROOT/$BOOT_ARCHIVE"
2502334Ssetjelockfs -f "/$ALT_ROOT"
251174Sjg
2522334Ssetjerm -rf "$rddir"
253