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
65*621Svikram#
66*621Svikram# mkisofs on s8 doesn't support functionality used by GRUB boot.
67*621Svikram# Use ufs format for boot archive instead.
68*621Svikram#
69*621Svikramrelease=`uname -r`
70*621Svikramif [ "$release" = "5.8" ]; then
71*621Svikram   format=ufs
72*621Svikramfi
73*621Svikram
740Sstevel@tonic-gateshift `expr $OPTIND - 1`
750Sstevel@tonic-gate
760Sstevel@tonic-gateif [ $# -eq 1 ]; then
770Sstevel@tonic-gate	ALT_ROOT=$1
780Sstevel@tonic-gate	echo "Creating ram disk on ${ALT_ROOT}"
790Sstevel@tonic-gatefi
800Sstevel@tonic-gate
81174Sjg# make directory for temp files safely
82174Sjgrm -rf ${rddir}
83174Sjgmkdir ${rddir}
84174Sjg
850Sstevel@tonic-gate# Clean up upon exit.
860Sstevel@tonic-gatetrap 'cleanup' EXIT
870Sstevel@tonic-gate
880Sstevel@tonic-gatefunction cleanup {
89174Sjg	umount -f ${rdmnt} 2>/dev/null
90174Sjg	lofiadm -d ${rdfile} 2>/dev/null
91174Sjg	rm -fr ${rddir} 2> /dev/null
920Sstevel@tonic-gate}
930Sstevel@tonic-gate
940Sstevel@tonic-gatefunction getsize {
950Sstevel@tonic-gate	# Estimate image size, add %10 overhead for ufs stuff
960Sstevel@tonic-gate	total_size=0
970Sstevel@tonic-gate	for file in $filelist
980Sstevel@tonic-gate	do
990Sstevel@tonic-gate		du -sk ${ALT_ROOT}/${file} | read size name
1000Sstevel@tonic-gate		(( total_size += size ))
1010Sstevel@tonic-gate	done
1020Sstevel@tonic-gate	(( total_size += total_size * 10 / 100 ))
1030Sstevel@tonic-gate}
1040Sstevel@tonic-gate
1050Sstevel@tonic-gatefunction create_ufs
1060Sstevel@tonic-gate{
1070Sstevel@tonic-gate	# should we exclude amd64 binaries?
1080Sstevel@tonic-gate	[ $is_amd64 -eq 0 ] && NO_AMD64="-name amd64 -prune"
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate	# calculate image size
1110Sstevel@tonic-gate	getsize
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate	mkfile ${total_size}k ${rdfile}
1140Sstevel@tonic-gate	lofidev=`lofiadm -a ${rdfile}`
1150Sstevel@tonic-gate	newfs ${lofidev} < /dev/null 2> /dev/null
1160Sstevel@tonic-gate	mkdir ${rdmnt}
1170Sstevel@tonic-gate	mount -F mntfs mnttab /etc/mnttab > /dev/null 2>&1
1180Sstevel@tonic-gate	mount -o nologging ${lofidev} ${rdmnt}
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate	# do the actual copy
1220Sstevel@tonic-gate	cd /${ALT_ROOT}
123387Ssetje	find $filelist -print ${NO_AMD64} 2> /dev/null | \
124387Ssetje	     cpio -pdum ${rdmnt} 2> /dev/null
1250Sstevel@tonic-gate	umount ${rdmnt}
1260Sstevel@tonic-gate	lofiadm -d ${rdfile}
1270Sstevel@tonic-gate	rmdir ${rdmnt}
1280Sstevel@tonic-gate	gzip -c ${rdfile} > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
1290Sstevel@tonic-gate}
1300Sstevel@tonic-gate
1310Sstevel@tonic-gatefunction create_isofs
1320Sstevel@tonic-gate{
1330Sstevel@tonic-gate	# should we exclude amd64 binaries?
1340Sstevel@tonic-gate	[ $is_amd64 = 0 ] && NO_AMD64="-m amd64"
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate	# create image directory seed with graft points
1370Sstevel@tonic-gate	mkdir ${rdmnt}
1380Sstevel@tonic-gate	files=
1390Sstevel@tonic-gate	isocmd="mkisofs -quiet -graft-points -dlrDJN -relaxed-filenames ${NO_AMD64}"
1400Sstevel@tonic-gate	for path in $filelist
1410Sstevel@tonic-gate	do
1420Sstevel@tonic-gate		if [ -d ${ALT_ROOT}/$path ]; then
1430Sstevel@tonic-gate			isocmd="$isocmd $path/=${ALT_ROOT}/$path"
1440Sstevel@tonic-gate			mkdir -p ${rdmnt}/$path
1450Sstevel@tonic-gate		elif [ -f ${ALT_ROOT}/$path ]; then
1460Sstevel@tonic-gate			files="$files $path"
1470Sstevel@tonic-gate		fi
1480Sstevel@tonic-gate	done
1490Sstevel@tonic-gate	cd /${ALT_ROOT}
1500Sstevel@tonic-gate	find $files 2> /dev/null | cpio -pdum ${rdmnt} 2> /dev/null
1510Sstevel@tonic-gate	isocmd="$isocmd ${rdmnt}"
152174Sjg	rm -f ${errlog}
153174Sjg	${isocmd} 2> ${errlog} | gzip > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
154174Sjg	if [ -s ${errlog} ]; then
155174Sjg		grep Error: ${errlog} >/dev/null 2>&1
156174Sjg		if [ $? -eq 0 ]; then
157174Sjg			grep Error: ${errlog}
158174Sjg			rm -f ${ALT_ROOT}/${BOOT_ARCHIVE}-new
159174Sjg		fi
160174Sjg	fi
161174Sjg	rm -f ${errlog}
1620Sstevel@tonic-gate}
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate#
1650Sstevel@tonic-gate# get filelist
1660Sstevel@tonic-gate#
167174Sjgfiles=${ALT_ROOT}/boot/solaris/filelist.ramdisk
1680Sstevel@tonic-gateif [ -f ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk ]; then
169174Sjg	files="$files ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk"
1700Sstevel@tonic-gatefi
171174Sjgfilelist=`cat $files | sort | uniq`
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate#
1740Sstevel@tonic-gate# decide if cpu is amd64 capable
1750Sstevel@tonic-gate#
1760Sstevel@tonic-gateprtconf -v /devices | grep CPU_not_amd64 > /dev/null 2>&1
1770Sstevel@tonic-gateis_amd64=$?
1780Sstevel@tonic-gate
1790Sstevel@tonic-gateecho "updating ${ALT_ROOT}/${BOOT_ARCHIVE}...this may take a minute"
1800Sstevel@tonic-gate
1810Sstevel@tonic-gateif [ $format = "ufs" ]; then
1820Sstevel@tonic-gate	create_ufs
1830Sstevel@tonic-gateelse
1840Sstevel@tonic-gate	create_isofs
1850Sstevel@tonic-gatefi
1860Sstevel@tonic-gate
187174Sjgif [ ! -f ${ALT_ROOT}/${BOOT_ARCHIVE}-new ]; then
188174Sjg	echo "update of ${ALT_ROOT}/${BOOT_ARCHIVE} failed"
189174Sjg	rm -rf ${rddir}
190174Sjg	exit 1
191174Sjgfi
192174Sjg
1930Sstevel@tonic-gate#
1940Sstevel@tonic-gate# For the diskless case, hardlink archive to /boot to make it
1950Sstevel@tonic-gate# visible via tftp. /boot is lofs mounted under /tftpboot/<hostname>.
1960Sstevel@tonic-gate# NOTE: this script must work on both client and server
1970Sstevel@tonic-gate#
1980Sstevel@tonic-gategrep "[	 ]/[	 ]*nfs[	 ]" $ALT_ROOT/etc/vfstab > /dev/null
1990Sstevel@tonic-gateif [ $? = 0 ]; then
2000Sstevel@tonic-gate	mv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
2010Sstevel@tonic-gate	rm -f ${ALT_ROOT}/boot/boot_archive
2020Sstevel@tonic-gate	ln ${ALT_ROOT}/${BOOT_ARCHIVE} ${ALT_ROOT}/boot/boot_archive
203174Sjg	rm -rf ${rddir}
2040Sstevel@tonic-gate	exit
2050Sstevel@tonic-gatefi
2060Sstevel@tonic-gate
2070Sstevel@tonic-gatelockfs -f /$ALT_ROOT
2080Sstevel@tonic-gatemv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
2090Sstevel@tonic-gatelockfs -f /$ALT_ROOT
210174Sjg
211174Sjgrm -rf ${rddir}
212