xref: /onnv-gate/usr/src/cmd/iscsid/iscsi-initiator (revision 12148:bde44e1ddd2e)
1*12148SZhang.Yi@Sun.COM#!/sbin/sh
2*12148SZhang.Yi@Sun.COM#
3*12148SZhang.Yi@Sun.COM# CDDL HEADER START
4*12148SZhang.Yi@Sun.COM#
5*12148SZhang.Yi@Sun.COM# The contents of this file are subject to the terms of the
6*12148SZhang.Yi@Sun.COM# Common Development and Distribution License (the "License").
7*12148SZhang.Yi@Sun.COM# You may not use this file except in compliance with the License.
8*12148SZhang.Yi@Sun.COM#
9*12148SZhang.Yi@Sun.COM# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*12148SZhang.Yi@Sun.COM# or http://www.opensolaris.org/os/licensing.
11*12148SZhang.Yi@Sun.COM# See the License for the specific language governing permissions
12*12148SZhang.Yi@Sun.COM# and limitations under the License.
13*12148SZhang.Yi@Sun.COM#
14*12148SZhang.Yi@Sun.COM# When distributing Covered Code, include this CDDL HEADER in each
15*12148SZhang.Yi@Sun.COM# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*12148SZhang.Yi@Sun.COM# If applicable, add the following below this CDDL HEADER, with the
17*12148SZhang.Yi@Sun.COM# fields enclosed by brackets "[]" replaced with your own identifying
18*12148SZhang.Yi@Sun.COM# information: Portions Copyright [yyyy] [name of copyright owner]
19*12148SZhang.Yi@Sun.COM#
20*12148SZhang.Yi@Sun.COM# CDDL HEADER END
21*12148SZhang.Yi@Sun.COM#
22*12148SZhang.Yi@Sun.COM
23*12148SZhang.Yi@Sun.COM#
24*12148SZhang.Yi@Sun.COM# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25*12148SZhang.Yi@Sun.COM#
26*12148SZhang.Yi@Sun.COM
27*12148SZhang.Yi@Sun.COM#
28*12148SZhang.Yi@Sun.COM# Start/stop iscsi initiator service
29*12148SZhang.Yi@Sun.COM#
30*12148SZhang.Yi@Sun.COM. /lib/svc/share/smf_include.sh
31*12148SZhang.Yi@Sun.COM
32*12148SZhang.Yi@Sun.COM#	checkmessage "fsck_device | mount_point"
33*12148SZhang.Yi@Sun.COM#
34*12148SZhang.Yi@Sun.COM# Simple auxilary routine to the shell function checkfs. Prints out
35*12148SZhang.Yi@Sun.COM# instructions for a manual file system check before entering the shell.
36*12148SZhang.Yi@Sun.COM#
37*12148SZhang.Yi@Sun.COMcheckmessage() {
38*12148SZhang.Yi@Sun.COM	echo "" > /dev/console
39*12148SZhang.Yi@Sun.COM	if [ "$1" != "" ] ; then
40*12148SZhang.Yi@Sun.COM		echo "WARNING - Unable to repair one or more \c" \
41*12148SZhang.Yi@Sun.COM			> /dev/console
42*12148SZhang.Yi@Sun.COM		echo "of the following filesystem(s):" > /dev/console
43*12148SZhang.Yi@Sun.COM		echo "\t$1" > /dev/console
44*12148SZhang.Yi@Sun.COM	else
45*12148SZhang.Yi@Sun.COM            	echo "WARNING - Unable to repair one or more filesystems." \
46*12148SZhang.Yi@Sun.COM			> /dev/console
47*12148SZhang.Yi@Sun.COM	fi
48*12148SZhang.Yi@Sun.COM        echo "Run fsck manually (fsck filesystem...)." > /dev/console
49*12148SZhang.Yi@Sun.COM	echo "" > /dev/console
50*12148SZhang.Yi@Sun.COM}
51*12148SZhang.Yi@Sun.COM
52*12148SZhang.Yi@Sun.COM#
53*12148SZhang.Yi@Sun.COM#	checkfs raw_device fstype mountpoint
54*12148SZhang.Yi@Sun.COM#
55*12148SZhang.Yi@Sun.COM# Check the file system specified. The return codes from fsck have the
56*12148SZhang.Yi@Sun.COM# following meanings.
57*12148SZhang.Yi@Sun.COM#	 0 - file system is unmounted and okay
58*12148SZhang.Yi@Sun.COM#	32 - file system is unmounted and needs checking (fsck -m only)
59*12148SZhang.Yi@Sun.COM#	33 - file system is already mounted
60*12148SZhang.Yi@Sun.COM#	34 - cannot stat device
61*12148SZhang.Yi@Sun.COM#	36 - uncorrectable errors detected - terminate normally (4.1 code 8)
62*12148SZhang.Yi@Sun.COM#	37 - a signal was caught during processing (4.1 exit 12)
63*12148SZhang.Yi@Sun.COM#	39 - uncorrectable errors detected - terminate rightaway (4.1 code 8)
64*12148SZhang.Yi@Sun.COM#	40 - for root, same as 0 (used by rcS to remount root)
65*12148SZhang.Yi@Sun.COM#
66*12148SZhang.Yi@Sun.COMcheckfs() {
67*12148SZhang.Yi@Sun.COM        /usr/sbin/fsck -F $2 -m $1  >/dev/null 2>&1
68*12148SZhang.Yi@Sun.COM
69*12148SZhang.Yi@Sun.COM	if [ $? -ne 0 ]
70*12148SZhang.Yi@Sun.COM	then
71*12148SZhang.Yi@Sun.COM           	# Determine fsck options by file system type
72*12148SZhang.Yi@Sun.COM		case "$2" in
73*12148SZhang.Yi@Sun.COM		ufs)	foptions="-o p"
74*12148SZhang.Yi@Sun.COM                        ;;
75*12148SZhang.Yi@Sun.COM		*)	foptions="-y"
76*12148SZhang.Yi@Sun.COM                        ;;
77*12148SZhang.Yi@Sun.COM		esac
78*12148SZhang.Yi@Sun.COM
79*12148SZhang.Yi@Sun.COM		echo "The "$3" file system ("$1") is being checked."
80*12148SZhang.Yi@Sun.COM		/usr/sbin/fsck -F $2 ${foptions} $1
81*12148SZhang.Yi@Sun.COM
82*12148SZhang.Yi@Sun.COM       		case $? in
83*12148SZhang.Yi@Sun.COM		0|40)	# file system OK
84*12148SZhang.Yi@Sun.COM			;;
85*12148SZhang.Yi@Sun.COM
86*12148SZhang.Yi@Sun.COM		*)	# couldn't fix the file system
87*12148SZhang.Yi@Sun.COM			echo "/usr/sbin/fsck failed with exit code "$?"."
88*12148SZhang.Yi@Sun.COM                        checkmessage "$1"
89*12148SZhang.Yi@Sun.COM                        ;;
90*12148SZhang.Yi@Sun.COM		esac
91*12148SZhang.Yi@Sun.COM	fi
92*12148SZhang.Yi@Sun.COM}
93*12148SZhang.Yi@Sun.COM
94*12148SZhang.Yi@Sun.COM
95*12148SZhang.Yi@Sun.COMmount_iscsi() {
96*12148SZhang.Yi@Sun.COM	err=0
97*12148SZhang.Yi@Sun.COM	iscsilist=""
98*12148SZhang.Yi@Sun.COM        exec < /etc/vfstab
99*12148SZhang.Yi@Sun.COM	while  read special fsckdev mountp fstype fsckpass automnt mntopts; do
100*12148SZhang.Yi@Sun.COM		case $special in
101*12148SZhang.Yi@Sun.COM			'#'* | '' )	# Ignore comments, empty lines.
102*12148SZhang.Yi@Sun.COM					continue
103*12148SZhang.Yi@Sun.COM					;;
104*12148SZhang.Yi@Sun.COM		'-')		# Ignore "no-action" lines.
105*12148SZhang.Yi@Sun.COM					continue
106*12148SZhang.Yi@Sun.COM					;;
107*12148SZhang.Yi@Sun.COM		esac
108*12148SZhang.Yi@Sun.COM		if [ "$automnt" != "iscsi" ]; then
109*12148SZhang.Yi@Sun.COM			continue
110*12148SZhang.Yi@Sun.COM		fi
111*12148SZhang.Yi@Sun.COM		if [ "$fstype" = "-" ]; then
112*12148SZhang.Yi@Sun.COM			echo "iscsi-initiator: FSType of iscsi LUN \c" 1>&2
113*12148SZhang.Yi@Sun.COM			echo "$special cannot be identified" 1>&2
114*12148SZhang.Yi@Sun.COM			continue
115*12148SZhang.Yi@Sun.COM		fi
116*12148SZhang.Yi@Sun.COM
117*12148SZhang.Yi@Sun.COM		#
118*12148SZhang.Yi@Sun.COM		# Ignore entries already mounted
119*12148SZhang.Yi@Sun.COM		#
120*12148SZhang.Yi@Sun.COM		/usr/bin/grep "	$mountp	" /etc/mnttab >/dev/null \
121*12148SZhang.Yi@Sun.COM		2>&1 && continue
122*12148SZhang.Yi@Sun.COM
123*12148SZhang.Yi@Sun.COM		#
124*12148SZhang.Yi@Sun.COM		# Can't fsck if no fsckdev is specified
125*12148SZhang.Yi@Sun.COM		#
126*12148SZhang.Yi@Sun.COM		if [ "$fsckdev" = "-" ]; then
127*12148SZhang.Yi@Sun.COM			iscsilist="$iscsilist $mountp"
128*12148SZhang.Yi@Sun.COM			continue
129*12148SZhang.Yi@Sun.COM		fi
130*12148SZhang.Yi@Sun.COM
131*12148SZhang.Yi@Sun.COM		#
132*12148SZhang.Yi@Sun.COM		# fsck everything else:
133*12148SZhang.Yi@Sun.COM 		#
134*12148SZhang.Yi@Sun.COM	 	# fsck -m simply returns true if the filesystem is
135*12148SZhang.Yi@Sun.COM		# suitable for mounting.
136*12148SZhang.Yi@Sun.COM	 	#
137*12148SZhang.Yi@Sun.COM		/usr/sbin/fsck -m -F $fstype $fsckdev >/dev/null 2>&1
138*12148SZhang.Yi@Sun.COM		case $? in
139*12148SZhang.Yi@Sun.COM		0|40)	iscsilist="$iscsilist $mountp"
140*12148SZhang.Yi@Sun.COM			continue
141*12148SZhang.Yi@Sun.COM			;;
142*12148SZhang.Yi@Sun.COM		32)	checkfs $fsckdev $fstype $mountp
143*12148SZhang.Yi@Sun.COM			iscsilist="$iscsilist $mountp"
144*12148SZhang.Yi@Sun.COM			continue
145*12148SZhang.Yi@Sun.COM			;;
146*12148SZhang.Yi@Sun.COM		33)	# already mounted
147*12148SZhang.Yi@Sun.COM			echo "$special already mounted"
148*12148SZhang.Yi@Sun.COM			;;
149*12148SZhang.Yi@Sun.COM		34)	# bogus special device
150*12148SZhang.Yi@Sun.COM			echo "Cannot stat $fsckdev - ignoring"
151*12148SZhang.Yi@Sun.COM			err=1
152*12148SZhang.Yi@Sun.COM			;;
153*12148SZhang.Yi@Sun.COM		*)	# uncorrectable errors
154*12148SZhang.Yi@Sun.COM			echo "$fsckdev uncorrectable error"
155*12148SZhang.Yi@Sun.COM			err=1
156*12148SZhang.Yi@Sun.COM			;;
157*12148SZhang.Yi@Sun.COM		esac
158*12148SZhang.Yi@Sun.COM	done
159*12148SZhang.Yi@Sun.COM
160*12148SZhang.Yi@Sun.COM	[ -z "$iscsilist" ] || /sbin/mount -a $iscsilist
161*12148SZhang.Yi@Sun.COM	for iscsilun in $iscsilist
162*12148SZhang.Yi@Sun.COM	do
163*12148SZhang.Yi@Sun.COM		/usr/bin/grep "	$iscsilun	" /etc/mnttab >/dev/null 2>&1
164*12148SZhang.Yi@Sun.COM		if [ $? -ne 0 ]; then
165*12148SZhang.Yi@Sun.COM			echo "Fail to mount $iscsilun"
166*12148SZhang.Yi@Sun.COM			err=1
167*12148SZhang.Yi@Sun.COM		fi
168*12148SZhang.Yi@Sun.COM	done
169*12148SZhang.Yi@Sun.COM	return $err
170*12148SZhang.Yi@Sun.COM}
171*12148SZhang.Yi@Sun.COM
172*12148SZhang.Yi@Sun.COMumount_iscsi () {
173*12148SZhang.Yi@Sun.COM	#
174*12148SZhang.Yi@Sun.COM	# Generate iscsi mountp list from /etc/vfstab
175*12148SZhang.Yi@Sun.COM	exec < /etc/vfstab
176*12148SZhang.Yi@Sun.COM	while  read special fsckdev mountp fstype fsckpass automnt mntopts; do
177*12148SZhang.Yi@Sun.COM		case $special in
178*12148SZhang.Yi@Sun.COM			'#'* | '')      continue;;  # Ignore comments,
179*12148SZhang.Yi@Sun.COM						    # empty lines.
180*12148SZhang.Yi@Sun.COM			'-')            continue;;  # Ignore "no-action lines.
181*12148SZhang.Yi@Sun.COM		esac
182*12148SZhang.Yi@Sun.COM		if [ "$automnt" != "iscsi" ]; then
183*12148SZhang.Yi@Sun.COM			continue
184*12148SZhang.Yi@Sun.COM		fi
185*12148SZhang.Yi@Sun.COM		/usr/bin/grep "	$mountp	" /etc/mnttab >/dev/null 2>&1
186*12148SZhang.Yi@Sun.COM		if [ $? -ne 0 ]; then
187*12148SZhang.Yi@Sun.COM			continue
188*12148SZhang.Yi@Sun.COM		fi
189*12148SZhang.Yi@Sun.COM		iscsilist="$iscsilist $mountp"
190*12148SZhang.Yi@Sun.COM	done
191*12148SZhang.Yi@Sun.COM
192*12148SZhang.Yi@Sun.COM	if [ -n "$iscsilist" ]; then
193*12148SZhang.Yi@Sun.COM		umount -a $iscsilist 1>&2
194*12148SZhang.Yi@Sun.COM		rc=$?
195*12148SZhang.Yi@Sun.COM	else
196*12148SZhang.Yi@Sun.COM		rc=0;
197*12148SZhang.Yi@Sun.COM	fi
198*12148SZhang.Yi@Sun.COM	return $rc
199*12148SZhang.Yi@Sun.COM}
200*12148SZhang.Yi@Sun.COM
201*12148SZhang.Yi@Sun.COMcase "$1" in
202*12148SZhang.Yi@Sun.COM'start')
203*12148SZhang.Yi@Sun.COM	/usr/bin/pgrep -P 1 -x iscsid
204*12148SZhang.Yi@Sun.COM	if [ $? -ne 0 ]; then
205*12148SZhang.Yi@Sun.COM		/lib/svc/method/iscsid
206*12148SZhang.Yi@Sun.COM	fi
207*12148SZhang.Yi@Sun.COM	if [ $? -eq 0 ]; then
208*12148SZhang.Yi@Sun.COM		delay=60
209*12148SZhang.Yi@Sun.COM		while [ $delay -gt 0 ]; do
210*12148SZhang.Yi@Sun.COM			delay=`expr $delay - 1`
211*12148SZhang.Yi@Sun.COM			mount_iscsi
212*12148SZhang.Yi@Sun.COM			if [ $? -eq 1 ]; then
213*12148SZhang.Yi@Sun.COM				if [ $delay -gt 0 ]; then
214*12148SZhang.Yi@Sun.COM					sleep 1
215*12148SZhang.Yi@Sun.COM					continue
216*12148SZhang.Yi@Sun.COM				else
217*12148SZhang.Yi@Sun.COM					echo "iscsi-initiator: mount iscsi \c"
218*12148SZhang.Yi@Sun.COM					echo "lun in /etc/vfstab fail."
219*12148SZhang.Yi@Sun.COM					umount_iscsi
220*12148SZhang.Yi@Sun.COM					exit $SMF_EXIT_ERR_CONFIG
221*12148SZhang.Yi@Sun.COM				fi
222*12148SZhang.Yi@Sun.COM			else
223*12148SZhang.Yi@Sun.COM				exit $SMF_EXIT_OK
224*12148SZhang.Yi@Sun.COM			fi
225*12148SZhang.Yi@Sun.COM		done
226*12148SZhang.Yi@Sun.COM	else
227*12148SZhang.Yi@Sun.COM		exit $?
228*12148SZhang.Yi@Sun.COM	fi
229*12148SZhang.Yi@Sun.COM	;;
230*12148SZhang.Yi@Sun.COM
231*12148SZhang.Yi@Sun.COM'stop')
232*12148SZhang.Yi@Sun.COM	umount_iscsi
233*12148SZhang.Yi@Sun.COM	/usr/bin/pkill -P 1 -x iscsid
234*12148SZhang.Yi@Sun.COM	exit 0
235*12148SZhang.Yi@Sun.COM	;;
236*12148SZhang.Yi@Sun.COM
237*12148SZhang.Yi@Sun.COM*)
238*12148SZhang.Yi@Sun.COM	echo "Usage: $0 { start | stop }"
239*12148SZhang.Yi@Sun.COM	exit 1
240*12148SZhang.Yi@Sun.COM	;;
241*12148SZhang.Yi@Sun.COMesac
242*12148SZhang.Yi@Sun.COMexit $SMF_EXIT_OK
243*12148SZhang.Yi@Sun.COM
244