xref: /titanic_51/usr/src/tools/scripts/Install.sh (revision b9e93c10c0a2a4bb069d38bb311021a9478c4711)
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 (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# Author:  Jeff Bonwick
27#
28#	Please report any bugs to bonwick@eng.
29#
30# How Install works:
31#
32#	Install performs the following steps:
33#
34#	1. Get the list of modules, configuration files, and links
35#	   that are desired.
36#
37#	2. Create the requested subset of /kernel in Install's temp space
38#	   (/tmp/Install.username by default.)
39#
40#	3. Create a tar file (/tmp/Install.username/Install.tar) based on (3).
41#
42#	4. If -n was specified, exit.  If a target was specified using -T,
43#	   rcp the tarfile to the target and exit.  If a target was specified
44#	   using -t, rsh to the target machine and untar the tarfile in the
45#	   target directory.
46#
47# If any of these steps fail, Install will give you an error message and,
48# in most cases, suggest corrective measures.  Then, you can recover the
49# install with "Install -R". (This is not required; it's just faster than
50# starting from scratch.)
51#
52# One final comment:  Unfortunately, tar and I disagree on what
53# constitutes a fatal error.  (tar -x will exit 0 even if it can't write
54# anything in the current directory.)  Thus, I am reduced to grepping stderr
55# for (what I consider) fatal and nonfatal error messages.  If you run into
56# a situation where this doesn't behave the way you think it should (either
57# an "Install failed" message after a successful install, or an "Install
58# complete" message after it bombs), please let me know.
59
60#
61# The CDPATH variable causes ksh's `cd' builtin to emit messages to stdout
62# under certain circumstances, which can really screw things up; unset it.
63#
64unset CDPATH
65
66INSTALL=`basename $0`
67DOT=`pwd`
68
69TRAILER="Install.$LOGNAME"
70INSTALL_STATE=${INSTALL_STATE-$HOME/.Install.state}
71export INSTALL_STATE
72INSTALL_DIR=${INSTALL_DIR-/tmp/$TRAILER}
73if [ "`basename $INSTALL_DIR`" != "$TRAILER" ]; then
74	INSTALL_DIR="$INSTALL_DIR/$TRAILER"
75fi
76export INSTALL_DIR
77INSTALL_LIB=${INSTALL_LIB-$HOME/LibInstall}
78export INSTALL_LIB
79INSTALL_RC=${INSTALL_RC-$HOME/.Installrc}
80export INSTALL_RC
81INSTALL_CP=${INSTALL_CP-"cp -p"}
82export INSTALL_CP
83INSTALL_RCP=${INSTALL_RCP-"rcp -p"}
84export INSTALL_RCP
85
86STATE=0
87
88DEFAULT_OPTIONS="-naq"
89GLOM=no
90GLOMNAME=kernel
91IMPL="default"
92WANT32="yes"
93WANT64="yes"
94
95modlist=/tmp/modlist$$
96# dummy directory for make state files.
97modstatedir=/tmp/modstate$$
98
99trap 'fail "User Interrupt" "You can resume by typing \"$INSTALL -R\""' 1 2 3 15
100
101usage() {
102	echo ""
103	echo $1
104	echo '
105Usage: Install	[ -w workspace ]
106		[ -s srcdir (default: usr/src/uts) ]
107		[ -k karch (e.g. sun4u; required if not deducible from pwd) ]
108		[ -t target (extract tar file on target, e.g. user@machine:/) ]
109		[ -T target (copy tar file to target, e.g. user@machine:/tmp) ]
110		[ -n (no target, just create tar file in /tmp (default)) ]
111		[ -u (install unix only) ]
112		[ -m (install modules only) ]
113		[ -a (install everything, i.e. unix + modules (default)) ]
114		[ -v (verbose output) ]
115		[ -V (REALLY verbose output) ]
116		[ -q (quiet (default)) ]
117		[ -c (clean up (remove temp files) when done (default) ]
118		[ -p (preserve temp files -- useful for debugging) ]
119		[ -L (library create: put tarfile in $INSTALL_LIB/env.karch) ]
120		[ -l lib (library extract: use $INSTALL_LIB/lib as source) ]
121		[ -D libdir (default: $HOME/LibInstall) ]
122		[ -d tempdir (Install work area (default: /tmp)) ]
123		[ -G glomname (put all files under platform/karch/glomname) ]
124		[ -i impl (e.g. sunfire; recommended with -G) ]
125		[ -x (update /etc/name_to_major et al) ]
126		[ -X (do not update /etc/name_to_major et al (default)) ]
127		[ -P (update /etc/path_to_inst -- generally not advisable) ]
128		[ -h (help -- prints this message) ]
129		[ -R (recover a previous Install) ]
130		[ -o objdir (object directory - either obj or debug (the default)) ]
131		[ -K (do not copy kmdb) ]
132		[ -3 32-bit modules only ]
133		[ -6 64-bit modules only ]
134		[ list of modules to install ]
135
136For full details:
137
138	man -M /ws/on297-gate/public/docs Install
139'
140	exit 1
141}
142
143#
144# Save the current state of Install
145#
146
147save_state() {
148	rm -f $INSTALL_STATE
149	(echo "# State of previous Install
150TARGET=$TARGET
151ENV_PATH=$ENV_PATH
152ENV_NAME=$ENV_NAME
153KARCH=$KARCH
154UTS=$UTS
155INSTALL_DIR=$INSTALL_DIR
156INSTALL_LIB=$INSTALL_LIB
157IMODE=$IMODE
158LIBCREATE=$LIBCREATE
159LIBSRC=$LIBSRC
160VERBOSE=$VERBOSE
161CLEANUP=$CLEANUP
162GLOM=$GLOM
163GLOMNAME=$GLOMNAME
164KMDB=$KMDB
165files='$files'
166STATE=$STATE" >$INSTALL_STATE) || verbose "Warning: cannot save state"
167}
168
169#
170# Restore the previous state of Install
171#
172
173restore_state() {
174	test -s $INSTALL_STATE || fail "Can't find $INSTALL_STATE"
175	eval "`cat $INSTALL_STATE`"
176}
177
178#
179# Install failed -- print error messages and exit 2
180#
181
182fail() {
183	save_state
184	#
185	# We might have gotten here via a trap.  So wait for any
186	# children (especially "make modlist") to exit before giving
187	# the error message or cleaning up.
188	#
189	wait
190	while [ $# -gt 0 ]
191	do
192		echo $1
193		shift
194	done
195	rm -rf $modstatedir
196	rm -f $modlist
197	echo "Install failed"
198	exit 2
199}
200
201#
202# Echo a string in verbose mode only
203#
204
205verbose() {
206	test "$VERBOSE" != "q" && echo $1
207}
208
209#
210# hack for tmpfs bug -- remove files gradually
211#
212
213remove_dir() {
214	test -d $1 || return
215	local_dot=`pwd`
216	cd $1
217	touch foo
218	rm -f `find . -type f -print`
219	cd $local_dot
220	rm -rf $1
221}
222
223#
224# Create a directory if it doesn't already exist.
225# mkdir will provide an error message, so don't provide an additional
226# message.
227#
228
229tstmkdir() {
230	[ -d $1 ] || mkdir -p $1 || fail
231}
232
233#
234# Patch up target directories for glommed kernel.
235# usage: fixglom listfile glomname
236#
237
238fixglom() {
239	nawk \
240	    -v glomname=$2 \
241	    -v karch=$KARCH '
242	$1 == "MOD" || $1 == "SYMLINK" {
243		sub(/^platform.*kernel/, "platform/" karch "/" glomname, $3)
244		sub(/^kernel/, "platform/" karch "/" glomname, $3)
245		sub(/^usr.kernel/, "platform/" karch "/" glomname, $3)
246		print
247	}
248	$1 == "LINK" {
249		sub(/^platform.*kernel/, "platform/" karch "/" glomname, $2)
250		sub(/^kernel/, "platform/" karch "/" glomname, $2)
251		sub(/^usr.kernel/, "platform/" karch "/" glomname, $2)
252		sub(/^platform.*kernel/, "platform/" karch "/" glomname, $4)
253		sub(/^kernel/, "platform/" karch "/" glomname, $4)
254		sub(/^usr.kernel/, "platform/" karch "/" glomname, $4)
255		print
256	}
257	$1 == "CONF" {
258		sub(/^platform.*kernel/, "platform/" karch "/" glomname, $2)
259		sub(/^kernel/, "platform/" karch "/" glomname, $2)
260		sub(/^usr.kernel/, "platform/" karch "/" glomname, $2)
261		print
262	}
263	' $1 > $1.new
264	mv $1.new $1
265}
266
267#
268# Remove entries from
269# usage: filtimpl listfile implname
270#
271
272filtimpl() {
273	nawk \
274	    -v impl=$2 '
275	$1 == "MOD" || $1 == "SYMLINK" {
276		if ($5 == "all" || $5 == impl)
277			print
278	}
279	$1 == "CONF" {
280		if ($4 == "all" || $4 == impl)
281			print
282	}
283	$1 == "LINK" {
284		if ($6 == "all" || $6 == impl)
285			print
286	}
287	' $1 > $1.new
288	mv $1.new $1
289}
290
291#
292# Filter the module list to match the user's request.
293# Usage: filtmod listfile modules
294#
295filtmod() {
296	nawk -v reqstring="$2" '
297	function modmatch(modname) {
298		if (reqstring == "All") {
299			return (1)
300		} else if (reqstring == "Modules") {
301			if (modname != "unix" && modname != "genunix")
302				return (1)
303		} else {
304			if (modname in reqmods)
305				return (1)
306		}
307		return (0)
308	}
309	BEGIN {
310		#
311		# The split call creates indexes 1, 2, 3, ...  We want
312		# the module names as indexes.
313		#
314		split(reqstring, tmpmods)
315		for (i in tmpmods)
316			reqmods[tmpmods[i]] = 1
317	}
318	$1 == "MOD" {
319		if (modmatch($2))
320			print
321	}
322	$1 == "CONF" {
323		if (modmatch($5))
324			print
325	}
326	$1 == "SYMLINK" {
327		if (modmatch($6))
328			print
329	}
330	$1 == "LINK" {
331		if (modmatch($3))
332			print
333	}
334	' $1 > $1.new
335	mv $1.new $1
336}
337
338#
339# Copy a module, or create a link, as needed.
340# See $SRC/uts/Makefile.targ ($(MODLIST_DEPS) target) for the format
341# of the different input lines.
342#
343
344copymod() {
345	case $1 in
346	MOD)
347		targdir=$INSTALL_FILES/$3
348		tstmkdir $targdir
349		target=$targdir/$2
350		verbose "$INSTALL_CP $6/${OBJD}$4/$2 $target"
351		$INSTALL_CP $6/${OBJD}$4/$2 $target || \
352		    fail "can't create $target"
353		;;
354	SYMLINK)
355		targdir=$INSTALL_FILES/$3
356		tstmkdir $targdir
357		target=$targdir/$4
358		rm -f $target
359		verbose "ln -s $2 $target"
360		ln -s $2 $target || fail "can't create $target"
361		;;
362	LINK)
363		targdir=$INSTALL_FILES/$4
364		tstmkdir $targdir
365		target=$targdir/$5
366		rm -f $target
367		verbose "ln $INSTALL_FILES/$2/$3 $target"
368		ln $INSTALL_FILES/$2/$3 $target || fail "can't create $target"
369		;;
370	CONF)
371		target=$INSTALL_FILES/$2
372		tstmkdir `dirname $target`
373		conffile=`basename $2`
374		verbose "$INSTALL_CP $3/$conffile $target"
375		$INSTALL_CP $3/$conffile $target
376		;;
377	*)
378		fail "unrecognized modlist entry: $*"
379		;;
380	esac
381}
382
383#
384# Copy kernel modules to $INSTALL_DIR
385#
386
387copy_kernel() {
388
389	case $KARCH in
390		sun4*)		ISA=sparc;	MACH=sparc	;;
391		i86*)		ISA=intel;	MACH=i386	;;
392		*)		fail "${KARCH}: invalid kernel architecture";;
393	esac
394	export MACH
395
396	if [ "$GLOM" = "no" ]; then
397		verbose "Source = $UTS, ISA = $ISA, kernel = $KARCH"
398	else
399		verbose "Source = $UTS, ISA = $ISA, kernel = $KARCH, impl = $IMPL"
400	fi
401
402	test -d $KARCH || fail "${KARCH}: invalid kernel architecture"
403	test -d $ISA || fail "${ISA}: invalid instruction set architecture"
404
405	tstmkdir $INSTALL_FILES
406	rm -rf $modstatedir
407	tstmkdir $modstatedir
408	export MODSTATE=$modstatedir/state
409
410	#
411	# Figure out which "make" to use.  dmake is faster than serial
412	# make, but dmake 7.3 has a bug that causes it to lose log
413	# output, which means the modlist might be incomplete.
414	#
415	make=dmake
416	dmvers=`$make -version`
417	if [ $? -ne 0 ]; then
418		make=/usr/ccs/bin/make
419	elif [[ $dmvers = *Distributed?Make?7.3* ]]; then
420		unset make
421		searchpath="/ws/onnv-tools/SUNWspro/SOS10/bin
422			/opt/SUNWspro/SOS10/bin
423			/opt/SUNWspro/bin"
424		for dmpath in $searchpath; do
425			verbose "Trying $dmpath/dmake"
426			if [ -x $dmpath/dmake ]; then
427				dmvers=`$dmpath/dmake -version`
428				if [[ $dmvers != *Distributed?Make?7.3* ]]; then
429					make="$dmpath/dmake"
430					break;
431				fi
432			fi
433		done
434		if [ -z $make ]; then
435			make=/usr/ccs/bin/make
436			echo "Warning: dmake 7.3 doesn't work with Install;" \
437				"using $make"
438		fi
439	fi
440
441	#
442	# Get a list of all modules, configuration files, and links
443	# that we might want to install.
444	#
445	verbose "Building module list..."
446	(cd $KARCH; MAKEFLAGS=e $make -K $MODSTATE modlist.karch) | \
447	    egrep "^MOD|^CONF|^LINK|^SYMLINK" > $modlist
448	[ $VERBOSE = "V" ] && cat $modlist
449	if [ "$GLOM" = "yes" ]; then
450		fixglom $modlist $GLOMNAME
451		filtimpl $modlist $IMPL
452	fi
453	if [[ -n "$files" && "$files" != All ]]; then
454		filtmod $modlist "$files"
455	fi
456
457	#
458	# Copy modules and create links.  For architectures with both
459	# 32- and 64-bit modules, we'll likely have duplicate
460	# configuration files, so do those after filtering out the
461	# duplicates.
462	#
463	verbose "Copying files to ${INSTALL_FILES}..."
464
465	#
466	# The IFS is reset to the newline character so we can buffer the
467	# output of grep without piping it directly to copymod, otherwise
468	# if fail() is called, then it will deadlock in fail()'s wait call
469	#
470	OIFS="$IFS"
471	IFS="
472	"
473	set -- `grep -v "^CONF" $modlist`;
474	IFS="$OIFS"
475	for onemod in "$@"; do
476		copymod $onemod
477	done
478
479	OIFS="$IFS"
480	IFS="
481	"
482	set -- `grep "^CONF" $modlist | sort | uniq`;
483	IFS="$OIFS"
484	for onemod in "$@"; do
485		copymod $onemod
486	done
487
488	#
489	# Add the glommed kernel name to the root archive
490	#
491	if [[ $GLOM == "yes" ]];
492	then
493		filelist="$INSTALL_FILES/etc/boot/solaris/filelist.ramdisk"
494		mkdir -p `dirname $filelist`
495		echo "platform/$KARCH/$GLOMNAME" >$filelist
496	fi
497
498	STATE=1 # all kernel modules copied correctly
499	save_state
500}
501
502kmdb_copy() {
503	typeset src="$1"
504	typeset destdir="$2"
505
506	if [[ ! -d $dest ]] ; then
507		[[ $VERBOSE != "q" ]] && echo "mkdir -p $destdir"
508
509		mkdir -p $destdir || fail "failed to create $destdir"
510	fi
511
512	[[ $VERBOSE != "q" ]] && echo "cp $src $destdir"
513
514	cp $src $destdir || fail "failed to copy $src to $destdir"
515}
516
517kmdb_copy_machkmods() {
518	typeset modbase="$1"
519	typeset destdir="$2"
520	typeset dir=
521	typeset kmod=
522
523	[[ ! -d $modbase ]] && return
524
525	for dir in $(find $modbase -name kmod) ; do
526		set -- $(echo $dir |tr '/' ' ')
527
528		[[ $# -lt 2 ]] && fail "invalid mach kmod dir $dir"
529
530		shift $(($# - 2))
531		kmod=$1
532
533		[[ ! -f $dir/$kmod ]] && continue
534
535		kmdb_copy $dir/$kmod $destdir
536	done
537}
538
539kmdb_copy_karchkmods() {
540	typeset modbase="$1"
541	typeset destdir="$2"
542	typeset bitdir="$3"
543	typeset dir=
544	typeset kmod=
545	typeset karch=
546
547	[[ ! -d $modbase ]] && return
548
549	for dir in $(find $modbase -name kmod) ; do
550		set -- $(echo $dir | tr '/' ' ')
551
552		[[ $# -lt 3 ]] && fail "invalid karch kmod dir $dir"
553
554		shift $(($# - 3))
555		kmod=$1
556		bdir=$2
557
558		[[ $bdir != $bitdir ]] && continue
559		[[ ! -f $dir/$1 ]] && continue
560
561		kmdb_copy $dir/$kmod $destdir
562	done
563}
564
565kmdb_copy_kmdbmod() {
566	typeset kmdbpath="$1"
567	typeset destdir="$2"
568
569	[[ ! -f $kmdbpath ]] && return 1
570
571	kmdb_copy $kmdbpath $destdir
572
573	return 0
574}
575
576copy_kmdb() {
577	typeset kmdbtgtdir=$INSTALL_FILES/platform/$KARCH/$GLOMNAME/misc
578	typeset bitdirs=
579	typeset isadir=
580	typeset b64srcdir=
581	typeset b64tgtdir=
582	typeset b32srcdir=
583	typeset b32tgtdir=
584	typeset machdir=
585	typeset platdir=
586
587	if [[ $KMDB = "no" || ! -d $SRC/cmd/mdb ]] ; then
588		# The kmdb copy was suppressed or the workspace doesn't contain
589		# the mdb subtree.  Either way, there's nothing to do.
590		STATE=2
591		save_state
592		return
593	fi
594
595	if [[ $(mach) = "i386" ]] ; then
596		isadir="intel"
597		b64srcdir="amd64"
598		b64tgtdir="amd64"
599		b32srcdir="ia32"
600		b32tgtdir="."
601	else
602		isadir="sparc"
603		b64srcdir="v9"
604		b64tgtdir="sparcv9"
605		b32srcdir="v7"
606		b32tgtdir="."
607	fi
608
609	typeset foundkmdb=no
610	typeset kmdbpath=
611	typeset destdir=
612
613	platdir=$INSTALL_FILES/platform/$KARCH/$GLOMNAME
614	if [[ $GLOM = "yes" ]] ; then
615		machdir=$platdir
616	else
617		machdir=$INSTALL_FILES/kernel
618	fi
619
620	srctrees=$SRC
621	if [[ -d $SRC/../closed && "$CLOSED_IS_PRESENT" != no ]]; then
622		srctrees="$srctrees $SRC/../closed"
623	fi
624	if [[ $WANT64 = "yes" ]] ; then
625		# kmdbmod for sparc and x86 are built and installed
626		# in different places
627		if [[ $(mach) = "i386" ]] ; then
628			kmdbpath=$SRC/cmd/mdb/$isadir/$b64srcdir/kmdb/kmdbmod
629			destdir=$machdir/misc/$b64tgtdir
630		else
631			kmdbpath=$SRC/cmd/mdb/$KARCH/$b64srcdir/kmdb/kmdbmod
632			destdir=$platdir/misc/$b64tgtdir
633		fi
634
635		if kmdb_copy_kmdbmod $kmdbpath $destdir ; then
636			foundkmdb="yes"
637
638			for tree in $srctrees; do
639				kmdb_copy_machkmods \
640				    $tree/cmd/mdb/$isadir/$b64srcdir \
641				    $machdir/kmdb/$b64tgtdir
642				kmdb_copy_karchkmods $tree/cmd/mdb/$KARCH \
643				    $platdir/kmdb/$b64tgtdir $b64srcdir
644			done
645		fi
646	fi
647
648	if [[ $WANT32 = "yes" ]] ; then
649		kmdbpath=$SRC/cmd/mdb/$isadir/$b32srcdir/kmdb/kmdbmod
650		destdir=$machdir/misc/$b32tgtdir
651
652		if kmdb_copy_kmdbmod $kmdbpath $destdir ; then
653			foundkmdb="yes"
654
655			for tree in $srctrees; do
656				kmdb_copy_machkmods \
657				    $tree/cmd/mdb/$isadir/$b32srcdir \
658				    $machdir/kmdb/$b32tgtdir
659				kmdb_copy_karchkmods $tree/cmd/mdb/$KARCH \
660				    $platdir/kmdb/$b32tgtdir $b32srcdir
661			done
662		fi
663	fi
664
665	# A kmdb-less workspace isn't fatal, but it is potentially problematic,
666	# as the changes made to uts may have altered something upon which kmdb
667	# depends.  We will therefore remind the user that they haven't built it
668	# yet.
669	if [[ $foundkmdb != "yes" ]] ; then
670		echo "WARNING: kmdb isn't built, and won't be included"
671	fi
672
673	STATE=2
674	save_state
675	return
676}
677
678#
679# Make tarfile
680#
681
682make_tarfile() {
683	echo "Creating tarfile $TARFILE"
684	test -d $INSTALL_FILES || fail "Can't find $INSTALL_FILES"
685	cd $INSTALL_FILES
686	rm -f $TARFILE files
687
688	# We don't want to change the permissions or ownership of pre-existing
689	# directories on the target machine, so we're going to take care to
690	# avoid including directories in the tarfile.  On extraction, tar won't
691	# modify pre-existing directories, and will create non-existent ones as
692	# the user doing the extraction.
693	find . ! -type d -print |fgrep -vx './files' >files
694	tar cf $TARFILE -I files || fail "Couldn't create tarfile $TARFILE"
695	STATE=3
696}
697
698#
699# Routines to copy files to the target machine
700#
701
702remote_fail() {
703	fail "" "$1" "" \
704		"Make sure that $TARGET_MACHINE is up." \
705"Check .rhosts in the home directory of user $TARGET_USER on $TARGET_MACHINE." \
706		"Check /etc/hosts.equiv, /etc/passwd, and /etc/shadow." \
707		"Change permissions on $TARGET_MACHINE as necessary." \
708		"Then, use \"$INSTALL -R\" to resume the install." ""
709}
710
711remote_install() {
712	if [ "$IMODE" = "n" ]; then
713		STATE=4
714		return 0
715	fi
716	test -s $TARFILE || fail "$TARFILE missing or empty"
717	verbose "Installing system on $TARGET"
718	test -d $INSTALL_DIR || fail "Can't find $INSTALL_DIR"
719	cd $INSTALL_DIR
720	rm -f errors fatal nonfatal
721	if [ "$IMODE" = "T" ]; then
722		EMESG="Can't rcp to $TARGET"
723		touch errors
724		sh -e${SHV}c "$INSTALL_RCP $TARFILE $TARGET/Install.tar"
725	else
726		EMESG="Can't rsh to $TARGET_MACHINE"
727		rsh -l $TARGET_USER $TARGET_MACHINE \
728		    "(cd $TARGET_DIR; /usr/bin/tar x${V}f -)" \
729		    <$TARFILE 2>errors
730	fi
731	test $? -ne 0 && remote_fail "$EMESG"
732	cd $INSTALL_DIR
733	egrep "set time|warning|blocksize" errors >nonfatal
734	egrep -v "set time|warning|blocksize" errors >fatal
735	if [ -s fatal ]; then
736		echo "Fatal errors from rsh:"
737		cat fatal
738		remote_fail "Can't install on $TARGET_MACHINE"
739	fi
740	if [ -s nonfatal -a "$VERBOSE" != "q" ]; then
741		echo "Non-fatal errors from rsh:"
742		cat nonfatal
743	fi
744	rm -f fatal nonfatal errors
745	test "$IMODE" = "T" && echo "Files can be extracted on \
746$TARGET_MACHINE using 'tar xvf $TARGET_DIR/Install.tar'"
747	STATE=4
748}
749
750okexit() {
751	cd /tmp
752	test "$CLEANUP" = c && remove_dir $INSTALL_DIR
753	save_state
754	rm -rf $modstatedir
755	rm -f $modlist
756	verbose "Install complete"
757	exit 0
758}
759
760#
761# Process options
762#
763
764RCOPTS=""
765LIBCREATE="no"
766LIBSRC=""
767ENV_PATH=$CODEMGR_WS
768OBJD="debug"
769KMDB="yes"
770
771test -s $INSTALL_RC && RCOPTS=`cat $INSTALL_RC`
772set $INSTALL $DEFAULT_OPTIONS $RCOPTS $*
773shift
774
775while getopts acd:D:G:hi:k:Kl:Lmno:pPqRs:t:T:uvVw:xX36 opt
776do
777	case $opt in
778	    w)	ENV_PATH="$OPTARG"; SRC="$ENV_PATH/usr/src";;
779	    s)	UTS="$OPTARG";;
780	    k)	KARCH="$OPTARG";;
781	  t|T)	TARGET="$OPTARG"; IMODE=$opt; CLEANUP="c";;
782	    n)	TARGET=""; IMODE="n"; CLEANUP="p";;
783	    u)	files="unix genunix";;
784	    m)	files="Modules";;
785	    a)	files="All";;
786	v|V|q)	VERBOSE=$opt;;
787	  c|p)	CLEANUP=$opt;;
788	    L)	LIBCREATE="yes"; CLEANUP="c";;
789	    l)	LIBSRC="$OPTARG";;
790	    D)	INSTALL_LIB="$OPTARG";;
791	    d)	INSTALL_DIR="$OPTARG/$TRAILER";;
792	    G)	GLOM=yes; GLOMNAME="$OPTARG";;
793	P|X|x)	echo "-$opt is obsolete; ignored";;
794	    h)	usage "${INSTALL}: installs unix and modules";;
795	    R)	x=$OPTIND; restore_state; OPTIND=$x;;
796	    i)	IMPL="$OPTARG";;
797	    o)	OBJD="$OPTARG";;
798	    K)  KMDB="no";;
799	    3)  WANT64="no";;
800	    6)  WANT32="no";;
801	   \?)	usage "Illegal option";;
802	esac
803done
804shift `expr $OPTIND - 1`
805
806ENV_NAME=`basename $ENV_PATH`
807
808#
809# The rest of the command line is a list of individual files to copy.
810# If non-null, this list overrides the -uma options.
811#
812
813if [[ $# -gt 0 ]] ; then
814	files="$*"
815	KMDB="no"
816fi
817
818case $VERBOSE in
819	v)	V="v"; SHV="x";;
820	V)	V="v"; SHV="x"; set -x;;
821	q)	V=""; SHV="";;
822esac
823
824#
825# Create temp directory for Install's files
826#
827
828tstmkdir $INSTALL_DIR
829
830TARFILE=$INSTALL_DIR/Install.${KARCH}.tar
831INSTALL_FILES=$INSTALL_DIR/$KARCH
832
833#
834# Extract the target machine and target directory from a target of the
835# form [user@]machine:/dir .
836#
837
838if [ "$IMODE" != "n" ]; then
839	eval `echo $TARGET | nawk -F':' '{
840		if (NF != 2 || !length($1) || !length($2))
841			print "usage \"Invalid target\""
842		m = $1; d = $2
843		if ($1 ~ /@/) {
844		    k = split($1, f, "@");
845		    if (k != 2 || !length(f[1]) || !length (f[2]))
846			    print "usage \"Invalid target\""
847		    u = f[1]; m = f[2]
848		}
849		print "TARGET_USER=" u ";"
850		print "TARGET_MACHINE=" m ";"
851		print "TARGET_DIR=" d ";"
852	}'`
853	if [ -z "$TARGET_USER" ]; then
854		TARGET_USER=$LOGNAME
855	fi
856fi
857
858#
859# Allow the use of library source or target for the install
860#
861
862if [ -n "$LIBSRC" ]; then
863	LIBSRC="`basename $LIBSRC .tar`.tar"
864	TARFILE=$INSTALL_LIB/$LIBSRC
865	test -s $TARFILE || fail "Can't find tarfile $TARFILE"
866	verbose "Installing from library tarfile $TARFILE"
867	STATE=3
868elif [ "$LIBCREATE" = "yes" ]; then
869	tstmkdir $INSTALL_LIB
870	TARFILE="$INSTALL_LIB/${ENV_NAME}.${KARCH}.tar"
871fi
872
873#
874# The next few lines allow recovery and activation with -R,
875# and library installs with -l.
876#
877
878[[ $STATE -eq 1 ]] && copy_kmdb
879[[ $STATE -eq 2 ]] && make_tarfile
880[[ $STATE -eq 3 ]] && remote_install
881[[ $STATE -eq 4 ]] && okexit
882
883save_state
884
885cd $DOT
886DOTDOT=`cd ..; pwd`
887
888#
889# Try to be smart: if DOTDOT ends in uts, then infer UTS and KARCH from DOT
890# Otherwise, if SRC is set, infer UTS = $SRC/uts.
891#
892
893if [ "`basename $DOTDOT`" = "uts" ]; then
894	UTS=$DOTDOT
895	KARCH=`basename $DOT`
896	if [ ! -n "$SRC" ]; then
897		SRC=`dirname $DOTDOT`
898		verbose "Setting SRC to $SRC"
899	fi
900	export SRC
901fi
902
903if [ -z "$UTS" -a -n "$SRC" ]; then
904	UTS="${SRC}/uts"
905	test -n "$KARCH" || fail "no karch specified (e.g. -k sun4u)"
906fi
907
908if [ "$LIBCREATE" = "yes" ]; then
909	TARFILE=$INSTALL_LIB/${ENV_NAME}.${KARCH}.tar
910else
911	TARFILE=$INSTALL_DIR/Install.${KARCH}.tar
912fi
913INSTALL_FILES=$INSTALL_DIR/$KARCH
914save_state
915
916cd $DOT
917test -z "$UTS" && fail 'Cannot find kernel sources -- $SRC not set'
918test -d "$UTS" || fail "${UTS}: no such directory"
919
920#
921# Convert UTS into an absolute path.
922#
923
924cd $UTS
925UTS=`pwd`
926
927test "`basename $UTS`" = "uts" || \
928	verbose "Warning: source path $UTS doesn't end in 'uts'"
929
930remove_dir $INSTALL_DIR/$KARCH
931rm -f $TARFILE
932
933copy_kernel	# sets STATE=1 if successful
934copy_kmdb	# sets STATE=2 if successful
935make_tarfile	# sets STATE=3 if successful
936remote_install	# sets STATE=4 if successful
937
938okexit
939