FreeBSD ramdisk – mdconfig

Creating a ramdisk on FreeBSD is straight forward but Google will lead you astray. The main problem with finding accurate results on how to create a ramdisk is that it’s not called a ramdisk. It’s technically referred to as a “memory-based disk” in FreeBSD. To make matters worse the name of the utility has recently changed. It use to be “vnconfig” and is now “mdconfig“.

Most of the articles instruct the user to create a startup script run out of rc.local that initializes and mounts the memory disk at startup. However, FreeBSD added proper rc scripts to create the ramdisk earlier in the boot process – for services that rely on its existence (Nagios).

There are two start up scripts in /etc/rc.d entitled mdconfig and mdconfig2. The first script runs early in the rc.order and completes the majority of the mdconfig options. The second script exists to perform mdconfig options that can’t be completed early in the boot process. They basically work as a team to complete the options you specify in the rc.conf file. Here’s an example block from rc.conf where I create a 128M ramdisk of type malloc entitled /dev/md0.

mdconfig_md0="-t malloc -s 128m"
mdconfig_md0_owner="nagios:nagios"
mdconfig_md0_perms="2775"
mdconfig_md0_cmd="su -m nagios -c \"mkdir /var/spool/nagios/ramdisk/{checkresults,rw}\""

The _owner and _perms options should be self-explanatory. The _cmd option will perform just about anything as long as the necessary services the command relies on have been started. Here is the necessary fstab entry:

/dev/md0        /var/spool/nagios/ramdisk    ufs    rw    0    0

That’s basically all there is to it. See man mdconfig and man rc.conf (search mdconfig). The type you choose should usually be tmpfs (not malloc) tmpfs will allow the OS to more safely manage the memory; by moving it to swap in cases where the memory is inactive. In this instance I rather it move virtually anything else to swap before status.dat so I used malloc. Also, you don’t need to restart for these changes to take effect (but you may want to for testing). You can run /etc/rc.d/mdconfig start and mdconfig2 start.  I don’t want to talk about how much time I spent finding this information. I hope this helps someone, let me know in the comments.



4 Responses to “FreeBSD ramdisk – mdconfig”

  1. rw says:

    This should probably be tmpfs – it’s more memory efficient since it understands deleted files.

    If you are going to use md devices then swap is preferred over malloc. Malloc is no faster than swap, the difference is that malloc will keep your files, even the deleted ones, unconditionally in memory even when running processes are short of it.

    128MB of memory is probably not a problem, but malloc md devices in general can lead to panics or poor performance.

  2. Bowlby says:

    Thanks for the tip and comment! I am aware tmpfs is usually the better choice. However, in this instance I rather use malloc and not allow Nagios to swap. I much rather cause the OS to swap something else(anything else) in order to free up memory. If status.dat hits the drive, even for a moment, Nagios checks would back up to the point of no return and the system might as well be offline. Granted the likelihood of it mattering either way is next to none if the system RAM is properly spec’d. I’ve updated the post to reflect tmpfs being the preferred choice.

  3. Jonathan says:

    Just what I was looking for.

    Thanks.

  4. [...] This post was mentioned on Twitter by Tech & Friki Stuff. Tech & Friki Stuff said: ryanbowlby.com » Blog Archive » FreeBSD ramdisk – mdconfig http://dlvr.it/11cms [...]

Leave a Reply