xref: /illumos-gate/usr/src/cmd/boot/scripts/create_ramdisk.ksh (revision 956e8222f10bf55e45b41d8b56084f72ebc113c9)
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}| cpio -pdum ${rdmnt} 2> /dev/null
115	umount ${rdmnt}
116	lofiadm -d ${rdfile}
117	rmdir ${rdmnt}
118	gzip -c ${rdfile} > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
119}
120
121function create_isofs
122{
123	# should we exclude amd64 binaries?
124	[ $is_amd64 = 0 ] && NO_AMD64="-m amd64"
125
126	# create image directory seed with graft points
127	mkdir ${rdmnt}
128	files=
129	isocmd="mkisofs -quiet -graft-points -dlrDJN -relaxed-filenames ${NO_AMD64}"
130	for path in $filelist
131	do
132		if [ -d ${ALT_ROOT}/$path ]; then
133			isocmd="$isocmd $path/=${ALT_ROOT}/$path"
134			mkdir -p ${rdmnt}/$path
135		elif [ -f ${ALT_ROOT}/$path ]; then
136			files="$files $path"
137		else
138			echo "/${ALT_ROOT}/$path not present"
139		fi
140	done
141	cd /${ALT_ROOT}
142	find $files 2> /dev/null | cpio -pdum ${rdmnt} 2> /dev/null
143	isocmd="$isocmd ${rdmnt}"
144	rm -f ${errlog}
145	${isocmd} 2> ${errlog} | gzip > ${ALT_ROOT}/${BOOT_ARCHIVE}-new
146	if [ -s ${errlog} ]; then
147		grep Error: ${errlog} >/dev/null 2>&1
148		if [ $? -eq 0 ]; then
149			grep Error: ${errlog}
150			rm -f ${ALT_ROOT}/${BOOT_ARCHIVE}-new
151		fi
152	fi
153	rm -f ${errlog}
154}
155
156#
157# get filelist
158#
159files=${ALT_ROOT}/boot/solaris/filelist.ramdisk
160if [ -f ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk ]; then
161	files="$files ${ALT_ROOT}/etc/boot/solaris/filelist.ramdisk"
162fi
163filelist=`cat $files | sort | uniq`
164
165#
166# decide if cpu is amd64 capable
167#
168prtconf -v /devices | grep CPU_not_amd64 > /dev/null 2>&1
169is_amd64=$?
170
171echo "updating ${ALT_ROOT}/${BOOT_ARCHIVE}...this may take a minute"
172
173if [ $format = "ufs" ]; then
174	create_ufs
175else
176	create_isofs
177fi
178
179if [ ! -f ${ALT_ROOT}/${BOOT_ARCHIVE}-new ]; then
180	echo "update of ${ALT_ROOT}/${BOOT_ARCHIVE} failed"
181	rm -rf ${rddir}
182	exit 1
183fi
184
185#
186# For the diskless case, hardlink archive to /boot to make it
187# visible via tftp. /boot is lofs mounted under /tftpboot/<hostname>.
188# NOTE: this script must work on both client and server
189#
190grep "[	 ]/[	 ]*nfs[	 ]" $ALT_ROOT/etc/vfstab > /dev/null
191if [ $? = 0 ]; then
192	mv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
193	rm -f ${ALT_ROOT}/boot/boot_archive
194	ln ${ALT_ROOT}/${BOOT_ARCHIVE} ${ALT_ROOT}/boot/boot_archive
195	rm -rf ${rddir}
196	exit
197fi
198
199lockfs -f /$ALT_ROOT
200mv ${ALT_ROOT}/${BOOT_ARCHIVE}-new ${ALT_ROOT}/${BOOT_ARCHIVE}
201lockfs -f /$ALT_ROOT
202
203rm -rf ${rddir}
204