mdconfig2 revision 160669
1#!/bin/sh 2# 3# Copyright (c) 2006 The FreeBSD Project 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 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. 14# 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 25# SUCH DAMAGE. 26# 27# $FreeBSD: head/etc/rc.d/mdconfig2 160669 2006-07-25 17:19:00Z pjd $ 28# 29 30# PROVIDE: mdconfig2 31# REQUIRE: mountcritremote 32# BEFORE: SERVERS 33 34. /etc/rc.subr 35 36name="mdconfig2" 37stop_cmd="mdconfig2_stop" 38start_cmd="mdconfig2_start" 39 40is_readonly() 41{ 42 local _mp _ret 43 44 _mp=$1 45 _ret=`mount | while read _line; do 46 case ${_line} in 47 *" ${_mp} "*read-only*) 48 echo "yes" 49 ;; 50 51 *) 52 ;; 53 esac; 54 done` 55 56 if [ -n "${_ret}" ]; then 57 return 0 58 else 59 return 1 60 fi 61} 62 63init_variables() 64{ 65 local _i 66 67 _fs="" 68 _mp="" 69 _mounted="no" 70 _dev="/dev/${_md}" 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 76 77 _type=${_config##*-t\ } 78 _type=${_type%%\ *} 79 if [ -z "${_type}" ]; then 80 err 1 "You need to specify \"-t <type>\" in mdconfig_${_md}" 81 fi 82 83 if [ "${_type}" = "vnode" ]; then 84 _file=${_config##*-f\ } 85 _file=${_file%%\ *} 86 if [ -z "${_file}" ]; then 87 err 2 "You need to specify \"-f <file>\" in mdconfig_${_md} for vnode devices" 88 fi 89 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" 94 fi 95 for _i in `df ${_file} 2>/dev/null`; do _fs=${_i}; done 96 fi 97 98 # Debugging help. 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}" 108} 109 110mdconfig2_start() 111{ 112 local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate _fsck_cmd _i 113 114 # If there are no devices return before loading geom_md.ko. 115 if [ -z "${_mdconfig2_list}" ]; then 116 return 117 fi 118 119 kldstat -q -m g_md || kldload geom_md || err 1 "geom_md failed to load." 120 121 for _md in ${_mdconfig2_list}; do 122 init_variables ${_md} 123 if [ ! -r ${_file} ]; then 124 err 3 "${_file} doesn't exist" 125 continue 126 fi 127 # First pass: create md(4) vnode devices from files stored on 128 # non-root partition. Swap and malloc md(4) devices have already 129 # been created. 130 if [ "${_type}" = "vnode" -a "${_fs}" != "/" ]; then 131 if is_readonly ${_fs}; then 132 warn "${_fs} is mounted read-only, skipping ${_md}." 133 continue 134 fi 135 if mdconfig -l -u ${_md} >/dev/null 2>&1; then 136 err 3 "${_md} already exists" 137 fi 138 echo "Creating ${_md} device (${_type})." 139 if ! mdconfig -a ${_config} -u ${_md}; then 140 echo "Creating ${_md} device failed, moving on." 141 continue 142 fi 143 # Skip fsck for uzip devices. 144 if [ "${_file}" != "${_file%.uzip}" ]; then 145 _fsck_cmd=":" 146 elif checkyesno background_fsck; then 147 _fsck_cmd="fsck -F" 148 else 149 _fsck_cmd="fsck" 150 fi 151 if ! eval ${_fsck_cmd} -p ${_dev} >/dev/null; then 152 echo "Fsck failed on ${_dev}, not mounting the filesystem." 153 continue 154 fi 155 if mount -d ${_dev} >/dev/null 2>&1; then 156 echo "Mounting ${_dev}." 157 mount ${_dev} 158 fi 159 fi 160 161 for _i in `df ${_dev} 2>/dev/null`; do _mp=${_i}; done 162 if [ ! -z "${_mp}" -a "${_mp}" = "${_mp%%%}" ]; then 163 _mounted="yes" 164 fi 165 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} 170 171 # Third pass: populate with foreign files. 172 if [ -n "${_files}" -o -n "${_populate}" ]; then 173 echo "Populating ${_dev}." 174 fi 175 if [ -n "${_files}" ]; then 176 cp -Rp ${_files} ${_mp} 177 fi 178 if [ -n "${_populate}" ]; then 179 eval ${_populate} 180 fi 181 fi 182 done 183} 184 185mdconfig2_stop() 186{ 187 local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate 188 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 194 continue 195 fi 196 if [ -z "${_mp}" -o "${_mp}" != "${_mp%%%}" ]; then 197 echo "Device ${_dev} isn't mounted." 198 else 199 echo "Umounting ${_dev}." 200 umount ${_dev} 201 fi 202 if mdconfig -l -u ${_md} >/dev/null 2>&1; then 203 echo "Destroying ${_md}." 204 mdconfig -d -u ${_md} 205 fi 206 fi 207 done 208} 209 210_mdconfig2_cmd="$1" 211if [ $# -gt 0 ]; then 212 shift 213fi 214[ -n "$*" ] && _mdconfig2_list="$*" 215 216load_rc_config $name 217 218_mdconfig2_unit=0 219if [ -z "${_mdconfig2_list}" ]; then 220 while :; do 221 eval _mdconfig2_config=\$mdconfig_md${_mdconfig2_unit} 222 if [ -z "${_mdconfig2_config}" ]; then 223 break 224 else 225 _mdconfig2_list="${_mdconfig2_list}${_mdconfig2_list:+ }md${_mdconfig2_unit}" 226 _mdconfig2_unit=$((${_mdconfig2_unit} + 1)) 227 fi 228 done 229fi 230 231run_rc_command "${_mdconfig2_cmd}" 232