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: releng/11.0/etc/rc.d/mdconfig 298514 2016-04-23 16:10:54Z lme $
28158722Sflz#
29158722Sflz
30158722Sflz# PROVIDE: mdconfig
31252310Shrs# REQUIRE: swap root
32158722Sflz
33158722Sflz. /etc/rc.subr
34158722Sflz
35158722Sflzname="mdconfig"
36298514Slmedesc="Create and control memory disks"
37158722Sflzstop_cmd="mdconfig_stop"
38158722Sflzstart_cmd="mdconfig_start"
39165683Syarstart_precmd='[ -n "${_mdconfig_list}" ]'
40165683Syarrequired_modules="geom_md:g_md"
41158722Sflz
42158722Sflzis_readonly()
43158722Sflz{
44158722Sflz	local _mp _ret
45158722Sflz
46158722Sflz	_mp=$1
47158722Sflz	_ret=`mount | while read _line; do
48158722Sflz		case ${_line} in
49158722Sflz		*" ${_mp} "*read-only*)
50158722Sflz			echo "yes"
51158722Sflz			;;
52208060Sdougb
53158722Sflz		*)
54158722Sflz			;;
55158722Sflz		esac;
56158722Sflz	done`
57158722Sflz
58158722Sflz	if [ -n "${_ret}" ]; then
59158722Sflz		return 0
60158722Sflz	else
61158722Sflz		return 1
62158722Sflz	fi
63158722Sflz}
64158722Sflz
65158722Sflzinit_variables()
66158722Sflz{
67158722Sflz	local _i
68158722Sflz
69158722Sflz	_fs=""
70158722Sflz	_mp=""
71158722Sflz	_dev="/dev/${_md}"
72158722Sflz	eval _config=\$mdconfig_${_md}
73158722Sflz	eval _newfs=\$mdconfig_${_md}_newfs
74158722Sflz
75158722Sflz	_type=${_config##*-t\ }
76158722Sflz	_type=${_type%%\ *}
77158722Sflz	if [ -z "${_type}" ]; then
78158722Sflz		err 1 "You need to specify \"-t <type>\" in mdconfig_${_md}"
79158722Sflz	fi
80158722Sflz
81158722Sflz	if [ "${_type}" = "vnode" ]; then
82158722Sflz		_file=${_config##*-f\ }
83158722Sflz		_file=${_file%%\ *}
84158722Sflz		if [ -z "${_file}" ]; then
85158722Sflz			err 2 "You need to specify \"-f <file>\" in mdconfig_${_md} for vnode devices"
86158722Sflz		fi
87158722Sflz		if [ "${_file}" != "${_file%.uzip}" ]; then
88158722Sflz			_dev="/dev/${_md}.uzip"
89158722Sflz		fi
90158722Sflz		for _i in `df ${_file} 2>/dev/null`; do _fs=${_i}; done
91158722Sflz	fi
92158722Sflz
93158722Sflz	# Debugging help.
94158722Sflz	debug "${_md} config: ${_config}"
95158722Sflz	debug "${_md} type: ${_type}"
96158722Sflz	debug "${_md} dev: ${_dev}"
97158722Sflz	debug "${_md} file: ${_file}"
98158722Sflz	debug "${_md} fs: ${_fs}"
99158722Sflz	debug "${_md} newfs flags: ${_newfs}"
100158722Sflz}
101158722Sflz
102158722Sflzmdconfig_start()
103158722Sflz{
104158722Sflz	local _md _mp _config _type _dev _file _fs _newfs _fsck_cmd
105158722Sflz
106158722Sflz	for _md in ${_mdconfig_list}; do
107158722Sflz		init_variables ${_md}
108158722Sflz		# Create md(4) devices of types swap, malloc and vnode if the
109158722Sflz		# file is on the root partition.
110158722Sflz		if [ "${_type}" != "vnode" -o "${_fs}" = "/" ]; then
111158722Sflz			if [ "${_type}" = "vnode" ]; then
112158722Sflz				if is_readonly ${_fs}; then
113158722Sflz					warn "${_fs} is mounted read-only, skipping ${_md}."
114158722Sflz					continue
115158722Sflz				fi
116165683Syar				if [ "${_file}" != "${_file%.uzip}" ]; then
117165683Syar					load_kld -m g_uzip geom_uzip || return 3
118171956Smatteo					# sleep a bit to allow creation of /dev/mdX.uzip
119171956Smatteo					sleep 2
120165683Syar				fi
121158722Sflz			fi
122158722Sflz			if mdconfig -l -u ${_md} >/dev/null 2>&1; then
123158722Sflz				err 3 "${_md} already exists"
124158722Sflz			fi
125158722Sflz			echo "Creating ${_md} device (${_type})."
126158722Sflz			if ! mdconfig -a ${_config} -u ${_md}; then
127158722Sflz				echo "Creating ${_md} device failed, moving on."
128158722Sflz				continue
129158722Sflz			fi
130158722Sflz			# Skip fsck for uzip devices.
131158722Sflz			if [ "${_type}" = "vnode" ]; then
132158722Sflz				if [ "${_file}" != "${_file%.uzip}" ]; then
133158722Sflz					_fsck_cmd=":"
134158722Sflz				elif checkyesno background_fsck; then
135158722Sflz					_fsck_cmd="fsck -F"
136158722Sflz				else
137158722Sflz					_fsck_cmd="fsck"
138158722Sflz				fi
139158722Sflz				if ! eval ${_fsck_cmd} -p ${_dev} >/dev/null; then
140158722Sflz					echo "Fsck failed on ${_dev}, not mounting the filesystem."
141158722Sflz					continue
142208060Sdougb
143158722Sflz				fi
144158722Sflz			else
145158722Sflz				newfs ${_newfs} ${_dev} >/dev/null
146158722Sflz			fi
147158722Sflz			if mount -d ${_dev} 2>&1 >/dev/null; then
148158722Sflz				echo "Mounting ${_dev}."
149158722Sflz				mount ${_dev}
150158722Sflz			fi
151158722Sflz		fi
152158722Sflz	done
153158722Sflz}
154158722Sflz
155158722Sflzmdconfig_stop()
156158722Sflz{
157158722Sflz	local _md _mp _config _type _dev _file _fs _newfs _i
158158722Sflz
159158722Sflz	for _md in ${_mdconfig_list}; do
160158722Sflz		init_variables ${_md}
161158722Sflz		if [ "${_type}" != "vnode" -o "${_fs}" = "/" ]; then
162158722Sflz			for _i in `df ${_dev} 2>/dev/null`; do _mp=${_i}; done
163158722Sflz			if [ -z "${_mp}" -o "${_mp}" != "${_mp%%%}" ]; then
164158722Sflz				echo "Device ${_dev} isn't mounted."
165158722Sflz			else
166158722Sflz				echo "Umounting ${_dev}."
167158722Sflz				umount ${_dev}
168158722Sflz			fi
169158722Sflz			if mdconfig -l -u ${_md} >/dev/null 2>&1; then
170158722Sflz				echo "Destroying ${_md}."
171158722Sflz				mdconfig -d -u ${_md}
172158722Sflz			fi
173158722Sflz		fi
174158722Sflz	done
175158722Sflz}
176158722Sflz
177158722Sflz_mdconfig_cmd="$1"
178158722Sflzif [ $# -gt 0 ]; then
179158722Sflz        shift
180158722Sflzfi
181158722Sflz[ -n "$*" ] && _mdconfig_list="$*"
182158722Sflz
183158722Sflzload_rc_config $name
184158722Sflz
185158722Sflzif [ -z "${_mdconfig_list}" ]; then
186264243Sdteske	for _mdconfig_config in `list_vars mdconfig_md[0-9]\* |
187264243Sdteske		sort_lite -nk1.12`
188264243Sdteske	do
189264243Sdteske		_mdconfig_unit=${_mdconfig_config#mdconfig_md}
190290163Sdteske		[ "${_mdconfig_unit#*[!0-9]}" = "$_mdconfig_unit" ] ||
191290163Sdteske			continue
192264243Sdteske		_mdconfig_list="$_mdconfig_list md$_mdconfig_unit"
193158722Sflz	done
194264243Sdteske	_mdconfig_list="${_mdconfig_list# }"
195158722Sflzfi
196208060Sdougb
197158722Sflzrun_rc_command "${_mdconfig_cmd}"
198