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