10Sstevel@tonic-gate#!/bin/ksh
20Sstevel@tonic-gate#
30Sstevel@tonic-gate# CDDL HEADER START
40Sstevel@tonic-gate#
50Sstevel@tonic-gate# The contents of this file are subject to the terms of the
61777Ssetje# Common Development and Distribution License (the "License").
71777Ssetje# You may not use this file except in compliance with the License.
80Sstevel@tonic-gate#
90Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate# See the License for the specific language governing permissions
120Sstevel@tonic-gate# and limitations under the License.
130Sstevel@tonic-gate#
140Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate#
200Sstevel@tonic-gate# CDDL HEADER END
210Sstevel@tonic-gate#
220Sstevel@tonic-gate
231777Ssetje# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate# Use is subject to license terms.
250Sstevel@tonic-gate#
261777Ssetje# ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate# utility to pack and unpack a boot/root archive
290Sstevel@tonic-gate# both ufs and hsfs (iso9660) format archives are unpacked
300Sstevel@tonic-gate# only ufs archives are generated
310Sstevel@tonic-gate#
320Sstevel@tonic-gate# usage: pack   <archive> <root>
330Sstevel@tonic-gate#        unpack <archive> <root>
340Sstevel@tonic-gate#        packmedia   <solaris_image> <root>
350Sstevel@tonic-gate#        unpackmedia <solaris_image> <root>
360Sstevel@tonic-gate#
370Sstevel@tonic-gate#   Where <root> is the directory to unpack to and will be cleaned out
380Sstevel@tonic-gate#   if it exists.
390Sstevel@tonic-gate#
400Sstevel@tonic-gate#   In the case of (un)packmedia, the image is packed or unpacked to/from
410Sstevel@tonic-gate#   Solaris media and all the things that don't go into the ramdisk image
420Sstevel@tonic-gate#   are (un)cpio'd as well
430Sstevel@tonic-gate#
441777Ssetje# This utility is also used to pack parts (in essence the window system,
451777Ssetje# usr/dt and usr/openwin) of the non ramdisk SPARC
461777Ssetje# miniroot. (un)packmedia will recognize that they are being run a SPARC
471777Ssetje# miniroot and do the appropriate work.
481777Ssetje#
490Sstevel@tonic-gate
500Sstevel@tonic-gateusage()
510Sstevel@tonic-gate{
520Sstevel@tonic-gate	printf "usage: root_archive pack <archive> <root>\n"
530Sstevel@tonic-gate	printf "       root_archive unpack <archive> <root>\n"
540Sstevel@tonic-gate	printf "       root_archive packmedia   <solaris_image> <root>\n"
550Sstevel@tonic-gate	printf "       root_archive unpackmedia <solaris_image> <root>\n"
560Sstevel@tonic-gate}
570Sstevel@tonic-gate
581777Ssetjearchive_X()
590Sstevel@tonic-gate{
601777Ssetje	MEDIA="$1"
611777Ssetje	MINIROOT="$2"
620Sstevel@tonic-gate
631777Ssetje	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
641777Ssetje	RELEASE=`basename "$RELEASE"`
650Sstevel@tonic-gate
661777Ssetje	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
671777Ssetje		CPIO_DIR="$MEDIA/$RELEASE/Tools/miniroot_extra"
681777Ssetje		mkdir -p "$CPIO_DIR"
691777Ssetje	else
701777Ssetje		CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
711777Ssetje	fi
720Sstevel@tonic-gate
730Sstevel@tonic-gate	# create the graphics and non-graphics X archive
740Sstevel@tonic-gate	#
751777Ssetje	cd "$MINIROOT/usr"
760Sstevel@tonic-gate	find openwin dt -print | cpio -ocmPuB 2> /dev/null | bzip2 > \
771777Ssetje	    "$CPIO_DIR/X.cpio.bz2"
780Sstevel@tonic-gate
790Sstevel@tonic-gate	find openwin/bin/mkfontdir \
800Sstevel@tonic-gate	     openwin/lib/installalias \
810Sstevel@tonic-gate	     openwin/server/lib/libfont.so.1 \
820Sstevel@tonic-gate	     openwin/server/lib/libtypesclr.so.0 \
830Sstevel@tonic-gate	         -print | cpio -ocmPuB 2> /dev/null | bzip2 > \
841777Ssetje	         "$CPIO_DIR/X_small.cpio.bz2"
850Sstevel@tonic-gate
860Sstevel@tonic-gate	rm -rf dt openwin
870Sstevel@tonic-gate	ln -s ../tmp/root/usr/dt
880Sstevel@tonic-gate	ln -s ../tmp/root/usr/openwin
890Sstevel@tonic-gate	cd ../..
901777Ssetje}
911777Ssetje
921777Ssetjepackmedia()
931777Ssetje{
941777Ssetje	MEDIA="$1"
951777Ssetje	MINIROOT="$2"
961777Ssetje
971777Ssetje	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
981777Ssetje	RELEASE=`basename "$RELEASE"`
991777Ssetje
1001777Ssetje	mkdir -p "$MEDIA/$RELEASE/Tools/Boot"
1011777Ssetje	mkdir -p "$MEDIA/boot"
1021777Ssetje
1031777Ssetje	cd "$MINIROOT"
1041777Ssetje
1051777Ssetje	# archive package databases to conserve memory
1061777Ssetje	#
1071777Ssetje	find tmp/root/var/sadm/install tmp/root/var/sadm/pkg -print | \
1081777Ssetje	    cpio -ocmPuB 2> /dev/null | bzip2 > \
1091777Ssetje	    "$MEDIA/$RELEASE/Tools/Boot/pkg_db.cpio.bz2"
1101777Ssetje
1111777Ssetje	rm -rf "$MINIROOT/tmp/root/var/sadm/install"
1121777Ssetje	rm -rf "$MINIROOT/tmp/root/var/sadm/pkg"
1131777Ssetje
1141777Ssetje	archive_X "$MEDIA" "$MINIROOT"
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate	# clear out 64 bit support to conserve memory
1170Sstevel@tonic-gate	#
1181777Ssetje	find "$MINIROOT" -name amd64 -type directory | xargs rm -rf
1190Sstevel@tonic-gate
1201777Ssetje	cp "$MINIROOT/platform/i86pc/multiboot" "$MEDIA/boot"
1210Sstevel@tonic-gate
122*1821Ssetje	# copy the install menu to menu.lst so we have a menu
123*1821Ssetje	# on the install media
124*1821Ssetje	#
125*1821Ssetje	if [ -f "${MINIROOT}/boot/grub/install_menu" ] ; then
126*1821Ssetje		cp ${MINIROOT}/boot/grub/install_menu \
127*1821Ssetje		    ${MEDIA}/boot/grub/menu.lst
128*1821Ssetje	fi
129*1821Ssetje
1301777Ssetje	cd "$MEDIA/$RELEASE/Tools/Boot"
1310Sstevel@tonic-gate	ln -sf ../../../boot/x86.miniroot
1320Sstevel@tonic-gate	ln -sf ../../../boot/multiboot
1330Sstevel@tonic-gate	ln -sf ../../../boot/grub/pxegrub
1341777Ssetje}
1350Sstevel@tonic-gate
1361777Ssetjeunarchive_X()
1371777Ssetje{
1381777Ssetje	MEDIA="$1"
1391777Ssetje	UNPACKED_ROOT="$2"
1401777Ssetje
1411777Ssetje	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
1421777Ssetje	RELEASE=`basename "$RELEASE"`
1431777Ssetje
1441777Ssetje	if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
1451777Ssetje		CPIO_DIR="$MEDIA/$RELEASE/Tools/miniroot_extra"
1461777Ssetje	else
1471777Ssetje		CPIO_DIR="$MEDIA/$RELEASE/Tools/Boot"
1480Sstevel@tonic-gate	fi
1491777Ssetje
1501777Ssetje	# unpack X
1511777Ssetje	#
1521777Ssetje	cd "$UNPACKED_ROOT/usr"
1531777Ssetje	rm -rf dt openwin
1541777Ssetje	bzcat "$CPIO_DIR/X.cpio.bz2" | cpio -icdmu 2> /dev/null
1550Sstevel@tonic-gate}
1560Sstevel@tonic-gate
1570Sstevel@tonic-gateunpackmedia()
1581777Ssetje{
1591777Ssetje	MEDIA="$1"
1601777Ssetje	UNPACKED_ROOT="$2"
1610Sstevel@tonic-gate
1621777Ssetje	RELEASE=`/bin/ls -d "$MEDIA/Solaris_"*`
1631777Ssetje	RELEASE=`basename "$RELEASE"`
1641777Ssetje
1651777Ssetje	unarchive_X "$MEDIA" "$UNPACKED_ROOT"
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate	# unpack package databases
1680Sstevel@tonic-gate	#
1691777Ssetje	cd "$UNPACKED_ROOT"
1701777Ssetje	bzcat "$MEDIA/$RELEASE/Tools/Boot/pkg_db.cpio.bz2" | cpio -icdmu \
1710Sstevel@tonic-gate	    2> /dev/null
1720Sstevel@tonic-gate}
1730Sstevel@tonic-gate
1740Sstevel@tonic-gatedo_unpack()
1750Sstevel@tonic-gate{
1761777Ssetje	rm -rf "$UNPACKED_ROOT"
1771777Ssetje	mkdir -p "$UNPACKED_ROOT"
1780Sstevel@tonic-gate	cd $MNT
1791777Ssetje	find . -print | cpio -pdum "$UNPACKED_ROOT" 2> /dev/null
1801777Ssetje	cd "$BASE"
1810Sstevel@tonic-gate	umount $MNT
1820Sstevel@tonic-gate}
1830Sstevel@tonic-gate
1840Sstevel@tonic-gateunpack()
1850Sstevel@tonic-gate{
1860Sstevel@tonic-gate
1871777Ssetje	if [ ! -f "$MR" ] ; then
1880Sstevel@tonic-gate		usage
1890Sstevel@tonic-gate		exit 1
1900Sstevel@tonic-gate	fi
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate	TMR=/tmp/mr$$
1931777Ssetje	gzcat "$MR" > $TMR
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate	lofidev=`/usr/sbin/lofiadm -a $TMR`
1960Sstevel@tonic-gate	if [ $? != 0 ] ; then
1970Sstevel@tonic-gate		echo lofi plumb failed
1980Sstevel@tonic-gate		exit 2
1990Sstevel@tonic-gate	fi
2000Sstevel@tonic-gate
2011777Ssetje	mkdir -p $MNT
2020Sstevel@tonic-gate
2031777Ssetje	FSTYP=`fstyp $lofidev`
2040Sstevel@tonic-gate
2051777Ssetje	if [ "$FSTYP" = ufs ] ; then
2060Sstevel@tonic-gate		/usr/sbin/mount -o ro,nologging $lofidev $MNT
2070Sstevel@tonic-gate		do_unpack
2081777Ssetje	elif [ "$FSTYP" = hsfs ] ; then
2090Sstevel@tonic-gate		/usr/sbin/mount -F hsfs -o ro $lofidev $MNT
2100Sstevel@tonic-gate		do_unpack
2110Sstevel@tonic-gate	else
2120Sstevel@tonic-gate		printf "invalid root archive\n"
2130Sstevel@tonic-gate	fi
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate	rmdir $MNT
2160Sstevel@tonic-gate	lofiadm -d $TMR
2170Sstevel@tonic-gate	rm $TMR
2180Sstevel@tonic-gate}
2190Sstevel@tonic-gate
2200Sstevel@tonic-gatepack()
2210Sstevel@tonic-gate{
2221777Ssetje	if [ ! -d "$UNPACKED_ROOT" -o -z "$MR" ] ; then
2230Sstevel@tonic-gate		usage
2240Sstevel@tonic-gate		exit 1
2250Sstevel@tonic-gate	fi
2260Sstevel@tonic-gate
2271777Ssetje	size=`du -sk "$UNPACKED_ROOT" | ( read size name; echo $size )`
2280Sstevel@tonic-gate	size=`expr $size + \( $size \* 10 \) / 100`
2291777Ssetje	rm -f "$MR"
2301777Ssetje	/usr/sbin/mkfile ${size}k "$MR"
2310Sstevel@tonic-gate
2321777Ssetje	lofidev=`/usr/sbin/lofiadm -a "$MR"`
2330Sstevel@tonic-gate	if [ $? != 0 ] ; then
2340Sstevel@tonic-gate		echo lofi plumb failed
2350Sstevel@tonic-gate		exit 2
2360Sstevel@tonic-gate	fi
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate	rlofidev=`echo $lofidev | sed s/lofi/rlofi/`
2390Sstevel@tonic-gate	newfs $rlofidev < /dev/null 2> /dev/null
2401777Ssetje	mkdir -p $MNT
2410Sstevel@tonic-gate	mount -o nologging $lofidev $MNT
2421777Ssetje	rmdir $MNT/lost+found
2431777Ssetje	cd "$UNPACKED_ROOT"
2440Sstevel@tonic-gate	find . -print | cpio -pdum $MNT 2> /dev/null
2450Sstevel@tonic-gate	lockfs -f $MNT
2460Sstevel@tonic-gate	umount $MNT
2470Sstevel@tonic-gate	rmdir $MNT
2481777Ssetje	lofiadm -d "$MR"
2490Sstevel@tonic-gate
2501777Ssetje	cd "$BASE"
2510Sstevel@tonic-gate
2521777Ssetje	rm -f "$MR.gz"
2531777Ssetje	gzip -f "$MR"
2541777Ssetje	mv "$MR.gz" "$MR"
2551777Ssetje	chmod a+r "$MR"
2560Sstevel@tonic-gate}
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate# main
2590Sstevel@tonic-gate#
2600Sstevel@tonic-gate
2610Sstevel@tonic-gateif [ $# != 3 ] ; then
2620Sstevel@tonic-gate	usage
2630Sstevel@tonic-gate	exit 1
2640Sstevel@tonic-gatefi
2650Sstevel@tonic-gate
2661777SsetjeUNPACKED_ROOT="$3"
2671777SsetjeBASE="`pwd`"
2680Sstevel@tonic-gateMNT=/tmp/mnt$$
2691777SsetjeMR="$2"
2700Sstevel@tonic-gate
2710Sstevel@tonic-gateif [ "`dirname $MR`" = . ] ; then
2721777Ssetje	MR="$BASE/$MR"
2730Sstevel@tonic-gatefi
2740Sstevel@tonic-gateif [ "`dirname $UNPACKED_ROOT`" = . ] ; then
2751777Ssetje	UNPACKED_ROOT="$BASE/$UNPACKED_ROOT"
2760Sstevel@tonic-gatefi
2770Sstevel@tonic-gate
2780Sstevel@tonic-gatecase $1 in
2790Sstevel@tonic-gate	packmedia)
2801777Ssetje		MEDIA="$MR"
2811777Ssetje		MR="$MR/boot/x86.miniroot"
2821777Ssetje
2831777Ssetje		if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
2841777Ssetje			archive_X "$MEDIA" "$UNPACKED_ROOT"
2851777Ssetje		else
2861777Ssetje			packmedia "$MEDIA" "$UNPACKED_ROOT"
2871777Ssetje			pack
2881777Ssetje		fi ;;
2890Sstevel@tonic-gate	unpackmedia)
2901777Ssetje		MEDIA="$MR"
2911777Ssetje		MR="$MR/boot/x86.miniroot"
2921777Ssetje
2931777Ssetje		if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
2941777Ssetje			unarchive_X "$MEDIA" "$UNPACKED_ROOT"
2951777Ssetje		else
2961777Ssetje			unpack
2971777Ssetje			unpackmedia "$MEDIA" "$UNPACKED_ROOT"
2981777Ssetje		fi ;;
2990Sstevel@tonic-gate	pack)	pack ;;
3000Sstevel@tonic-gate	unpack)	unpack ;;
3010Sstevel@tonic-gate	*)	usage ;;
3020Sstevel@tonic-gateesac
303