mdconfig revision 158722
1158722Sflz#!/bin/sh
2158722Sflz#
3158722Sflz# Copyright (c) 2006  The FreeBSD Project
4158722Sflz# All rights reserved.
5158722Sflz#
6158722Sflz# Redistribution and use in source and binary forms, with or without
7158722Sflz# modification, are permitted provided that the following conditions
8158722Sflz# are met:
9158722Sflz# 1. Redistributions of source code must retain the above copyright
10158722Sflz#    notice, this list of conditions and the following disclaimer.
11158722Sflz# 2. Redistributions in binary form must reproduce the above copyright
12158722Sflz#    notice, this list of conditions and the following disclaimer in the
13158722Sflz#    documentation and/or other materials provided with the distribution.
14158722Sflz#
15158722Sflz# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16158722Sflz# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17158722Sflz# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18158722Sflz# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19158722Sflz# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20158722Sflz# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21158722Sflz# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22158722Sflz# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23158722Sflz# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24158722Sflz# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25158722Sflz# SUCH DAMAGE.
26158722Sflz#
27158722Sflz# $FreeBSD: head/etc/rc.d/mdconfig 158722 2006-05-18 15:29:27Z flz $
28158722Sflz#
29158722Sflz
30158722Sflz# PROVIDE: mdconfig
31158722Sflz# REQUIRE: localswap
32158722Sflz# BEFORE: mountcritlocal
33158722Sflz
34158722Sflz. /etc/rc.subr
35158722Sflz
36158722Sflzname="mdconfig"
37158722Sflzstop_cmd="mdconfig_stop"
38158722Sflzstart_cmd="mdconfig_start"
39158722Sflz
40158722Sflzis_readonly()
41158722Sflz{
42158722Sflz	local _mp _ret
43158722Sflz
44158722Sflz	_mp=$1
45158722Sflz	_ret=`mount | while read _line; do
46158722Sflz		case ${_line} in
47158722Sflz		*" ${_mp} "*read-only*)
48158722Sflz			echo "yes"
49158722Sflz			;;
50158722Sflz		
51158722Sflz		*)
52158722Sflz			;;
53158722Sflz		esac;
54158722Sflz	done`
55158722Sflz
56158722Sflz	if [ -n "${_ret}" ]; then
57158722Sflz		return 0
58158722Sflz	else
59158722Sflz		return 1
60158722Sflz	fi
61158722Sflz}
62158722Sflz
63158722Sflzinit_variables()
64158722Sflz{
65158722Sflz	local _i
66158722Sflz
67158722Sflz	_fs=""
68158722Sflz	_mp=""
69158722Sflz	_dev="/dev/${_md}"
70158722Sflz	eval _config=\$mdconfig_${_md}
71158722Sflz	eval _newfs=\$mdconfig_${_md}_newfs
72158722Sflz
73158722Sflz	_type=${_config##*-t\ }
74158722Sflz	_type=${_type%%\ *}
75158722Sflz	if [ -z "${_type}" ]; then
76158722Sflz		err 1 "You need to specify \"-t <type>\" in mdconfig_${_md}"
77158722Sflz	fi
78158722Sflz
79158722Sflz	if [ "${_type}" = "vnode" ]; then
80158722Sflz		_file=${_config##*-f\ }
81158722Sflz		_file=${_file%%\ *}
82158722Sflz		if [ -z "${_file}" ]; then
83158722Sflz			err 2 "You need to specify \"-f <file>\" in mdconfig_${_md} for vnode devices"
84158722Sflz		fi
85158722Sflz		if [ "${_file}" != "${_file%.uzip}" ]; then
86158722Sflz			# Load geom_uzip kernel module if needed
87158722Sflz			require_kld geom_uzip
88158722Sflz			_dev="/dev/${_md}.uzip"
89158722Sflz		fi
90158722Sflz		for _i in `df ${_file} 2>/dev/null`; do _fs=${_i}; done
91158722Sflz	fi
92158722Sflz
93158722Sflz	# Debugging help.
94158722Sflz	debug "${_md} config: ${_config}"
95158722Sflz	debug "${_md} type: ${_type}"
96158722Sflz	debug "${_md} dev: ${_dev}"
97158722Sflz	debug "${_md} file: ${_file}"
98158722Sflz	debug "${_md} fs: ${_fs}"
99158722Sflz	debug "${_md} newfs flags: ${_newfs}"
100158722Sflz}
101158722Sflz
102158722Sflzmdconfig_start()
103158722Sflz{
104158722Sflz	local _md _mp _config _type _dev _file _fs _newfs _fsck_cmd
105158722Sflz
106158722Sflz	require_kld g_md
107158722Sflz
108158722Sflz	for _md in ${_mdconfig_list}; do
109158722Sflz		init_variables ${_md}
110158722Sflz		# Create md(4) devices of types swap, malloc and vnode if the
111158722Sflz		# file is on the root partition.
112158722Sflz		if [ "${_type}" != "vnode" -o "${_fs}" = "/" ]; then
113158722Sflz			if [ "${_type}" = "vnode" ]; then
114158722Sflz				if is_readonly ${_fs}; then
115158722Sflz					warn "${_fs} is mounted read-only, skipping ${_md}."
116158722Sflz					continue
117158722Sflz				fi
118158722Sflz			fi
119158722Sflz			if mdconfig -l -u ${_md} >/dev/null 2>&1; then
120158722Sflz				err 3 "${_md} already exists"
121158722Sflz			fi
122158722Sflz			echo "Creating ${_md} device (${_type})."
123158722Sflz			if ! mdconfig -a ${_config} -u ${_md}; then
124158722Sflz				echo "Creating ${_md} device failed, moving on."
125158722Sflz				continue
126158722Sflz			fi
127158722Sflz			# Skip fsck for uzip devices.
128158722Sflz			if [ "${_type}" = "vnode" ]; then
129158722Sflz				if [ "${_file}" != "${_file%.uzip}" ]; then
130158722Sflz					_fsck_cmd=":"
131158722Sflz				elif checkyesno background_fsck; then
132158722Sflz					_fsck_cmd="fsck -F"
133158722Sflz				else
134158722Sflz					_fsck_cmd="fsck"
135158722Sflz				fi
136158722Sflz				if ! eval ${_fsck_cmd} -p ${_dev} >/dev/null; then
137158722Sflz					echo "Fsck failed on ${_dev}, not mounting the filesystem."
138158722Sflz					continue
139158722Sflz					
140158722Sflz				fi
141158722Sflz			else
142158722Sflz				newfs ${_newfs} ${_dev} >/dev/null
143158722Sflz			fi
144158722Sflz			if mount -d ${_dev} 2>&1 >/dev/null; then
145158722Sflz				echo "Mounting ${_dev}."
146158722Sflz				mount ${_dev}
147158722Sflz			fi
148158722Sflz		fi
149158722Sflz	done
150158722Sflz}
151158722Sflz
152158722Sflzmdconfig_stop()
153158722Sflz{
154158722Sflz	local _md _mp _config _type _dev _file _fs _newfs _i
155158722Sflz
156158722Sflz	for _md in ${_mdconfig_list}; do
157158722Sflz		init_variables ${_md}
158158722Sflz		if [ "${_type}" != "vnode" -o "${_fs}" = "/" ]; then
159158722Sflz			for _i in `df ${_dev} 2>/dev/null`; do _mp=${_i}; done
160158722Sflz			if [ -z "${_mp}" -o "${_mp}" != "${_mp%%%}" ]; then
161158722Sflz				echo "Device ${_dev} isn't mounted."
162158722Sflz			else
163158722Sflz				echo "Umounting ${_dev}."
164158722Sflz				umount ${_dev}
165158722Sflz			fi
166158722Sflz			if mdconfig -l -u ${_md} >/dev/null 2>&1; then
167158722Sflz				echo "Destroying ${_md}."
168158722Sflz				mdconfig -d -u ${_md}
169158722Sflz			fi
170158722Sflz		fi
171158722Sflz	done
172158722Sflz}
173158722Sflz
174158722Sflz_mdconfig_cmd="$1"
175158722Sflzif [ $# -gt 0 ]; then
176158722Sflz        shift
177158722Sflzfi
178158722Sflz[ -n "$*" ] && _mdconfig_list="$*"
179158722Sflz
180158722Sflzload_rc_config $name
181158722Sflz
182158722Sflz_mdconfig_unit=0
183158722Sflzif [ -z "${_mdconfig_list}" ]; then
184158722Sflz	while :; do
185158722Sflz		eval _mdconfig_config=\$mdconfig_md${_mdconfig_unit}
186158722Sflz		if [ -z "${_mdconfig_config}" ]; then
187158722Sflz			break
188158722Sflz		else
189158722Sflz			_mdconfig_list="${_mdconfig_list}${_mdconfig_list:+ }md${_mdconfig_unit}"
190158722Sflz			_mdconfig_unit=$((${_mdconfig_unit} + 1))
191158722Sflz		fi
192158722Sflz	done
193158722Sflzfi
194158722Sflz	
195158722Sflzrun_rc_command "${_mdconfig_cmd}"
196