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