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