xref: /illumos-gate/usr/src/cmd/boot/scripts/root_archive.ksh (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
1#!/bin/ksh
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, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23
24# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29# utility to pack and unpack a boot/root archive
30# both ufs and hsfs (iso9660) format archives are unpacked
31# only ufs archives are generated
32#
33# usage: pack   <archive> <root>
34#        unpack <archive> <root>
35#        packmedia   <solaris_image> <root>
36#        unpackmedia <solaris_image> <root>
37#
38#   Where <root> is the directory to unpack to and will be cleaned out
39#   if it exists.
40#
41#   In the case of (un)packmedia, the image is packed or unpacked to/from
42#   Solaris media and all the things that don't go into the ramdisk image
43#   are (un)cpio'd as well
44#
45
46usage()
47{
48	printf "usage: root_archive pack <archive> <root>\n"
49	printf "       root_archive unpack <archive> <root>\n"
50	printf "       root_archive packmedia   <solaris_image> <root>\n"
51	printf "       root_archive unpackmedia <solaris_image> <root>\n"
52}
53
54packmedia()
55{
56	MEDIA=$1
57	MINIROOT=$2
58
59	RELEASE=`ls -d ${MEDIA}/Solaris_*`
60	RELEASE=`basename ${RELEASE}`
61
62	mkdir -p ${MEDIA}/${RELEASE}/Tools/Boot
63	mkdir -p ${MEDIA}/boot
64
65	cd ${MINIROOT}
66
67	# archive package databases to conserve memory
68	#
69	find tmp/root/var/sadm/install tmp/root/var/sadm/pkg -print | \
70	    cpio -ocmPuB 2> /dev/null | bzip2 > \
71	    ${MEDIA}/${RELEASE}/Tools/Boot/pkg_db.cpio.bz2
72
73	rm -rf ${MINIROOT}/tmp/root/var/sadm/install
74	rm -rf ${MINIROOT}/tmp/root/var/sadm/pkg
75
76	# create the graphics and non-graphics X archive
77	#
78	cd ${MINIROOT}/usr
79	find openwin dt -print | cpio -ocmPuB 2> /dev/null | bzip2 > \
80	    ${MEDIA}/${RELEASE}/Tools/Boot/X.cpio.bz2
81
82	find openwin/bin/mkfontdir \
83	     openwin/lib/installalias \
84	     openwin/server/lib/libfont.so.1 \
85	     openwin/server/lib/libtypesclr.so.0 \
86	         -print | cpio -ocmPuB 2> /dev/null | bzip2 > \
87	         ${MEDIA}/${RELEASE}/Tools/Boot/X_small.cpio.bz2
88
89	rm -rf dt openwin
90	ln -s ../tmp/root/usr/dt
91	ln -s ../tmp/root/usr/openwin
92	cd ../..
93
94	# clear out 64 bit support to conserve memory
95	#
96	find ${MINIROOT} -name amd64 -type directory | xargs rm -rf
97
98	cp ${MINIROOT}/platform/i86pc/multiboot ${MEDIA}/boot
99
100	# XXX fix as soon as we deliver boot/grub/install_menu
101	#
102	if [ -f "${MINIROOT}/boot/grub/install_menu" ] ; then
103		cp ${MINIROOT}/boot/grub/install_menu \
104		    ${MEDIA}/boot/grub/menu.lst
105	elif [ -f "/ws/boot-gate/usr/src/grub/menu.lst.cd_dvd" ] ; then
106		cp /ws/boot-gate/usr/src/grub/menu.lst.cd_dvd \
107		    ${MEDIA}/boot/grub/menu.lst
108	elif [ -f "/ws/boot-gate/usr/src/grub/install_menu" ] ; then
109		cp /ws/boot-gate/usr/src/grub/install_menu \
110		    ${MEDIA}/boot/grub/menu.lst
111	fi
112
113	cd ${MEDIA}/${RELEASE}/Tools/Boot
114	ln -sf ../../../boot/x86.miniroot
115	ln -sf ../../../boot/multiboot
116	ln -sf ../../../boot/grub/pxegrub
117
118	# XXX fix once SUNWgzip is included in the miniroot
119	if [ ! -f "${MINIROOT}/usr/bin/gzip" ] ; then
120		cp /usr/bin/gzip ${MINIROOT}/usr/bin
121	fi
122}
123
124unpackmedia()
125 {
126	MEDIA=$1
127	UNPACKED_ROOT=$2
128
129	RELEASE=`ls -d ${MEDIA}/Solaris_*`
130	RELEASE=`basename ${RELEASE}`
131
132	# unpack X
133	#
134	cd ${UNPACKED_ROOT}/usr
135	rm -rf dt openwin
136	bzcat ${MEDIA}/${RELEASE}/Tools/Boot/X.cpio.bz2 | cpio -icdmu \
137	    2> /dev/null
138
139	# unpack package databases
140	#
141	cd $UNPACKED_ROOT
142	bzcat ${MEDIA}/${RELEASE}/Tools/Boot/pkg_db.cpio.bz2 | cpio -icdmu \
143	    2> /dev/null
144}
145
146do_unpack()
147{
148	rm -rf $UNPACKED_ROOT
149	mkdir $UNPACKED_ROOT
150	cd $MNT
151	find . -print | cpio -pdum $UNPACKED_ROOT 2> /dev/null
152	cd $BASE
153	umount $MNT
154}
155
156unpack()
157{
158
159	if [ ! -f "${MR}" ] ; then
160		usage
161		exit 1
162	fi
163
164	TMR=/tmp/mr$$
165	gzcat $MR > $TMR
166
167	lofidev=`/usr/sbin/lofiadm -a $TMR`
168	if [ $? != 0 ] ; then
169		echo lofi plumb failed
170		exit 2
171	fi
172
173	mkdir $MNT
174
175	FSTYP=`fstyp ${lofidev}`
176
177	if [ "${FSTYP}" = ufs ] ; then
178		/usr/sbin/mount -o ro,nologging $lofidev $MNT
179		do_unpack
180	elif [ "${FSTYP}" = hsfs ] ; then
181		/usr/sbin/mount -F hsfs -o ro $lofidev $MNT
182		do_unpack
183	else
184		printf "invalid root archive\n"
185	fi
186
187	rmdir $MNT
188	lofiadm -d $TMR
189	rm $TMR
190}
191
192pack()
193{
194	if [ ! -d "${UNPACKED_ROOT}" -o "X${MR}" = "X" ] ; then
195		usage
196		exit 1
197	fi
198
199	size=`du -sk ${UNPACKED_ROOT} | (read size name; echo ${size})`
200	size=`expr $size + \( $size \* 10 \) / 100`
201	rm -f ${MR}
202	/usr/sbin/mkfile ${size}k $MR
203
204	lofidev=`/usr/sbin/lofiadm -a $MR`
205	if [ $? != 0 ] ; then
206		echo lofi plumb failed
207		exit 2
208	fi
209
210	rlofidev=`echo $lofidev | sed s/lofi/rlofi/`
211	newfs $rlofidev < /dev/null 2> /dev/null
212	mkdir $MNT
213	mount -o nologging $lofidev $MNT
214	rmdir ${MNT}/lost+found
215	cd $UNPACKED_ROOT
216	find . -print | cpio -pdum $MNT 2> /dev/null
217	lockfs -f $MNT
218	umount $MNT
219	rmdir $MNT
220	lofiadm -d $MR
221
222	cd $BASE
223
224	rm -f ${MR}.gz
225	gzip -f $MR
226	mv ${MR}.gz $MR
227	chmod a+r $MR
228}
229
230# main
231#
232
233if [ $# != 3 ] ; then
234	usage
235	exit 1
236fi
237
238UNPACKED_ROOT=$3
239BASE=`pwd`
240MNT=/tmp/mnt$$
241MR=$2
242
243if [ "`dirname $MR`" = . ] ; then
244	MR=${BASE}/${MR}
245fi
246if [ "`dirname $UNPACKED_ROOT`" = . ] ; then
247	UNPACKED_ROOT=${BASE}/${UNPACKED_ROOT}
248fi
249
250case $1 in
251	packmedia)
252		MEDIA=$MR
253		MR=${MR}/boot/x86.miniroot
254		packmedia $MEDIA $UNPACKED_ROOT
255		pack ;;
256	unpackmedia)
257		MEDIA=$MR
258		MR=${MR}/boot/x86.miniroot
259		unpack
260		unpackmedia $MEDIA $UNPACKED_ROOT ;;
261	pack)	pack ;;
262	unpack)	unpack ;;
263	*)	usage ;;
264esac
265