xref: /titanic_52/usr/src/cmd/boot/scripts/root_archive.ksh (revision a93a1f58a8763fa69172980b98e3d24720c1136e)
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 (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 2007 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
58cleanup()
59{
60	if [ -d $MNT ] ; then
61		umount $MNT 2> /dev/null
62		rmdir $MNT
63	fi
64
65	lofiadm -d "$TMR" 2>/dev/null
66	rm -f "$TMR" "$TMR.gz"
67}
68
69archive_Gnome()
70{
71	MEDIA="$1"
72	MINIROOT="$2"
73
74	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
75	RELEASE=`basename "$RELEASE"`
76
77	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
78		CPIO_DIR="$MEDIA/$RELEASE/Tools/miniroot_extra"
79		mkdir -p "$CPIO_DIR"
80	else
81		CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
82	fi
83
84
85	# Create the gnome archive
86	#
87	(
88		# Prepopulate the gconf database. This needs to be done and
89		# done first for several reasons. 1) Archiving out the gnome
90		# libraries and binaries causes the gconftool-2 to not run
91		# appropriately at boot time. 2) The binaries and libraries
92		# needed to run this are big and thus we want to archive
93		# them separately. 3) Having schemas prepopluated in the
94		# miniroot means faster boot times.
95		#
96
97		cd "$MINIROOT"
98		HOME="./tmp/root"
99		export HOME
100		umask 0022
101		GCONF_CONFIG_SOURCE="xml:merged:"$MINIROOT"/.tmp_proto/root/etc/gconf/gconf.xml.defaults"
102		export GCONF_CONFIG_SOURCE
103		SCHEMADIR="$MINIROOT/.tmp_proto/root/etc/gconf/schemas"
104		export SCHEMADIR
105		/usr/bin/gconftool-2 --makefile-install-rule $SCHEMADIR/*.schemas >/dev/null 2>&1
106		# usr/share gnome stuff
107		cd "$MINIROOT"
108		find usr/share/GConf usr/share/application-registry \
109		    usr/share/autostart usr/share/dbus-1 usr/share/dtds \
110		    usr/share/emacs usr/share/gnome usr/share/gnome-2.0 \
111		    usr/share/gnome-background-properties \
112		    usr/share/gtk-engines usr/share/gui-install \
113		    usr/share/icon-naming-utils usr/share/control-center \
114		    usr/share/icons usr/share/locale usr/share/metacity \
115		    usr/share/mime usr/share/mime-info usr/share/pixmaps \
116		    usr/share/scrollkeeper usr/share/sgml usr/share/themes \
117		    usr/share/xml \
118		    -print > /tmp/gnome_share.$$ 2>/dev/null
119
120		if [ ! -f /tmp/gnome_share.$$ ] ; then
121			echo "/tmp/gnome_share.$$ file list not found."
122			return
123		fi
124
125		# usr/lib gnome stuff
126
127		find usr/lib/libgnome*\.so\.* \
128		    usr/lib/libgst*\.so\.* usr/lib/libgconf*\.so\.* \
129		    usr/lib/libgdk*\.so\.* usr/lib/libgtk*\.so\.* \
130		    usr/lib/libglade*\.so\.* usr/lib/libmetacity*\.so\.* \
131		    usr/lib/libfontconfig*\.so\.* usr/lib/libgmodule*\.so\.* \
132		    usr/lib/libgobject*\.so\.* usr/lib/libgthread*\.so\.* \
133		    usr/lib/libpopt*\.so\.* usr/lib/libstartup*\.so\.* \
134		    usr/lib/libexif*\.so\.* usr/lib/libtiff*\.so\.* \
135		    usr/lib/libdbus*\.so\.* usr/lib/libstartup*\.so\.* \
136		    usr/lib/libexif*\.so\.* usr/lib/libORBit*\.so\.* \
137	 	    usr/lib/libmlib*\.so\.* usr/lib/libxsl*\.so\.* \
138		    usr/lib/libpango*\.so\.* usr/lib/libpng*\.so\.* \
139		    usr/lib/liboil*\.so\.* usr/lib/libbonobo*\.so\.* \
140		    usr/lib/libart*\.so\.* usr/lib/libcairo*\.so\.* \
141		    usr/lib/libjpeg*\.so\.* \
142		    usr/lib/libpolkit*\.so\.* \
143			-print | egrep -v '\.so\.[0]$' > \
144		       /tmp/gnome_lib.$$ 2>/dev/null
145
146		find usr/lib/nautilus usr/lib/pango usr/lib/iconv \
147		    usr/lib/metacity-dialog usr/lib/window-manager-settings \
148		    usr/lib/bonobo-2.0 usr/lib/bononbo usr/lib/gtk-2.0 \
149		    usr/lib/GConf usr/lib/bonobo-activation-server \
150		    usr/lib/python2.4 usr/lib/gstreamer-0.10 \
151		    usr/lib/gconf-sanity-check-2 usr/lib/gconfd \
152		    usr/lib/gnome-vfs-2.0 usr/lib/dbus-daemon \
153		    usr/lib/gnome-vfs-daemon usr/lib/gnome-settings-daemon \
154		    usr/lib/gnome_segv2 usr/lib/orbit-2.0 \
155		    usr/lib/libmlib \
156		    print > /tmp/gnome_libdir.$$ 2>/dev/null
157
158		if [ ! -f /tmp/gnome_lib.$$  -a ! -f gnome_libdir.$$ ] ; then
159			echo "/tmp/gnome_lib.$$ file list not found."
160			return
161		fi
162
163		# /usr/sfw gnome stuff
164		find usr/sfw/bin usr/sfw/include usr/sfw/share usr/sfw/src \
165		    -print > /tmp/gnome_sfw.$$ 2>/dev/null
166
167		if [ ! -f /tmp/gnome_sfw.$$ ] ; then
168			echo "/tmp/gnome_sfw.$$ file list not found."
169			return
170		fi
171
172		# gnome app binaries usr/bin
173		find usr/bin/gnome* usr/bin/gui-install usr/bin/bonobo* \
174		    usr/bin/gtk-* usr/bin/fax* usr/bin/gdk* usr/bin/gif2tiff \
175		    usr/bin/install-lan \
176		    usr/bin/metacity* usr/bin/gst-* usr/bin/gconftool-2 \
177		    usr/bin/pango* usr/bin/desktop* usr/bin/djpeg \
178		    usr/bin/notify-send usr/bin/oil-bugreport \
179		    usr/bin/bmp2tiff usr/bin/thembus-theme-applier \
180		    usr/bin/thumbnail usr/lib/update-* \
181		    usr/bin/ras2tiff usr/bin/raw2tiff usr/bin/rdjpgcom \
182		    usr/bin/thumbnail usr/bin/dbus* \
183		    usr/bin/tiff* usr/bin/rgb2ycbcr \
184		    usr/bin/fc-cache usr/bin/fc-list \
185			-print > /tmp/gnome_bin.$$ 2>/dev/null
186
187		if [ ! -f /tmp/gnome_bin.$$ ] ; then
188			echo "/tmp/gnome_bin.$$ file list not found."
189			return
190		fi
191
192		# Cat all the files together and create the gnome archive
193		#
194
195		cat /tmp/gnome_libdir.$$ /tmp/gnome_lib.$$ \
196		     /tmp/gnome_share.$$ /tmp/gnome_sfw.$$ /tmp/gnome_bin.$$ \
197		    > /tmp/gnome.$$
198
199		if [ ! -f /tmp/gnome.$$ ] ; then
200			echo "/tmp/gnome.$$ file not found."
201			return
202		fi
203		# Save off this file in the miniroot for use later
204		# when unpacking. Clean up old cruft if there.
205		#
206
207		if [ -f .tmp_proto/gnome_saved ]; then
208			rm -f .tmp_proto/gnome_saved
209		fi
210
211		cp /tmp/gnome.$$ .tmp_proto/gnome_saved
212
213		# Create gnome archive
214		#
215
216		cpio -ocmPuB < /tmp/gnome.$$ 2>/dev/null | bzip2 > \
217		    "$CPIO_DIR/gnome.cpio.bz2"
218
219		# Remove files from miniroot that are in archive.
220		# Create symlinks for files in archive
221
222		rm -rf `cat /tmp/gnome_share.$$`
223
224		for i in `cat /tmp/gnome_share.$$`
225		do
226			ln -s /tmp/root/$i $i 2>/dev/null
227		done
228
229		rm -rf `cat /tmp/gnome_lib.$$`
230		for i in `cat /tmp/gnome_lib.$$`
231		do
232			ln -s /tmp/root/$i $i 2>/dev/null
233		done
234
235		rm -rf `cat /tmp/gnome_libdir.$$`
236		for i in `cat /tmp/gnome_libdir.$$`
237		do
238			ln -s /tmp/root/$i $i 2>/dev/null
239		done
240
241		rm -rf `cat /tmp/gnome_sfw.$$`
242		for i in `cat /tmp/gnome_sfw.$$`
243		do
244			ln -s /tmp/root/$i $i 2>/dev/null
245		done
246
247		rm -rf `cat /tmp/gnome_bin.$$`
248		for i in `cat /tmp/gnome_bin.$$`
249		do
250			ln -s /tmp/root/$i $i 2>/dev/null
251		done
252		rm -f /tmp/gnome_share.$$
253		rm -f /tmp/gnome_lib.$$
254		rm -f /tmp/gnome_libdir.$$
255		rm -f /tmp/gnome_bin.$$
256	)
257}
258
259archive_JavaGUI()
260{
261	MEDIA="$1"
262	MINIROOT="$2"
263
264	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
265	RELEASE=`basename "$RELEASE"`
266
267	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
268		CPIO_DIR="$MEDIA/$RELEASE/Tools/miniroot_extra"
269		mkdir -p "$CPIO_DIR"
270	else
271		CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
272	fi
273
274	# Archive the java wizard components that are only used in the
275	# non developer express path.
276	#
277	(
278		# path is usr/lib/install/data
279		cd "$MINIROOT"
280		find usr/lib/install/data/wizards \
281		    -print > /tmp/java_ui.$$ 2>/dev/null
282
283		if [ ! -f /tmp/java_ui.$$ ] ; then
284			echo "/tmp/java_ui.$$ file list not found."
285			return
286		fi
287
288		cpio -ocmPuB < /tmp/java_ui.$$ 2>/dev/null | bzip2 > \
289		    "$CPIO_DIR/javaui.cpio.bz2"
290
291		rm -rf `cat /tmp/java_ui.$$`
292		ln -s /tmp/root/usr/lib/install/data/wizards usr/lib/install/data/wizards 2>/dev/null
293
294		rm -f /tmp/java_ui.$$
295
296	)
297}
298
299archive_Misc()
300{
301	MEDIA="$1"
302	MINIROOT="$2"
303
304	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
305	RELEASE=`basename "$RELEASE"`
306
307	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
308		CPIO_DIR="$MEDIA/$RELEASE/Tools/miniroot_extra"
309		mkdir -p "$CPIO_DIR"
310	else
311		CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
312	fi
313
314	# Archive misc stuff that is needed by non devex installer
315	#
316	(
317		# usr/lib stuff
318		cd "$MINIROOT"
319		find usr/lib/lp -print > /tmp/lp.$$ 2>/dev/null
320		if [ ! -f /tmp/lp.$$ ] ; then
321			echo "/tmp/lp.$$ file list not found."
322			return
323		fi
324
325		cpio -ocmPuB < /tmp/lp.$$ 2>/dev/null | bzip2 > \
326		    "$CPIO_DIR/lpmisc.cpio.bz2"
327
328		rm -rf `cat /tmp/lp.$$`
329		ln -s /tmp/root/usr/lib/lp usr/lib/lp 2>/dev/null
330
331		rm -f /tmp/lp.$$
332	)
333
334}
335
336archive_Perl()
337{
338	MEDIA="$1"
339	MINIROOT="$2"
340
341	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
342	RELEASE=`basename "$RELEASE"`
343
344	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
345		CPIO_DIR="$MEDIA/$RELEASE/Tools/miniroot_extra"
346		mkdir -p "$CPIO_DIR"
347	else
348		CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
349	fi
350
351	# Archive perl, it is only needed by gnome gui.
352	#
353	(
354		# in usr
355		cd "$MINIROOT"
356		find usr/perl5 -print > /tmp/perl.$$ 2>/dev/null
357
358		if [ ! -f /tmp/perl.$$ ] ; then
359			echo "/tmp/perl.$$ file list not found."
360			return
361		fi
362		cpio -ocmPuB < /tmp/perl.$$ 2>/dev/null | bzip2 > \
363		    "$CPIO_DIR/perl.cpio.bz2"
364
365		rm -rf `cat /tmp/perl.$$` 2>/dev/null
366		ln -s /tmp/root/perl5 usr/perl5 2>/dev/null
367
368		rm -f /tmp/perl.$$
369	)
370}
371archive_X()
372{
373	MEDIA="$1"
374	MINIROOT="$2"
375
376	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
377	RELEASE=`basename "$RELEASE"`
378
379	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
380		CPIO_DIR="$MEDIA/$RELEASE/Tools/miniroot_extra"
381		mkdir -p "$CPIO_DIR"
382	else
383		CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
384	fi
385
386	# create the graphics and non-graphics X archive
387	#
388	(
389		cd "$MINIROOT"
390		find usr/openwin usr/dt usr/X11 -print 2> /dev/null |\
391		    cpio -ocmPuB 2> /dev/null | bzip2 > "$CPIO_DIR/X.cpio.bz2"
392
393		find usr/openwin/bin/mkfontdir \
394		     usr/openwin/lib/installalias \
395		     usr/openwin/server/lib/libfont.so.1 \
396		     usr/openwin/server/lib/libtypesclr.so.0 \
397			 -print | cpio -ocmPuB 2> /dev/null | bzip2 > \
398			 "$CPIO_DIR/X_small.cpio.bz2"
399
400		rm -rf usr/dt usr/openwin usr/X11
401		ln -s /tmp/root/usr/dt usr/dt
402		ln -s /tmp/root/usr/openwin usr/openwin
403		ln -s /tmp/root/usr/X11 usr/X11
404	)
405}
406
407packmedia()
408{
409	MEDIA="$1"
410	MINIROOT="$2"
411
412	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
413	RELEASE=`basename "$RELEASE"`
414
415	mkdir -p "$MEDIA/$RELEASE/Tools/Boot"
416	mkdir -p "$MEDIA/boot/amd64"
417	mkdir -p "$MEDIA/boot/platform/i86pc/kernel"
418	mkdir -p "$MEDIA/boot/platform/i86pc/kernel/amd64"
419	mkdir -p "$MEDIA/boot/platform/i86xpv/kernel"
420	mkdir -p "$MEDIA/boot/platform/i86xpv/kernel/amd64"
421
422	# archive package databases to conserve memory
423	#
424	(
425		cd "$MINIROOT"
426		find tmp/root/var/sadm/install tmp/root/var/sadm/pkg -print | \
427		    cpio -ocmPuB 2> /dev/null | bzip2 > \
428		    "$MEDIA/$RELEASE/Tools/Boot/pkg_db.cpio.bz2"
429	)
430
431	rm -rf "$MINIROOT/tmp/root/var/sadm/install"
432	rm -rf "$MINIROOT/tmp/root/var/sadm/pkg"
433
434	archive_X "$MEDIA" "$MINIROOT"
435
436	# Take out the gnome and java parts of the installer from
437	# the miniroot. These are not required to boot the system
438	# and start the installers.
439
440	archive_Gnome "$MEDIA" "$MINIROOT"
441	archive_JavaGUI "$MEDIA" "$MINIROOT"
442	archive_Perl "$MEDIA" "$MINIROOT"
443	archive_Misc "$MEDIA" "$MINIROOT"
444
445	cp "$MINIROOT/platform/i86pc/multiboot" "$MEDIA/boot"
446	cp "$MINIROOT/platform/i86pc/kernel/unix" \
447	    "$MEDIA/boot/platform/i86pc/kernel/unix"
448	cp "$MINIROOT/platform/i86pc/kernel/amd64/unix" \
449	    "$MEDIA/boot/platform/i86pc/kernel/amd64/unix"
450	cp "$MINIROOT/platform/i86xpv/kernel/unix" \
451	    "$MEDIA/boot/platform/i86xpv/kernel/unix"
452	cp "$MINIROOT/platform/i86xpv/kernel/amd64/unix" \
453	    "$MEDIA/boot/platform/i86xpv/kernel/amd64/unix"
454
455	# copy the install menu to menu.lst so we have a menu
456	# on the install media
457	#
458	if [ -f "${MINIROOT}/boot/grub/install_menu" ] ; then
459		cp ${MINIROOT}/boot/grub/install_menu \
460		    ${MEDIA}/boot/grub/menu.lst
461	fi
462
463	(
464		cd "$MEDIA/$RELEASE/Tools/Boot"
465		ln -sf ../../../boot/x86.miniroot
466		ln -sf ../../../boot/platform/i86pc/kernel/unix
467		ln -sf ../../../boot/platform/i86pc/kernel/amd64/unix
468		ln -sf ../../../boot/platform/i86xpv/kernel/unix
469		ln -sf ../../../boot/platform/i86xpv/kernel/amd64/unix
470		ln -sf ../../../boot/multiboot
471		ln -sf ../../../boot/grub/pxegrub
472	)
473}
474
475unarchive_X()
476{
477	MEDIA="$1"
478	UNPACKED_ROOT="$2"
479
480	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
481	RELEASE=`basename "$RELEASE"`
482
483	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
484		CPIO_DIR="$MEDIA/$RELEASE/Tools/miniroot_extra"
485	else
486		CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
487	fi
488
489	# unpack X
490	#
491	(
492		cd "$UNPACKED_ROOT"
493		rm -rf usr/dt usr/openwin usr/X11
494		bzcat "$CPIO_DIR/X.cpio.bz2" | cpio -icdmu 2> /dev/null
495	)
496}
497
498unpackmedia()
499{
500	MEDIA="$1"
501	UNPACKED_ROOT="$2"
502
503	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
504	RELEASE=`basename "$RELEASE"`
505
506	unarchive_X "$MEDIA" "$UNPACKED_ROOT"
507
508	# unpack package databases
509	#
510	(
511		cd "$UNPACKED_ROOT"
512		bzcat "$MEDIA/$RELEASE/Tools/Boot/pkg_db.cpio.bz2" |
513		    cpio -icdmu 2> /dev/null
514
515		# unpack gnome, perl, java and misc
516		# Remove symlinks left from unpacking x86.miniroot so that
517		# unpacking subsequent archives will populate appropriately.
518		#
519		rm -rf usr/perl5
520		rm -rf usr/lib/install/data/wizards
521		rm -rf usr/lib/lp
522
523		# Gnome list saved off from packmedia
524		for i in `cat .tmp_proto/gnome_saved`
525		do
526			rm -rf $i
527		done
528
529		bzcat "$MEDIA/$RELEASE/Tools/Boot/gnome.cpio.bz2" |
530		    cpio -icdmu 2>/dev/null
531		bzcat "$MEDIA/$RELEASE/Tools/Boot/javaui.cpio.bz2" |
532		    cpio -icdmu 2>/dev/null
533		bzcat "$MEDIA/$RELEASE/Tools/Boot/lpmisc.cpio.bz2" |
534		    cpio -icdmu 2>/dev/null
535		bzcat "$MEDIA/$RELEASE/Tools/Boot/perl.cpio.bz2" |
536		    cpio -icdmu 2>/dev/null
537	)
538}
539
540do_unpack()
541{
542	rm -rf "$UNPACKED_ROOT"
543	mkdir -p "$UNPACKED_ROOT"
544	(
545		cd $MNT
546		find . -print | cpio -pdum "$UNPACKED_ROOT" 2> /dev/null
547	)
548	umount $MNT
549}
550
551unpack()
552{
553
554	if [ ! -f "$MR" ] ; then
555		usage
556		exit 1
557	fi
558
559	gzcat "$MR" > $TMR
560
561	LOFIDEV=`/usr/sbin/lofiadm -a $TMR`
562	if [ $? != 0 ] ; then
563		echo lofi plumb failed
564		exit 2
565	fi
566
567	mkdir -p $MNT
568
569	FSTYP=`fstyp $LOFIDEV`
570
571	if [ "$FSTYP" = ufs ] ; then
572		/usr/sbin/mount -o ro,nologging $LOFIDEV $MNT
573		do_unpack
574	elif [ "$FSTYP" = hsfs ] ; then
575		/usr/sbin/mount -F hsfs -o ro $LOFIDEV $MNT
576		do_unpack
577	else
578		printf "invalid root archive\n"
579	fi
580
581	rmdir $MNT
582	lofiadm -d $TMR ; LOFIDEV=
583	rm $TMR
584}
585
586pack()
587{
588	if [ ! -d "$UNPACKED_ROOT" -o -z "$MR" ] ; then
589		usage
590		exit 1
591	fi
592
593	# Estimate image size and add %10 overhead for ufs stuff.
594	# Note, we can't use du here in case $UNPACKED_ROOT is on a filesystem,
595	# e.g. zfs, in which the disk usage is less than the sum of the file
596	# sizes.  The nawk code
597	#
598	#	{t += ($7 % 1024) ? (int($7 / 1024) + 1) * 1024 : $7}
599	#
600	# below rounds up the size of a file/directory, in bytes, to the
601	# next multiple of 1024.  This mimics the behavior of ufs especially
602	# with directories.  This results in a total size that's slightly
603	# bigger than if du was called on a ufs directory.
604	size=$(find "$UNPACKED_ROOT" -ls | nawk '
605	    {t += ($7 % 1024) ? (int($7 / 1024) + 1) * 1024 : $7}
606	    END {print int(t * 1.10 / 1024)}')
607
608	/usr/sbin/mkfile ${size}k "$TMR"
609
610	LOFIDEV=`/usr/sbin/lofiadm -a "$TMR"`
611	if [ $? != 0 ] ; then
612		echo lofi plumb failed
613		exit 2
614	fi
615
616	RLOFIDEV=`echo $LOFIDEV | sed s/lofi/rlofi/`
617	newfs $RLOFIDEV < /dev/null 2> /dev/null
618	mkdir -p $MNT
619	mount -o nologging $LOFIDEV $MNT
620	rmdir $MNT/lost+found
621	(
622		cd "$UNPACKED_ROOT"
623		find . -print | cpio -pdum $MNT 2> /dev/null
624	)
625	lockfs -f $MNT
626	umount $MNT
627	rmdir $MNT
628	lofiadm -d $LOFIDEV
629	LOFIDEV=
630
631	rm -f "$TMR.gz"
632	gzip -f "$TMR"
633	mv "$TMR.gz" "$MR"
634	chmod a+r "$MR"
635}
636
637# main
638#
639
640EXTRA_SPACE=0
641STRIP_AMD64=
642
643while getopts s:6 opt ; do
644	case $opt in
645	s)	EXTRA_SPACE="$OPTARG"
646		;;
647	6)	STRIP_AMD64=false
648		;;
649	*)	usage
650		exit 1
651		;;
652	esac
653done
654shift `expr $OPTIND - 1`
655
656if [ $# != 3 ] ; then
657	usage
658	exit 1
659fi
660
661UNPACKED_ROOT="$3"
662BASE="`pwd`"
663MNT=/tmp/mnt$$
664TMR=/tmp/mr$$
665LOFIDEV=
666MR="$2"
667
668if [ "`dirname $MR`" = . ] ; then
669	MR="$BASE/$MR"
670fi
671if [ "`dirname $UNPACKED_ROOT`" = . ] ; then
672	UNPACKED_ROOT="$BASE/$UNPACKED_ROOT"
673fi
674
675trap cleanup EXIT
676
677case $1 in
678	packmedia)
679		MEDIA="$MR"
680		MR="$MEDIA/boot/x86.miniroot"
681
682		if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
683			archive_X "$MEDIA" "$UNPACKED_ROOT"
684		else
685			packmedia "$MEDIA" "$UNPACKED_ROOT"
686
687			# create the 64-bit miniroot
688			# if the -6 option was passed, don't strip
689			# the 64-bit modules from the 32-bit miniroot
690			MR="$MEDIA/boot/amd64/x86.miniroot"
691			pack
692
693			if [ "$STRIP_AMD64" = false ] ; then
694				ln $MR $MEDIA/boot/x86.miniroot
695			else
696				# create the 32-bit miniroot
697				MR="$MEDIA/boot/x86.miniroot"
698				find "$UNPACKED_ROOT" -name amd64 \
699				    -type directory | xargs rm -rf
700				pack
701			fi
702		fi ;;
703	unpackmedia)
704		MEDIA="$MR"
705		MR="$MR/boot/x86.miniroot"
706
707		if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
708			unarchive_X "$MEDIA" "$UNPACKED_ROOT"
709		else
710			unpack
711			unpackmedia "$MEDIA" "$UNPACKED_ROOT"
712		fi ;;
713	pack)	pack ;;
714	unpack)	unpack ;;
715	*)	usage ;;
716esac
717