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