xref: /titanic_52/usr/src/cmd/svc/shell/fs_include.sh (revision ace1a5f11236a072fca1b5e0ea1416a083a9f2aa)
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 2005 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#	35	modified root or something equally dangerous
114#	36	uncorrectable errors detected - terminate normally (4.1 code 8)
115#	37	a signal was caught during processing (4.1 exit 12)
116#	39	uncorrectable errors detected - terminate rightaway (4.1 code 8)
117#	40	 for root, same as 0 (used here to remount root)
118#
119checkfs() {
120	# skip checking if the fsckdev is "-"
121	[ "x$1" = x- ] && return
122
123	# if fsck isn't present, it is probably because either the mount of
124	# /usr failed or the /usr filesystem is badly damanged.  In either
125	# case, there is not much to be done automatically.  Fail with
126	# a message to the user.
127
128	if [ ! -x /usr/sbin/fsck ]; then
129		cecho ""
130		cecho "WARNING - /usr/sbin/fsck not found.  Most likely the"
131		cecho "mount of /usr failed or the /usr filesystem is badly"
132		cecho "damaged."
133		cecho ""
134		return 1
135	fi
136
137	/usr/sbin/fsck -F $2 -m $1 >/dev/null 2>&1
138
139	if [ $? -ne 0 ]; then
140		# Determine fsck options by file system type
141		case $2 in
142			ufs)	foptions="-o p"
143				;;
144			*)	foptions="-y"
145				;;
146		esac
147
148		cecho "The $3 file system ($1) is being checked."
149		/usr/sbin/fsck -F $2 $foptions $1
150
151		case $? in
152		0|40)	# File system OK
153			;;
154
155		1|34|36|37|39)	# couldn't fix the file system - fail
156			checkmessage "$1" "$2" "$3"
157			return 1
158			;;
159
160		*)	# fsck child process killed (+ error code 35)
161			checkmessage2 "$1" "$2" "$3" "$?"
162			return 1
163			;;
164		esac
165	fi
166
167	return 0
168}
169
170#
171# checkopt option option-string
172# -> ($option, $otherops)
173#
174#   Check to see if a given mount option is present in the comma
175#   separated list gotten from vfstab.
176#
177#	Returns:
178#	${option}       : the option if found the empty string if not found
179#	${otherops}     : the option string with the found option deleted
180#
181checkopt() {
182	option=
183	otherops=
184
185	[ "x$2" = x- ] && return
186
187	searchop="$1"
188	set -- `IFS=, ; echo $2`
189
190	while [ $# -gt 0 ]; do
191		if [ "x$1" = "x$searchop" ]; then
192			option="$1"
193		else
194			if [ -z "$otherops" ]; then
195				otherops="$1"
196			else
197				otherops="${otherops},$1"
198			fi
199		fi
200		shift
201	done
202}
203
204#
205# hasopts $opts $allopts
206#
207#   Check if all options from the list $opts are present in $allopts.
208#   Both $opts and $allopts should be in comma separated format.
209#
210# Return 0 on success, and 1 otherwise.
211#
212hasopts() {
213	opts="$1"
214	allopts="$2"
215
216	set -- `IFS=, ; echo $opts`
217	while [ $# -gt 0 ]; do
218		if [ "$1" != "remount" ]; then
219			checkopt $1 $allopts
220			#
221			# Don't report errors if the filesystem is already
222			# read-write when mounting it as read-only.
223			#
224			[ -z "$option" ] && [ "$1" = "ro" ] && \
225				checkopt rw $allopts
226			[ -z "$option" ] && return 1
227		fi
228		shift
229	done
230	return 0
231}
232
233#
234# mounted $path $fsopts $fstype
235#
236#   Check whether the specified file system of the given type is currently
237#   mounted with all required filesystem options by going through /etc/mnttab
238#   in our standard input.
239#
240#   Return values:
241#   0	Success.
242#   1	The filesystem is not currently mounted, or mounted without required
243#	options, or a filesystem of a different type is mounted instead.
244#
245mounted() {
246	path="$1"
247	fsopts="$2"
248	fstype="$3"
249
250	while read mntspec mntpath mnttype mntopts on; do
251		[ "$mntpath" = "$path" ] || continue
252		[ "$fstype" != "-" ] && [ "$mnttype" != "$fstype" ] && return 1
253		[ "$fsopts" = "-" ] && return 0
254		hasopts $fsopts $mntopts && return 0
255	done
256	return 1
257}
258
259#
260# mountfs $opts $path $type $fsopts $special
261#
262#   Try to mount a filesystem.  If failed, display our standard error
263#   message on the console and print more details about what happened
264#   to our service log.
265#
266# Arguments:
267#   $opts	- options for mount(1M)				[optional]
268#   $path	- mount point
269#   $type	- file system type				[optional]
270#   $fsopts	- file system specific options (-o)		[optional]
271#   $special	- device on which the file system resides	[optional]
272#
273# Return codes:
274#   0		- success.
275#   otherwise	- error code returned by mount(1M).
276#
277mountfs() {
278	opts="$1"
279	path="$2"
280	special="$5"
281
282	#
283	# Take care of optional arguments
284	#
285	[ "$opts" = "-" ] && opts=""
286	[ "$special" = "-" ] &&	special=""
287	[ "$3" = "-" ] && type=""
288	[ "$3" != "-" ] && type="-F $3"
289	[ "$4" = "-" ] && fsopts=""
290	[ "$4" != "-" ] && fsopts="-o $4"
291
292	cmd="/sbin/mount $opts $type $fsopts $special $path"
293	msg=`$cmd 2>&1`
294	err=$?
295
296	[ $err = 0 ] && return 0
297
298	#
299	# If the specified file system is already mounted with all
300	# required options, and has the same filesystem type
301	# then ignore errors and return success
302	#
303	mounted $path $4 $3 < /etc/mnttab && return 0
304
305	echo "ERROR: $SMF_FMRI failed to mount $path "\
306	     "(see 'svcs -x' for details)" > /dev/msglog
307	echo "ERROR: $cmd failed, err=$err"
308	echo $msg
309	return $err
310}
311