xref: /titanic_41/usr/src/cmd/svc/shell/fs_include.sh (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1#!/bin/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, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#
24# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T.
28# All rights reserved.
29#
30#
31#ident	"%Z%%M%	%I%	%E% SMI"
32
33vfstab=${vfstab:=/etc/vfstab}
34
35#
36# readvfstab mount_point
37#   -> (special, fsckdev, mountp, fstype, fsckpass, automnt, mntopts)
38#
39#   A vfstab-like input stream is scanned for the mount point specified
40#   as $1.  Returns the fields of vfstab in the following shell
41#   variables:
42#
43#       special		block device
44#       fsckdev		raw device
45#       mountp		mount point (must match $1, if found)
46#       fstype		file system type
47#       fsckpass	fsck(1M) pass number
48#       automnt		automount flag (yes or no)
49#       mntopts		file system-specific mount options.
50#
51#   If the mount point can not be found in the standard input stream,
52#   then all fields are set to empty values.  This function assumes that
53#   stdin is already set /etc/vfstab (or other appropriate input
54#   stream).
55#
56readvfstab() {
57	while read special fsckdev mountp fstype fsckpass automnt mntopts; do
58		case "$special" in
59			'' )	# Ignore empty lines.
60				continue
61				;;
62
63			'#'* )	# Ignore comment lines.
64				continue
65				;;
66
67			'-')	# Ignore "no-action" lines.
68				continue
69				;;
70		esac
71
72		[ "x$mountp" = "x$1" ] && break
73	done
74}
75
76cecho() {
77	echo $*
78	echo $* >/dev/msglog
79}
80
81#
82# checkmessage raw_device fstype mountpoint
83# checkmessage2 raw_device fstype mountpoint
84#
85#   Two simple auxilary routines to the shell function checkfs.  Both
86#   display instructions for a manual file system check.
87#
88checkmessage() {
89	cecho ""
90	cecho "WARNING - Unable to repair the $3 filesystem. Run fsck"
91	cecho "manually (fsck -F $2 $1)."
92	cecho ""
93}
94
95checkmessage2() {
96	cecho ""
97	cecho "WARNING - fatal error from fsck - error $4"
98	cecho "Unable to repair the $3 filesystem. Run fsck manually"
99	cecho "(fsck -F $2 $1)."
100	cecho ""
101}
102
103#
104# checkfs raw_device fstype mountpoint
105#
106#   Check the file system specified. The return codes from fsck have the
107#   following meanings.
108#
109#	 0	file system is unmounted and okay
110#	32	file system is unmounted and needs checking (fsck -m only)
111#	33	file system is already mounted
112#	34	cannot stat device
113#	36	uncorrectable errors detected - terminate normally (4.1 code 8)
114#	37	a signal was caught during processing (4.1 exit 12)
115#	39	uncorrectable errors detected - terminate rightaway (4.1 code 8)
116#	40	 for root, same as 0 (used here to remount root)
117#
118checkfs() {
119	# skip checking if the fsckdev is "-"
120	[ "x$1" = x- ] && return
121
122	# if fsck isn't present, it is probably because either the mount of
123	# /usr failed or the /usr filesystem is badly damanged.  In either
124	# case, there is not much to be done automatically.  Fail with
125	# a message to the user.
126
127	if [ ! -x /usr/sbin/fsck ]; then
128		cecho ""
129		cecho "WARNING - /usr/sbin/fsck not found.  Most likely the"
130		cecho "mount of /usr failed or the /usr filesystem is badly"
131		cecho "damaged."
132		cecho ""
133		return 1
134	fi
135
136	/usr/sbin/fsck -F $2 -m $1 >/dev/null 2>&1
137
138	if [ $? -ne 0 ]; then
139		# Determine fsck options by file system type
140		case $2 in
141			ufs)	foptions="-o p"
142				;;
143			*)	foptions="-y"
144				;;
145		esac
146
147		cecho "The $3 file system ($1) is being checked."
148		/usr/sbin/fsck -F $2 $foptions $1
149
150		case $? in
151		0|40)	# File system OK
152			;;
153
154		1|34|36|37|39)	# couldn't fix the file system - fail
155			checkmessage "$1" "$2" "$3"
156			return 1
157			;;
158
159		*)	# fsck child process killed (+ error code 35)
160			checkmessage2 "$1" "$2" "$3" "$?"
161			return 1
162			;;
163		esac
164	fi
165
166	return 0
167}
168
169#
170# checkopt option option-string
171# -> ($option, $otherops)
172#
173#   Check to see if a given mount option is present in the comma
174#   separated list gotten from vfstab.
175#
176#	Returns:
177#	${option}       : the option if found the empty string if not found
178#	${otherops}     : the option string with the found option deleted
179#
180checkopt() {
181	option=
182	otherops=
183
184	[ "x$2" = x- ] && return
185
186	searchop="$1"
187	set -- `IFS=, ; echo $2`
188
189	while [ $# -gt 0 ]; do
190		if [ "x$1" = "x$searchop" ]; then
191			option="$1"
192		else
193			if [ -z "$otherops" ]; then
194				otherops="$1"
195			else
196				otherops="${otherops},$1"
197			fi
198		fi
199		shift
200	done
201}
202
203#
204# hasopts $opts $allopts
205#
206#   Check if all options from the list $opts are present in $allopts.
207#   Both $opts and $allopts should be in comma separated format.
208#
209# Return 0 on success, and 1 otherwise.
210#
211hasopts() {
212	opts="$1"
213	allopts="$2"
214
215	set -- `IFS=, ; echo $opts`
216	while [ $# -gt 0 ]; do
217		if [ "$1" != "remount" ]; then
218			checkopt $1 $allopts
219			#
220			# Don't report errors if the filesystem is already
221			# read-write when mounting it as read-only.
222			#
223			[ -z "$option" ] && [ "$1" = "ro" ] && \
224				checkopt rw $allopts
225			[ -z "$option" ] && return 1
226		fi
227		shift
228	done
229	return 0
230}
231
232#
233# mounted $path $fsopts $fstype
234#
235#   Check whether the specified file system of the given type is currently
236#   mounted with all required filesystem options by going through /etc/mnttab
237#   in our standard input.
238#
239#   Return values:
240#   0	Success.
241#   1	The filesystem is not currently mounted, or mounted without required
242#	options, or a filesystem of a different type is mounted instead.
243#
244mounted() {
245	path="$1"
246	fsopts="$2"
247	fstype="$3"
248
249	while read mntspec mntpath mnttype mntopts on; do
250		[ "$mntpath" = "$path" ] || continue
251		[ "$fstype" != "-" ] && [ "$mnttype" != "$fstype" ] && return 1
252		[ "$fsopts" = "-" ] && return 0
253		hasopts $fsopts $mntopts && return 0
254	done
255	return 1
256}
257
258#
259# mountfs $opts $path $type $fsopts $special
260#
261#   Try to mount a filesystem.  If failed, display our standard error
262#   message on the console and print more details about what happened
263#   to our service log.
264#
265# Arguments:
266#   $opts	- options for mount(1M)				[optional]
267#   $path	- mount point
268#   $type	- file system type				[optional]
269#   $fsopts	- file system specific options (-o)		[optional]
270#   $special	- device on which the file system resides	[optional]
271#
272# Return codes:
273#   0		- success.
274#   otherwise	- error code returned by mount(1M).
275#
276mountfs() {
277	opts="$1"
278	path="$2"
279	special="$5"
280
281	#
282	# Take care of optional arguments
283	#
284	[ "$opts" = "-" ] && opts=""
285	[ "$special" = "-" ] &&	special=""
286	[ "$3" = "-" ] && type=""
287	[ "$3" != "-" ] && type="-F $3"
288	[ "$4" = "-" ] && fsopts=""
289	[ "$4" != "-" ] && fsopts="-o $4"
290
291	cmd="/sbin/mount $opts $type $fsopts $special $path"
292	msg=`$cmd 2>&1`
293	err=$?
294
295	[ $err = 0 ] && return 0
296
297	#
298	# If the specified file system is already mounted with all
299	# required options, and has the same filesystem type
300	# then ignore errors and return success
301	#
302	mounted $path $4 $3 < /etc/mnttab && return 0
303
304	echo "ERROR: $SMF_FMRI failed to mount $path "\
305	     "(see 'svcs -x' for details)" > /dev/msglog
306	echo "ERROR: $cmd failed, err=$err"
307	echo $msg
308	return $err
309}
310