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