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
60Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
70Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
80Sstevel@tonic-gate# with the License.
90Sstevel@tonic-gate#
100Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
110Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
120Sstevel@tonic-gate# See the License for the specific language governing permissions
130Sstevel@tonic-gate# and limitations under the License.
140Sstevel@tonic-gate#
150Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
160Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
170Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
180Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
190Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
200Sstevel@tonic-gate#
210Sstevel@tonic-gate# CDDL HEADER END
220Sstevel@tonic-gate#
230Sstevel@tonic-gate
240Sstevel@tonic-gate# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate# Use is subject to license terms.
260Sstevel@tonic-gate
270Sstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate#
300Sstevel@tonic-gate# basic setup
310Sstevel@tonic-gate#
32174Sjgrddir=/tmp/create_ramdisk.$$.tmp
33174Sjgrdfile=${rddir}/rd.file
34174Sjgrdmnt=${rddir}/rd.mount
35174Sjgerrlog=${rddir}/rd.errlog
36174Sjg
370Sstevel@tonic-gateformat=ufs
380Sstevel@tonic-gateALT_ROOT=
390Sstevel@tonic-gateNO_AMD64=
400Sstevel@tonic-gate
410Sstevel@tonic-gateBOOT_ARCHIVE=platform/i86pc/boot_archive
420Sstevel@tonic-gate
430Sstevel@tonic-gateexport PATH=${PATH}:/usr/sbin:/usr/bin:/sbin
440Sstevel@tonic-gate
450Sstevel@tonic-gate#
460Sstevel@tonic-gate# Parse options
470Sstevel@tonic-gate#
480Sstevel@tonic-gatewhile getopts R: OPT 2> /dev/null
490Sstevel@tonic-gatedo
500Sstevel@tonic-gate        case $OPT in
510Sstevel@tonic-gate        R)      ALT_ROOT="$OPTARG"
520Sstevel@tonic-gate		if [ "$ALT_ROOT" != "/" ]; then
530Sstevel@tonic-gate			echo "Creating ram disk on ${ALT_ROOT}"
540Sstevel@tonic-gate		fi
550Sstevel@tonic-gate		;;
560Sstevel@tonic-gate        ?)      echo Usage: ${0##*/}: [-R \<root\>]
570Sstevel@tonic-gate		exit ;;
580Sstevel@tonic-gate        esac
590Sstevel@tonic-gatedone
600Sstevel@tonic-gate
610Sstevel@tonic-gateif [ -x /usr/bin/mkisofs -o -x /tmp/bfubin/mkisofs ] ; then
620Sstevel@tonic-gate	format=isofs
630Sstevel@tonic-gatefi
640Sstevel@tonic-gate
650Sstevel@tonic-gateshift `expr $OPTIND - 1`
660Sstevel@tonic-gate
670Sstevel@tonic-gateif [ $# -eq 1 ]; then
680Sstevel@tonic-gate	ALT_ROOT=$1
690Sstevel@tonic-gate	echo "Creating ram disk on ${ALT_ROOT}"
700Sstevel@tonic-gatefi
710Sstevel@tonic-gate
72174Sjg# make directory for temp files safely
73174Sjgrm -rf ${rddir}
74174Sjgmkdir ${rddir}
75174Sjg
760Sstevel@tonic-gate# Clean up upon exit.
770Sstevel@tonic-gatetrap 'cleanup' EXIT
780Sstevel@tonic-gate
790Sstevel@tonic-gatefunction cleanup {
80174Sjg	umount -f ${rdmnt} 2>/dev/null
81174Sjg	lofiadm -d ${rdfile} 2>/dev/null
82174Sjg	rm -fr ${rddir} 2> /dev/null
830Sstevel@tonic-gate}
840Sstevel@tonic-gate
850Sstevel@tonic-gatefunction getsize {
860Sstevel@tonic-gate	# Estimate image size, add %10 overhead for ufs stuff
870Sstevel@tonic-gate	total_size=0
880Sstevel@tonic-gate	for file in $filelist
890Sstevel@tonic-gate	do
900Sstevel@tonic-gate		du -sk ${ALT_ROOT}/${file} | read size name
910Sstevel@tonic-gate		(( total_size += size ))
920Sstevel@tonic-gate	done
930Sstevel@tonic-gate	(( total_size += total_size * 10 / 100 ))
940Sstevel@tonic-gate}
950Sstevel@tonic-gate
960Sstevel@tonic-gatefunction create_ufs
970Sstevel@tonic-gate{
980Sstevel@tonic-gate	# should we exclude amd64 binaries?
990Sstevel@tonic-gate	[ $is_amd64 -eq 0 ] && NO_AMD64="-name amd64 -prune"
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate	# calculate image size
1020Sstevel@tonic-gate	getsize
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate	mkfile ${total_size}k ${rdfile}
1050Sstevel@tonic-gate	lofidev=`lofiadm -a ${rdfile}`
1060Sstevel@tonic-gate	newfs ${lofidev} < /dev/null 2> /dev/null
1070Sstevel@tonic-gate	mkdir ${rdmnt}
1080Sstevel@tonic-gate	mount -F mntfs mnttab /etc/mnttab > /dev/null 2>&1
1090Sstevel@tonic-gate	mount -o nologging ${lofidev} ${rdmnt}
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate	# do the actual copy
1130Sstevel@tonic-gate	cd /${ALT_ROOT}
114*387Ssetje	find $filelist -print ${NO_AMD64} 2> /dev/null | \
115*387Ssetje	     cpio -pdum ${rdmnt} 2> /dev/null
1160Sstevel@tonic-gate	umount ${rdmnt}
1170Sstevel@tonic-gate	lofiadm -d ${rdfile}
1180Sstevel@tonic-gate	rmdir ${rdmnt}
1190Sstevel@tonic-gate	gzip -c ${rdfile} > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
1200Sstevel@tonic-gate}
1210Sstevel@tonic-gate
1220Sstevel@tonic-gatefunction create_isofs
1230Sstevel@tonic-gate{
1240Sstevel@tonic-gate	# should we exclude amd64 binaries?
1250Sstevel@tonic-gate	[ $is_amd64 = 0 ] && NO_AMD64="-m amd64"
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate	# create image directory seed with graft points
1280Sstevel@tonic-gate	mkdir ${rdmnt}
1290Sstevel@tonic-gate	files=
1300Sstevel@tonic-gate	isocmd="mkisofs -quiet -graft-points -dlrDJN -relaxed-filenames ${NO_AMD64}"
1310Sstevel@tonic-gate	for path in $filelist
1320Sstevel@tonic-gate	do
1330Sstevel@tonic-gate		if [ -d ${ALT_ROOT}/$path ]; then
1340Sstevel@tonic-gate			isocmd="$isocmd $path/=${ALT_ROOT}/$path"
1350Sstevel@tonic-gate			mkdir -p ${rdmnt}/$path
1360Sstevel@tonic-gate		elif [ -f ${ALT_ROOT}/$path ]; then
1370Sstevel@tonic-gate			files="$files $path"
1380Sstevel@tonic-gate		fi
1390Sstevel@tonic-gate	done
1400Sstevel@tonic-gate	cd /${ALT_ROOT}
1410Sstevel@tonic-gate	find $files 2> /dev/null | cpio -pdum ${rdmnt} 2> /dev/null
1420Sstevel@tonic-gate	isocmd="$isocmd ${rdmnt}"
143174Sjg	rm -f ${errlog}
144174Sjg	${isocmd} 2> ${errlog} | gzip > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
145174Sjg	if [ -s ${errlog} ]; then
146174Sjg		grep Error: ${errlog} >/dev/null 2>&1
147174Sjg		if [ $? -eq 0 ]; then
148174Sjg			grep Error: ${errlog}
149174Sjg			rm -f ${ALT_ROOT}/${BOOT_ARCHIVE}-new
150174Sjg		fi
151174Sjg	fi
152174Sjg	rm -f ${errlog}
1530Sstevel@tonic-gate}
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate#
1560Sstevel@tonic-gate# get filelist
1570Sstevel@tonic-gate#
158174Sjgfiles=${ALT_ROOT}/boot/solaris/filelist.ramdisk
1590Sstevel@tonic-gateif [ -f ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk ]; then
160174Sjg	files="$files ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk"
1610Sstevel@tonic-gatefi
162174Sjgfilelist=`cat $files | sort | uniq`
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate#
1650Sstevel@tonic-gate# decide if cpu is amd64 capable
1660Sstevel@tonic-gate#
1670Sstevel@tonic-gateprtconf -v /devices | grep CPU_not_amd64 > /dev/null 2>&1
1680Sstevel@tonic-gateis_amd64=$?
1690Sstevel@tonic-gate
1700Sstevel@tonic-gateecho "updating ${ALT_ROOT}/${BOOT_ARCHIVE}...this may take a minute"
1710Sstevel@tonic-gate
1720Sstevel@tonic-gateif [ $format = "ufs" ]; then
1730Sstevel@tonic-gate	create_ufs
1740Sstevel@tonic-gateelse
1750Sstevel@tonic-gate	create_isofs
1760Sstevel@tonic-gatefi
1770Sstevel@tonic-gate
178174Sjgif [ ! -f ${ALT_ROOT}/${BOOT_ARCHIVE}-new ]; then
179174Sjg	echo "update of ${ALT_ROOT}/${BOOT_ARCHIVE} failed"
180174Sjg	rm -rf ${rddir}
181174Sjg	exit 1
182174Sjgfi
183174Sjg
1840Sstevel@tonic-gate#
1850Sstevel@tonic-gate# For the diskless case, hardlink archive to /boot to make it
1860Sstevel@tonic-gate# visible via tftp. /boot is lofs mounted under /tftpboot/<hostname>.
1870Sstevel@tonic-gate# NOTE: this script must work on both client and server
1880Sstevel@tonic-gate#
1890Sstevel@tonic-gategrep "[	 ]/[	 ]*nfs[	 ]" $ALT_ROOT/etc/vfstab > /dev/null
1900Sstevel@tonic-gateif [ $? = 0 ]; then
1910Sstevel@tonic-gate	mv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
1920Sstevel@tonic-gate	rm -f ${ALT_ROOT}/boot/boot_archive
1930Sstevel@tonic-gate	ln ${ALT_ROOT}/${BOOT_ARCHIVE} ${ALT_ROOT}/boot/boot_archive
194174Sjg	rm -rf ${rddir}
1950Sstevel@tonic-gate	exit
1960Sstevel@tonic-gatefi
1970Sstevel@tonic-gate
1980Sstevel@tonic-gatelockfs -f /$ALT_ROOT
1990Sstevel@tonic-gatemv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
2000Sstevel@tonic-gatelockfs -f /$ALT_ROOT
201174Sjg
202174Sjgrm -rf ${rddir}
203