xref: /titanic_41/usr/src/cmd/boot/scripts/root_archive.ksh (revision d89fccd8788afe1e920f842edd883fe192a1b8fe)
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	cd "$MEDIA/$RELEASE/Tools/Boot"
123	ln -sf ../../../boot/x86.miniroot
124	ln -sf ../../../boot/multiboot
125	ln -sf ../../../boot/grub/pxegrub
126}
127
128unarchive_X()
129{
130	MEDIA="$1"
131	UNPACKED_ROOT="$2"
132
133	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
134	RELEASE=`basename "$RELEASE"`
135
136	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
137		CPIO_DIR="$MEDIA/$RELEASE/Tools/miniroot_extra"
138	else
139		CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
140	fi
141
142	# unpack X
143	#
144	cd "$UNPACKED_ROOT/usr"
145	rm -rf dt openwin
146	bzcat "$CPIO_DIR/X.cpio.bz2" | cpio -icdmu 2> /dev/null
147}
148
149unpackmedia()
150{
151	MEDIA="$1"
152	UNPACKED_ROOT="$2"
153
154	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
155	RELEASE=`basename "$RELEASE"`
156
157	unarchive_X "$MEDIA" "$UNPACKED_ROOT"
158
159	# unpack package databases
160	#
161	cd "$UNPACKED_ROOT"
162	bzcat "$MEDIA/$RELEASE/Tools/Boot/pkg_db.cpio.bz2" | cpio -icdmu \
163	    2> /dev/null
164}
165
166do_unpack()
167{
168	rm -rf "$UNPACKED_ROOT"
169	mkdir -p "$UNPACKED_ROOT"
170	cd $MNT
171	find . -print | cpio -pdum "$UNPACKED_ROOT" 2> /dev/null
172	cd "$BASE"
173	umount $MNT
174}
175
176unpack()
177{
178
179	if [ ! -f "$MR" ] ; then
180		usage
181		exit 1
182	fi
183
184	TMR=/tmp/mr$$
185	gzcat "$MR" > $TMR
186
187	lofidev=`/usr/sbin/lofiadm -a $TMR`
188	if [ $? != 0 ] ; then
189		echo lofi plumb failed
190		exit 2
191	fi
192
193	mkdir -p $MNT
194
195	FSTYP=`fstyp $lofidev`
196
197	if [ "$FSTYP" = ufs ] ; then
198		/usr/sbin/mount -o ro,nologging $lofidev $MNT
199		do_unpack
200	elif [ "$FSTYP" = hsfs ] ; then
201		/usr/sbin/mount -F hsfs -o ro $lofidev $MNT
202		do_unpack
203	else
204		printf "invalid root archive\n"
205	fi
206
207	rmdir $MNT
208	lofiadm -d $TMR
209	rm $TMR
210}
211
212pack()
213{
214	if [ ! -d "$UNPACKED_ROOT" -o -z "$MR" ] ; then
215		usage
216		exit 1
217	fi
218
219	size=`du -sk "$UNPACKED_ROOT" | ( read size name; echo $size )`
220	size=`expr $size + \( $size \* 10 \) / 100`
221	rm -f "$MR"
222	/usr/sbin/mkfile ${size}k "$MR"
223
224	lofidev=`/usr/sbin/lofiadm -a "$MR"`
225	if [ $? != 0 ] ; then
226		echo lofi plumb failed
227		exit 2
228	fi
229
230	rlofidev=`echo $lofidev | sed s/lofi/rlofi/`
231	newfs $rlofidev < /dev/null 2> /dev/null
232	mkdir -p $MNT
233	mount -o nologging $lofidev $MNT
234	rmdir $MNT/lost+found
235	cd "$UNPACKED_ROOT"
236	find . -print | cpio -pdum $MNT 2> /dev/null
237	lockfs -f $MNT
238	umount $MNT
239	rmdir $MNT
240	lofiadm -d "$MR"
241
242	cd "$BASE"
243
244	rm -f "$MR.gz"
245	gzip -f "$MR"
246	mv "$MR.gz" "$MR"
247	chmod a+r "$MR"
248}
249
250# main
251#
252
253if [ $# != 3 ] ; then
254	usage
255	exit 1
256fi
257
258UNPACKED_ROOT="$3"
259BASE="`pwd`"
260MNT=/tmp/mnt$$
261MR="$2"
262
263if [ "`dirname $MR`" = . ] ; then
264	MR="$BASE/$MR"
265fi
266if [ "`dirname $UNPACKED_ROOT`" = . ] ; then
267	UNPACKED_ROOT="$BASE/$UNPACKED_ROOT"
268fi
269
270case $1 in
271	packmedia)
272		MEDIA="$MR"
273		MR="$MR/boot/x86.miniroot"
274
275		if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
276			archive_X "$MEDIA" "$UNPACKED_ROOT"
277		else
278			packmedia "$MEDIA" "$UNPACKED_ROOT"
279			pack
280		fi ;;
281	unpackmedia)
282		MEDIA="$MR"
283		MR="$MR/boot/x86.miniroot"
284
285		if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
286			unarchive_X "$MEDIA" "$UNPACKED_ROOT"
287		else
288			unpack
289			unpackmedia "$MEDIA" "$UNPACKED_ROOT"
290		fi ;;
291	pack)	pack ;;
292	unpack)	unpack ;;
293	*)	usage ;;
294esac
295