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