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 desc="Create and control memory disks"
38 stop_cmd="mdconfig2_stop"
39 start_cmd="mdconfig2_start"
40 start_precmd='[ -n "${_mdconfig2_list}" ]'
41 required_modules="geom_md:g_md"
48 _ret=`mount | while read _line; do
50 *" ${_mp} "*read-only*)
59 if [ -n "${_ret}" ]; then
74 eval _config=\$mdconfig_${_md}
75 eval _owner=\$mdconfig_${_md}_owner
76 eval _perms=\$mdconfig_${_md}_perms
77 eval _files=\$mdconfig_${_md}_files
78 eval _populate=\$mdconfig_${_md}_cmd
80 _type=${_config##*-t\ }
82 if [ -z "${_type}" ]; then
83 err 1 "You need to specify \"-t <type>\" in mdconfig_${_md}"
86 if [ "${_type}" = "vnode" ]; then
87 _file=${_config##*-f\ }
89 if [ -z "${_file}" ]; then
90 err 2 "You need to specify \"-f <file>\" in mdconfig_${_md} for vnode devices"
93 if [ "${_file}" != "${_file%.uzip}" ]; then
94 _dev="/dev/${_md}.uzip"
96 for _i in `df ${_file} 2>/dev/null`; do _fs=${_i}; done
100 debug "${_md} config: ${_config}"
101 debug "${_md} type: ${_type}"
102 debug "${_md} dev: ${_dev}"
103 debug "${_md} file: ${_file}"
104 debug "${_md} fs: ${_fs}"
105 debug "${_md} owner: ${_owner}"
106 debug "${_md} perms: ${_perms}"
107 debug "${_md} files: ${_files}"
108 debug "${_md} populate cmd: ${_populate}"
113 local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate _fsck_cmd _i
115 for _md in ${_mdconfig2_list}; do
116 init_variables ${_md}
117 if [ ! -r ${_file} ]; then
118 err 3 "${_file} doesn't exist"
121 # First pass: create md(4) vnode devices from files stored on
122 # non-root partition. Swap and malloc md(4) devices have already
124 if [ "${_type}" = "vnode" -a "${_fs}" != "/" ]; then
125 if [ "${_file}" != "${_file%.uzip}" ]; then
126 load_kld -m g_uzip geom_uzip || return 3
128 if is_readonly ${_fs}; then
129 warn "${_fs} is mounted read-only, skipping ${_md}."
132 if mdconfig -l -u ${_md} >/dev/null 2>&1; then
133 err 3 "${_md} already exists"
135 echo "Creating ${_md} device (${_type})."
136 if ! mdconfig -a ${_config} -u ${_md}; then
137 echo "Creating ${_md} device failed, moving on."
140 # Skip fsck for uzip devices.
141 if [ "${_file}" != "${_file%.uzip}" ]; then
143 elif checkyesno background_fsck; then
148 if ! eval ${_fsck_cmd} -p ${_dev} >/dev/null; then
149 echo "Fsck failed on ${_dev}, not mounting the filesystem."
152 if mount -d ${_dev} >/dev/null 2>&1; then
153 echo "Mounting ${_dev}."
158 for _i in `df ${_dev} 2>/dev/null`; do _mp=${_i}; done
159 if [ ! -z "${_mp}" -a "${_mp}" = "${_mp%%%}" ]; then
163 if checkyesno _mounted; then
164 # Second pass: change permissions and ownership.
165 [ -z "${_owner}" ] || chown -f ${_owner} ${_dev} ${_mp}
166 [ -z "${_perms}" ] || chmod -f ${_perms} ${_dev} ${_mp}
168 # Third pass: populate with foreign files.
169 if [ -n "${_files}" -o -n "${_populate}" ]; then
170 echo "Populating ${_dev}."
172 if [ -n "${_files}" ]; then
173 cp -Rp ${_files} ${_mp}
175 if [ -n "${_populate}" ]; then
184 local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate
186 for _md in ${_mdconfig2_list}; do
187 init_variables ${_md}
188 if [ "${_type}" = "vnode" ]; then
189 for i in `df ${_dev} 2>/dev/null`; do _mp=$i; done
190 if [ ! -r "${_file}" -o "${_fs}" = "/" ]; then
193 if [ -z "${_mp}" -o "${_mp}" != "${_mp%%%}" ]; then
194 echo "Device ${_dev} isn't mounted."
196 echo "Umounting ${_dev}."
199 if mdconfig -l -u ${_md} >/dev/null 2>&1; then
200 echo "Destroying ${_md}."
201 mdconfig -d -u ${_md}
208 if [ $# -gt 0 ]; then
211 [ -n "$*" ] && _mdconfig2_list="$*"
215 if [ -z "${_mdconfig2_list}" ]; then
216 for _mdconfig2_config in `list_vars mdconfig_md[0-9]\* |
219 _mdconfig2_unit=${_mdconfig2_config#mdconfig_md}
220 [ "${_mdconfig2_unit#*[!0-9]}" = "$_mdconfig2_unit" ] ||
222 _mdconfig2_list="$_mdconfig2_list md$_mdconfig2_unit"
224 _mdconfig2_list="${_mdconfig2_list# }"
227 run_rc_command "${_mdconfig2_cmd}"