mdconfig2 revision 298514
1158722Sflz#!/bin/sh 2158722Sflz# 3158722Sflz# Copyright (c) 2006 The FreeBSD Project 4158722Sflz# All rights reserved. 5158722Sflz# 6158722Sflz# Redistribution and use in source and binary forms, with or without 7158722Sflz# modification, are permitted provided that the following conditions 8158722Sflz# are met: 9158722Sflz# 1. Redistributions of source code must retain the above copyright 10158722Sflz# notice, this list of conditions and the following disclaimer. 11158722Sflz# 2. Redistributions in binary form must reproduce the above copyright 12158722Sflz# notice, this list of conditions and the following disclaimer in the 13158722Sflz# documentation and/or other materials provided with the distribution. 14158722Sflz# 15158722Sflz# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16158722Sflz# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17158722Sflz# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18158722Sflz# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19158722Sflz# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20158722Sflz# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21158722Sflz# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22158722Sflz# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23158722Sflz# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24158722Sflz# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25158722Sflz# SUCH DAMAGE. 26158722Sflz# 27158722Sflz# $FreeBSD: head/etc/rc.d/mdconfig2 298514 2016-04-23 16:10:54Z lme $ 28158722Sflz# 29158722Sflz 30158722Sflz# PROVIDE: mdconfig2 31158722Sflz# REQUIRE: mountcritremote 32158722Sflz# BEFORE: SERVERS 33158722Sflz 34158722Sflz. /etc/rc.subr 35158722Sflz 36158722Sflzname="mdconfig2" 37298514Slmedesc="Create and control memory disks" 38158722Sflzstop_cmd="mdconfig2_stop" 39158722Sflzstart_cmd="mdconfig2_start" 40165683Syarstart_precmd='[ -n "${_mdconfig2_list}" ]' 41165683Syarrequired_modules="geom_md:g_md" 42158722Sflz 43158722Sflzis_readonly() 44158722Sflz{ 45158722Sflz local _mp _ret 46158722Sflz 47158722Sflz _mp=$1 48158722Sflz _ret=`mount | while read _line; do 49158722Sflz case ${_line} in 50158722Sflz *" ${_mp} "*read-only*) 51158722Sflz echo "yes" 52158722Sflz ;; 53208060Sdougb 54158722Sflz *) 55158722Sflz ;; 56158722Sflz esac; 57158722Sflz done` 58158722Sflz 59158722Sflz if [ -n "${_ret}" ]; then 60158722Sflz return 0 61158722Sflz else 62158722Sflz return 1 63158722Sflz fi 64158722Sflz} 65158722Sflz 66158722Sflzinit_variables() 67158722Sflz{ 68158722Sflz local _i 69158722Sflz 70158722Sflz _fs="" 71158722Sflz _mp="" 72158722Sflz _mounted="no" 73158722Sflz _dev="/dev/${_md}" 74158722Sflz eval _config=\$mdconfig_${_md} 75158722Sflz eval _owner=\$mdconfig_${_md}_owner 76158722Sflz eval _perms=\$mdconfig_${_md}_perms 77158722Sflz eval _files=\$mdconfig_${_md}_files 78158722Sflz eval _populate=\$mdconfig_${_md}_cmd 79158722Sflz 80158722Sflz _type=${_config##*-t\ } 81158722Sflz _type=${_type%%\ *} 82158722Sflz if [ -z "${_type}" ]; then 83158722Sflz err 1 "You need to specify \"-t <type>\" in mdconfig_${_md}" 84158722Sflz fi 85158722Sflz 86158722Sflz if [ "${_type}" = "vnode" ]; then 87158722Sflz _file=${_config##*-f\ } 88158722Sflz _file=${_file%%\ *} 89158722Sflz if [ -z "${_file}" ]; then 90158722Sflz err 2 "You need to specify \"-f <file>\" in mdconfig_${_md} for vnode devices" 91158722Sflz fi 92158722Sflz 93158722Sflz if [ "${_file}" != "${_file%.uzip}" ]; then 94158722Sflz _dev="/dev/${_md}.uzip" 95158722Sflz fi 96158722Sflz for _i in `df ${_file} 2>/dev/null`; do _fs=${_i}; done 97158722Sflz fi 98158722Sflz 99158722Sflz # Debugging help. 100158722Sflz debug "${_md} config: ${_config}" 101158722Sflz debug "${_md} type: ${_type}" 102158722Sflz debug "${_md} dev: ${_dev}" 103158722Sflz debug "${_md} file: ${_file}" 104158722Sflz debug "${_md} fs: ${_fs}" 105158722Sflz debug "${_md} owner: ${_owner}" 106158722Sflz debug "${_md} perms: ${_perms}" 107158722Sflz debug "${_md} files: ${_files}" 108158722Sflz debug "${_md} populate cmd: ${_populate}" 109158722Sflz} 110158722Sflz 111158722Sflzmdconfig2_start() 112158722Sflz{ 113158722Sflz local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate _fsck_cmd _i 114158722Sflz 115158722Sflz for _md in ${_mdconfig2_list}; do 116158722Sflz init_variables ${_md} 117158722Sflz if [ ! -r ${_file} ]; then 118158722Sflz err 3 "${_file} doesn't exist" 119158722Sflz continue 120158722Sflz fi 121158722Sflz # First pass: create md(4) vnode devices from files stored on 122158722Sflz # non-root partition. Swap and malloc md(4) devices have already 123158722Sflz # been created. 124158722Sflz if [ "${_type}" = "vnode" -a "${_fs}" != "/" ]; then 125165683Syar if [ "${_file}" != "${_file%.uzip}" ]; then 126165683Syar load_kld -m g_uzip geom_uzip || return 3 127165683Syar fi 128158722Sflz if is_readonly ${_fs}; then 129158722Sflz warn "${_fs} is mounted read-only, skipping ${_md}." 130158722Sflz continue 131158722Sflz fi 132158722Sflz if mdconfig -l -u ${_md} >/dev/null 2>&1; then 133158722Sflz err 3 "${_md} already exists" 134158722Sflz fi 135158722Sflz echo "Creating ${_md} device (${_type})." 136158722Sflz if ! mdconfig -a ${_config} -u ${_md}; then 137158722Sflz echo "Creating ${_md} device failed, moving on." 138158722Sflz continue 139158722Sflz fi 140158722Sflz # Skip fsck for uzip devices. 141158722Sflz if [ "${_file}" != "${_file%.uzip}" ]; then 142158722Sflz _fsck_cmd=":" 143158722Sflz elif checkyesno background_fsck; then 144158722Sflz _fsck_cmd="fsck -F" 145158722Sflz else 146158722Sflz _fsck_cmd="fsck" 147158722Sflz fi 148158722Sflz if ! eval ${_fsck_cmd} -p ${_dev} >/dev/null; then 149158722Sflz echo "Fsck failed on ${_dev}, not mounting the filesystem." 150158722Sflz continue 151158722Sflz fi 152158722Sflz if mount -d ${_dev} >/dev/null 2>&1; then 153158722Sflz echo "Mounting ${_dev}." 154158722Sflz mount ${_dev} 155158722Sflz fi 156158722Sflz fi 157158722Sflz 158158722Sflz for _i in `df ${_dev} 2>/dev/null`; do _mp=${_i}; done 159158722Sflz if [ ! -z "${_mp}" -a "${_mp}" = "${_mp%%%}" ]; then 160158722Sflz _mounted="yes" 161158722Sflz fi 162158722Sflz 163158722Sflz if checkyesno _mounted; then 164158722Sflz # Second pass: change permissions and ownership. 165158722Sflz [ -z "${_owner}" ] || chown -f ${_owner} ${_dev} ${_mp} 166158722Sflz [ -z "${_perms}" ] || chmod -f ${_perms} ${_dev} ${_mp} 167158722Sflz 168158722Sflz # Third pass: populate with foreign files. 169158722Sflz if [ -n "${_files}" -o -n "${_populate}" ]; then 170158722Sflz echo "Populating ${_dev}." 171158722Sflz fi 172158722Sflz if [ -n "${_files}" ]; then 173158722Sflz cp -Rp ${_files} ${_mp} 174158722Sflz fi 175158722Sflz if [ -n "${_populate}" ]; then 176158722Sflz eval ${_populate} 177158722Sflz fi 178158722Sflz fi 179158722Sflz done 180158722Sflz} 181158722Sflz 182158722Sflzmdconfig2_stop() 183158722Sflz{ 184158722Sflz local _md _fs _mp _mounted _dev _config _type _file _owner _perms _files _populate 185158722Sflz 186158722Sflz for _md in ${_mdconfig2_list}; do 187158722Sflz init_variables ${_md} 188158722Sflz if [ "${_type}" = "vnode" ]; then 189158722Sflz for i in `df ${_dev} 2>/dev/null`; do _mp=$i; done 190158722Sflz if [ ! -r "${_file}" -o "${_fs}" = "/" ]; then 191158722Sflz continue 192158722Sflz fi 193158722Sflz if [ -z "${_mp}" -o "${_mp}" != "${_mp%%%}" ]; then 194158722Sflz echo "Device ${_dev} isn't mounted." 195158722Sflz else 196158722Sflz echo "Umounting ${_dev}." 197158722Sflz umount ${_dev} 198158722Sflz fi 199158722Sflz if mdconfig -l -u ${_md} >/dev/null 2>&1; then 200158722Sflz echo "Destroying ${_md}." 201158722Sflz mdconfig -d -u ${_md} 202158722Sflz fi 203158722Sflz fi 204158722Sflz done 205158722Sflz} 206158722Sflz 207158722Sflz_mdconfig2_cmd="$1" 208158722Sflzif [ $# -gt 0 ]; then 209158722Sflz shift 210158722Sflzfi 211158722Sflz[ -n "$*" ] && _mdconfig2_list="$*" 212158722Sflz 213158722Sflzload_rc_config $name 214158722Sflz 215158722Sflzif [ -z "${_mdconfig2_list}" ]; then 216264243Sdteske for _mdconfig2_config in `list_vars mdconfig_md[0-9]\* | 217264243Sdteske sort_lite -nk1.12` 218264243Sdteske do 219264243Sdteske _mdconfig2_unit=${_mdconfig2_config#mdconfig_md} 220290163Sdteske [ "${_mdconfig2_unit#*[!0-9]}" = "$_mdconfig2_unit" ] || 221290163Sdteske continue 222264243Sdteske _mdconfig2_list="$_mdconfig2_list md$_mdconfig2_unit" 223158722Sflz done 224264243Sdteske _mdconfig2_list="${_mdconfig2_list# }" 225158722Sflzfi 226208060Sdougb 227158722Sflzrun_rc_command "${_mdconfig2_cmd}" 228