xref: /titanic_44/usr/src/cmd/boot/scripts/create_ramdisk.ksh (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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#
32rdfile=/tmp/ramdisk.$$
33rdmnt=/tmp/rd_mount.$$
34format=ufs
35ALT_ROOT=
36NO_AMD64=
37
38BOOT_ARCHIVE=platform/i86pc/boot_archive
39
40export PATH=${PATH}:/usr/sbin:/usr/bin:/sbin
41
42#
43# Parse options
44#
45while getopts R: OPT 2> /dev/null
46do
47        case $OPT in
48        R)      ALT_ROOT="$OPTARG"
49		if [ "$ALT_ROOT" != "/" ]; then
50			echo "Creating ram disk on ${ALT_ROOT}"
51		fi
52		;;
53        ?)      echo Usage: ${0##*/}: [-R \<root\>]
54		exit ;;
55        esac
56done
57
58if [ -x /usr/bin/mkisofs -o -x /tmp/bfubin/mkisofs ] ; then
59	format=isofs
60fi
61
62shift `expr $OPTIND - 1`
63
64if [ $# -eq 1 ]; then
65	ALT_ROOT=$1
66	echo "Creating ram disk on ${ALT_ROOT}"
67fi
68
69# Clean up upon exit.
70trap 'cleanup' EXIT
71
72function cleanup {
73	umount -f $rdmnt 2>/dev/null
74	lofiadm -d $rdfile 2>/dev/null
75	rm -fr $rdfile $rdfile.gz $rdmnt 2> /dev/null
76}
77
78function getsize {
79	# Estimate image size, add %10 overhead for ufs stuff
80	total_size=0
81	for file in $filelist
82	do
83		du -sk ${ALT_ROOT}/${file} | read size name
84		(( total_size += size ))
85	done
86	(( total_size += total_size * 10 / 100 ))
87}
88
89function create_ufs
90{
91	# should we exclude amd64 binaries?
92	[ $is_amd64 -eq 0 ] && NO_AMD64="-name amd64 -prune"
93
94	# calculate image size
95	getsize
96
97	mkfile ${total_size}k ${rdfile}
98	lofidev=`lofiadm -a ${rdfile}`
99	newfs ${lofidev} < /dev/null 2> /dev/null
100	mkdir ${rdmnt}
101	mount -F mntfs mnttab /etc/mnttab > /dev/null 2>&1
102	mount -o nologging ${lofidev} ${rdmnt}
103
104
105	# do the actual copy
106	cd /${ALT_ROOT}
107	find $filelist -print ${NO_AMD64}| cpio -pdum $rdmnt 2> /dev/null
108	umount ${rdmnt}
109	lofiadm -d ${rdfile}
110	rmdir ${rdmnt}
111	gzip -c ${rdfile} > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
112}
113
114function create_isofs
115{
116	# should we exclude amd64 binaries?
117	[ $is_amd64 = 0 ] && NO_AMD64="-m amd64"
118
119	# create image directory seed with graft points
120	mkdir ${rdmnt}
121	files=
122	isocmd="mkisofs -quiet -graft-points -dlrDJN -relaxed-filenames ${NO_AMD64}"
123	for path in $filelist
124	do
125		if [ -d ${ALT_ROOT}/$path ]; then
126			isocmd="$isocmd $path/=${ALT_ROOT}/$path"
127			mkdir -p ${rdmnt}/$path
128		elif [ -f ${ALT_ROOT}/$path ]; then
129			files="$files $path"
130		else
131			echo "/${ALT_ROOT}/$path not present"
132		fi
133	done
134	cd /${ALT_ROOT}
135	find $files 2> /dev/null | cpio -pdum ${rdmnt} 2> /dev/null
136	isocmd="$isocmd ${rdmnt}"
137	${isocmd} 2> /dev/null | gzip > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
138}
139
140#
141# get filelist
142#
143filelist=$(< ${ALT_ROOT}/boot/solaris/filelist.ramdisk)
144if [ -f ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk ]; then
145	filelist="$filelist $(< ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk)"
146fi
147
148#
149# decide if cpu is amd64 capable
150#
151prtconf -v /devices | grep CPU_not_amd64 > /dev/null 2>&1
152is_amd64=$?
153
154echo "updating ${ALT_ROOT}/${BOOT_ARCHIVE}...this may take a minute"
155
156if [ $format = "ufs" ]; then
157	create_ufs
158else
159	create_isofs
160fi
161
162#
163# For the diskless case, hardlink archive to /boot to make it
164# visible via tftp. /boot is lofs mounted under /tftpboot/<hostname>.
165# NOTE: this script must work on both client and server
166#
167grep "[	 ]/[	 ]*nfs[	 ]" $ALT_ROOT/etc/vfstab > /dev/null
168if [ $? = 0 ]; then
169	mv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
170	rm -f ${ALT_ROOT}/boot/boot_archive
171	ln ${ALT_ROOT}/${BOOT_ARCHIVE} ${ALT_ROOT}/boot/boot_archive
172	exit
173fi
174
175lockfs -f /$ALT_ROOT
176mv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
177lockfs -f /$ALT_ROOT
178