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"
45 _ret=`mount | while read _line; do
47 *" ${_mp} "*read-only*)
56 if [ -n "${_ret}" ]; then
71 eval _config=\$mdconfig_${_md}
72 eval _owner=\$mdconfig_${_md}_owner
73 eval _perms=\$mdconfig_${_md}_perms
74 eval _files=\$mdconfig_${_md}_files
75 eval _populate=\$mdconfig_${_md}_cmd
77 _type=${_config##*-t\ }
79 if [ -z "${_type}" ]; then
80 err 1 "You need to specify \"-t <type>\" in mdconfig_${_md}"
83 if [ "${_type}" = "vnode" ]; then
84 _file=${_config##*-f\ }
86 if [ -z "${_file}" ]; then
87 err 2 "You need to specify \"-f <file>\" in mdconfig_${_md} for vnode devices"
90 if [ "${_file}" != "${_file%.uzip}" ]; then
91 # Load geom_uzip kernel module if needed
92 kldstat -q -m g_uzip || kldload geom_uzip || err 1 "geom_uzip failed to load."
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 # If there are no devices return before loading geom_md.ko.
115 if [ -z "${_mdconfig2_list}" ]; then
119 kldstat -q -m g_md || kldload geom_md || err 1 "geom_md failed to load."
121 for _md in ${_mdconfig2_list}; do
122 init_variables ${_md}
123 if [ ! -r ${_file} ]; then
124 err 3 "${_file} doesn't exist"
127 # First pass: create md(4) vnode devices from files stored on
128 # non-root partition. Swap and malloc md(4) devices have already
130 if [ "${_type}" = "vnode" -a "${_fs}" != "/" ]; then
131 if is_readonly ${_fs}; then
132 warn "${_fs} is mounted read-only, skipping ${_md}."
135 if mdconfig -l -u ${_md} >/dev/null 2>&1; then
136 err 3 "${_md} already exists"
138 echo "Creating ${_md} device (${_type})."
139 if ! mdconfig -a ${_config} -u ${_md}; then
140 echo "Creating ${_md} device failed, moving on."
143 # Skip fsck for uzip devices.
144 if [ "${_file}" != "${_file%.uzip}" ]; then
146 elif checkyesno background_fsck; then
151 if ! eval ${_fsck_cmd} -p ${_dev} >/dev/null; then
152 echo "Fsck failed on ${_dev}, not mounting the filesystem."
155 if mount -d ${_dev} >/dev/null 2>&1; then
156 echo "Mounting ${_dev}."
161 for _i in `df ${_dev} 2>/dev/null`; do _mp=${_i}; done
162 if [ ! -z "${_mp}" -a "${_mp}" = "${_mp%%%}" ]; then
166 if checkyesno _mounted; then
167 # Second pass: change permissions and ownership.
168 [ -z "${_owner}" ] || chown -f ${_owner} ${_dev} ${_mp}
169 [ -z "${_perms}" ] || chmod -f ${_perms} ${_dev} ${_mp}
171 # Third pass: populate with foreign files.
172 if [ -n "${_files}" -o -n "${_populate}" ]; then
173 echo "Populating ${_dev}."
175 if [ -n "${_files}" ]; then
176 cp -Rp ${_files} ${_mp}
178 if [ -n "${_populate}" ]; then
187 local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate
189 for _md in ${_mdconfig2_list}; do
190 init_variables ${_md}
191 if [ "${_type}" = "vnode" ]; then
192 for i in `df ${_dev} 2>/dev/null`; do _mp=$i; done
193 if [ ! -r "${_file}" -o "${_fs}" = "/" ]; then
196 if [ -z "${_mp}" -o "${_mp}" != "${_mp%%%}" ]; then
197 echo "Device ${_dev} isn't mounted."
199 echo "Umounting ${_dev}."
202 if mdconfig -l -u ${_md} >/dev/null 2>&1; then
203 echo "Destroying ${_md}."
204 mdconfig -d -u ${_md}
211 if [ $# -gt 0 ]; then
214 [ -n "$*" ] && _mdconfig2_list="$*"
219 if [ -z "${_mdconfig2_list}" ]; then
221 eval _mdconfig2_config=\$mdconfig_md${_mdconfig2_unit}
222 if [ -z "${_mdconfig2_config}" ]; then
225 _mdconfig2_list="${_mdconfig2_list}${_mdconfig2_list:+ }md${_mdconfig2_unit}"
226 _mdconfig2_unit=$((${_mdconfig2_unit} + 1))
231 run_rc_command "${_mdconfig2_cmd}"