xref: /titanic_41/usr/src/cmd/boot/scripts/root_archive.ksh (revision b6917abefc343244b784f0cc34bc65b01469c3bf)
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/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	#
541	# jumpstart utilities in usr/sbin/install.d
542	#
543	if [ -d "$MINIROOT/usr/sbin/install.d" ] ; then
544		(
545		cd ${MINIROOT}
546		find usr/sbin/install.d/chkprobe \
547		    -print | cpio -ocmPuB 2> /dev/null | bzip2 > \
548		    ${MEDIA}/${RELEASE}/Tools/Boot/usr_sbin_install_d.cpio.bz2
549		)
550	fi
551}
552
553unarchive_X()
554{
555	MEDIA="$1"
556	UNPACKED_ROOT="$2"
557
558	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
559	RELEASE=`basename "$RELEASE"`
560
561	CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
562
563	# unpack X
564	#
565	(
566		cd "$UNPACKED_ROOT"
567		rm -rf usr/dt usr/openwin usr/X11
568		bzcat "$CPIO_DIR/X.cpio.bz2" | cpio -icdmu 2> /dev/null
569	)
570}
571
572unpackmedia()
573{
574	MEDIA="$1"
575	UNPACKED_ROOT="$2"
576
577	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
578	RELEASE=`basename "$RELEASE"`
579
580	unarchive_X "$MEDIA" "$UNPACKED_ROOT"
581
582	# unpack package databases
583	#
584	(
585		cd "$UNPACKED_ROOT"
586		bzcat "$MEDIA/$RELEASE/Tools/Boot/pkg_db.cpio.bz2" |
587		    cpio -icdmu 2> /dev/null
588
589		# unpack gnome, perl, java and misc
590		# Remove symlinks left from unpacking x86.miniroot so that
591		# unpacking subsequent archives will populate appropriately.
592		#
593		rm -rf usr/perl5
594		rm -rf usr/lib/install/data/wizards
595		rm -rf usr/lib/lp
596
597		# Gnome list saved off from packmedia
598		for i in `cat .tmp_proto/gnome_saved`
599		do
600			rm -rf $i
601		done
602
603		bzcat "$MEDIA/$RELEASE/Tools/Boot/gnome.cpio.bz2" |
604		    cpio -icdmu 2>/dev/null
605		bzcat "$MEDIA/$RELEASE/Tools/Boot/javaui.cpio.bz2" |
606		    cpio -icdmu 2>/dev/null
607		bzcat "$MEDIA/$RELEASE/Tools/Boot/lpmisc.cpio.bz2" |
608		    cpio -icdmu 2>/dev/null
609		bzcat "$MEDIA/$RELEASE/Tools/Boot/perl.cpio.bz2" |
610		    cpio -icdmu 2>/dev/null
611	)
612}
613
614do_unpack()
615{
616	rm -rf "$UNPACKED_ROOT"
617	mkdir -p "$UNPACKED_ROOT"
618	(
619		cd $MNT
620		find . -print | cpio -pdum "$UNPACKED_ROOT" 2> /dev/null
621	)
622	umount $MNT
623}
624
625unpack()
626{
627
628	if [ ! -f "$MR" ] ; then
629		usage
630		exit 1
631	fi
632
633	if [ `basename $MR` = x86.miniroot ] ; then
634		gzcat "$MR" > $TMR
635	else
636		REALTHING=true ; export REALTHING
637		TMR="$MR"
638	fi
639
640	LOFIDEV=`/usr/sbin/lofiadm -a $TMR`
641	if [ $? != 0 ] ; then
642		echo lofi plumb failed
643		exit 2
644	fi
645
646	mkdir -p $MNT
647
648	FSTYP=`fstyp $LOFIDEV`
649
650	if [ "$FSTYP" = ufs ] ; then
651		/usr/sbin/mount -o ro,nologging $LOFIDEV $MNT
652		do_unpack
653	elif [ "$FSTYP" = hsfs ] ; then
654		/usr/sbin/mount -F hsfs -o ro $LOFIDEV $MNT
655		do_unpack
656	else
657		printf "invalid root archive\n"
658	fi
659
660
661	rmdir $MNT
662	lofiadm -d $TMR ; LOFIDEV=
663	if [ "$REALTHING" != true ] ; then
664		rm $TMR
665	fi
666}
667
668compress()
669{
670	SRC=$1
671	DST=$2
672
673	(
674		cd $SRC
675		filelist=`find .`
676
677		for file in $filelist ; do
678
679			file=`echo $file | sed s#^./##`
680
681			# copy all files over to preserve hard links
682			#
683			echo $file | cpio -pdum $DST 2> /dev/null
684
685			if [ -f $file ] && [ -s $file ] && [ ! -h $file ] ; then
686				fiocompress -mc $file $DST/$file &
687			fi
688
689		done
690
691		# now re-copy a couple of uncompressed files
692		#
693
694		find kernel platform -name unix | cpio -pdum $DST 2> /dev/null
695		find kernel platform -name genunix | cpio -pdum $DST \
696		    2> /dev/null
697		find kernel platform -name platmod | cpio -pdum $DST \
698		    2> /dev/null
699		find `find kernel platform -name cpu` | cpio -pdum $DST \
700		    2> /dev/null
701		find `find kernel platform -name kmdb\*` | cpio -pdum $DST \
702		    2> /dev/null
703		find kernel/misc/sparcv9/ctf kernel/fs/sparcv9/dcfs \
704		    etc/system etc/name_to_major etc/path_to_inst \
705		    etc/name_to_sysnum | cpio -pdum $DST 2> /dev/null
706	)
707}
708
709root_is_ramdisk()
710{
711	grep -v "set root_is_ramdisk=" "$UNPACKED_ROOT"/etc/system | \
712	    grep -v "set ramdisk_size=" > /tmp/system.$$
713	cat /tmp/system.$$ > "$UNPACKED_ROOT"/etc/system
714	rm /tmp/system.$$
715
716	echo set root_is_ramdisk=1 >> "$UNPACKED_ROOT"/etc/system
717	echo set ramdisk_size=$1 >> "$UNPACKED_ROOT"/etc/system
718}
719
720pack()
721{
722	if [ ! -d "$UNPACKED_ROOT" -o -z "$MR" ] ; then
723		usage
724		exit 1
725	fi
726
727	# always compress on sparc if fiocompress exists
728	#
729	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] && \
730	    [ -x /usr/sbin/fiocompress ] ; then
731		COMPRESS=true
732	fi
733
734	# Estimate image size and add %10 overhead for ufs stuff.
735	# Note, we can't use du here in case $UNPACKED_ROOT is on a filesystem,
736	# e.g. zfs, in which the disk usage is less than the sum of the file
737	# sizes.  The nawk code
738	#
739	#	{t += ($7 % 1024) ? (int($7 / 1024) + 1) * 1024 : $7}
740	#
741	# below rounds up the size of a file/directory, in bytes, to the
742	# next multiple of 1024.  This mimics the behavior of ufs especially
743	# with directories.  This results in a total size that's slightly
744	# bigger than if du was called on a ufs directory.
745	#
746	# if the operation in turn is compressing the files the amount
747	# of typical shrinkage is used to come up with a useful archive
748	# size
749	size=$(find "$UNPACKED_ROOT" -ls | nawk '
750	    {t += ($7 % 1024) ? (int($7 / 1024) + 1) * 1024 : $7}
751	    END {print int(t * 1.10 / 1024)}')
752	if [ "$COMPRESS" = true ] ; then
753		size=`echo $size | nawk '{s = $1} END {print int(s * .53)}'`
754	fi
755
756	/usr/sbin/mkfile ${size}k "$TMR"
757
758	LOFIDEV=`/usr/sbin/lofiadm -a "$TMR"`
759	if [ $? != 0 ] ; then
760		echo lofi plumb failed
761		exit 2
762	fi
763
764	RLOFIDEV=`echo $LOFIDEV | sed s/lofi/rlofi/`
765	newfs $RLOFIDEV < /dev/null 2> /dev/null
766	mkdir -p $MNT
767	mount -o nologging $LOFIDEV $MNT
768	rmdir $MNT/lost+found
769
770	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
771		root_is_ramdisk $size
772	fi
773
774	(
775		cd "$UNPACKED_ROOT"
776		if [ "$COMPRESS" = true ] ; then
777			compress . $MNT
778		else
779			find . -print | cpio -pdum $MNT 2> /dev/null
780		fi
781	)
782	lockfs -f $MNT
783	umount $MNT
784	rmdir $MNT
785
786	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
787		"$UNPACKED_ROOT/usr/sbin/installboot" \
788		    "$UNPACKED_ROOT/platform/sun4u/lib/fs/ufs/bootblk" \
789		    $RLOFIDEV
790	fi
791
792	lofiadm -d $LOFIDEV
793	LOFIDEV=
794
795	rm -f "$TMR.gz"
796
797	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
798		mv "$TMR" "$MR"
799	else
800		gzip -f "$TMR"
801		mv "$TMR.gz" "$MR"
802	fi
803
804	chmod a+r "$MR"
805}
806
807strip_amd64()
808{
809	find "$UNPACKED_ROOT" -name amd64 -type directory | xargs rm -rf
810}
811
812# main
813#
814
815EXTRA_SPACE=0
816STRIP_AMD64=
817COMPRESS=
818
819PATH=/usr/sbin:/usr/bin:/opt/sfw/bin ; export PATH
820
821while getopts s:6c opt ; do
822	case $opt in
823	s)	EXTRA_SPACE="$OPTARG"
824		;;
825	6)	STRIP_AMD64=false
826		;;
827	c)	COMPRESS=true
828		;;
829	*)	usage
830		exit 1
831		;;
832	esac
833done
834shift `expr $OPTIND - 1`
835
836if [ $# != 3 ] ; then
837	usage
838	exit 1
839fi
840
841UNPACKED_ROOT="$3"
842BASE="`pwd`"
843MNT=/tmp/mnt$$
844TMR=/tmp/mr$$
845LOFIDEV=
846MR="$2"
847
848if [ "`dirname $MR`" = . ] ; then
849	MR="$BASE/$MR"
850fi
851if [ "`dirname $UNPACKED_ROOT`" = . ] ; then
852	UNPACKED_ROOT="$BASE/$UNPACKED_ROOT"
853fi
854
855
856MEDIA="$MR"
857
858trap cleanup EXIT
859
860case $1 in
861	packmedia)
862		if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
863			ARCHIVE=sparc.miniroot
864		else
865			ARCHIVE=x86.miniroot
866		fi
867		MR="$MEDIA/boot/$ARCHIVE"
868
869		packmedia "$MEDIA" "$UNPACKED_ROOT"
870		pack
871
872		;;
873	unpackmedia)
874		if [ -f "$MEDIA/boot/sparc.miniroot" ] ; then
875			ARCHIVE=sparc.miniroot
876		else
877			ARCHIVE=x86.miniroot
878		fi
879		MR="$MEDIA/boot/$ARCHIVE"
880		unpack
881		unpackmedia "$MEDIA" "$UNPACKED_ROOT"
882		;;
883	pack)	pack
884		;;
885	unpack)	unpack
886		;;
887	*)	usage
888		;;
889esac
890