xref: /titanic_51/usr/src/cmd/boot/scripts/create_ramdisk.ksh (revision 6451fdbca2f79129a3a09d2fe3f6dd4d062bebff)
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
65#
66# mkisofs on s8 doesn't support functionality used by GRUB boot.
67# Use ufs format for boot archive instead.
68#
69release=`uname -r`
70if [ "$release" = "5.8" ]; then
71   format=ufs
72fi
73
74shift `expr $OPTIND - 1`
75
76if [ $# -eq 1 ]; then
77	ALT_ROOT=$1
78	echo "Creating ram disk on ${ALT_ROOT}"
79fi
80
81# make directory for temp files safely
82rm -rf ${rddir}
83mkdir ${rddir}
84
85# Clean up upon exit.
86trap 'cleanup' EXIT
87
88function cleanup {
89	umount -f ${rdmnt} 2>/dev/null
90	lofiadm -d ${rdfile} 2>/dev/null
91	rm -fr ${rddir} 2> /dev/null
92}
93
94function getsize {
95	# Estimate image size, add %10 overhead for ufs stuff
96	total_size=0
97	for file in $filelist
98	do
99		du -sk ${ALT_ROOT}/${file} | read size name
100		(( total_size += size ))
101	done
102	(( total_size += total_size * 10 / 100 ))
103}
104
105function create_ufs
106{
107	# should we exclude amd64 binaries?
108	[ $is_amd64 -eq 0 ] && NO_AMD64="-name amd64 -prune"
109
110	# calculate image size
111	getsize
112
113	mkfile ${total_size}k ${rdfile}
114	lofidev=`lofiadm -a ${rdfile}`
115	newfs ${lofidev} < /dev/null 2> /dev/null
116	mkdir ${rdmnt}
117	mount -F mntfs mnttab /etc/mnttab > /dev/null 2>&1
118	mount -o nologging ${lofidev} ${rdmnt}
119
120
121	# do the actual copy
122	cd /${ALT_ROOT}
123	find $filelist -print ${NO_AMD64} 2> /dev/null | \
124	     cpio -pdum ${rdmnt} 2> /dev/null
125	umount ${rdmnt}
126	lofiadm -d ${rdfile}
127	rmdir ${rdmnt}
128	gzip -c ${rdfile} > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
129}
130
131function create_isofs
132{
133	# should we exclude amd64 binaries?
134	[ $is_amd64 = 0 ] && NO_AMD64="-m amd64"
135
136	# create image directory seed with graft points
137	mkdir ${rdmnt}
138	files=
139	isocmd="mkisofs -quiet -graft-points -dlrDJN -relaxed-filenames ${NO_AMD64}"
140	for path in $filelist
141	do
142		if [ -d ${ALT_ROOT}/$path ]; then
143			isocmd="$isocmd $path/=${ALT_ROOT}/$path"
144			mkdir -p ${rdmnt}/$path
145		elif [ -f ${ALT_ROOT}/$path ]; then
146			files="$files $path"
147		fi
148	done
149	cd /${ALT_ROOT}
150	find $files 2> /dev/null | cpio -pdum ${rdmnt} 2> /dev/null
151	isocmd="$isocmd ${rdmnt}"
152	rm -f ${errlog}
153	${isocmd} 2> ${errlog} | gzip > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
154	if [ -s ${errlog} ]; then
155		grep Error: ${errlog} >/dev/null 2>&1
156		if [ $? -eq 0 ]; then
157			grep Error: ${errlog}
158			rm -f ${ALT_ROOT}/${BOOT_ARCHIVE}-new
159		fi
160	fi
161	rm -f ${errlog}
162}
163
164#
165# get filelist
166#
167files=${ALT_ROOT}/boot/solaris/filelist.ramdisk
168if [ -f ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk ]; then
169	files="$files ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk"
170fi
171filelist=`cat $files | sort | uniq`
172
173#
174# decide if cpu is amd64 capable
175#
176prtconf -v /devices | grep CPU_not_amd64 > /dev/null 2>&1
177is_amd64=$?
178
179echo "updating ${ALT_ROOT}/${BOOT_ARCHIVE}...this may take a minute"
180
181if [ $format = "ufs" ]; then
182	create_ufs
183else
184	create_isofs
185fi
186
187if [ ! -f ${ALT_ROOT}/${BOOT_ARCHIVE}-new ]; then
188	echo "update of ${ALT_ROOT}/${BOOT_ARCHIVE} failed"
189	rm -rf ${rddir}
190	exit 1
191fi
192
193#
194# For the diskless case, hardlink archive to /boot to make it
195# visible via tftp. /boot is lofs mounted under /tftpboot/<hostname>.
196# NOTE: this script must work on both client and server
197#
198grep "[	 ]/[	 ]*nfs[	 ]" $ALT_ROOT/etc/vfstab > /dev/null
199if [ $? = 0 ]; then
200	mv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
201	rm -f ${ALT_ROOT}/boot/boot_archive
202	ln ${ALT_ROOT}/${BOOT_ARCHIVE} ${ALT_ROOT}/boot/boot_archive
203	rm -rf ${rddir}
204	exit
205fi
206
207lockfs -f /$ALT_ROOT
208mv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
209lockfs -f /$ALT_ROOT
210
211rm -rf ${rddir}
212