Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 1 Mar 2005 22:48:47 GMT
From:      "Wojciech A. Koszek" <dunstan@freebsd.czest.pl>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   conf/78257: [PATCH] /etc/rc.d/memdisk for /dev/md* creation at startup
Message-ID:  <200503012248.j21MmlLG035989@freebsd.czest.pl>
Resent-Message-ID: <200503012250.j21MoFM3017032@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         78257
>Category:       conf
>Synopsis:       [PATCH] /etc/rc.d/memdisk for /dev/md* creation at startup
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Mar 01 22:50:15 GMT 2005
>Closed-Date:
>Last-Modified:
>Originator:     Wojciech A. Koszek
>Release:        FreeBSD 5.4-PRERELEASE i386
>Organization:
>Environment:
System: FreeBSD dunstan.freebsd.czest.pl 5.4-PRERELEASE FreeBSD 5.4-PRERELEASE #10: Sat Feb 26 23:44:07 CET 2005 dunstan@dunstan.freebsd.czest.pl:/usr/obj/usr/src/sys/HOME7 i386


>Description:
Currently, there is no way to create memory disks on system startup, and
there is no way to attach/detach large number of such disks at once. I
wanted to have easy way to automatically attach images in order to mount them
from /etc/fstab. I've created simple script designed to fit in /etc/rc.d.
Idea is similar to /etc/rc.d/ramdisk but without previous filesystem
creation. You may enable it's functionality by setting memdisk_enable to
"YES" in /etc/rc.conf. Each disk is configured by variables:

memdisk_<devicename>_type="TYPE"

where TYPE may be either "malloc", "swap" or "vnode". For "swap" or "malloc"
memory disk you have to set:

memdisk_<devicename>_size="SIZE"

where SIZE is just like this value used in mdconfig(8). For "vnode" type you
have to set:

memdisk_<devicename>_file="PATH"

where match is full path to file which you'd like to attach as disk.

<devicename> may me typical name of memory disk device: "md0", "md1", ... ,
"mdN". Disks configured with these variables has to be enabled with:

memdisk_list="LIST"

where LIST is string containing memory disk names separated with white
spaces. Disk specific options used by mdconfig(8) can be passed with:

memdisk_<devicename>_flags="FLAGS"

where FLAGS is string contating options passed to mdconfig(8). Additionally,
it's possible to set diffrent interface than /dev/mdctl (memdisk_device)
option).


Sample working configuration may look like:
[rc.conf]
[..]
memdisk_enable="YES"
memdisk_list="0 1 2"

memdisk_0_type="malloc"
memdisk_0_size="1m"
memdisk_0_flags="-s 1024"

memdisk_1_type="vnode"
memdisk_1_file="/tmp/f/etc.iso"

memdisk_2_type="swap"
memdisk_2_size="10m"
[..]

I hope you'll find it useful.
>How-To-Repeat:

>Fix:
Patch [diff.memdisk]:
adds:
 src/etc/rc.d/memdisk
modifies:
 src/etc/rc.d/Makefile
 src/etc/defaults/rc.conf
 src/share/man/man5/rc.conf.5

--- diff.memdisk begins here ---

Patch against FreeBSD 5.4-PRERELEASE, kern.osreldate: 503102.

diff -uprP /usr/src/etc/defaults/rc.conf src/etc/defaults/rc.conf
--- /usr/src/etc/defaults/rc.conf	Sun Feb 27 11:07:11 2005
+++ src/etc/defaults/rc.conf	Tue Mar  1 22:54:11 2005
@@ -477,6 +477,13 @@ ugidfw_enable="NO"	# Load mac_bsdextende
 bsdextended_script="/etc/rc.bsdextended"	# Default mac_bsdextended(4)
 						# ruleset file.
 
+
+##############################################################
+### /dev/md* configuration  ##################################
+##############################################################
+memdisk_enable="NO"
+memdisk_device="/dev/mdctl"
+
 ##############################################################
 ### Jail Configuration #######################################
 ##############################################################
diff -uprP /usr/src/etc/rc.d/Makefile src/etc/rc.d/Makefile
--- /usr/src/etc/rc.d/Makefile	Sat Oct 30 19:03:33 2004
+++ src/etc/rc.d/Makefile	Tue Mar  1 22:43:14 2005
@@ -19,7 +19,7 @@ FILES=	DAEMON LOGIN NETWORKING SERVERS \
 	jail \
 	kadmind kerberos keyserv kldxref kpasswdd \
 	ldconfig local localpkg lomac lpd \
-	mixer motd mountcritlocal mountcritremote \
+	mixer motd memdisk mountcritlocal mountcritremote \
 	mountd moused mroute6d mrouted msgs \
 	named natd netif netoptions \
 	network_ipv6 nfsclient nfsd \
diff -uprP /usr/src/etc/rc.d/memdisk src/etc/rc.d/memdisk
--- /usr/src/etc/rc.d/memdisk	Thu Jan  1 01:00:00 1970
+++ src/etc/rc.d/memdisk	Tue Mar  1 22:58:16 2005
@@ -0,0 +1,102 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Wojciech A. Koszek
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE PROJECT ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+# PROVIDE:	memdisk
+# REQUIRE:	root
+# BEFORE:	mountcritlocal
+# KEYWORD:	nojail
+
+. /etc/rc.subr
+name="memdisk"
+start_cmd="memdisk_start"
+stop_cmd="memdisk_stop"
+
+# Do not rely on mdconfig(8)
+memdisk_modpresent()
+{
+	if [ ! -e ${memdisk_device:-"/dev/mdctl"} ]
+	then
+		kldstat -v | grep -q g_md || kldload g_md;
+	fi
+}
+
+# Create memory disk
+memdisk_create()
+{
+	_mdev="$1"
+
+	eval md_size=\"\$memdisk_${_mdev}_size\";
+	eval md_file=\"\$memdisk_${_mdev}_file\";
+	eval md_flags=\"\$memdisk_${_mdev}_flags\";
+	eval md_type=\"\$memdisk_${_mdev}_type\";
+
+	case "${md_type}" in
+	[Mm][Aa][Ll][Ll][Oo][Cc])
+		md_type="malloc";
+		_mdevopt="-s ${md_size}";
+		;;
+	[Ss][Ww][Aa][Pp])
+		md_type="swap";
+		_mdevopt="-s ${md_size}";
+		;;
+	[Vv][Nn][Oo][Dd][Ee])
+		md_type="vnode";
+		_mdevopt="-f ${md_file}";
+		;;
+	*)
+		echo "You must specify correct type of memory disk";
+		return
+	esac
+	mdconfig -a -t ${md_type} ${_mdevopt} ${md_flags} -u ${_mdev} 2>&1;
+}
+
+# Start
+memdisk_start()
+{
+	if checkyesno memdisk_enable; then
+		memdisk_modpresent;
+		echo -n "Configuring memory devices: ";
+		for _mdev in ${memdisk_list}; do
+			memdisk_create "$_mdev" && echo -n "md${_mdev} "
+		done
+		echo;
+	fi
+}
+
+# Destroy created devices
+memdisk_stop()
+{
+	if checkyesno memdisk_enable; then
+		echo -n "Deinitializing memory devices: ";
+		for _mdev in ${memdisk_list}; do
+			mdconfig -d -u "${_mdev}" && echo -n "md${_mdev} " 2>&1;
+		done;
+		echo;
+	fi
+}
+
+load_rc_config $name
+run_rc_command "$1"
diff -uprP /usr/src/share/man/man5/rc.conf.5 src/share/man/man5/rc.conf.5
--- /usr/src/share/man/man5/rc.conf.5	Fri Feb 25 22:46:26 2005
+++ src/share/man/man5/rc.conf.5	Tue Mar  1 23:03:27 2005
@@ -3092,6 +3092,52 @@ The default
 ruleset file to load.
 The default value of this variable is
 .Pa /etc/rc.bsdextended .
+.It Va memdisk_enable
+.Pq Vt str
+Responsible for creation memory disks on startup.
+Set this to
+.Dq Li YES
+to enable this functionality. Default value is 
+.Dq Li NO .
+.It memdisk_list
+.Pq Vt str
+A list of one or more memory disks you want to configure on
+startup.
+.It memdisk_ Ns Ao Ar X Ac Ns Va _type
+.Pq Vt str
+Type of memory disk passed with
+.Fl t Ar type
+option to
+.Xr mdconfig 8
+for disk
+.Ar X .
+Value may be
+.Ar malloc ,
+.Ar swap
+or
+.Ar vnode.
+.It memdisk_ Ns Ao Ar X Ac Ns Va _size
+.Pq Vt str
+Size of memory disk
+.Ar X ,
+when it's type set with
+.Va memdisk_ Ns Ao Ar X Ac Ns Va _type
+is either
+.Ar malloc
+or
+.Ar swap .
+.It memdisk_ Ns Ao Ar X Ac Ns Va _file
+.Pq Vt str
+Full path to file used as memory disk when type set with
+.Va memdisk_ Ns Ao Ar X Ac Ns Va _type
+is
+.Ar vnode .
+.It memdisk_ Ns Ao Ar X Ac Ns Va _flags
+.Pq Vt str
+Additional flags passed to
+.Xr mdconfig 8
+while configuring memory disk
+.Ar X .
 .It Va ramdisk_units
 .Pq Vt str
 A list of one or more ramdisk units to configure with
--- diff.memdisk ends here ---


>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200503012248.j21MmlLG035989>