1*0Sstevel@tonic-gate#!/bin/ksh -p
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# CDDL HEADER START
4*0Sstevel@tonic-gate#
5*0Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*0Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*0Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*0Sstevel@tonic-gate# with the License.
9*0Sstevel@tonic-gate#
10*0Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*0Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*0Sstevel@tonic-gate# See the License for the specific language governing permissions
13*0Sstevel@tonic-gate# and limitations under the License.
14*0Sstevel@tonic-gate#
15*0Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*0Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*0Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*0Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*0Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*0Sstevel@tonic-gate#
21*0Sstevel@tonic-gate# CDDL HEADER END
22*0Sstevel@tonic-gate#
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gate# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*0Sstevel@tonic-gate# Use is subject to license terms.
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate#
30*0Sstevel@tonic-gate# basic setup
31*0Sstevel@tonic-gate#
32*0Sstevel@tonic-gaterdfile=/tmp/ramdisk.$$
33*0Sstevel@tonic-gaterdmnt=/tmp/rd_mount.$$
34*0Sstevel@tonic-gateformat=ufs
35*0Sstevel@tonic-gateALT_ROOT=
36*0Sstevel@tonic-gateNO_AMD64=
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gateBOOT_ARCHIVE=platform/i86pc/boot_archive
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gateexport PATH=${PATH}:/usr/sbin:/usr/bin:/sbin
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate#
43*0Sstevel@tonic-gate# Parse options
44*0Sstevel@tonic-gate#
45*0Sstevel@tonic-gatewhile getopts R: OPT 2> /dev/null
46*0Sstevel@tonic-gatedo
47*0Sstevel@tonic-gate        case $OPT in
48*0Sstevel@tonic-gate        R)      ALT_ROOT="$OPTARG"
49*0Sstevel@tonic-gate		if [ "$ALT_ROOT" != "/" ]; then
50*0Sstevel@tonic-gate			echo "Creating ram disk on ${ALT_ROOT}"
51*0Sstevel@tonic-gate		fi
52*0Sstevel@tonic-gate		;;
53*0Sstevel@tonic-gate        ?)      echo Usage: ${0##*/}: [-R \<root\>]
54*0Sstevel@tonic-gate		exit ;;
55*0Sstevel@tonic-gate        esac
56*0Sstevel@tonic-gatedone
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gateif [ -x /usr/bin/mkisofs -o -x /tmp/bfubin/mkisofs ] ; then
59*0Sstevel@tonic-gate	format=isofs
60*0Sstevel@tonic-gatefi
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gateshift `expr $OPTIND - 1`
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gateif [ $# -eq 1 ]; then
65*0Sstevel@tonic-gate	ALT_ROOT=$1
66*0Sstevel@tonic-gate	echo "Creating ram disk on ${ALT_ROOT}"
67*0Sstevel@tonic-gatefi
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate# Clean up upon exit.
70*0Sstevel@tonic-gatetrap 'cleanup' EXIT
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gatefunction cleanup {
73*0Sstevel@tonic-gate	umount -f $rdmnt 2>/dev/null
74*0Sstevel@tonic-gate	lofiadm -d $rdfile 2>/dev/null
75*0Sstevel@tonic-gate	rm -fr $rdfile $rdfile.gz $rdmnt 2> /dev/null
76*0Sstevel@tonic-gate}
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gatefunction getsize {
79*0Sstevel@tonic-gate	# Estimate image size, add %10 overhead for ufs stuff
80*0Sstevel@tonic-gate	total_size=0
81*0Sstevel@tonic-gate	for file in $filelist
82*0Sstevel@tonic-gate	do
83*0Sstevel@tonic-gate		du -sk ${ALT_ROOT}/${file} | read size name
84*0Sstevel@tonic-gate		(( total_size += size ))
85*0Sstevel@tonic-gate	done
86*0Sstevel@tonic-gate	(( total_size += total_size * 10 / 100 ))
87*0Sstevel@tonic-gate}
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gatefunction create_ufs
90*0Sstevel@tonic-gate{
91*0Sstevel@tonic-gate	# should we exclude amd64 binaries?
92*0Sstevel@tonic-gate	[ $is_amd64 -eq 0 ] && NO_AMD64="-name amd64 -prune"
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate	# calculate image size
95*0Sstevel@tonic-gate	getsize
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate	mkfile ${total_size}k ${rdfile}
98*0Sstevel@tonic-gate	lofidev=`lofiadm -a ${rdfile}`
99*0Sstevel@tonic-gate	newfs ${lofidev} < /dev/null 2> /dev/null
100*0Sstevel@tonic-gate	mkdir ${rdmnt}
101*0Sstevel@tonic-gate	mount -F mntfs mnttab /etc/mnttab > /dev/null 2>&1
102*0Sstevel@tonic-gate	mount -o nologging ${lofidev} ${rdmnt}
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate	# do the actual copy
106*0Sstevel@tonic-gate	cd /${ALT_ROOT}
107*0Sstevel@tonic-gate	find $filelist -print ${NO_AMD64}| cpio -pdum $rdmnt 2> /dev/null
108*0Sstevel@tonic-gate	umount ${rdmnt}
109*0Sstevel@tonic-gate	lofiadm -d ${rdfile}
110*0Sstevel@tonic-gate	rmdir ${rdmnt}
111*0Sstevel@tonic-gate	gzip -c ${rdfile} > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
112*0Sstevel@tonic-gate}
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gatefunction create_isofs
115*0Sstevel@tonic-gate{
116*0Sstevel@tonic-gate	# should we exclude amd64 binaries?
117*0Sstevel@tonic-gate	[ $is_amd64 = 0 ] && NO_AMD64="-m amd64"
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate	# create image directory seed with graft points
120*0Sstevel@tonic-gate	mkdir ${rdmnt}
121*0Sstevel@tonic-gate	files=
122*0Sstevel@tonic-gate	isocmd="mkisofs -quiet -graft-points -dlrDJN -relaxed-filenames ${NO_AMD64}"
123*0Sstevel@tonic-gate	for path in $filelist
124*0Sstevel@tonic-gate	do
125*0Sstevel@tonic-gate		if [ -d ${ALT_ROOT}/$path ]; then
126*0Sstevel@tonic-gate			isocmd="$isocmd $path/=${ALT_ROOT}/$path"
127*0Sstevel@tonic-gate			mkdir -p ${rdmnt}/$path
128*0Sstevel@tonic-gate		elif [ -f ${ALT_ROOT}/$path ]; then
129*0Sstevel@tonic-gate			files="$files $path"
130*0Sstevel@tonic-gate		else
131*0Sstevel@tonic-gate			echo "/${ALT_ROOT}/$path not present"
132*0Sstevel@tonic-gate		fi
133*0Sstevel@tonic-gate	done
134*0Sstevel@tonic-gate	cd /${ALT_ROOT}
135*0Sstevel@tonic-gate	find $files 2> /dev/null | cpio -pdum ${rdmnt} 2> /dev/null
136*0Sstevel@tonic-gate	isocmd="$isocmd ${rdmnt}"
137*0Sstevel@tonic-gate	${isocmd} 2> /dev/null | gzip > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
138*0Sstevel@tonic-gate}
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate#
141*0Sstevel@tonic-gate# get filelist
142*0Sstevel@tonic-gate#
143*0Sstevel@tonic-gatefilelist=$(< ${ALT_ROOT}/boot/solaris/filelist.ramdisk)
144*0Sstevel@tonic-gateif [ -f ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk ]; then
145*0Sstevel@tonic-gate	filelist="$filelist $(< ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk)"
146*0Sstevel@tonic-gatefi
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate#
149*0Sstevel@tonic-gate# decide if cpu is amd64 capable
150*0Sstevel@tonic-gate#
151*0Sstevel@tonic-gateprtconf -v /devices | grep CPU_not_amd64 > /dev/null 2>&1
152*0Sstevel@tonic-gateis_amd64=$?
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gateecho "updating ${ALT_ROOT}/${BOOT_ARCHIVE}...this may take a minute"
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gateif [ $format = "ufs" ]; then
157*0Sstevel@tonic-gate	create_ufs
158*0Sstevel@tonic-gateelse
159*0Sstevel@tonic-gate	create_isofs
160*0Sstevel@tonic-gatefi
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate#
163*0Sstevel@tonic-gate# For the diskless case, hardlink archive to /boot to make it
164*0Sstevel@tonic-gate# visible via tftp. /boot is lofs mounted under /tftpboot/<hostname>.
165*0Sstevel@tonic-gate# NOTE: this script must work on both client and server
166*0Sstevel@tonic-gate#
167*0Sstevel@tonic-gategrep "[	 ]/[	 ]*nfs[	 ]" $ALT_ROOT/etc/vfstab > /dev/null
168*0Sstevel@tonic-gateif [ $? = 0 ]; then
169*0Sstevel@tonic-gate	mv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
170*0Sstevel@tonic-gate	rm -f ${ALT_ROOT}/boot/boot_archive
171*0Sstevel@tonic-gate	ln ${ALT_ROOT}/${BOOT_ARCHIVE} ${ALT_ROOT}/boot/boot_archive
172*0Sstevel@tonic-gate	exit
173*0Sstevel@tonic-gatefi
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gatelockfs -f /$ALT_ROOT
176*0Sstevel@tonic-gatemv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
177*0Sstevel@tonic-gatelockfs -f /$ALT_ROOT
178