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$
28158722Sflz#
29158722Sflz
30158722Sflz# PROVIDE: mdconfig2
31158722Sflz# REQUIRE: mountcritremote
32158722Sflz# BEFORE: SERVERS
33158722Sflz
34158722Sflz. /etc/rc.subr
35158722Sflz
36158722Sflzname="mdconfig2"
37158722Sflzstop_cmd="mdconfig2_stop"
38158722Sflzstart_cmd="mdconfig2_start"
39165683Syarstart_precmd='[ -n "${_mdconfig2_list}" ]'
40165683Syarrequired_modules="geom_md:g_md"
41158722Sflz
42158722Sflzis_readonly()
43158722Sflz{
44158722Sflz	local _mp _ret
45158722Sflz
46158722Sflz	_mp=$1
47158722Sflz	_ret=`mount | while read _line; do
48158722Sflz		case ${_line} in
49158722Sflz		*" ${_mp} "*read-only*)
50158722Sflz			echo "yes"
51158722Sflz			;;
52208060Sdougb
53158722Sflz		*)
54158722Sflz			;;
55158722Sflz		esac;
56158722Sflz	done`
57158722Sflz
58158722Sflz	if [ -n "${_ret}" ]; then
59158722Sflz		return 0
60158722Sflz	else
61158722Sflz		return 1
62158722Sflz	fi
63158722Sflz}
64158722Sflz
65158722Sflzinit_variables()
66158722Sflz{
67158722Sflz	local _i
68158722Sflz
69158722Sflz	_fs=""
70158722Sflz	_mp=""
71158722Sflz	_mounted="no"
72158722Sflz	_dev="/dev/${_md}"
73158722Sflz	eval _config=\$mdconfig_${_md}
74158722Sflz	eval _owner=\$mdconfig_${_md}_owner
75158722Sflz	eval _perms=\$mdconfig_${_md}_perms
76158722Sflz	eval _files=\$mdconfig_${_md}_files
77158722Sflz	eval _populate=\$mdconfig_${_md}_cmd
78158722Sflz
79158722Sflz	_type=${_config##*-t\ }
80158722Sflz	_type=${_type%%\ *}
81158722Sflz	if [ -z "${_type}" ]; then
82158722Sflz		err 1 "You need to specify \"-t <type>\" in mdconfig_${_md}"
83158722Sflz	fi
84158722Sflz
85158722Sflz	if [ "${_type}" = "vnode" ]; then
86158722Sflz		_file=${_config##*-f\ }
87158722Sflz		_file=${_file%%\ *}
88158722Sflz		if [ -z "${_file}" ]; then
89158722Sflz			err 2 "You need to specify \"-f <file>\" in mdconfig_${_md} for vnode devices"
90158722Sflz		fi
91158722Sflz
92158722Sflz		if [ "${_file}" != "${_file%.uzip}" ]; then
93158722Sflz			_dev="/dev/${_md}.uzip"
94158722Sflz		fi
95158722Sflz		for _i in `df ${_file} 2>/dev/null`; do _fs=${_i}; done
96158722Sflz	fi
97158722Sflz
98158722Sflz	# Debugging help.
99158722Sflz	debug "${_md} config: ${_config}"
100158722Sflz	debug "${_md} type: ${_type}"
101158722Sflz	debug "${_md} dev: ${_dev}"
102158722Sflz	debug "${_md} file: ${_file}"
103158722Sflz	debug "${_md} fs: ${_fs}"
104158722Sflz	debug "${_md} owner: ${_owner}"
105158722Sflz	debug "${_md} perms: ${_perms}"
106158722Sflz	debug "${_md} files: ${_files}"
107158722Sflz	debug "${_md} populate cmd: ${_populate}"
108158722Sflz}
109158722Sflz
110158722Sflzmdconfig2_start()
111158722Sflz{
112158722Sflz	local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate _fsck_cmd _i
113158722Sflz
114158722Sflz	for _md in ${_mdconfig2_list}; do
115158722Sflz		init_variables ${_md}
116158722Sflz		if [ ! -r ${_file} ]; then
117158722Sflz			err 3 "${_file} doesn't exist"
118158722Sflz			continue
119158722Sflz		fi
120158722Sflz		# First pass: create md(4) vnode devices from files stored on
121158722Sflz		# non-root partition. Swap and malloc md(4) devices have already
122158722Sflz		# been created.
123158722Sflz		if [ "${_type}" = "vnode" -a "${_fs}" != "/" ]; then
124165683Syar			if [ "${_file}" != "${_file%.uzip}" ]; then
125165683Syar				load_kld -m g_uzip geom_uzip || return 3
126165683Syar			fi
127158722Sflz			if is_readonly ${_fs}; then
128158722Sflz				warn "${_fs} is mounted read-only, skipping ${_md}."
129158722Sflz				continue
130158722Sflz			fi
131158722Sflz			if mdconfig -l -u ${_md} >/dev/null 2>&1; then
132158722Sflz				err 3 "${_md} already exists"
133158722Sflz			fi
134158722Sflz			echo "Creating ${_md} device (${_type})."
135158722Sflz			if ! mdconfig -a ${_config} -u ${_md}; then
136158722Sflz				echo "Creating ${_md} device failed, moving on."
137158722Sflz				continue
138158722Sflz			fi
139158722Sflz			# Skip fsck for uzip devices.
140158722Sflz			if [ "${_file}" != "${_file%.uzip}" ]; then
141158722Sflz				_fsck_cmd=":"
142158722Sflz			elif checkyesno background_fsck; then
143158722Sflz				_fsck_cmd="fsck -F"
144158722Sflz			else
145158722Sflz				_fsck_cmd="fsck"
146158722Sflz			fi
147158722Sflz			if ! eval ${_fsck_cmd} -p ${_dev} >/dev/null; then
148158722Sflz				echo "Fsck failed on ${_dev}, not mounting the filesystem."
149158722Sflz				continue
150158722Sflz			fi
151158722Sflz			if mount -d ${_dev} >/dev/null 2>&1; then
152158722Sflz				echo "Mounting ${_dev}."
153158722Sflz				mount ${_dev}
154158722Sflz			fi
155158722Sflz		fi
156158722Sflz
157158722Sflz		for _i in `df ${_dev} 2>/dev/null`; do _mp=${_i}; done
158158722Sflz		if [ ! -z "${_mp}" -a "${_mp}" = "${_mp%%%}" ]; then
159158722Sflz			_mounted="yes"
160158722Sflz		fi
161158722Sflz
162158722Sflz		if checkyesno _mounted; then
163158722Sflz			# Second pass: change permissions and ownership.
164158722Sflz			[ -z "${_owner}" ] || chown -f ${_owner} ${_dev} ${_mp}
165158722Sflz			[ -z "${_perms}" ] || chmod -f ${_perms} ${_dev} ${_mp}
166158722Sflz
167158722Sflz			# Third pass: populate with foreign files.
168158722Sflz			if [ -n "${_files}" -o -n "${_populate}" ]; then
169158722Sflz				echo "Populating ${_dev}."
170158722Sflz			fi
171158722Sflz			if [ -n "${_files}" ]; then
172158722Sflz				cp -Rp ${_files} ${_mp}
173158722Sflz			fi
174158722Sflz			if [ -n "${_populate}" ]; then
175158722Sflz				eval ${_populate}
176158722Sflz			fi
177158722Sflz		fi
178158722Sflz	done
179158722Sflz}
180158722Sflz
181158722Sflzmdconfig2_stop()
182158722Sflz{
183158722Sflz	local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate
184158722Sflz
185158722Sflz	for _md in ${_mdconfig2_list}; do
186158722Sflz		init_variables ${_md}
187158722Sflz		if [ "${_type}" = "vnode" ]; then
188158722Sflz			for i in `df ${_dev} 2>/dev/null`; do _mp=$i; done
189158722Sflz			if [ ! -r "${_file}" -o "${_fs}" = "/" ]; then
190158722Sflz				continue
191158722Sflz			fi
192158722Sflz			if [ -z "${_mp}" -o "${_mp}" != "${_mp%%%}" ]; then
193158722Sflz				echo "Device ${_dev} isn't mounted."
194158722Sflz			else
195158722Sflz				echo "Umounting ${_dev}."
196158722Sflz				umount ${_dev}
197158722Sflz			fi
198158722Sflz			if mdconfig -l -u ${_md} >/dev/null 2>&1; then
199158722Sflz				echo "Destroying ${_md}."
200158722Sflz				mdconfig -d -u ${_md}
201158722Sflz			fi
202158722Sflz		fi
203158722Sflz	done
204158722Sflz}
205158722Sflz
206158722Sflz_mdconfig2_cmd="$1"
207158722Sflzif [ $# -gt 0 ]; then
208158722Sflz        shift
209158722Sflzfi
210158722Sflz[ -n "$*" ] && _mdconfig2_list="$*"
211158722Sflz
212158722Sflzload_rc_config $name
213158722Sflz
214158722Sflzif [ -z "${_mdconfig2_list}" ]; then
215264439Sdteske	for _mdconfig2_config in `list_vars mdconfig_md[0-9]\* |
216264439Sdteske		sort_lite -nk1.12`
217264439Sdteske	do
218264439Sdteske		_mdconfig2_unit=${_mdconfig2_config#mdconfig_md}
219264439Sdteske		_mdconfig2_list="$_mdconfig2_list md$_mdconfig2_unit"
220158722Sflz	done
221264439Sdteske	_mdconfig2_list="${_mdconfig2_list# }"
222158722Sflzfi
223208060Sdougb
224158722Sflzrun_rc_command "${_mdconfig2_cmd}"
225