BSD DevCenter
oreilly.comSafari Books Online.Conferences.


Big Scary Daemons

BSD Tricks: MFS


BSD's memory file system, or MFS, allows you to create a "virtual hard disk" out of RAM. Essentially, you can use system memory as a hard drive. You can write files to this virtual hard disk just like any other hard disk in your system.

Since files on an MFS partition are already in memory, they can be accessed with a memory-to-memory copy. This is extremely fast compared to a disk-to-memory copy. As with everything else in memory, however, you lose all data on that partition when the computer goes down.

You can use MFS to provide a virtual disk drive to diskless workstations. Some people even store news server history files on MFS, to really boost performance. MFS on a database lockfile partition can also noticably improve performance.

You can even use MFS as a "scratch" partition, where you regularly compile, compress/decompress, or manipulate small files. This is not necessarily a good idea; if you run short on memory, MFS can bring a system to its knees.

If you have enough RAM, you can even set up an MFS /usr/obj; "make world" will fly like nothing you've ever seen before.

All BSDs support MFS. I'm using FreeBSD as a reference platform. While the commands will be similar on NetBSD and OpenBSD, you'll want to check your system man pages and kernel documentation before blindly following along.

An MFS partition is blank when mounted. You can either a) copy needed files to an MFS partition after boot, or b) use a mountpoint that traditionally starts off blank, such as /tmp.

MFS is effective even in situations where the system swaps regularly. The FreeBSD kernel keeps any information being actively used in memory, while transferring unused information to swap. This is excellent for partitions like /tmp, where small, frequently used files can be accessed quickly. Large files are usually less frequently accessed and end up in swap, with performance roughly similar to standard UFS.

You can even have an MFS partition larger than your physical memory. MFS will use swap space. This allows you to temporarily decompress large files in that partition. While FreeBSD will even let you mount an MFS partition larger than the total of your real memory and swap space, you'll get in trouble if you try to use it.

MFS, like UFS, cannot be a loadable module. The following FreeBSD kernel option will allow you to use MFS:

options    MFS    #Memory File System

Compile and install your kernel, then reboot.

You can mount the MFS filesystem at boot. First, identify which partition is your swap:

grep swap /etc/fstab
/dev/ad0s1b    none    swap  sw   0   0

On my machine, it's ad0s1b. If you have multiple swap partitions, choose one.

Add a line like this to your /etc/fstab:

/dev/ad0s1b   /var/db/lockfiles  mfs  rw  0  0

You can choose to add any other standard mounting options to your MFS partition (see "man 8 mount"). Common choices include nosuid, nodev, and async. The async option is perfectly safe to use with MFS, unlike other file systems: If your system should crash, you have to repopulate the MFS partition anyway.

You can also mount an MFS file system after boot. Simply pick your mountpoint and issue a mount_mfs command:

mount_mfs -s 1310720 /dev/ad0s1b /usr/obj

This mounts a 320 meg MFS file system under /usr/obj. You'll lose /usr/obj when you reboot, but if you always build the world from scratch and have already merged /etc, it won't matter.

The -s flag specifies a maximum size, in 512-K blocks. If you want your MFS partition to simply grow to the limits of available memory, you can skip that flag. This can come back to bite you, however, so be sure it's what you want.

One thing to note is that a newly-mounted MFS partition will conceal any files beneath that mountpoint. For example, if you have an existing /tmp with files in it, and you mount an MFS /tmp, the system will not be able to access any files in the old /tmp. If you have programs running that expect to access files in /tmp (such as X), this can cause problems. When you unmount the MFS partition, the files are revealed again. It is possible to show both sets of files if you use union mounts, but FreeBSD's union mounts are notoriously and dangerously buggy. Any man page that includes the words "USE AT YOUR OWN RISK. BEWARE OF DOG. SLIPPERY WHEN WET" means trouble.

Carefully used, MFS can squeeze a bit more performance out of your system. Clumsily used, it can cause a variety of entertaining problems. Enjoy.

Michael W. Lucas

Read more Big Scary Daemons columns.

Discuss this article in the Operating Systems Forum.

Return to the BSD DevCenter.


Sponsored by: