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