xref: /titanic_44/usr/src/cmd/stmsboot/stmsboot.sh (revision 064ed339d53d38ca3b43105dc6fc88512efed351)
1#!/sbin/sh
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 2006 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26#ident	"%Z%%M%	%I%	%E% SMI"
27
28PATH=/usr/bin:/usr/sbin:$PATH; export PATH
29STMSBOOTUTIL=/lib/mpxio/stmsboot_util
30STMSMETHODSCRIPT=/lib/svc/method/mpxio-upgrade
31STMSINSTANCE=platform/sun4u/mpxio-upgrade:default
32FPCONF=/kernel/drv/fp.conf
33TMPFPCONF=/var/run/tmp.fp.conf.$$
34VFSTAB=/etc/vfstab
35SAVEDIR=/etc/mpxio
36RECOVERFILE=$SAVEDIR/recover_instructions
37SVCCFG_RECOVERY=$SAVEDIR/svccfg_recover
38USAGE=`gettext "Usage: stmsboot -e | -d | -u | -L | -l controller_number"`
39
40#
41# Copy all entries (including comments) from source driver.conf to destination
42# driver.conf except those entries which contain mpxio-disable property.
43# Take into consideration entries that spawn more than one line.
44#
45# $1	source driver.conf file
46# $2	destination driver.conf file
47#
48# Returns 0 on success, non zero on failure.
49#
50delete_mpxio_disable_entries()
51{
52	sed '
53		/^[ 	]*#/{ p
54			      d
55			    }
56		s/[ 	]*$//
57		/^$/{ p
58		      d
59		    }
60		/mpxio-disable[ 	]*=.*;$/d
61		/;$/{ p
62		      d
63		    }
64		:rdnext
65		N
66		s/[ 	]*$//
67		/[^;]$/b rdnext
68		/mpxio-disable[ 	]*=/d' $1 > $2
69
70	return $?
71}
72
73#
74# backup the last saved copy of the specified files.
75# $*	files to backup
76#
77backup_lastsaved()
78{
79	for file in $*
80	do
81		file=`basename $file`
82		if [ -f $SAVEDIR/$file ]; then
83			mv $SAVEDIR/$file $SAVEDIR/${file}.old
84		fi
85	done
86}
87
88#
89# build recover instructions
90#
91# $1	1 to include boot script in the instructions
92#	0 otherwise
93#
94build_recover()
95{
96	gettext "Instructions to recover your previous STMS configuration (if in case the system does not boot):\n\n" > $RECOVERFILE
97	echo "\tboot net \c"  >> $RECOVERFILE
98	gettext "(or from a cd/dvd/another disk)\n" >> $RECOVERFILE
99	echo "\tfsck <your-root-device>" >> $RECOVERFILE
100	echo "\tmount <your-root-device> /mnt" >> $RECOVERFILE
101
102	if [ "x$cmd" = xupdate ]; then
103		gettext "\tUndo the modifications you made to STMS configuration.\n\tFor example undo any changes you made to " >> $RECOVERFILE
104		echo "/mnt$FPCONF." >> $RECOVERFILE
105	else
106		echo "\tcp /mnt${SAVEDIR}/fp.conf /mnt$FPCONF" >> $RECOVERFILE
107	fi
108
109	if [ $1 -eq 1 ]; then
110		echo "\tcp /mnt${SAVEDIR}/vfstab /mnt$VFSTAB" >> $RECOVERFILE
111
112		echo "repository /mnt/etc/svc/repository.db" > $SVCCFG_RECOVERY
113		echo "select $STMSINSTANCE" >> $SVCCFG_RECOVERY
114		echo "setprop general/enabled=false" >> $SVCCFG_RECOVERY
115		echo "exit" >> $SVCCFG_RECOVERY
116
117		echo "\t/usr/sbin/svccfg -f /mnt$SVCCFG_RECOVERY" >> $RECOVERFILE
118	fi
119
120	rootdisk=`mount | grep "/ on " | cut -f 3 -d " "`
121	echo "\tumount /mnt\n\treboot\n\n${rootdisk} \c" >> $RECOVERFILE
122	gettext "was your root device,\nbut it could be named differently after you boot net.\n" >> $RECOVERFILE
123}
124
125#
126# Arrange for /etc/vfstab and dump configuration to be updated
127# during the next reboot. If the cmd is "enable" or "disable", copy
128# $TMPFPCONF to $FPCONF.
129#
130# Returns 0 on success, 1 on failure.
131#
132update_sysfiles()
133{
134	gettext "WARNING: This operation will require a reboot.\nDo you want to continue ? [y/n] (default: y) "
135	read response
136
137	if [ "x$response" != x -a "x$response" != xy -a \
138	    "x$response" != xY ]; then
139		rm -f $TMPFPCONF
140		return 0
141	fi
142
143	need_bootscript=1
144	if [ "x$cmd" = xenable -o "x$cmd" = xdisable ]; then
145		cp $FPCONF $SAVEDIR
146		cp $TMPFPCONF $FPCONF
147		rm -f $TMPFPCONF
148
149		#
150		# there is no need to update the system files in the following
151		# cases:
152		# - we are enabling mpxio and the system has no configured
153		#   disks accessible by phci paths.
154		# - we are disabling mpxio and the system has no configured
155		#   disks accessible by vhci paths.
156		#
157		if [ "x$cmd" = xenable ]; then
158			ls -l /dev/dsk/*s2 2> /dev/null | \
159			    egrep -s "/fp@.*/ssd@.*"
160		else
161			ls -l /dev/dsk/*s2 2> /dev/null | \
162			    egrep -s "/scsi_vhci.*/ssd@.*"
163		fi
164
165		if [ $? -ne 0 ]; then
166			need_bootscript=0
167		fi
168	fi
169
170	if [ $need_bootscript -eq 1 ]; then
171		#
172		# Enable the mpxio-upgrade service, but don't run it now.
173		# The service will run during the next reboot and will do
174		# the actual job of modifying the system files.
175		#
176		svcadm disable -t $STMSINSTANCE
177		svccfg -f - << EOF
178select $STMSINSTANCE
179setprop general/enabled = true
180EOF
181	fi
182
183	build_recover $need_bootscript
184
185	gettext "The changes will come into effect after rebooting the system.\nReboot the system now ? [y/n] (default: y) "
186	read response
187
188	if [ "x$response" = x -o "x$response" = xy -o \
189	    "x$response" = xY ]; then
190		reboot
191	fi
192
193	return 0
194}
195
196#
197# Enable or disable mpxio as specified by the cmd.
198# Returns 0 on success, 1 on failure.
199#
200configure_mpxio()
201{
202	if [ "x$cmd" = xenable ]; then
203		propval=no
204		msg=`gettext "STMS already enabled."`
205	else
206		propval=yes
207		msg=`gettext "STMS already disabled."`
208	fi
209
210	if delete_mpxio_disable_entries $FPCONF $TMPFPCONF; then
211		echo "mpxio-disable=\"${propval}\";" >> $TMPFPCONF
212		if diff -b $FPCONF $TMPFPCONF > /dev/null; then
213			rm -f $TMPFPCONF
214			echo "$msg"
215			return 0
216		fi
217		update_sysfiles
218		return $?
219	else
220		rm -f $TMPFPCONF
221		gettext "failed to update " 1>&2
222		echo "$FPCONF." 1>&2
223		gettext "No changes were made to your STMS configuration.\n" 1>&2
224		return 1
225	fi
226}
227
228setcmd()
229{
230	if [ "x$cmd" = xnone ]; then
231		cmd=$1
232	else
233		echo "$USAGE" 1>&2
234		exit 2
235	fi
236}
237
238cmd=none
239
240# process options
241while getopts eduLl: c
242do
243	case $c in
244	e)	setcmd enable;;
245	d)	setcmd disable;;
246	u)	setcmd update;;
247	L)	setcmd listall;;
248	l)	setcmd list
249		controller=$OPTARG;;
250	\?)	echo "$USAGE" 1>&2
251		exit 2;;
252	esac
253done
254
255if [ "x$cmd" = xnone ]; then
256	echo "$USAGE" 1>&2
257	exit 2
258fi
259
260set `id`
261if [ "$1" != "uid=0(root)" ]; then
262	gettext "You must be super-user to run this script.\n" 1>&2
263	exit 1
264fi
265
266# just a sanity check
267if [ ! -f $STMSBOOTUTIL -o ! -f $STMSMETHODSCRIPT ]; then
268	fmt=`gettext "Can't find %s and/or %s"`
269	printf "$fmt\n" "$STMSBOOTUTIL" "$STMSMETHODSCRIPT" 1>&2
270	exit 1
271fi
272
273svcprop -q $STMSINSTANCE
274if [ $? -ne 0 ]; then
275	fmt=`gettext "Can't find %s service"`
276	printf "$fmt\n" "$STMSINSTANCE" 1>&2
277	exit 1
278fi
279
280if [ "x$cmd" = xenable -o "x$cmd" = xdisable -o "x$cmd" = xupdate ]; then
281	#
282	# The bootup script doesn't work on cache-only-clients as the script
283	# is executed before the plumbing for cachefs mounting of root is done.
284	#
285	if mount -v | egrep -s " on / type (nfs|cachefs) "; then
286		gettext "This command option is not supported on systems with nfs or cachefs mounted root filesystem.\n" 1>&2
287		exit 1
288	fi
289
290	if [ -d $SAVEDIR ]; then
291		#
292		# keep a copy of the last saved files, useful for manual
293		# recovery in case of a problem.
294		#
295		backup_lastsaved $FPCONF $VFSTAB
296	else
297		mkdir $SAVEDIR
298	fi
299
300fi
301
302if [ "x$cmd" = xenable -o "x$cmd" = xdisable ]; then
303	configure_mpxio $cmd
304elif [ "x$cmd" = xupdate ]; then
305	update_sysfiles
306elif [ "x$cmd" = xlist ]; then
307	$STMSBOOTUTIL -l $controller
308else
309	$STMSBOOTUTIL -L
310fi
311
312exit $?
313