mdconfig2 revision 158722
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 158722 2006-05-18 15:29:27Z flz $ 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 require_kld geom_uzip 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 require_kld g_md 115 116 for _md in ${_mdconfig2_list}; do 117 init_variables ${_md} 118 if [ ! -r ${_file} ]; then 119 err 3 "${_file} doesn't exist" 120 continue 121 fi 122 # First pass: create md(4) vnode devices from files stored on 123 # non-root partition. Swap and malloc md(4) devices have already 124 # been created. 125 if [ "${_type}" = "vnode" -a "${_fs}" != "/" ]; then 126 if is_readonly ${_fs}; then 127 warn "${_fs} is mounted read-only, skipping ${_md}." 128 continue 129 fi 130 if mdconfig -l -u ${_md} >/dev/null 2>&1; then 131 err 3 "${_md} already exists" 132 fi 133 echo "Creating ${_md} device (${_type})." 134 if ! mdconfig -a ${_config} -u ${_md}; then 135 echo "Creating ${_md} device failed, moving on." 136 continue 137 fi 138 # Skip fsck for uzip devices. 139 if [ "${_file}" != "${_file%.uzip}" ]; then 140 _fsck_cmd=":" 141 elif checkyesno background_fsck; then 142 _fsck_cmd="fsck -F" 143 else 144 _fsck_cmd="fsck" 145 fi 146 if ! eval ${_fsck_cmd} -p ${_dev} >/dev/null; then 147 echo "Fsck failed on ${_dev}, not mounting the filesystem." 148 continue 149 fi 150 if mount -d ${_dev} >/dev/null 2>&1; then 151 echo "Mounting ${_dev}." 152 mount ${_dev} 153 fi 154 fi 155 156 for _i in `df ${_dev} 2>/dev/null`; do _mp=${_i}; done 157 if [ ! -z "${_mp}" -a "${_mp}" = "${_mp%%%}" ]; then 158 _mounted="yes" 159 fi 160 161 if checkyesno _mounted; then 162 # Second pass: change permissions and ownership. 163 [ -z "${_owner}" ] || chown -f ${_owner} ${_dev} ${_mp} 164 [ -z "${_perms}" ] || chmod -f ${_perms} ${_dev} ${_mp} 165 166 # Third pass: populate with foreign files. 167 if [ -n "${_files}" -o -n "${_populate}" ]; then 168 echo "Populating ${_dev}." 169 fi 170 if [ -n "${_files}" ]; then 171 cp -Rp ${_files} ${_mp} 172 fi 173 if [ -n "${_populate}" ]; then 174 eval ${_populate} 175 fi 176 fi 177 done 178} 179 180mdconfig2_stop() 181{ 182 local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate 183 184 for _md in ${_mdconfig2_list}; do 185 init_variables ${_md} 186 if [ "${_type}" = "vnode" ]; then 187 for i in `df ${_dev} 2>/dev/null`; do _mp=$i; done 188 if [ ! -r "${_file}" -o "${_fs}" = "/" ]; then 189 continue 190 fi 191 if [ -z "${_mp}" -o "${_mp}" != "${_mp%%%}" ]; then 192 echo "Device ${_dev} isn't mounted." 193 else 194 echo "Umounting ${_dev}." 195 umount ${_dev} 196 fi 197 if mdconfig -l -u ${_md} >/dev/null 2>&1; then 198 echo "Destroying ${_md}." 199 mdconfig -d -u ${_md} 200 fi 201 fi 202 done 203} 204 205_mdconfig2_cmd="$1" 206if [ $# -gt 0 ]; then 207 shift 208fi 209[ -n "$*" ] && _mdconfig2_list="$*" 210 211load_rc_config $name 212 213_mdconfig2_unit=0 214if [ -z "${_mdconfig2_list}" ]; then 215 while :; do 216 eval _mdconfig2_config=\$mdconfig_md${_mdconfig2_unit} 217 if [ -z "${_mdconfig2_config}" ]; then 218 break 219 else 220 _mdconfig2_list="${_mdconfig2_list}${_mdconfig2_list:+ }md${_mdconfig2_unit}" 221 _mdconfig2_unit=$((${_mdconfig2_unit} + 1)) 222 fi 223 done 224fi 225 226run_rc_command "${_mdconfig2_cmd}" 227