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		#
175		# Must use -o largefiles here to ensure the read-only
176		# mount does not fail as a result of having a large
177		# file present on /usr.
178		#
179		if [ "$mntopts" = "-" ]; then
180			mntopts='ro,largefiles'
181		else
182			checkopt largefiles $mntopts
183			if [ "$option" != "largefiles" ]; then
184				mntopts="largefiles,$mntopts"
185			fi
186
187			checkopt ro $mntopts
188			if [ "$option" != "ro" ]; then
189				mntopts="ro,$mntopts"
190			fi
191
192			# Requesting logging on a read-only mount
193			# causes errors to be displayed, so remove
194			# "logging" from the list of options.
195			checkopt logging $mntopts
196			if [ "$option" = "logging" ]; then
197				mntopts="$otherops"
198			fi
199		fi
200
201		# In case of a manual restart of the service, mount
202		# will emit messages if /usr is already mounted.
203		# So redirect the output to /dev/null.
204		$MOUNT -m -F $fstype -o $mntopts $new_special /usr \
205>/dev/null 2>&1
206		ret_val=$?
207		if [ $ret_val -eq 0 ]; then
208			usrmounted=1
209		fi
210	fi
211
212	return $ret_val
213}
214
215# update system dump configuration
216update_dumpconf()
217{
218	# Disable device-in-use checking (done in libdiskmgt).
219	# Without disabling this check, the configuration of dump device
220	# would fail as the device-in-use code incorrectly concludes that
221	# the device is in use and hence prevents configuration of the dump
222	# device.
223	NOINUSE_CHECK=1
224	export NOINUSE_CHECK
225
226	DUMPISZFS=`$AWK -F"=" '/DUMPADM_DEVICE/ {print $2}' /etc/dumpadm.conf|$EGREP zvol`
227	if [ -z "$DUMPISZFS" ]; then
228		set -- `$DUMPADM -u 2>&1 | $EGREP 'cannot use /dev.* as dump device'`
229		if [ -n "$4" ]; then
230			newname=`$STMSBOOTUTIL -m $4`
231			if [ $? -eq 0 ]; then
232				if $DUMPADM -d $newname > /dev/msglog 2> /dev/console; then
233					cecho "stmsboot: dump configuration \
234					has been updated."
235				else
236					mpxio_error "failed to configure \
237					the dump device.\nold \
238					dump device name: $4"
239					return 1
240				fi
241			fi
242		fi
243	else
244		# make sure we can get to it, force zfs to load fully
245		$LS $DUMPISZFS >>/dev/null 2>&1
246		cecho "stmsboot: dump on ZFS, no dumpadm update required"
247	fi
248	return 0
249}
250
251# Update bootpath for x86 here when we are enabling mpxio on root
252update_bootpath()
253{
254	cur_bootpath=`$STMSBOOTUTIL -b`
255	if [ $? -ne 0 ]; then
256		cecho "stmsboot: ERROR! Unable to retrieve bootpath property\n"
257		exit 1
258	fi
259
260	# Since on x64 platforms the eeprom command doesn't update the
261	# kernel, the file /boot/solaris/bootenv.rc and the kernel's
262	# bootpath variable have a good chance of differing. We do some
263	# extra handwaving to get the correct bootpath variable setting.
264
265	ONDISKVER=`$AWK '/bootpath/ {print $3}' /boot/solaris/bootenv.rc|\
266		$SED -e"s,',,g"`
267	if [ "$ONDISKVER" != "$cur_bootpath" ]; then
268		cur_bootpath="$ONDISKVER"
269	fi
270
271	NEWBOOTPATH=""
272	for path in $cur_bootpath; do
273		mapped=`$STMSBOOTUTIL -p $path`
274		if [ "$mapped" != "NOT_MAPPED" ]; then
275			if [ "$mapped" != "$path" ]; then
276				NEWBOOTPATH=`echo "$path " | \
277				    $SED -e"s|$path|$mapped|"`" $NEWBOOTPATH"
278			else
279				NEWBOOTPATH="$NEWBOOTPATH $path"
280			fi
281		fi
282	done
283	# now strip off leading and trailing space chars
284	new_bootpath=`echo $NEWBOOTPATH`
285	$EEPROM bootpath="$new_bootpath"
286	cecho "stmsboot: bootpath has been updated"
287	cecho ""
288}
289
290# Now do the actual work
291mpxio_main()
292{
293	# NOTE: If the first attempt to run the service has failed due to an
294	# expected error, users should be able to manually rerun the service.
295	#
296	# First mount /usr read only. This must be done to run
297	# utilities such as fsck and devfsadm.
298	# In the case of a manual rerun of the service, mounting of /usr here
299	# fails if /usr already happens to be mounted. It is better that we
300	# do not mount /usr if already mounted, but there seems to be no
301	# apparent way to check whether /usr is mounted or not as we mount
302	# /usr without making an entry into /etc/mnttab. So instead of
303	# explicitly checking for mount failures, we just do a sanity check
304	# by looking for some file (in this case devfsadm) in /usr.
305	#
306	mpxio_mount_usr
307	if [ ! -s $DEVFSADM ]; then
308		mpxio_error "failed to mount the /usr filesystem."
309		return
310	fi
311
312	if mpxio_mount_root; then
313		# create /dev links
314		cecho "stmsboot: configuring devices"
315		$DEVFSADM
316
317		if [ -n "$ISROOTDEVPATH" ]; then
318			ISROOTDEV=`$STMSBOOTUTIL -o $ISROOTDEVPATH`
319		fi
320
321		# update /etc/vfstab to reflect device name changes
322		$STMSBOOTUTIL -u >/dev/msglog 2>&1
323		if [ $? -eq 0 ]; then
324			$CP /etc/vfstab /etc/vfstab.old
325			# handle active-active paths, where the probe order
326			# for the hba reports a different path to what the
327			# boot-device variable gives us
328			if [ -n "$ISROOTDEV" ]; then
329				ROOTDEVCHK=`grep $ISROOTDEV /etc/vfstab`
330				if [ $? -ne 0 ]; then
331					# we got a different path for root
332					exec < $SAVEDIR/vfstab.new; readvfstab /
333					FILEDEV=`$ECHO $special | \
334					    $SED -e"s,/dev/dsk/,," -e"s,s[0-9]*,,"`
335					$SED -e"s,$FILEDEV,$ISROOTDEV,g" < \
336					    $SAVEDIR/vfstab.new > /etc/vfstab
337				fi
338			else
339				$CP $SAVEDIR/vfstab.new /etc/vfstab
340			fi
341			$RM $SAVEDIR/vfstab.new
342			cecho ""
343			cecho "stmsboot: vfstab has been updated"
344
345			update_dumpconf
346
347			MACH=`$UNAME -p`
348			if [ "$MACH" = "i386" ]; then
349				# only update bootpath here for x86
350				update_bootpath
351			fi
352			cecho "stmsboot: now regenerating boot archive"
353			$BOOTADM update-archive
354		else
355			mpxio_error "failed to update /etc/vfstab."
356		fi
357
358		$SVCADM disable system/device/mpxio-upgrade
359
360		if [ $usrmounted -eq 1 ]; then
361			cecho "stmsboot: rebooting the system now."
362			$REBOOT
363		fi
364	else
365		mpxio_error "failed to mount the root filesystem."
366	fi
367}
368
369mpxio_main
370