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