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