xref: /titanic_52/usr/src/cmd/boot/scripts/root_archive.ksh (revision f498645a3eecf2ddd304b4ea9c7f1b4c155ff79e)
1#!/bin/ksh
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 (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26# ident	"%Z%%M%	%I%	%E% SMI"
27
28# utility to pack and unpack a boot/root archive
29# both ufs and hsfs (iso9660) format archives are unpacked
30# only ufs archives are generated
31#
32# usage: pack   <archive> <root>
33#        unpack <archive> <root>
34#        packmedia   <solaris_image> <root>
35#        unpackmedia <solaris_image> <root>
36#
37#   Where <root> is the directory to unpack to and will be cleaned out
38#   if it exists.
39#
40#   In the case of (un)packmedia, the image is packed or unpacked to/from
41#   Solaris media and all the things that don't go into the ramdisk image
42#   are (un)cpio'd as well
43#
44# This utility is also used to pack parts (in essence the window system,
45# usr/dt and usr/openwin) of the non ramdisk SPARC
46# miniroot. (un)packmedia will recognize that they are being run a SPARC
47# miniroot and do the appropriate work.
48#
49
50usage()
51{
52	printf "usage: root_archive pack <archive> <root>\n"
53	printf "       root_archive unpack <archive> <root>\n"
54	printf "       root_archive packmedia   <solaris_image> <root>\n"
55	printf "       root_archive unpackmedia <solaris_image> <root>\n"
56}
57
58archive_X()
59{
60	MEDIA="$1"
61	MINIROOT="$2"
62
63	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
64	RELEASE=`basename "$RELEASE"`
65
66	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
67		CPIO_DIR="$MEDIA/$RELEASE/Tools/miniroot_extra"
68		mkdir -p "$CPIO_DIR"
69	else
70		CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
71	fi
72
73	# create the graphics and non-graphics X archive
74	#
75	cd "$MINIROOT/usr"
76	find openwin dt -print | cpio -ocmPuB 2> /dev/null | bzip2 > \
77	    "$CPIO_DIR/X.cpio.bz2"
78
79	find openwin/bin/mkfontdir \
80	     openwin/lib/installalias \
81	     openwin/server/lib/libfont.so.1 \
82	     openwin/server/lib/libtypesclr.so.0 \
83	         -print | cpio -ocmPuB 2> /dev/null | bzip2 > \
84	         "$CPIO_DIR/X_small.cpio.bz2"
85
86	rm -rf dt openwin
87	ln -s ../tmp/root/usr/dt
88	ln -s ../tmp/root/usr/openwin
89	cd ../..
90}
91
92packmedia()
93{
94	MEDIA="$1"
95	MINIROOT="$2"
96
97	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
98	RELEASE=`basename "$RELEASE"`
99
100	mkdir -p "$MEDIA/$RELEASE/Tools/Boot"
101	mkdir -p "$MEDIA/boot"
102
103	cd "$MINIROOT"
104
105	# archive package databases to conserve memory
106	#
107	find tmp/root/var/sadm/install tmp/root/var/sadm/pkg -print | \
108	    cpio -ocmPuB 2> /dev/null | bzip2 > \
109	    "$MEDIA/$RELEASE/Tools/Boot/pkg_db.cpio.bz2"
110
111	rm -rf "$MINIROOT/tmp/root/var/sadm/install"
112	rm -rf "$MINIROOT/tmp/root/var/sadm/pkg"
113
114	archive_X "$MEDIA" "$MINIROOT"
115
116	# clear out 64 bit support to conserve memory
117	#
118	find "$MINIROOT" -name amd64 -type directory | xargs rm -rf
119
120	cp "$MINIROOT/platform/i86pc/multiboot" "$MEDIA/boot"
121
122	# copy the install menu to menu.lst so we have a menu
123	# on the install media
124	#
125	if [ -f "${MINIROOT}/boot/grub/install_menu" ] ; then
126		cp ${MINIROOT}/boot/grub/install_menu \
127		    ${MEDIA}/boot/grub/menu.lst
128	fi
129
130	cd "$MEDIA/$RELEASE/Tools/Boot"
131	ln -sf ../../../boot/x86.miniroot
132	ln -sf ../../../boot/multiboot
133	ln -sf ../../../boot/grub/pxegrub
134}
135
136unarchive_X()
137{
138	MEDIA="$1"
139	UNPACKED_ROOT="$2"
140
141	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
142	RELEASE=`basename "$RELEASE"`
143
144	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
145		CPIO_DIR="$MEDIA/$RELEASE/Tools/miniroot_extra"
146	else
147		CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
148	fi
149
150	# unpack X
151	#
152	cd "$UNPACKED_ROOT/usr"
153	rm -rf dt openwin
154	bzcat "$CPIO_DIR/X.cpio.bz2" | cpio -icdmu 2> /dev/null
155}
156
157unpackmedia()
158{
159	MEDIA="$1"
160	UNPACKED_ROOT="$2"
161
162	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
163	RELEASE=`basename "$RELEASE"`
164
165	unarchive_X "$MEDIA" "$UNPACKED_ROOT"
166
167	# unpack package databases
168	#
169	cd "$UNPACKED_ROOT"
170	bzcat "$MEDIA/$RELEASE/Tools/Boot/pkg_db.cpio.bz2" | cpio -icdmu \
171	    2> /dev/null
172}
173
174do_unpack()
175{
176	rm -rf "$UNPACKED_ROOT"
177	mkdir -p "$UNPACKED_ROOT"
178	cd $MNT
179	find . -print | cpio -pdum "$UNPACKED_ROOT" 2> /dev/null
180	cd "$BASE"
181	umount $MNT
182}
183
184unpack()
185{
186
187	if [ ! -f "$MR" ] ; then
188		usage
189		exit 1
190	fi
191
192	TMR=/tmp/mr$$
193	gzcat "$MR" > $TMR
194
195	lofidev=`/usr/sbin/lofiadm -a $TMR`
196	if [ $? != 0 ] ; then
197		echo lofi plumb failed
198		exit 2
199	fi
200
201	mkdir -p $MNT
202
203	FSTYP=`fstyp $lofidev`
204
205	if [ "$FSTYP" = ufs ] ; then
206		/usr/sbin/mount -o ro,nologging $lofidev $MNT
207		do_unpack
208	elif [ "$FSTYP" = hsfs ] ; then
209		/usr/sbin/mount -F hsfs -o ro $lofidev $MNT
210		do_unpack
211	else
212		printf "invalid root archive\n"
213	fi
214
215	rmdir $MNT
216	lofiadm -d $TMR
217	rm $TMR
218}
219
220pack()
221{
222	if [ ! -d "$UNPACKED_ROOT" -o -z "$MR" ] ; then
223		usage
224		exit 1
225	fi
226
227	size=`du -sk "$UNPACKED_ROOT" | ( read size name; echo $size )`
228	size=`expr $size + \( $size \* 10 \) / 100`
229	rm -f "$MR"
230	/usr/sbin/mkfile ${size}k "$MR"
231
232	lofidev=`/usr/sbin/lofiadm -a "$MR"`
233	if [ $? != 0 ] ; then
234		echo lofi plumb failed
235		exit 2
236	fi
237
238	rlofidev=`echo $lofidev | sed s/lofi/rlofi/`
239	newfs $rlofidev < /dev/null 2> /dev/null
240	mkdir -p $MNT
241	mount -o nologging $lofidev $MNT
242	rmdir $MNT/lost+found
243	cd "$UNPACKED_ROOT"
244	find . -print | cpio -pdum $MNT 2> /dev/null
245	lockfs -f $MNT
246	umount $MNT
247	rmdir $MNT
248	lofiadm -d "$MR"
249
250	cd "$BASE"
251
252	rm -f "$MR.gz"
253	gzip -f "$MR"
254	mv "$MR.gz" "$MR"
255	chmod a+r "$MR"
256}
257
258# main
259#
260
261if [ $# != 3 ] ; then
262	usage
263	exit 1
264fi
265
266UNPACKED_ROOT="$3"
267BASE="`pwd`"
268MNT=/tmp/mnt$$
269MR="$2"
270
271if [ "`dirname $MR`" = . ] ; then
272	MR="$BASE/$MR"
273fi
274if [ "`dirname $UNPACKED_ROOT`" = . ] ; then
275	UNPACKED_ROOT="$BASE/$UNPACKED_ROOT"
276fi
277
278case $1 in
279	packmedia)
280		MEDIA="$MR"
281		MR="$MR/boot/x86.miniroot"
282
283		if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
284			archive_X "$MEDIA" "$UNPACKED_ROOT"
285		else
286			packmedia "$MEDIA" "$UNPACKED_ROOT"
287			pack
288		fi ;;
289	unpackmedia)
290		MEDIA="$MR"
291		MR="$MR/boot/x86.miniroot"
292
293		if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
294			unarchive_X "$MEDIA" "$UNPACKED_ROOT"
295		else
296			unpack
297			unpackmedia "$MEDIA" "$UNPACKED_ROOT"
298		fi ;;
299	pack)	pack ;;
300	unpack)	unpack ;;
301	*)	usage ;;
302esac
303