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