xref: /freebsd/libexec/rc/rc.d/mdconfig (revision f99f0ee14e3af81c23150a6a340259ca8a33d01a)
10696600cSBjoern A. Zeeb#!/bin/sh
20696600cSBjoern A. Zeeb#
30696600cSBjoern A. Zeeb# Copyright (c) 2006  The FreeBSD Project
40696600cSBjoern A. Zeeb# All rights reserved.
50696600cSBjoern A. Zeeb#
60696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
70696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
80696600cSBjoern A. Zeeb# are met:
90696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
100696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
110696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
120696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
130696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
140696600cSBjoern A. Zeeb#
150696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
160696600cSBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170696600cSBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
180696600cSBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
190696600cSBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
200696600cSBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
210696600cSBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
220696600cSBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
230696600cSBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
240696600cSBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250696600cSBjoern A. Zeeb# SUCH DAMAGE.
260696600cSBjoern A. Zeeb#
270696600cSBjoern A. Zeeb#
280696600cSBjoern A. Zeeb
290696600cSBjoern A. Zeeb# PROVIDE: mdconfig
300696600cSBjoern A. Zeeb# REQUIRE: swap root
310696600cSBjoern A. Zeeb
320696600cSBjoern A. Zeeb. /etc/rc.subr
330696600cSBjoern A. Zeeb
340696600cSBjoern A. Zeebname="mdconfig"
350696600cSBjoern A. Zeebdesc="Create and control memory disks"
360696600cSBjoern A. Zeebstop_cmd="mdconfig_stop"
370696600cSBjoern A. Zeebstart_cmd="mdconfig_start"
380696600cSBjoern A. Zeebstart_precmd='[ -n "${_mdconfig_list}" ]'
390696600cSBjoern A. Zeebrequired_modules="geom_md:g_md"
400696600cSBjoern A. Zeeb
410696600cSBjoern A. Zeebis_readonly()
420696600cSBjoern A. Zeeb{
430696600cSBjoern A. Zeeb	local _mp _ret
440696600cSBjoern A. Zeeb
450696600cSBjoern A. Zeeb	_mp=$1
460696600cSBjoern A. Zeeb	_ret=`mount | while read _line; do
470696600cSBjoern A. Zeeb		case ${_line} in
480696600cSBjoern A. Zeeb		*" ${_mp} "*read-only*)
490696600cSBjoern A. Zeeb			echo "yes"
500696600cSBjoern A. Zeeb			;;
510696600cSBjoern A. Zeeb
520696600cSBjoern A. Zeeb		*)
530696600cSBjoern A. Zeeb			;;
540696600cSBjoern A. Zeeb		esac;
550696600cSBjoern A. Zeeb	done`
560696600cSBjoern A. Zeeb
570696600cSBjoern A. Zeeb	if [ -n "${_ret}" ]; then
580696600cSBjoern A. Zeeb		return 0
590696600cSBjoern A. Zeeb	else
600696600cSBjoern A. Zeeb		return 1
610696600cSBjoern A. Zeeb	fi
620696600cSBjoern A. Zeeb}
630696600cSBjoern A. Zeeb
640696600cSBjoern A. Zeebinit_variables()
650696600cSBjoern A. Zeeb{
660696600cSBjoern A. Zeeb	local _i
670696600cSBjoern A. Zeeb
680696600cSBjoern A. Zeeb	_fs=""
690696600cSBjoern A. Zeeb	_mp=""
700696600cSBjoern A. Zeeb	_dev="/dev/${_md}"
710696600cSBjoern A. Zeeb	eval _config=\$mdconfig_${_md}
720696600cSBjoern A. Zeeb	eval _newfs=\$mdconfig_${_md}_newfs
730696600cSBjoern A. Zeeb
740696600cSBjoern A. Zeeb	_type=${_config##*-t\ }
750696600cSBjoern A. Zeeb	_type=${_type%%\ *}
760696600cSBjoern A. Zeeb	if [ -z "${_type}" ]; then
770696600cSBjoern A. Zeeb		err 1 "You need to specify \"-t <type>\" in mdconfig_${_md}"
780696600cSBjoern A. Zeeb	fi
790696600cSBjoern A. Zeeb
800696600cSBjoern A. Zeeb	if [ "${_type}" = "vnode" ]; then
810696600cSBjoern A. Zeeb		_file=${_config##*-f\ }
820696600cSBjoern A. Zeeb		_file=${_file%%\ *}
830696600cSBjoern A. Zeeb		if [ -z "${_file}" ]; then
840696600cSBjoern A. Zeeb			err 2 "You need to specify \"-f <file>\" in mdconfig_${_md} for vnode devices"
850696600cSBjoern A. Zeeb		fi
860696600cSBjoern A. Zeeb		if [ "${_file}" != "${_file%.uzip}" ]; then
870696600cSBjoern A. Zeeb			_dev="/dev/${_md}.uzip"
880696600cSBjoern A. Zeeb		fi
890696600cSBjoern A. Zeeb		for _i in `df ${_file} 2>/dev/null`; do _fs=${_i}; done
900696600cSBjoern A. Zeeb	fi
910696600cSBjoern A. Zeeb
920696600cSBjoern A. Zeeb	# Debugging help.
930696600cSBjoern A. Zeeb	debug "${_md} config: ${_config}"
940696600cSBjoern A. Zeeb	debug "${_md} type: ${_type}"
950696600cSBjoern A. Zeeb	debug "${_md} dev: ${_dev}"
960696600cSBjoern A. Zeeb	debug "${_md} file: ${_file}"
970696600cSBjoern A. Zeeb	debug "${_md} fs: ${_fs}"
980696600cSBjoern A. Zeeb	debug "${_md} newfs flags: ${_newfs}"
990696600cSBjoern A. Zeeb}
1000696600cSBjoern A. Zeeb
1010696600cSBjoern A. Zeebmdconfig_start()
1020696600cSBjoern A. Zeeb{
1030696600cSBjoern A. Zeeb	local _md _mp _config _type _dev _file _fs _newfs _fsck_cmd
1040696600cSBjoern A. Zeeb
1050696600cSBjoern A. Zeeb	for _md in ${_mdconfig_list}; do
1060696600cSBjoern A. Zeeb		init_variables ${_md}
1070696600cSBjoern A. Zeeb		# Create md(4) devices of types swap, malloc and vnode if the
1080696600cSBjoern A. Zeeb		# file is on the root partition.
1090696600cSBjoern A. Zeeb		if [ "${_type}" != "vnode" -o "${_fs}" = "/" ]; then
1100696600cSBjoern A. Zeeb			if [ "${_type}" = "vnode" ]; then
1110696600cSBjoern A. Zeeb				if is_readonly ${_fs}; then
1120696600cSBjoern A. Zeeb					warn "${_fs} is mounted read-only, skipping ${_md}."
1130696600cSBjoern A. Zeeb					continue
1140696600cSBjoern A. Zeeb				fi
1150696600cSBjoern A. Zeeb				if [ "${_file}" != "${_file%.uzip}" ]; then
116b11974c2SChris Rees					load_kld -m g_uzip geom_uzip || return 3
1170696600cSBjoern A. Zeeb					# sleep a bit to allow creation of /dev/mdX.uzip
1180696600cSBjoern A. Zeeb					sleep 2
1190696600cSBjoern A. Zeeb				fi
1200696600cSBjoern A. Zeeb			fi
1210696600cSBjoern A. Zeeb			if mdconfig -l -u ${_md} >/dev/null 2>&1; then
1220696600cSBjoern A. Zeeb				err 3 "${_md} already exists"
1230696600cSBjoern A. Zeeb			fi
1240696600cSBjoern A. Zeeb			echo "Creating ${_md} device (${_type})."
1250696600cSBjoern A. Zeeb			if ! mdconfig -a ${_config} -u ${_md}; then
1260696600cSBjoern A. Zeeb				echo "Creating ${_md} device failed, moving on."
1270696600cSBjoern A. Zeeb				continue
1280696600cSBjoern A. Zeeb			fi
1290696600cSBjoern A. Zeeb			# Skip fsck for uzip devices.
1300696600cSBjoern A. Zeeb			if [ "${_type}" = "vnode" ]; then
1310696600cSBjoern A. Zeeb				if [ "${_file}" != "${_file%.uzip}" ]; then
1320696600cSBjoern A. Zeeb					_fsck_cmd=":"
1330696600cSBjoern A. Zeeb				elif checkyesno background_fsck; then
1340696600cSBjoern A. Zeeb					_fsck_cmd="fsck -F"
1350696600cSBjoern A. Zeeb				else
1360696600cSBjoern A. Zeeb					_fsck_cmd="fsck"
1370696600cSBjoern A. Zeeb				fi
1380696600cSBjoern A. Zeeb				if ! eval ${_fsck_cmd} -p ${_dev} >/dev/null; then
1390696600cSBjoern A. Zeeb					echo "Fsck failed on ${_dev}, not mounting the filesystem."
1400696600cSBjoern A. Zeeb					continue
1410696600cSBjoern A. Zeeb
1420696600cSBjoern A. Zeeb				fi
1430696600cSBjoern A. Zeeb			else
1440696600cSBjoern A. Zeeb				newfs ${_newfs} ${_dev} >/dev/null
1450696600cSBjoern A. Zeeb			fi
1460696600cSBjoern A. Zeeb			if mount -d ${_dev} 2>&1 >/dev/null; then
1470696600cSBjoern A. Zeeb				echo "Mounting ${_dev}."
1480696600cSBjoern A. Zeeb				mount ${_dev}
1490696600cSBjoern A. Zeeb			fi
1500696600cSBjoern A. Zeeb		fi
1510696600cSBjoern A. Zeeb	done
1520696600cSBjoern A. Zeeb}
1530696600cSBjoern A. Zeeb
1540696600cSBjoern A. Zeebmdconfig_stop()
1550696600cSBjoern A. Zeeb{
1560696600cSBjoern A. Zeeb	local _md _mp _config _type _dev _file _fs _newfs _i
1570696600cSBjoern A. Zeeb
1580696600cSBjoern A. Zeeb	for _md in ${_mdconfig_list}; do
1590696600cSBjoern A. Zeeb		init_variables ${_md}
1600696600cSBjoern A. Zeeb		if [ "${_type}" != "vnode" -o "${_fs}" = "/" ]; then
1610696600cSBjoern A. Zeeb			for _i in `df ${_dev} 2>/dev/null`; do _mp=${_i}; done
1620696600cSBjoern A. Zeeb			if [ -z "${_mp}" -o "${_mp}" != "${_mp%%%}" ]; then
1630696600cSBjoern A. Zeeb				echo "Device ${_dev} isn't mounted."
1640696600cSBjoern A. Zeeb			else
1650696600cSBjoern A. Zeeb				echo "Umounting ${_dev}."
1660696600cSBjoern A. Zeeb				umount ${_dev}
1670696600cSBjoern A. Zeeb			fi
1680696600cSBjoern A. Zeeb			if mdconfig -l -u ${_md} >/dev/null 2>&1; then
1690696600cSBjoern A. Zeeb				echo "Destroying ${_md}."
1700696600cSBjoern A. Zeeb				mdconfig -d -u ${_md}
1710696600cSBjoern A. Zeeb			fi
1720696600cSBjoern A. Zeeb		fi
1730696600cSBjoern A. Zeeb	done
1740696600cSBjoern A. Zeeb}
1750696600cSBjoern A. Zeeb
1760696600cSBjoern A. Zeeb_mdconfig_cmd="$1"
1770696600cSBjoern A. Zeebif [ $# -gt 0 ]; then
1780696600cSBjoern A. Zeeb        shift
1790696600cSBjoern A. Zeebfi
1800696600cSBjoern A. Zeeb[ -n "$*" ] && _mdconfig_list="$*"
1810696600cSBjoern A. Zeeb
1820696600cSBjoern A. Zeebload_rc_config $name
1830696600cSBjoern A. Zeeb
184*f99f0ee1SAlexander Leidinger# doesn't make sense to run in a svcj: config setting
185*f99f0ee1SAlexander Leidingermdconfig_svcj="NO"
186*f99f0ee1SAlexander Leidinger
1870696600cSBjoern A. Zeebif [ -z "${_mdconfig_list}" ]; then
1880696600cSBjoern A. Zeeb	for _mdconfig_config in `list_vars mdconfig_md[0-9]\* |
1890696600cSBjoern A. Zeeb		sort_lite -nk1.12`
1900696600cSBjoern A. Zeeb	do
1910696600cSBjoern A. Zeeb		_mdconfig_unit=${_mdconfig_config#mdconfig_md}
1920696600cSBjoern A. Zeeb		[ "${_mdconfig_unit#*[!0-9]}" = "$_mdconfig_unit" ] ||
1930696600cSBjoern A. Zeeb			continue
1940696600cSBjoern A. Zeeb		_mdconfig_list="$_mdconfig_list md$_mdconfig_unit"
1950696600cSBjoern A. Zeeb	done
1960696600cSBjoern A. Zeeb	_mdconfig_list="${_mdconfig_list# }"
1970696600cSBjoern A. Zeebfi
1980696600cSBjoern A. Zeeb
1990696600cSBjoern A. Zeebrun_rc_command "${_mdconfig_cmd}"
200