xref: /titanic_52/usr/src/cmd/boot/scripts/root_archive.ksh (revision 94d05f6c7f329fdf908da99ab50b37d3d33f9fe5)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27# utility to pack and unpack a boot/root archive
28# both ufs and hsfs (iso9660) format archives are unpacked
29# only ufs archives are generated
30#
31# usage: pack   <archive> <root>
32#        unpack <archive> <root>
33#        packmedia   <solaris_image> <root>
34#        unpackmedia <solaris_image> <root>
35#
36#   Where <root> is the directory to unpack to and will be cleaned out
37#   if it exists.
38#
39#   In the case of (un)packmedia, the image is packed or unpacked to/from
40#   Solaris media and all the things that don't go into the ramdisk image
41#   are (un)cpio'd as well
42#
43
44usage()
45{
46	printf "usage: root_archive pack <archive> <root>\n"
47	printf "       root_archive unpack <archive> <root>\n"
48	printf "       root_archive packmedia   <solaris_image> <root>\n"
49	printf "       root_archive unpackmedia <solaris_image> <root>\n"
50}
51
52cleanup()
53{
54	if [ -d $MNT ] ; then
55		umount $MNT 2> /dev/null
56		rmdir $MNT
57	fi
58
59	lofiadm -d "$TMR" 2>/dev/null
60        if [ "$REALTHING" != true ] ; then
61		rm -f "$TMR"
62	fi
63	rm -f "$TMR.gz"
64	rm -f /tmp/flist$$
65}
66
67preload_Gnome()
68{
69	MEDIA="$1"
70	MINIROOT="$2"
71
72
73	(
74		# Prepopulate the gconf database. This needs to be done and
75		# done first for several reasons. 1) Archiving out the gnome
76		# libraries and binaries causes the gconftool-2 to not run
77		# appropriately at boot time. 2) The binaries and libraries
78		# needed to run this are big and thus we want to archive
79		# them separately. 3) Having schemas prepopluated in the
80		# miniroot means faster boot times.
81		#
82
83		cd "$MINIROOT"
84		HOME="./tmp/root"
85		export HOME
86		umask 0022
87		mumble=.tmp_proto/root/etc/gconf/gconf.xml.defaults
88		GCONF_CONFIG_SOURCE="xml:merged:$MINIROOT/$mumble"
89		export GCONF_CONFIG_SOURCE
90		SCHEMADIR="$MINIROOT/.tmp_proto/root/etc/gconf/schemas"
91		export SCHEMADIR
92		/usr/bin/gconftool-2 --makefile-install-rule \
93		    $SCHEMADIR/*.schemas >/dev/null 2>&1
94		echo '
95		xml:readwrite:/tmp/root/.gconf
96		xml:readonly:/etc/gconf/gconf.xml.defaults
97		' > /"$MINIROOT"/.tmp_proto/root/etc/gconf/2/path
98	)
99}
100
101archive_Gnome()
102{
103	MEDIA="$1"
104	MINIROOT="$2"
105
106	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
107	RELEASE=`basename "$RELEASE"`
108	CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
109
110	# Create the gnome archive
111	#
112	(
113		# usr/share gnome stuff
114		cd "$MINIROOT"
115		find usr/share/GConf usr/share/application-registry \
116		    usr/share/autostart usr/share/dbus-1 usr/share/dtds \
117		    usr/share/emacs usr/share/gnome usr/share/gnome-2.0 \
118		    usr/share/gnome-background-properties \
119		    usr/share/gtk-engines usr/share/gui-install \
120		    usr/share/icon-naming-utils usr/share/control-center \
121		    usr/share/icons usr/share/locale usr/share/metacity \
122		    usr/share/mime usr/share/mime-info usr/share/pixmaps \
123		    usr/share/scrollkeeper usr/share/sgml usr/share/themes \
124		    usr/share/xml \
125		    -print > /tmp/gnome_share.$$ 2>/dev/null
126
127		if [ ! -f /tmp/gnome_share.$$ ] ; then
128			echo "/tmp/gnome_share.$$ file list not found."
129			return
130		fi
131
132		# usr/lib gnome stuff
133
134		find usr/lib/libgnome*\.so\.* \
135		    usr/lib/libgst*\.so\.* usr/lib/libgconf*\.so\.* \
136		    usr/lib/libgdk*\.so\.* usr/lib/libgtk*\.so\.* \
137		    usr/lib/libglade*\.so\.* usr/lib/libmetacity*\.so\.* \
138		    usr/lib/libfontconfig*\.so\.* usr/lib/libgmodule*\.so\.* \
139		    usr/lib/libgobject*\.so\.* usr/lib/libgthread*\.so\.* \
140		    usr/lib/libpopt*\.so\.* usr/lib/libstartup*\.so\.* \
141		    usr/lib/libexif*\.so\.* usr/lib/libtiff*\.so\.* \
142		    usr/lib/libdbus*\.so\.* usr/lib/libstartup*\.so\.* \
143		    usr/lib/libexif*\.so\.* usr/lib/libORBit*\.so\.* \
144	 	    usr/lib/libmlib*\.so\.* usr/lib/libxsl*\.so\.* \
145		    usr/lib/libpango*\.so\.* usr/lib/libpng*\.so\.* \
146		    usr/lib/liboil*\.so\.* usr/lib/libbonobo*\.so\.* \
147		    usr/lib/libart*\.so\.* usr/lib/libcairo*\.so\.* \
148		    usr/lib/libjpeg*\.so\.* \
149		    usr/lib/libpolkit*\.so\.* \
150			-print | egrep -v '\.so\.[0]$' > \
151		       /tmp/gnome_lib.$$ 2>/dev/null
152
153		find usr/lib/nautilus usr/lib/pango usr/lib/iconv \
154		    usr/lib/metacity-dialog usr/lib/window-manager-settings \
155		    usr/lib/bonobo-2.0 usr/lib/bononbo usr/lib/gtk-2.0 \
156		    usr/lib/GConf usr/lib/bonobo-activation-server \
157		    usr/lib/python2.4 usr/lib/gstreamer-0.10 \
158		    usr/lib/gconf-sanity-check-2 usr/lib/gconfd \
159		    usr/lib/gnome-vfs-2.0 usr/lib/dbus-daemon \
160		    usr/lib/gnome-vfs-daemon usr/lib/gnome-settings-daemon \
161		    usr/lib/gnome_segv2 usr/lib/orbit-2.0 \
162		    usr/lib/libmlib \
163		    print > /tmp/gnome_libdir.$$ 2>/dev/null
164
165		if [ ! -f /tmp/gnome_lib.$$  -a ! -f gnome_libdir.$$ ] ; then
166			echo "/tmp/gnome_lib.$$ file list not found."
167			return
168		fi
169
170		# /usr/sfw gnome stuff
171		find usr/sfw/bin usr/sfw/include usr/sfw/share usr/sfw/src \
172		    -print > /tmp/gnome_sfw.$$ 2>/dev/null
173
174		if [ ! -f /tmp/gnome_sfw.$$ ] ; then
175			echo "/tmp/gnome_sfw.$$ file list not found."
176			return
177		fi
178
179		# gnome app binaries usr/bin
180		find usr/bin/gnome* usr/bin/gui-install usr/bin/bonobo* \
181		    usr/bin/gtk-* usr/bin/fax* usr/bin/gdk* usr/bin/gif2tiff \
182		    usr/bin/install-lan \
183		    usr/bin/metacity* usr/bin/gst-* usr/bin/gconftool-2 \
184		    usr/bin/pango* usr/bin/desktop* usr/bin/djpeg \
185		    usr/bin/notify-send usr/bin/oil-bugreport \
186		    usr/bin/bmp2tiff usr/bin/thembus-theme-applier \
187		    usr/bin/thumbnail usr/lib/update-* \
188		    usr/bin/ras2tiff usr/bin/raw2tiff usr/bin/rdjpgcom \
189		    usr/bin/thumbnail usr/bin/dbus* \
190		    usr/bin/tiff* usr/bin/rgb2ycbcr \
191		    usr/bin/fc-cache usr/bin/fc-list \
192			-print > /tmp/gnome_bin.$$ 2>/dev/null
193
194		if [ ! -f /tmp/gnome_bin.$$ ] ; then
195			echo "/tmp/gnome_bin.$$ file list not found."
196			return
197		fi
198
199		# Cat all the files together and create the gnome archive
200		#
201
202		cat /tmp/gnome_libdir.$$ /tmp/gnome_lib.$$ \
203		     /tmp/gnome_share.$$ /tmp/gnome_sfw.$$ /tmp/gnome_bin.$$ \
204		    > /tmp/gnome.$$
205
206		if [ ! -f /tmp/gnome.$$ ] ; then
207			echo "/tmp/gnome.$$ file not found."
208			return
209		fi
210		# Save off this file in the miniroot for use later
211		# when unpacking. Clean up old cruft if there.
212		#
213
214		if [ -f .tmp_proto/gnome_saved ]; then
215			rm -f .tmp_proto/gnome_saved
216		fi
217
218		cp /tmp/gnome.$$ .tmp_proto/gnome_saved
219
220		# Create gnome archive
221		#
222
223		cpio -ocmPuB < /tmp/gnome.$$ 2>/dev/null | bzip2 > \
224		    "$CPIO_DIR/gnome.cpio.bz2"
225
226		# Remove files from miniroot that are in archive.
227		# Create symlinks for files in archive
228
229		rm -rf `cat /tmp/gnome_share.$$`
230
231		for i in `cat /tmp/gnome_share.$$`
232		do
233			ln -s /tmp/root/$i $i 2>/dev/null
234		done
235
236		rm -rf `cat /tmp/gnome_lib.$$`
237		for i in `cat /tmp/gnome_lib.$$`
238		do
239			ln -s /tmp/root/$i $i 2>/dev/null
240		done
241
242		rm -rf `cat /tmp/gnome_libdir.$$`
243		for i in `cat /tmp/gnome_libdir.$$`
244		do
245			ln -s /tmp/root/$i $i 2>/dev/null
246		done
247
248		rm -rf `cat /tmp/gnome_sfw.$$`
249		for i in `cat /tmp/gnome_sfw.$$`
250		do
251			ln -s /tmp/root/$i $i 2>/dev/null
252		done
253
254		rm -rf `cat /tmp/gnome_bin.$$`
255		for i in `cat /tmp/gnome_bin.$$`
256		do
257			ln -s /tmp/root/$i $i 2>/dev/null
258		done
259		rm -f /tmp/gnome_share.$$
260		rm -f /tmp/gnome_lib.$$
261		rm -f /tmp/gnome_libdir.$$
262		rm -f /tmp/gnome_bin.$$
263	)
264}
265
266archive_JavaGUI()
267{
268	MEDIA="$1"
269	MINIROOT="$2"
270
271	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
272	RELEASE=`basename "$RELEASE"`
273
274	CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
275
276	# Archive the java wizard components that are only used in the
277	# non developer express path.
278	#
279	(
280		# path is usr/lib/install/data
281		cd "$MINIROOT"
282		find usr/lib/install/data/wizards \
283		    -print > /tmp/java_ui.$$ 2>/dev/null
284
285		if [ ! -f /tmp/java_ui.$$ ] ; then
286			echo "/tmp/java_ui.$$ file list not found."
287			return
288		fi
289
290		cpio -ocmPuB < /tmp/java_ui.$$ 2>/dev/null | bzip2 > \
291		    "$CPIO_DIR/javaui.cpio.bz2"
292
293		rm -rf `cat /tmp/java_ui.$$`
294		ln -s /tmp/root/usr/lib/install/data/wizards \
295		    usr/lib/install/data/wizards 2>/dev/null
296
297		rm -f /tmp/java_ui.$$
298
299	)
300}
301
302archive_Misc()
303{
304	MEDIA="$1"
305	MINIROOT="$2"
306
307	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
308	RELEASE=`basename "$RELEASE"`
309
310	CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
311
312	# Archive misc stuff that is needed by non devex installer
313	#
314	(
315		# usr/lib stuff
316		cd "$MINIROOT"
317		find usr/lib/lp -print > /tmp/lp.$$ 2>/dev/null
318		if [ ! -f /tmp/lp.$$ ] ; then
319			echo "/tmp/lp.$$ file list not found."
320			return
321		fi
322
323		cpio -ocmPuB < /tmp/lp.$$ 2>/dev/null | bzip2 > \
324		    "$CPIO_DIR/lpmisc.cpio.bz2"
325
326		rm -rf `cat /tmp/lp.$$`
327		ln -s /tmp/root/usr/lib/lp usr/lib/lp 2>/dev/null
328
329		rm -f /tmp/lp.$$
330	)
331
332}
333
334archive_Perl()
335{
336	MEDIA="$1"
337	MINIROOT="$2"
338
339	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
340	RELEASE=`basename "$RELEASE"`
341
342	CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
343
344	# Archive perl, it is only needed by gnome gui.
345	#
346	(
347		# in usr
348		cd "$MINIROOT"
349		find usr/perl5 -print > /tmp/perl.$$ 2>/dev/null
350
351		if [ ! -f /tmp/perl.$$ ] ; then
352			echo "/tmp/perl.$$ file list not found."
353			return
354		fi
355		cpio -ocmPuB < /tmp/perl.$$ 2>/dev/null | bzip2 > \
356		    "$CPIO_DIR/perl.cpio.bz2"
357
358		rm -rf `cat /tmp/perl.$$` 2>/dev/null
359		ln -s /tmp/root/usr/perl5 usr/perl5 2>/dev/null
360
361		rm -f /tmp/perl.$$
362	)
363}
364archive_X()
365{
366	MEDIA="$1"
367	MINIROOT="$2"
368
369	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
370	RELEASE=`basename "$RELEASE"`
371
372	CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
373
374	# create the graphics and non-graphics X archive
375	#
376	(
377		cd "$MINIROOT"
378		find usr/openwin usr/dt usr/X11 -print 2> /dev/null |\
379		    cpio -ocmPuB 2> /dev/null | bzip2 > "$CPIO_DIR/X.cpio.bz2"
380
381		find usr/openwin/bin/mkfontdir \
382		     usr/openwin/lib/installalias \
383		     usr/openwin/server/lib/libfont.so.1 \
384		     usr/openwin/server/lib/libtypesclr.so.0 \
385			 -print | cpio -ocmPuB 2> /dev/null | bzip2 > \
386			 "$CPIO_DIR/X_small.cpio.bz2"
387
388		rm -rf usr/dt usr/openwin usr/X11
389		ln -s /tmp/root/usr/dt usr/dt
390		ln -s /tmp/root/usr/openwin usr/openwin
391		ln -s /tmp/root/usr/X11 usr/X11
392	)
393}
394
395archive_lu()
396{
397	MEDIA="$1"
398	MINIROOT="$2"
399
400	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
401	RELEASE=`basename "$RELEASE"`
402
403	CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
404
405	(
406		cd "$MINIROOT"
407		find usr/lib/install usr/snadm usr/sbin \
408		    boot/grub boot/solaris/bootenv.rc \
409		    tmp/root/boot/grub tmp/root/boot/solaris/bootenv.rc \
410		    2> /dev/null | cpio -ocmPuB 2> /dev/null | bzip2 \
411		    > "$CPIO_DIR"/lu.cpio.bz2
412		ls platform > "$CPIO_DIR/lu.platforms"
413	)
414}
415
416cleanout_pkgdata()
417{
418	rm -Rf tmp/root/var/sadm/install tmp/root/var/sadm/pkg
419}
420
421packmedia()
422{
423	MEDIA="$1"
424	MINIROOT="$2"
425
426	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
427	RELEASE=`basename "$RELEASE"`
428	ARCHIVES="X X_small perl lpmisc javaui gnome"
429
430	mkdir -p "$MEDIA/$RELEASE/Tools/Boot"
431
432	if [ -d "$MINIROOT/platform/i86pc" ] ; then
433		mkdir -p "$MEDIA/boot/amd64"
434		mkdir -p "$MEDIA/boot/platform/i86pc/kernel"
435		mkdir -p "$MEDIA/boot/platform/i86pc/kernel/amd64"
436		mkdir -p "$MEDIA/boot/platform/i86xpv/kernel"
437		mkdir -p "$MEDIA/boot/platform/i86xpv/kernel/amd64"
438		cp "$MINIROOT/platform/i86pc/multiboot" "$MEDIA/boot"
439		cp "$MINIROOT/platform/i86pc/kernel/unix" \
440		    "$MEDIA/boot/platform/i86pc/kernel/unix"
441		cp "$MINIROOT/platform/i86pc/kernel/amd64/unix" \
442		    "$MEDIA/boot/platform/i86pc/kernel/amd64/unix"
443		cp "$MINIROOT/platform/i86xpv/kernel/unix" \
444		    "$MEDIA/boot/platform/i86xpv/kernel/unix"
445		cp "$MINIROOT/platform/i86xpv/kernel/amd64/unix" \
446		    "$MEDIA/boot/platform/i86xpv/kernel/amd64/unix"
447		(
448			cd "$MEDIA/$RELEASE/Tools/Boot"
449			ln -sf ../../../boot/x86.miniroot
450			ln -sf ../../../boot/multiboot
451			ln -sf ../../../boot/platform/i86pc/kernel/unix
452			ln -sf ../../../boot/platform/i86pc/kernel/amd64/unix
453			ln -sf ../../../boot/platform/i86xpv/kernel/unix
454			ln -sf ../../../boot/platform/i86xpv/kernel/amd64/unix
455			ln -sf ../../../boot/grub/pxegrub
456		)
457	fi
458
459	if [ -d "$MINIROOT/platform/sun4u" ] ; then
460		mkdir -p "$MEDIA/boot"
461		dd if="$MINIROOT/platform/sun4u/lib/fs/hsfs/bootblk" \
462		    of="$MEDIA/boot/hsfs.bootblock" \
463		    bs=1b oseek=1 count=15 conv=sync 2> /dev/null
464	fi
465
466	for arch in sun4u sun4v ; do
467		if [ -d "$MINIROOT/platform/$arch" ] ; then
468			archdir="$MEDIA/$RELEASE/Tools/Boot/platform/$arch"
469			mkdir -p $archdir
470			ln -sf ../../../../../boot/sparc.miniroot \
471			    "$archdir/boot_archive"
472			cp "$MINIROOT/usr/platform/$arch/lib/fs/nfs/inetboot" \
473			    "$archdir"
474			cp "$MINIROOT/platform/$arch/wanboot" \
475			    "$archdir"
476			mkdir -p "$MEDIA/platform/$arch"
477			ln -sf ../../boot/sparc.miniroot \
478			    "$MEDIA/platform/$arch/boot_archive"
479			ln -sf ../../$RELEASE/Tools/Boot/platform/$arch/wanboot \
480			    "$MEDIA/platform/$arch/wanboot"
481		fi
482	done
483
484	# archive package databases to conserve memory
485	#
486	(
487		cd "$MINIROOT"
488		find tmp/root/var/sadm/install tmp/root/var/sadm/pkg -print | \
489		    cpio -ocmPuB 2> /dev/null | bzip2 > \
490		    "$MEDIA/$RELEASE/Tools/Boot/pkg_db.cpio.bz2"
491
492		cleanout_pkgdata
493	)
494
495	archive_lu "$MEDIA" "$MINIROOT"
496
497	archive_X "$MEDIA" "$MINIROOT"
498
499	# Take out the gnome and java parts of the installer from
500	# the miniroot. These are not required to boot the system
501	# and start the installers.
502
503	if [ -d "$MINIROOT/platform/i86pc" ] ; then
504		preload_Gnome "$MEDIA" "$MINIROOT"
505		archive_Gnome "$MEDIA" "$MINIROOT"
506		archive_JavaGUI "$MEDIA" "$MINIROOT"
507		archive_Misc "$MEDIA" "$MINIROOT"
508		archive_Perl "$MEDIA" "$MINIROOT"
509		MR="$MEDIA/boot/amd64/x86.miniroot"
510		pack
511
512        	# Now that the 64-bit archives & miniroot have been created,
513        	# restore the files from archives and save the 64-bit
514        	# archives. Strip the 64-bit objects and create the
515		# 32-bit archives and miniroot
516
517		unpackmedia "$MEDIA" "$MINIROOT"
518		cleanout_pkgdata
519		mkdir -p "$MEDIA/$RELEASE/Tools/Boot/amd64"
520		for i in $ARCHIVES; do
521			mv "$MEDIA/$RELEASE/Tools/Boot/${i}.cpio.bz2" \
522				"$MEDIA/$RELEASE/Tools/Boot/amd64"
523		done
524		if [ -z "$STRIP_AMD64" ]; then
525			strip_amd64
526		fi
527
528		archive_X "$MEDIA" "$MINIROOT"
529		archive_Gnome "$MEDIA" "$MINIROOT"
530		archive_JavaGUI "$MEDIA" "$MINIROOT"
531		archive_Perl "$MEDIA" "$MINIROOT"
532		archive_Misc "$MEDIA" "$MINIROOT"
533		MR="$MEDIA/boot/x86.miniroot"
534	fi
535
536	# copy the install menu to menu.lst so we have a menu
537	# on the install media
538	#
539	if [ -f "$MINIROOT/boot/grub/install_menu" ] ; then
540		cp $MINIROOT/boot/grub/install_menu \
541		    $MEDIA/boot/grub/menu.lst
542	fi
543
544	#
545	# jumpstart utilities in usr/sbin/install.d
546	#
547	if [ -d "$MINIROOT/usr/sbin/install.d" ] ; then
548		(
549		cd ${MINIROOT}
550		find usr/sbin/install.d/chkprobe \
551		    -print | cpio -ocmPuB 2> /dev/null | bzip2 > \
552		    ${MEDIA}/${RELEASE}/Tools/Boot/usr_sbin_install_d.cpio.bz2
553		)
554	fi
555}
556
557unarchive_X()
558{
559	MEDIA="$1"
560	UNPACKED_ROOT="$2"
561
562	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
563	RELEASE=`basename "$RELEASE"`
564
565	CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
566
567	# unpack X
568	#
569	(
570		cd "$UNPACKED_ROOT"
571		rm -rf usr/dt usr/openwin usr/X11
572		bzcat "$CPIO_DIR/X.cpio.bz2" | cpio -icdmu 2> /dev/null
573	)
574}
575
576unpackmedia()
577{
578	MEDIA="$1"
579	UNPACKED_ROOT="$2"
580
581	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
582	RELEASE=`basename "$RELEASE"`
583
584	unarchive_X "$MEDIA" "$UNPACKED_ROOT"
585
586	# unpack package databases
587	#
588	(
589		cd "$UNPACKED_ROOT"
590		bzcat "$MEDIA/$RELEASE/Tools/Boot/pkg_db.cpio.bz2" |
591		    cpio -icdmu 2> /dev/null
592
593		# unpack gnome, perl, java and misc
594		# Remove symlinks left from unpacking x86.miniroot so that
595		# unpacking subsequent archives will populate appropriately.
596		#
597		rm -rf usr/perl5
598		rm -rf usr/lib/install/data/wizards
599		rm -rf usr/lib/lp
600
601		# Gnome list saved off from packmedia
602		for i in `cat .tmp_proto/gnome_saved`
603		do
604			rm -rf $i
605		done
606
607		bzcat "$MEDIA/$RELEASE/Tools/Boot/gnome.cpio.bz2" |
608		    cpio -icdmu 2>/dev/null
609		bzcat "$MEDIA/$RELEASE/Tools/Boot/javaui.cpio.bz2" |
610		    cpio -icdmu 2>/dev/null
611		bzcat "$MEDIA/$RELEASE/Tools/Boot/lpmisc.cpio.bz2" |
612		    cpio -icdmu 2>/dev/null
613		bzcat "$MEDIA/$RELEASE/Tools/Boot/perl.cpio.bz2" |
614		    cpio -icdmu 2>/dev/null
615	)
616}
617
618do_unpack()
619{
620	rm -rf "$UNPACKED_ROOT"
621	mkdir -p "$UNPACKED_ROOT"
622	(
623		cd $MNT
624		find . -print | cpio -pdum "$UNPACKED_ROOT" 2> /dev/null
625	)
626	umount $MNT
627}
628
629unpack()
630{
631
632	if [ ! -f "$MR" ] ; then
633		usage
634		exit 1
635	fi
636
637	if [ `uname -i` = i86pc ] ; then
638		gzcat "$MR" > $TMR
639	else
640		REALTHING=true ; export REALTHING
641		TMR="$MR"
642	fi
643
644	LOFIDEV=`/usr/sbin/lofiadm -a $TMR`
645	if [ $? != 0 ] ; then
646		echo lofi plumb failed
647		exit 2
648	fi
649
650	mkdir -p $MNT
651
652	FSTYP=`fstyp $LOFIDEV`
653
654	if [ "$FSTYP" = ufs ] ; then
655		/usr/sbin/mount -o ro,nologging $LOFIDEV $MNT
656		do_unpack
657	elif [ "$FSTYP" = hsfs ] ; then
658		/usr/sbin/mount -F hsfs -o ro $LOFIDEV $MNT
659		do_unpack
660	else
661		printf "invalid root archive\n"
662	fi
663
664
665	rmdir $MNT
666	lofiadm -d $TMR ; LOFIDEV=
667	if [ "$REALTHING" != true ] ; then
668		rm $TMR
669	fi
670}
671
672compress()
673{
674	SRC=$1
675	DST=$2
676
677	(
678		cd $SRC
679		filelist=`find .`
680
681		for file in $filelist ; do
682
683			file=`echo $file | sed s#^./##`
684
685			# copy all files over to preserve hard links
686			#
687			echo $file | cpio -pdum $DST 2> /dev/null
688
689			if [ -f $file ] && [ -s $file ] && [ ! -h $file ] ; then
690				fiocompress -mc $file $DST/$file &
691			fi
692
693		done
694
695		wait `pgrep fiocompress`
696
697		# now re-copy a couple of uncompressed files
698
699		if [ -d "$SRC/platform/i86pc" ] ; then
700			find `cat boot/solaris/filelist.ramdisk` -type file \
701			    -print 2> /dev/null > /tmp/flist$$
702			find usr/kernel -type file -print 2> /dev/null \
703			    >> /tmp/flist$$
704			# some of the files are replaced with links into
705			# tmp/root on the miniroot, so find the backing files
706			# from there as well and add them to the list ti
707			# be copied uncompressed
708			(
709				cd $SRC/tmp/root
710				find `cat ../../boot/solaris/filelist.ramdisk` \
711				    -type file -print 2> /dev/null | \
712				    sed 's#^#tmp/root/#' >> /tmp/flist$$
713			)
714			flist=`cat /tmp/flist$$`
715			(
716				cd $DST
717				rm -f $flist
718			)
719			for file in $flist ; do
720				echo $file | cpio -pdum $DST 2> /dev/null
721			done
722		else
723			find kernel platform -name unix | \
724			    cpio -pdum $DST 2> /dev/null
725			find kernel platform -name genunix | cpio -pdum $DST \
726			    2> /dev/null
727			find kernel platform -name platmod | cpio -pdum $DST \
728			    2> /dev/null
729			find `find kernel platform -name cpu` | \
730			    cpio -pdum $DST 2> /dev/null
731			find `find kernel platform -name kmdb\*` | \
732				cpio -pdum $DST 2> /dev/null
733			find kernel/misc/sparcv9/ctf kernel/fs/sparcv9/dcfs \
734			    kernel/misc/ctf kernel/fs/dcfs \
735			    etc/system etc/name_to_major etc/path_to_inst \
736			    etc/name_to_sysnum | cpio -pdum $DST 2> /dev/null
737		fi
738	)
739}
740
741root_is_ramdisk()
742{
743	grep -v "set root_is_ramdisk=" "$UNPACKED_ROOT"/etc/system | \
744	    grep -v "set ramdisk_size=" > /tmp/system.$$
745	cat /tmp/system.$$ > "$UNPACKED_ROOT"/etc/system
746	rm /tmp/system.$$
747
748	echo set root_is_ramdisk=1 >> "$UNPACKED_ROOT"/etc/system
749	echo set ramdisk_size=$1 >> "$UNPACKED_ROOT"/etc/system
750}
751
752pack()
753{
754	if [ ! -d "$UNPACKED_ROOT" -o -z "$MR" ] ; then
755		usage
756		exit 1
757	fi
758
759	# always compress if fiocompress exists
760	#
761	if [ -x /usr/sbin/fiocompress ] ; then
762		COMPRESS=true
763	fi
764
765	# Estimate image size and add %10 overhead for ufs stuff.
766	# Note, we can't use du here in case $UNPACKED_ROOT is on a filesystem,
767	# e.g. zfs, in which the disk usage is less than the sum of the file
768	# sizes.  The nawk code
769	#
770	#	{t += ($7 % 1024) ? (int($7 / 1024) + 1) * 1024 : $7}
771	#
772	# below rounds up the size of a file/directory, in bytes, to the
773	# next multiple of 1024.  This mimics the behavior of ufs especially
774	# with directories.  This results in a total size that's slightly
775	# bigger than if du was called on a ufs directory.
776	#
777	# if the operation in turn is compressing the files the amount
778	# of typical shrinkage is used to come up with a useful archive
779	# size
780	size=$(find "$UNPACKED_ROOT" -ls | nawk '
781	    {t += ($7 % 1024) ? (int($7 / 1024) + 1) * 1024 : $7}
782	    END {print int(t * 1.10 / 1024)}')
783	if [ "$COMPRESS" = true ] ; then
784		size=`echo $size | nawk '{s = $1} END {print int(s * 0.6)}'`
785	fi
786
787	/usr/sbin/mkfile ${size}k "$TMR"
788
789	LOFIDEV=`/usr/sbin/lofiadm -a "$TMR"`
790	if [ $? != 0 ] ; then
791		echo lofi plumb failed
792		exit 2
793	fi
794
795	RLOFIDEV=`echo $LOFIDEV | sed s/lofi/rlofi/`
796	newfs $RLOFIDEV < /dev/null 2> /dev/null
797	mkdir -p $MNT
798	mount -o nologging $LOFIDEV $MNT
799	rmdir $MNT/lost+found
800
801	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
802		root_is_ramdisk $size
803	fi
804
805	(
806		cd "$UNPACKED_ROOT"
807		if [ "$COMPRESS" = true ] ; then
808			compress . $MNT
809		else
810			find . -print | cpio -pdum $MNT 2> /dev/null
811		fi
812	)
813	lockfs -f $MNT
814	umount $MNT
815	rmdir $MNT
816
817	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
818		"$UNPACKED_ROOT/usr/sbin/installboot" \
819		    "$UNPACKED_ROOT/platform/sun4u/lib/fs/ufs/bootblk" \
820		    $RLOFIDEV
821	fi
822
823	lofiadm -d $LOFIDEV
824	LOFIDEV=
825
826	rm -f "$TMR.gz"
827
828	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
829		mv "$TMR" "$MR"
830	else
831		gzip -f "$TMR"
832		mv "$TMR.gz" "$MR"
833	fi
834
835	chmod a+r "$MR"
836}
837
838strip_amd64()
839{
840	find "$UNPACKED_ROOT" -name amd64 -type directory | xargs rm -rf
841}
842
843# main
844#
845
846EXTRA_SPACE=0
847STRIP_AMD64=
848COMPRESS=
849
850PATH=/usr/sbin:/usr/bin:/opt/sfw/bin ; export PATH
851
852while getopts s:6c opt ; do
853	case $opt in
854	s)	EXTRA_SPACE="$OPTARG"
855		;;
856	6)	STRIP_AMD64=false
857		;;
858	c)	COMPRESS=true
859		;;
860	*)	usage
861		exit 1
862		;;
863	esac
864done
865shift `expr $OPTIND - 1`
866
867if [ $# != 3 ] ; then
868	usage
869	exit 1
870fi
871
872UNPACKED_ROOT="$3"
873BASE="`pwd`"
874MNT=/tmp/mnt$$
875TMR=/tmp/mr$$
876LOFIDEV=
877MR="$2"
878
879if [ "`dirname $MR`" = . ] ; then
880	MR="$BASE/$MR"
881fi
882if [ "`dirname $UNPACKED_ROOT`" = . ] ; then
883	UNPACKED_ROOT="$BASE/$UNPACKED_ROOT"
884fi
885
886
887MEDIA="$MR"
888
889trap cleanup EXIT
890
891case $1 in
892	packmedia)
893		if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
894			ARCHIVE=sparc.miniroot
895		else
896			ARCHIVE=x86.miniroot
897		fi
898		MR="$MEDIA/boot/$ARCHIVE"
899
900		packmedia "$MEDIA" "$UNPACKED_ROOT"
901		pack
902
903		;;
904	unpackmedia)
905		if [ -f "$MEDIA/boot/sparc.miniroot" ] ; then
906			ARCHIVE=sparc.miniroot
907		else
908			ARCHIVE=x86.miniroot
909		fi
910		MR="$MEDIA/boot/$ARCHIVE"
911		unpack
912		unpackmedia "$MEDIA" "$UNPACKED_ROOT"
913		;;
914	pack)	pack
915		;;
916	unpack)	unpack
917		;;
918	*)	usage
919		;;
920esac
921