3 # Copyright (c) 2006 The FreeBSD Project
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # REQUIRE: mountcritremote
37 stop_cmd="mdconfig2_stop"
38 start_cmd="mdconfig2_start"
39 start_precmd='[ -n "${_mdconfig2_list}" ]'
40 required_modules="geom_md:g_md"
47 _ret=`mount | while read _line; do
49 *" ${_mp} "*read-only*)
58 if [ -n "${_ret}" ]; then
73 eval _config=\$mdconfig_${_md}
74 eval _owner=\$mdconfig_${_md}_owner
75 eval _perms=\$mdconfig_${_md}_perms
76 eval _files=\$mdconfig_${_md}_files
77 eval _populate=\$mdconfig_${_md}_cmd
79 _type=${_config##*-t\ }
81 if [ -z "${_type}" ]; then
82 err 1 "You need to specify \"-t <type>\" in mdconfig_${_md}"
85 if [ "${_type}" = "vnode" ]; then
86 _file=${_config##*-f\ }
88 if [ -z "${_file}" ]; then
89 err 2 "You need to specify \"-f <file>\" in mdconfig_${_md} for vnode devices"
92 if [ "${_file}" != "${_file%.uzip}" ]; then
93 _dev="/dev/${_md}.uzip"
95 for _i in `df ${_file} 2>/dev/null`; do _fs=${_i}; done
99 debug "${_md} config: ${_config}"
100 debug "${_md} type: ${_type}"
101 debug "${_md} dev: ${_dev}"
102 debug "${_md} file: ${_file}"
103 debug "${_md} fs: ${_fs}"
104 debug "${_md} owner: ${_owner}"
105 debug "${_md} perms: ${_perms}"
106 debug "${_md} files: ${_files}"
107 debug "${_md} populate cmd: ${_populate}"
112 local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate _fsck_cmd _i
114 for _md in ${_mdconfig2_list}; do
115 init_variables ${_md}
116 if [ ! -r ${_file} ]; then
117 err 3 "${_file} doesn't exist"
120 # First pass: create md(4) vnode devices from files stored on
121 # non-root partition. Swap and malloc md(4) devices have already
123 if [ "${_type}" = "vnode" -a "${_fs}" != "/" ]; then
124 if [ "${_file}" != "${_file%.uzip}" ]; then
125 load_kld -m g_uzip geom_uzip || return 3
127 if is_readonly ${_fs}; then
128 warn "${_fs} is mounted read-only, skipping ${_md}."
131 if mdconfig -l -u ${_md} >/dev/null 2>&1; then
132 err 3 "${_md} already exists"
134 echo "Creating ${_md} device (${_type})."
135 if ! mdconfig -a ${_config} -u ${_md}; then
136 echo "Creating ${_md} device failed, moving on."
139 # Skip fsck for uzip devices.
140 if [ "${_file}" != "${_file%.uzip}" ]; then
142 elif checkyesno background_fsck; then
147 if ! eval ${_fsck_cmd} -p ${_dev} >/dev/null; then
148 echo "Fsck failed on ${_dev}, not mounting the filesystem."
151 if mount -d ${_dev} >/dev/null 2>&1; then
152 echo "Mounting ${_dev}."
157 for _i in `df ${_dev} 2>/dev/null`; do _mp=${_i}; done
158 if [ ! -z "${_mp}" -a "${_mp}" = "${_mp%%%}" ]; then
162 if checkyesno _mounted; then
163 # Second pass: change permissions and ownership.
164 [ -z "${_owner}" ] || chown -f ${_owner} ${_dev} ${_mp}
165 [ -z "${_perms}" ] || chmod -f ${_perms} ${_dev} ${_mp}
167 # Third pass: populate with foreign files.
168 if [ -n "${_files}" -o -n "${_populate}" ]; then
169 echo "Populating ${_dev}."
171 if [ -n "${_files}" ]; then
172 cp -Rp ${_files} ${_mp}
174 if [ -n "${_populate}" ]; then
183 local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate
185 for _md in ${_mdconfig2_list}; do
186 init_variables ${_md}
187 if [ "${_type}" = "vnode" ]; then
188 for i in `df ${_dev} 2>/dev/null`; do _mp=$i; done
189 if [ ! -r "${_file}" -o "${_fs}" = "/" ]; then
192 if [ -z "${_mp}" -o "${_mp}" != "${_mp%%%}" ]; then
193 echo "Device ${_dev} isn't mounted."
195 echo "Umounting ${_dev}."
198 if mdconfig -l -u ${_md} >/dev/null 2>&1; then
199 echo "Destroying ${_md}."
200 mdconfig -d -u ${_md}
207 if [ $# -gt 0 ]; then
210 [ -n "$*" ] && _mdconfig2_list="$*"
214 if [ -z "${_mdconfig2_list}" ]; then
215 for _mdconfig2_config in `list_vars mdconfig_md[0-9]\* |
218 _mdconfig2_unit=${_mdconfig2_config#mdconfig_md}
219 _mdconfig2_list="$_mdconfig2_list md$_mdconfig2_unit"
221 _mdconfig2_list="${_mdconfig2_list# }"
224 run_rc_command "${_mdconfig2_cmd}"