# mdconfig at system boot



## Leander (Apr 23, 2012)

Hi fellows,


```
truncate -s 1G /mnt/VirtualDisk
chmod 0600 /mnt/VirtualDisk
mdconfig -a -t vnode -f /mnt/VirtualDisk
```

works perfectly, but problem is that I still need the same md setup after a reboot as well. Unfortunately the system doesn't remember the mdconfig setup. So I found /etc/rc.d/mdconfig and /etc/rc.d/mdconfig2 and I started to add the following into my rc.conf:

```
rc_debug="YES"
rc_info="YES"
mdconfig_md2="-t vnode -f /mnt/VirtualDisk"
```
*A*fter adding this, I tried [cmd=]/etc/rc.d/mdconfig start[/cmd] and [cmd=]/etc/rc.d/mdconfig2 start[/cmd] but nothing helped, md2 wasn't listed by [cmd=]mdconfig -lv[/cmd] After scanning a bit through the rc.d/mdconfig scripts, I figured it may want to have the actual md_n_ device to start so I tried [cmd=]/etc/rc.d/mdconfig start md2[/cmd] and voila:

```
FreeBSD [~]# mdconfig -lv
md0	swap	  512M
md1	vnode	 4096M	/var/swap

FreeBSD [~]# /etc/rc.d/mdconfig start md2
/etc/rc.d/mdconfig: DEBUG: run_rc_command: start_precmd: [ -n "${_mdconfig_list}" ] 
/etc/rc.d/mdconfig: DEBUG: load_kld: geom_md kernel module already loaded.
/etc/rc.d/mdconfig: DEBUG: run_rc_command: doit: mdconfig_start 
/etc/rc.d/mdconfig: DEBUG: md2 config: -t vnode -f /mnt/VirtualDisk
/etc/rc.d/mdconfig: DEBUG: md2 type: vnode
/etc/rc.d/mdconfig: DEBUG: md2 dev: /dev/md2
/etc/rc.d/mdconfig: DEBUG: md2 file: /mnt/VirtualDisk
/etc/rc.d/mdconfig: DEBUG: md2 fs: /
/etc/rc.d/mdconfig: DEBUG: md2 newfs flags: 
Creating md2 device (vnode).
/etc/rc.d/mdconfig: DEBUG: checkyesno: background_fsck is set to YES.
fsck: Could not determine filesystem type
Fsck failed on /dev/md2, not mounting the filesystem.

FreeBSD [~]# mdconfig -lv
md0	swap	  512M
md1	vnode	 4096M	/var/swap
md2	vnode	 1024M	/mnt/VirtualDisk

FreeBSD [~]# mdconfig -du 2
```
But I'm unhappy about the script complaining about fsck and about the mount issue. Fact is, that I do not want to *fsck* neither mount it since it will be an iSCSI share. Still I could ignore this error since md2 is now listed by [cmd=]mdconfig -lv[/cmd] But th*e*n I tried rc.d/mdconfig2 and it doesn't seem to complain about fsck:

```
FreeBSD [~]# /etc/rc.d/mdconfig2 start md2
/etc/rc.d/mdconfig2: DEBUG: run_rc_command: start_precmd: [ -n "${_mdconfig2_list}" ] 
/etc/rc.d/mdconfig2: DEBUG: load_kld: geom_md kernel module already loaded.
/etc/rc.d/mdconfig2: DEBUG: run_rc_command: doit: mdconfig2_start 
/etc/rc.d/mdconfig2: DEBUG: md2 config: -t vnode -f /mnt/VirtualDisk
/etc/rc.d/mdconfig2: DEBUG: md2 type: vnode
/etc/rc.d/mdconfig2: DEBUG: md2 dev: /dev/md2
/etc/rc.d/mdconfig2: DEBUG: md2 file: /mnt/VirtualDisk
/etc/rc.d/mdconfig2: DEBUG: md2 fs: /
/etc/rc.d/mdconfig2: DEBUG: md2 owner: 
/etc/rc.d/mdconfig2: DEBUG: md2 perms: 
/etc/rc.d/mdconfig2: DEBUG: md2 files: 
/etc/rc.d/mdconfig2: DEBUG: md2 populate cmd: 
/etc/rc.d/mdconfig2: DEBUG: checkyesno: _mounted is set to no.
```
 But `mdconfig -lv` doesn't list it - so it's not configured?

So what am I doing wrong? My aim is to initialize a simple md device right at system boot in order to let eg. iSCSI (istg) benefit of the md device.

Any hints or ideas?


----------



## usdmatt (Apr 26, 2012)

Have you tried changing the variable in rc.conf to mdconfig_md0 instead of mdconfig_md2?

I've no experience with the mdconfig startup script but I wouldn*'*t be surprised if it runs in a loop, looking for 0 first, processing that, then 1, etc. The ifconfig scripts work like this for aliases. You'll probably find it's looking for mdconfig_md0, not finding it and giving up (unless you specify the md number as you say).

That may make */etc/rc.d/mdconfig start* work but looking at the script I can't see any obvious way of stopping it trying to format and mount the device. You may end up having to make a custom rc script to run the commands on boot.


----------



## usdmatt (Apr 27, 2012)

The following rc script should create the required md device on boot and remove it on shutdown (tested on 8.3-RELEASE). I'm not sure whether istg will be starting/stopping on boot/shutdown but if so the REQUIRE/KEYWORD stuff at the top may require tweaking to make sure istg starts after this and stops first.

(Note the device is hardcoded to /dev/md100)


```
#!/bin/sh

# PROVIDE: iscsimd
# REQUIRE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="iscsimd"
rcvar="iscsimd_enable"
start_cmd="iscsimd_start"
stop_cmd="iscsimd_stop"

disk="/mnt/VirtualDisk"
size="1G"

iscsimd_start()
{
        /usr/bin/truncate -s ${size} ${disk}
	/bin/chmod 0600 ${disk}
	/sbin/mdconfig -a -t vnode -f ${disk} -u 100
}

iscsimd_stop()
{
        /sbin/mdconfig -d -u 100
	/bin/unlink ${disk}
}

load_rc_config $name
run_rc_command "$1"
```

Save it as /usr/local/etc/rc.d/iscsimd, permissions 0500 and then add 
	
	



```
iscsimd_enable="YES"
```
 to /etc/rc.conf.
It will then run on boot/shutdown and can be run manually as follows:


```
# service iscsimd start
# service iscsimd stop
```


----------



## Leander (Apr 28, 2012)

Nice work, hey!
Thanks a lot


----------



## Leander (Apr 29, 2012)

Here is a tuned version of previous rc script. The method of evalu*at*ing md[n] is limited to 100 only! Please let me know if someone knows a better way of getting the "n" of md_file_md[n]. I tried my luck in this thread: http://forums.freebsd.org/showthread.php?t=31707

rc.conf entries can look like:

```
### Memory Disks
md_enable="YES"
md_file_md0="/mnt/VirtualDisk"
md_file_md1="/mnt/FileX"
md_file_md12="/mnt/SomeOtherFile"
```

The rc script for this would be:

```
#!/bin/sh

# PROVIDE: md
# REQUIRE: localswap root
# BEFORE: SERVERS

. /etc/rc.subr

name="md"
rcvar="md_enable"
start_cmd="${name}_start"
stop_cmd="md_stop"
required_modules="geom_md:g_md"


md_start() {
    for i in $(seq 0 100); do

        unset MD_FILE
        eval MD_FILE=\$md_file_md${i}

        if [ ! -z $MD_FILE ]; then
            if [ -f $MD_FILE ]; then

                if [ ! -z "$(/sbin/mdconfig -lv | /usr/bin/grep "${MD_FILE}")" ]; then
                    USED_MD="$(/sbin/mdconfig -lv | /usr/bin/grep "${MD_FILE}" | awk '{print $1}')"
                    echo "mdconfig: Skipped md${i}. File $MD_FILE is configured as $USED_MD already."
                    unset USED_MD
                else
                    /sbin/mdconfig -a -t vnode -f $MD_FILE -u ${i} && \
                    MSG="mdconfig: Created /dev/md${i}" && \
                    SIZE=$(diskinfo -v /dev/md${i} | grep "mediasize in bytes" | grep -E -o "\(.*\)") && \
                    printf "%s %s %-10s %s\n" $MSG $SIZE
                fi

            else
                echo "mdconfig: Error: Skipped md${i}. File $MD_FILE not found."
            fi
        fi

    done
}

md_stop() {
    echo "Sorry - this script does not support \"stop\". Use i.e."
    echo "'mdconfig -du 3' in order to unconfigure /dev/md3"
}


load_rc_config $name
run_rc_command "$1"
```

*D*on't forget to [cmd=]chmod 0555 /usr/local/etc/rc.d/md[/cmd] the rc script. There is no stop since stop could cause collateral damage to data on md. There is also no file creation if a file doesn't exist. It is necessary to invoke i.e.

```
truncate -s 1G /mnt/VirtualDisk
chmod 0600 /mnt/VirtualDisk
```
 once before 
	
	



```
service md start
```
 as mentioned earlier already.


----------

