xref: /illumos-gate/usr/src/cmd/svc/shell/fs_include.sh (revision bbf21555)
17c478bd9Sstevel@tonic-gate#!/bin/sh
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
65b4dc236Smishra# Common Development and Distribution License (the "License").
75b4dc236Smishra# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate#
23a227b7f4Shs# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
267c478bd9Sstevel@tonic-gate# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T.
277c478bd9Sstevel@tonic-gate# All rights reserved.
287c478bd9Sstevel@tonic-gate#
297c478bd9Sstevel@tonic-gate#
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gatevfstab=${vfstab:=/etc/vfstab}
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate#
347c478bd9Sstevel@tonic-gate# readvfstab mount_point
357c478bd9Sstevel@tonic-gate#   -> (special, fsckdev, mountp, fstype, fsckpass, automnt, mntopts)
367c478bd9Sstevel@tonic-gate#
377c478bd9Sstevel@tonic-gate#   A vfstab-like input stream is scanned for the mount point specified
387c478bd9Sstevel@tonic-gate#   as $1.  Returns the fields of vfstab in the following shell
397c478bd9Sstevel@tonic-gate#   variables:
407c478bd9Sstevel@tonic-gate#
417c478bd9Sstevel@tonic-gate#       special		block device
427c478bd9Sstevel@tonic-gate#       fsckdev		raw device
437c478bd9Sstevel@tonic-gate#       mountp		mount point (must match $1, if found)
447c478bd9Sstevel@tonic-gate#       fstype		file system type
45*bbf21555SRichard Lowe#       fsckpass	fsck(8) pass number
467c478bd9Sstevel@tonic-gate#       automnt		automount flag (yes or no)
477c478bd9Sstevel@tonic-gate#       mntopts		file system-specific mount options.
487c478bd9Sstevel@tonic-gate#
497c478bd9Sstevel@tonic-gate#   If the mount point can not be found in the standard input stream,
507c478bd9Sstevel@tonic-gate#   then all fields are set to empty values.  This function assumes that
517c478bd9Sstevel@tonic-gate#   stdin is already set /etc/vfstab (or other appropriate input
527c478bd9Sstevel@tonic-gate#   stream).
537c478bd9Sstevel@tonic-gate#
547c478bd9Sstevel@tonic-gatereadvfstab() {
557c478bd9Sstevel@tonic-gate	while read special fsckdev mountp fstype fsckpass automnt mntopts; do
567c478bd9Sstevel@tonic-gate		case "$special" in
577c478bd9Sstevel@tonic-gate			'' )	# Ignore empty lines.
587c478bd9Sstevel@tonic-gate				continue
597c478bd9Sstevel@tonic-gate				;;
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate			'#'* )	# Ignore comment lines.
627c478bd9Sstevel@tonic-gate				continue
637c478bd9Sstevel@tonic-gate				;;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate			'-')	# Ignore "no-action" lines.
667c478bd9Sstevel@tonic-gate				continue
677c478bd9Sstevel@tonic-gate				;;
687c478bd9Sstevel@tonic-gate		esac
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate		[ "x$mountp" = "x$1" ] && break
717c478bd9Sstevel@tonic-gate	done
727c478bd9Sstevel@tonic-gate}
737c478bd9Sstevel@tonic-gate
74e7cbe64fSgwreadswapdev() {
75e7cbe64fSgw	while read special fsckdev mountp fstype fsckpass automnt mntopts; do
76e7cbe64fSgw		# Ignore comments, empty lines, and no-action lines
77e7cbe64fSgw		case "$special" in
78e7cbe64fSgw		'#'* | '' | '-') continue;;
79e7cbe64fSgw		esac
80e7cbe64fSgw
81e7cbe64fSgw		[ "$fstype" != swap ] && continue
82e7cbe64fSgw
83e7cbe64fSgw		[ "x$special" = "x$1" ] && break
84e7cbe64fSgw	done
85e7cbe64fSgw}
86e7cbe64fSgw
87a227b7f4Shs#
88a227b7f4Shs# readmnttab mount_point
89a227b7f4Shs#   -> (special, mountp, fstype, mntopts, mnttime)
90a227b7f4Shs#
91a227b7f4Shs#   A mnttab-like input stream is scanned for the mount point specified
92a227b7f4Shs#   as $1.  Returns the fields of mnttab in the following shell
93a227b7f4Shs#   variables:
94a227b7f4Shs#
95a227b7f4Shs#       special		block device
96a227b7f4Shs#       mountp		mount point (must match $1, if found)
97a227b7f4Shs#       fstype		file system type
98a227b7f4Shs#       mntopts		file system-specific mount options.
99a227b7f4Shs#	mnttime		time at which file system was mounted
100a227b7f4Shs#
101a227b7f4Shs#   If the mount point can not be found in the standard input stream,
102a227b7f4Shs#   then all fields are set to empty values.  This function assumes that
103a227b7f4Shs#   stdin is already set to /etc/mnttab (or other appropriate input
104a227b7f4Shs#   stream).
105a227b7f4Shs#
106a227b7f4Shsreadmnttab() {
107a227b7f4Shs	while read special mountp fstype mntopts mnttime; do
108a227b7f4Shs		[ "x$mountp" = "x$1" ] && break
109a227b7f4Shs	done
110a227b7f4Shs}
111a227b7f4Shs
1127c478bd9Sstevel@tonic-gatececho() {
1137c478bd9Sstevel@tonic-gate	echo $*
1147c478bd9Sstevel@tonic-gate	echo $* >/dev/msglog
1157c478bd9Sstevel@tonic-gate}
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate#
1187c478bd9Sstevel@tonic-gate# checkmessage raw_device fstype mountpoint
1197c478bd9Sstevel@tonic-gate# checkmessage2 raw_device fstype mountpoint
1207c478bd9Sstevel@tonic-gate#
1217c478bd9Sstevel@tonic-gate#   Two simple auxilary routines to the shell function checkfs.  Both
1227c478bd9Sstevel@tonic-gate#   display instructions for a manual file system check.
1237c478bd9Sstevel@tonic-gate#
1247c478bd9Sstevel@tonic-gatecheckmessage() {
1257c478bd9Sstevel@tonic-gate	cecho ""
1267c478bd9Sstevel@tonic-gate	cecho "WARNING - Unable to repair the $3 filesystem. Run fsck"
1277c478bd9Sstevel@tonic-gate	cecho "manually (fsck -F $2 $1)."
1287c478bd9Sstevel@tonic-gate	cecho ""
1297c478bd9Sstevel@tonic-gate}
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gatecheckmessage2() {
1327c478bd9Sstevel@tonic-gate	cecho ""
1337c478bd9Sstevel@tonic-gate	cecho "WARNING - fatal error from fsck - error $4"
1347c478bd9Sstevel@tonic-gate	cecho "Unable to repair the $3 filesystem. Run fsck manually"
1357c478bd9Sstevel@tonic-gate	cecho "(fsck -F $2 $1)."
1367c478bd9Sstevel@tonic-gate	cecho ""
1377c478bd9Sstevel@tonic-gate}
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate#
1407c478bd9Sstevel@tonic-gate# checkfs raw_device fstype mountpoint
1417c478bd9Sstevel@tonic-gate#
1427c478bd9Sstevel@tonic-gate#   Check the file system specified. The return codes from fsck have the
1437c478bd9Sstevel@tonic-gate#   following meanings.
1447c478bd9Sstevel@tonic-gate#
1457c478bd9Sstevel@tonic-gate#	 0	file system is unmounted and okay
1467c478bd9Sstevel@tonic-gate#	32	file system is unmounted and needs checking (fsck -m only)
1477c478bd9Sstevel@tonic-gate#	33	file system is already mounted
1487c478bd9Sstevel@tonic-gate#	34	cannot stat device
149355d6bb5Sswilcox#	35	modified root or something equally dangerous
1507c478bd9Sstevel@tonic-gate#	36	uncorrectable errors detected - terminate normally (4.1 code 8)
1517c478bd9Sstevel@tonic-gate#	37	a signal was caught during processing (4.1 exit 12)
1527c478bd9Sstevel@tonic-gate#	39	uncorrectable errors detected - terminate rightaway (4.1 code 8)
1537c478bd9Sstevel@tonic-gate#	40	 for root, same as 0 (used here to remount root)
1547c478bd9Sstevel@tonic-gate#
1557c478bd9Sstevel@tonic-gatecheckfs() {
1567c478bd9Sstevel@tonic-gate	# skip checking if the fsckdev is "-"
1577c478bd9Sstevel@tonic-gate	[ "x$1" = x- ] && return
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate	# if fsck isn't present, it is probably because either the mount of
1607c478bd9Sstevel@tonic-gate	# /usr failed or the /usr filesystem is badly damanged.  In either
1617c478bd9Sstevel@tonic-gate	# case, there is not much to be done automatically.  Fail with
1627c478bd9Sstevel@tonic-gate	# a message to the user.
1637c478bd9Sstevel@tonic-gate	if [ ! -x /usr/sbin/fsck ]; then
1647c478bd9Sstevel@tonic-gate		cecho ""
1657c478bd9Sstevel@tonic-gate		cecho "WARNING - /usr/sbin/fsck not found.  Most likely the"
1667c478bd9Sstevel@tonic-gate		cecho "mount of /usr failed or the /usr filesystem is badly"
1677c478bd9Sstevel@tonic-gate		cecho "damaged."
1687c478bd9Sstevel@tonic-gate		cecho ""
1697c478bd9Sstevel@tonic-gate		return 1
1707c478bd9Sstevel@tonic-gate	fi
1717c478bd9Sstevel@tonic-gate
172d805fbaaSsch	# If a filesystem-specific fsck binary is unavailable, then no
173d805fbaaSsch	# fsck pass is required.
174d805fbaaSsch	[ ! -x /usr/lib/fs/$2/fsck ] && [ ! -x /etc/fs/$2/fsck ] && return
175d805fbaaSsch
1767c478bd9Sstevel@tonic-gate	/usr/sbin/fsck -F $2 -m $1 >/dev/null 2>&1
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate	if [ $? -ne 0 ]; then
1797c478bd9Sstevel@tonic-gate		# Determine fsck options by file system type
1807c478bd9Sstevel@tonic-gate		case $2 in
1817c478bd9Sstevel@tonic-gate			ufs)	foptions="-o p"
1827c478bd9Sstevel@tonic-gate				;;
1837c478bd9Sstevel@tonic-gate			*)	foptions="-y"
1847c478bd9Sstevel@tonic-gate				;;
1857c478bd9Sstevel@tonic-gate		esac
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate		cecho "The $3 file system ($1) is being checked."
1887c478bd9Sstevel@tonic-gate		/usr/sbin/fsck -F $2 $foptions $1
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate		case $? in
1917c478bd9Sstevel@tonic-gate		0|40)	# File system OK
1927c478bd9Sstevel@tonic-gate			;;
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate		1|34|36|37|39)	# couldn't fix the file system - fail
1957c478bd9Sstevel@tonic-gate			checkmessage "$1" "$2" "$3"
1967c478bd9Sstevel@tonic-gate			return 1
1977c478bd9Sstevel@tonic-gate			;;
1985b4dc236Smishra		33)	# already mounted
1995b4dc236Smishra			return 0
2005b4dc236Smishra			;;
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate		*)	# fsck child process killed (+ error code 35)
2037c478bd9Sstevel@tonic-gate			checkmessage2 "$1" "$2" "$3" "$?"
2047c478bd9Sstevel@tonic-gate			return 1
2057c478bd9Sstevel@tonic-gate			;;
2067c478bd9Sstevel@tonic-gate		esac
2077c478bd9Sstevel@tonic-gate	fi
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate	return 0
2107c478bd9Sstevel@tonic-gate}
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate#
2137c478bd9Sstevel@tonic-gate# checkopt option option-string
2147c478bd9Sstevel@tonic-gate# -> ($option, $otherops)
2157c478bd9Sstevel@tonic-gate#
2167c478bd9Sstevel@tonic-gate#   Check to see if a given mount option is present in the comma
2177c478bd9Sstevel@tonic-gate#   separated list gotten from vfstab.
2187c478bd9Sstevel@tonic-gate#
2197c478bd9Sstevel@tonic-gate#	Returns:
2207c478bd9Sstevel@tonic-gate#	${option}       : the option if found the empty string if not found
2217c478bd9Sstevel@tonic-gate#	${otherops}     : the option string with the found option deleted
2227c478bd9Sstevel@tonic-gate#
2237c478bd9Sstevel@tonic-gatecheckopt() {
2247c478bd9Sstevel@tonic-gate	option=
2257c478bd9Sstevel@tonic-gate	otherops=
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate	[ "x$2" = x- ] && return
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate	searchop="$1"
2307c478bd9Sstevel@tonic-gate	set -- `IFS=, ; echo $2`
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate	while [ $# -gt 0 ]; do
2337c478bd9Sstevel@tonic-gate		if [ "x$1" = "x$searchop" ]; then
2347c478bd9Sstevel@tonic-gate			option="$1"
2357c478bd9Sstevel@tonic-gate		else
2367c478bd9Sstevel@tonic-gate			if [ -z "$otherops" ]; then
2377c478bd9Sstevel@tonic-gate				otherops="$1"
2387c478bd9Sstevel@tonic-gate			else
2397c478bd9Sstevel@tonic-gate				otherops="${otherops},$1"
2407c478bd9Sstevel@tonic-gate			fi
2417c478bd9Sstevel@tonic-gate		fi
2427c478bd9Sstevel@tonic-gate		shift
2437c478bd9Sstevel@tonic-gate	done
2447c478bd9Sstevel@tonic-gate}
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate#
2477c478bd9Sstevel@tonic-gate# hasopts $opts $allopts
2487c478bd9Sstevel@tonic-gate#
2497c478bd9Sstevel@tonic-gate#   Check if all options from the list $opts are present in $allopts.
2507c478bd9Sstevel@tonic-gate#   Both $opts and $allopts should be in comma separated format.
2517c478bd9Sstevel@tonic-gate#
2527c478bd9Sstevel@tonic-gate# Return 0 on success, and 1 otherwise.
2537c478bd9Sstevel@tonic-gate#
2547c478bd9Sstevel@tonic-gatehasopts() {
2557c478bd9Sstevel@tonic-gate	opts="$1"
2567c478bd9Sstevel@tonic-gate	allopts="$2"
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate	set -- `IFS=, ; echo $opts`
2597c478bd9Sstevel@tonic-gate	while [ $# -gt 0 ]; do
2607c478bd9Sstevel@tonic-gate		if [ "$1" != "remount" ]; then
2617c478bd9Sstevel@tonic-gate			checkopt $1 $allopts
2627c478bd9Sstevel@tonic-gate			#
2637c478bd9Sstevel@tonic-gate			# Don't report errors if the filesystem is already
2647c478bd9Sstevel@tonic-gate			# read-write when mounting it as read-only.
2657c478bd9Sstevel@tonic-gate			#
2667c478bd9Sstevel@tonic-gate			[ -z "$option" ] && [ "$1" = "ro" ] && \
2677c478bd9Sstevel@tonic-gate				checkopt rw $allopts
2687c478bd9Sstevel@tonic-gate			[ -z "$option" ] && return 1
2697c478bd9Sstevel@tonic-gate		fi
2707c478bd9Sstevel@tonic-gate		shift
2717c478bd9Sstevel@tonic-gate	done
2727c478bd9Sstevel@tonic-gate	return 0
2737c478bd9Sstevel@tonic-gate}
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate#
2767c478bd9Sstevel@tonic-gate# mounted $path $fsopts $fstype
2777c478bd9Sstevel@tonic-gate#
2787c478bd9Sstevel@tonic-gate#   Check whether the specified file system of the given type is currently
2797c478bd9Sstevel@tonic-gate#   mounted with all required filesystem options by going through /etc/mnttab
2807c478bd9Sstevel@tonic-gate#   in our standard input.
2817c478bd9Sstevel@tonic-gate#
2827c478bd9Sstevel@tonic-gate#   Return values:
2837c478bd9Sstevel@tonic-gate#   0	Success.
2847c478bd9Sstevel@tonic-gate#   1	The filesystem is not currently mounted, or mounted without required
2857c478bd9Sstevel@tonic-gate#	options, or a filesystem of a different type is mounted instead.
2867c478bd9Sstevel@tonic-gate#
2877c478bd9Sstevel@tonic-gatemounted() {
2887c478bd9Sstevel@tonic-gate	path="$1"
2897c478bd9Sstevel@tonic-gate	fsopts="$2"
2907c478bd9Sstevel@tonic-gate	fstype="$3"
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate	while read mntspec mntpath mnttype mntopts on; do
2937c478bd9Sstevel@tonic-gate		[ "$mntpath" = "$path" ] || continue
2947c478bd9Sstevel@tonic-gate		[ "$fstype" != "-" ] && [ "$mnttype" != "$fstype" ] && return 1
2957c478bd9Sstevel@tonic-gate		[ "$fsopts" = "-" ] && return 0
2967c478bd9Sstevel@tonic-gate		hasopts $fsopts $mntopts && return 0
2977c478bd9Sstevel@tonic-gate	done
2987c478bd9Sstevel@tonic-gate	return 1
2997c478bd9Sstevel@tonic-gate}
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate#
3027c478bd9Sstevel@tonic-gate# mountfs $opts $path $type $fsopts $special
3037c478bd9Sstevel@tonic-gate#
3047c478bd9Sstevel@tonic-gate#   Try to mount a filesystem.  If failed, display our standard error
3057c478bd9Sstevel@tonic-gate#   message on the console and print more details about what happened
3067c478bd9Sstevel@tonic-gate#   to our service log.
3077c478bd9Sstevel@tonic-gate#
3087c478bd9Sstevel@tonic-gate# Arguments:
309*bbf21555SRichard Lowe#   $opts	- options for mount(8)				[optional]
3107c478bd9Sstevel@tonic-gate#   $path	- mount point
3117c478bd9Sstevel@tonic-gate#   $type	- file system type				[optional]
3127c478bd9Sstevel@tonic-gate#   $fsopts	- file system specific options (-o)		[optional]
3137c478bd9Sstevel@tonic-gate#   $special	- device on which the file system resides	[optional]
3147c478bd9Sstevel@tonic-gate#
3157c478bd9Sstevel@tonic-gate# Return codes:
3167c478bd9Sstevel@tonic-gate#   0		- success.
317*bbf21555SRichard Lowe#   otherwise	- error code returned by mount(8).
3187c478bd9Sstevel@tonic-gate#
3197c478bd9Sstevel@tonic-gatemountfs() {
3207c478bd9Sstevel@tonic-gate	opts="$1"
3217c478bd9Sstevel@tonic-gate	path="$2"
3227c478bd9Sstevel@tonic-gate	special="$5"
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate	#
3257c478bd9Sstevel@tonic-gate	# Take care of optional arguments
3267c478bd9Sstevel@tonic-gate	#
3277c478bd9Sstevel@tonic-gate	[ "$opts" = "-" ] && opts=""
3287c478bd9Sstevel@tonic-gate	[ "$special" = "-" ] &&	special=""
3297c478bd9Sstevel@tonic-gate	[ "$3" = "-" ] && type=""
3307c478bd9Sstevel@tonic-gate	[ "$3" != "-" ] && type="-F $3"
3317c478bd9Sstevel@tonic-gate	[ "$4" = "-" ] && fsopts=""
3327c478bd9Sstevel@tonic-gate	[ "$4" != "-" ] && fsopts="-o $4"
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate	cmd="/sbin/mount $opts $type $fsopts $special $path"
3357c478bd9Sstevel@tonic-gate	msg=`$cmd 2>&1`
3367c478bd9Sstevel@tonic-gate	err=$?
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate	[ $err = 0 ] && return 0
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate	#
3417c478bd9Sstevel@tonic-gate	# If the specified file system is already mounted with all
3427c478bd9Sstevel@tonic-gate	# required options, and has the same filesystem type
3437c478bd9Sstevel@tonic-gate	# then ignore errors and return success
3447c478bd9Sstevel@tonic-gate	#
3457c478bd9Sstevel@tonic-gate	mounted $path $4 $3 < /etc/mnttab && return 0
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate	echo "ERROR: $SMF_FMRI failed to mount $path "\
3487c478bd9Sstevel@tonic-gate	     "(see 'svcs -x' for details)" > /dev/msglog
3497c478bd9Sstevel@tonic-gate	echo "ERROR: $cmd failed, err=$err"
3507c478bd9Sstevel@tonic-gate	echo $msg
3517c478bd9Sstevel@tonic-gate	return $err
3527c478bd9Sstevel@tonic-gate}
353