xref: /titanic_44/usr/src/cmd/stmsboot/mpxio-upgrade (revision a60349c89adffc0902b2353230891d8e7f2b24d9)
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#
24# Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
25# Copyright 2016 Nexenta Systems, Inc.
26#
27
28. /lib/svc/share/fs_include.sh
29. /lib/svc/share/net_include.sh
30
31# Make sure that the essential libraries can be found.
32LD_LIBRARY_PATH=/lib; export LD_LIBRARY_PATH
33STMSBOOTUTIL=/lib/mpxio/stmsboot_util
34SAVEDIR=/etc/mpxio
35BOOTDEVICES=$SAVEDIR/boot-devices
36RECOVERFILE=$SAVEDIR/recover_instructions
37DEVFSADM=/usr/sbin/devfsadm
38DUMPADM=/usr/sbin/dumpadm
39ISROOTDEV=""
40ISROOTDEVPATH=""
41usrmounted=0
42UNAME=/usr/bin/uname
43ECHO=/usr/bin/echo
44CAT=/usr/bin/cat
45CP=/usr/bin/cp
46DF=/usr/bin/df
47LS=/usr/bin/ls
48RM=/usr/bin/rm
49EGREP=/usr/bin/egrep
50SED=/usr/bin/sed
51ZPOOL=/usr/sbin/zpool
52AWK=/usr/bin/awk
53MOUNT=/sbin/mount
54UMOUNT=/sbin/mount
55EEPROM=/usr/sbin/eeprom
56BOOTADM=/usr/sbin/bootadm
57SVCADM=/usr/sbin/svcadm
58REBOOT=/usr/sbin/reboot
59
60mpxio_error()
61{
62	cecho "\nERROR: stmsboot: $1"
63	#
64	# display recovery instructions - the first call logs to the service
65	# log and the second call displays on the console.
66	#
67	shcat $RECOVERFILE
68	shcat $RECOVERFILE >/dev/msglog 2>&1
69	cecho "These instructions were also logged to the file $RECOVERFILE\n"
70}
71
72#
73# root ("/") is already mounted read only by the kernel.
74# Remount the root read-write.
75#
76mpxio_mount_root()
77{
78	HASZFSROOT=`$DF -g / |grep zfs`
79	RVAL=""
80
81	# In single-user maintenance mode, we don't have a writable
82	# root partition, so we _cannot_ use devlinks. Therefore we
83	# have to do some dancing - first mount the physical path
84	# read-write, then re-run $STMSBOOTUTIL to get the real
85	# devlink mapping, and then re-mount the root slice. Of course,
86	# if we all used ZFS this wouldn't be such a pain!
87	exec < $vfstab; readvfstab /
88	# ZFS root environments should _not_ have an entry for /
89	# in their /etc/vfstab.
90	if [ -n "$special" ]; then
91		# sanity check for ZFSRoot _and_ / in /etc/vfstab
92		if [ -n "$HASZFSROOT" ]; then
93			# ERROR - this would cause a failure later
94			# so let root know about it now and provide
95			# a chance to handle it before filesystem/usr
96			cecho "stmsboot: System has ZFS Root *and* an entry for / in /etc/vfstab\nstmsboot: Please remove the / entry from /etc/vfstab and then run\n# svcadm clear mpxio-upgrade"
97			exit 1
98		fi
99		ISPHYS=`echo $special |$AWK '/^\/dev\/dsk/ {print}'`;
100		if [ -z "$ISPHYS" ]; then
101			# a metadevice, /dev/vx
102			new_special=$special
103			$MOUNT -o remount,rw $new_special / >/dev/msglog 2>&1
104		else
105			new_special=`$STMSBOOTUTIL -m $special`
106			if [ "$new_special" = "NOT_MAPPED" ]; then
107				# this is a bad state to be in, exit
108				cecho "Error: Your root device is not mapped."
109				exit 1
110			fi
111	       		checkopt "llock" $mntopts
112			mntopts='remount'
113			[ -n "$otherops" ] && mntopts="${mntopts},${otherops}"
114			RVAL=`$MOUNT -m -F $fstype -o $mntopts $new_special \
115				$mountp >/dev/msglog 2>&1`
116			# if we've got active-active paths to our rootvp and
117			# the first path returned by $STMSBOOTUTIL is not the
118			# same as the one we booted from, then we need some
119			# handwaving due to restrictions in the ufs module
120			# (see the remountfs() function in
121			# $SRC/uts/common/fs/ufs/ufs_vfsops.c)
122			if [ $? -eq 0 ]; then
123				# now re-run $STMSBOOTUTIL to get the real
124				# mapping for this device
125				new_special=`$STMSBOOTUTIL -m $special`
126				# mount root for real
127				$MOUNT -o remount,rw $new_special / \
128				    >/dev/msglog 2>&1
129			else
130				for device in `$CAT $BOOTDEVICES`; do
131					new_special="/devices${device}"
132					$MOUNT -m -F $fstype -o $mntopts \
133					    $new_special $mountp >/dev/msglog 2>&1
134					if [ $? -eq 0 ]; then
135						# success, break out
136						ISROOTDEVPATH=`$ECHO $device | \
137						$AWK -F":" '{print $1}'`
138					    break;
139					fi
140				done
141				if [ -n "$RVAL" ]; then
142					cecho "Error: Unable to remount your root device"
143					exit 1;
144				fi
145			fi
146		fi
147	else
148		if [ -z "$HASZFSROOT" ]; then
149			cecho "stmsboot: Error: your root slice is invalid"
150			exit 1
151		else
152			cecho "stmsboot: Root is on ZFS"
153		fi
154	fi
155}
156
157#
158# mount /usr read only
159#
160mpxio_mount_usr()
161{
162	exec < $vfstab; readvfstab "/usr"
163	ret_val=0
164	if [ -n "$mountp" ]; then
165		case "$special" in
166		/dev/vx/*)
167			new_special=$special
168			;;
169		*)
170			new_special=`$STMSBOOTUTIL -m $special`
171			;;
172		esac
173
174		if [ "$fstype" = "cachefs" ]; then
175			# Mount read-only without the cache.
176			case "$mntopts" in
177			*backfstype=nfs*)
178				cfsbacktype=nfs
179				;;
180			*backfstype=hsfs*)
181				cfsbacktype=hsfs
182				;;
183			*)
184				cecho 'stmsboot: invalid vfstab entry for /usr'
185				cfsbacktype=nfs
186				;;
187			esac
188			# see the comment below for /dev/null
189			$MOUNT -m -F $cfsbacktype -o ro $new_special $mountp \
190>/dev/null 2>&1
191			ret_val=$?
192		else
193			#
194			# Must use -o largefiles here to ensure the read-only
195			# mount does not fail as a result of having a large
196			# file present on /usr.
197			#
198			if [ "$mntopts" = "-" ]; then
199				mntopts='ro,largefiles'
200			else
201				checkopt largefiles $mntopts
202				if [ "$option" != "largefiles" ]; then
203					mntopts="largefiles,$mntopts"
204				fi
205
206				checkopt ro $mntopts
207				if [ "$option" != "ro" ]; then
208					mntopts="ro,$mntopts"
209				fi
210
211				# Requesting logging on a read-only mount
212				# causes errors to be displayed, so remove
213				# "logging" from the list of options.
214				checkopt logging $mntopts
215				if [ "$option" = "logging" ]; then
216					mntopts="$otherops"
217				fi
218			fi
219
220			# In case of a manual restart of the service, mount
221			# will emit messages if /usr is already mounted.
222			# So redirect the output to /dev/null.
223			$MOUNT -m -F $fstype -o $mntopts $new_special /usr \
224>/dev/null 2>&1
225			ret_val=$?
226		fi
227		if [ $ret_val -eq 0 ]; then
228			usrmounted=1
229		fi
230	fi
231
232	return $ret_val
233}
234
235# update system dump configuration
236update_dumpconf()
237{
238	# Disable device-in-use checking (done in libdiskmgt).
239	# Without disabling this check, the configuration of dump device
240	# would fail as the device-in-use code incorrectly concludes that
241	# the device is in use and hence prevents configuration of the dump
242	# device.
243	NOINUSE_CHECK=1
244	export NOINUSE_CHECK
245
246	DUMPISZFS=`$AWK -F"=" '/DUMPADM_DEVICE/ {print $2}' /etc/dumpadm.conf|$EGREP zvol`
247	if [ -z "$DUMPISZFS" ]; then
248		set -- `$DUMPADM -u 2>&1 | $EGREP 'cannot use /dev.* as dump device'`
249		if [ -n "$4" ]; then
250			newname=`$STMSBOOTUTIL -m $4`
251			if [ $? -eq 0 ]; then
252				if $DUMPADM -d $newname > /dev/msglog 2> /dev/console; then
253					cecho "stmsboot: dump configuration \
254					has been updated."
255				else
256					mpxio_error "failed to configure \
257					the dump device.\nold \
258					dump device name: $4"
259					return 1
260				fi
261			fi
262		fi
263	else
264		# make sure we can get to it, force zfs to load fully
265		$LS $DUMPISZFS >>/dev/null 2>&1
266		cecho "stmsboot: dump on ZFS, no dumpadm update required"
267	fi
268	return 0
269}
270
271# Update bootpath for x86 here when we are enabling mpxio on root
272update_bootpath()
273{
274	cur_bootpath=`$STMSBOOTUTIL -b`
275	if [ $? -ne 0 ]; then
276		cecho "stmsboot: ERROR! Unable to retrieve bootpath property\n"
277		exit 1
278	fi
279
280	# Since on x64 platforms the eeprom command doesn't update the
281	# kernel, the file /boot/solaris/bootenv.rc and the kernel's
282	# bootpath variable have a good chance of differing. We do some
283	# extra handwaving to get the correct bootpath variable setting.
284
285	ONDISKVER=`$AWK '/bootpath/ {print $3}' /boot/solaris/bootenv.rc|\
286		$SED -e"s,',,g"`
287	if [ "$ONDISKVER" != "$cur_bootpath" ]; then
288		cur_bootpath="$ONDISKVER"
289	fi
290
291	NEWBOOTPATH=""
292	for path in $cur_bootpath; do
293		mapped=`$STMSBOOTUTIL -p $path`
294		if [ "$mapped" != "NOT_MAPPED" ]; then
295			if [ "$mapped" != "$path" ]; then
296				NEWBOOTPATH=`echo "$path " | \
297				    $SED -e"s|$path|$mapped|"`" $NEWBOOTPATH"
298			else
299				NEWBOOTPATH="$NEWBOOTPATH $path"
300			fi
301		fi
302	done
303	# now strip off leading and trailing space chars
304	new_bootpath=`echo $NEWBOOTPATH`
305	$EEPROM bootpath="$new_bootpath"
306	cecho "stmsboot: bootpath has been updated"
307	cecho ""
308}
309
310# Now do the actual work
311mpxio_main()
312{
313	# NOTE: If the first attempt to run the service has failed due to an
314	# expected error, users should be able to manually rerun the service.
315	#
316	# First mount /usr read only. This must be done to run
317	# utilities such as fsck and devfsadm.
318	# In the case of a manual rerun of the service, mounting of /usr here
319	# fails if /usr already happens to be mounted. It is better that we
320	# do not mount /usr if already mounted, but there seems to be no
321	# apparent way to check whether /usr is mounted or not as we mount
322	# /usr without making an entry into /etc/mnttab. So instead of
323	# explicitly checking for mount failures, we just do a sanity check
324	# by looking for some file (in this case devfsadm) in /usr.
325	#
326	mpxio_mount_usr
327	if [ ! -s $DEVFSADM ]; then
328		mpxio_error "failed to mount the /usr filesystem."
329		return
330	fi
331
332	if mpxio_mount_root; then
333		# create /dev links
334		cecho "stmsboot: configuring devices"
335		$DEVFSADM
336
337		if [ -n "$ISROOTDEVPATH" ]; then
338			ISROOTDEV=`$STMSBOOTUTIL -o $ISROOTDEVPATH`
339		fi
340
341		# update /etc/vfstab to reflect device name changes
342		$STMSBOOTUTIL -u >/dev/msglog 2>&1
343		if [ $? -eq 0 ]; then
344			$CP /etc/vfstab /etc/vfstab.old
345			# handle active-active paths, where the probe order
346			# for the hba reports a different path to what the
347			# boot-device variable gives us
348			if [ -n "$ISROOTDEV" ]; then
349				ROOTDEVCHK=`grep $ISROOTDEV /etc/vfstab`
350				if [ $? -ne 0 ]; then
351					# we got a different path for root
352					exec < $SAVEDIR/vfstab.new; readvfstab /
353					FILEDEV=`$ECHO $special | \
354					    $SED -e"s,/dev/dsk/,," -e"s,s[0-9]*,,"`
355					$SED -e"s,$FILEDEV,$ISROOTDEV,g" < \
356					    $SAVEDIR/vfstab.new > /etc/vfstab
357				fi
358			else
359				$CP $SAVEDIR/vfstab.new /etc/vfstab
360			fi
361			$RM $SAVEDIR/vfstab.new
362			cecho ""
363			cecho "stmsboot: vfstab has been updated"
364
365			update_dumpconf
366
367			MACH=`$UNAME -p`
368			if [ "$MACH" = "i386" ]; then
369				# only update bootpath here for x86
370				update_bootpath
371			fi
372			cecho "stmsboot: now regenerating boot archive"
373			$BOOTADM update-archive
374		else
375			mpxio_error "failed to update /etc/vfstab."
376		fi
377
378		$SVCADM disable system/device/mpxio-upgrade
379
380		if [ $usrmounted -eq 1 ]; then
381			cecho "stmsboot: rebooting the system now."
382			$REBOOT
383		fi
384	else
385		mpxio_error "failed to mount the root filesystem."
386	fi
387}
388
389mpxio_main
390