Memory Filesystems

In addition to creating partitions on raw disk, OpenBSD lets you create partitions in system memory. A memory filesystem (MFS), or memory disk, lives in your machine’s RAM, rather than on a physical disk. Reading and writing files to and from such a filesystem is much faster than accessing those same files on a spinning disk, which makes a memory-backed filesystem a huge optimization for certain applications.

If MFSs sound too good to be true for high-performance environments, that’s because they are. Understand their limits before you implement them everywhere. First, RAM does not persist across reboots or shutdowns, so either will erase the contents of an MFS. While this might seem obvious, I’ve surprised myself more than once by losing a file stored on a filesystem I had forgotten was an MFS. Furthermore, if your system crashes, you’ll lose any data stored on an MFS.

You can use an MFS partition as scratch space to rapidly compile, compress, decompress, or otherwise manipulate temporary files. I’ve seen news server histories, database locks, and other application-specific files stored on MFSs.

An MFS works even in situations where the system regularly swaps. The kernel retains any information being actively used in memory, while transferring unused information to swap space. This is excellent for small partitions like /tmp, in which small, frequently used files can be quickly accessed. Files that are less frequently accessed end up in swap space, which gives performance similar to accessing a physical disk.

One last word of caution: Don’t make heavy use of MFSs if you don’t have RAM to spare. If you run short on combined memory and swap space, your system will perform very poorly.

Creating MFS Partitions

Create temporary MFS partitions with mount_mfs(8). Like other mount_ commands, mount_mfs takes two arguments: the physical device and a mount point. Unlike physical disks, memory doesn’t have a device node, so use the device node of the system swap space. If you have multiple swap partitions, pick whichever you like.

Here is how you can create a memory-backed filesystem by passing a swap partition, /dev/sd0b, and a desired mount point, /mnt, as arguments to mount_mfs:

# mount_mfs /dev/sd0b /mnt

The size of this partition will be limited only by the size of your swap partition.

You can create smaller memory-backed filesystems, so that you will have memory and/or swap space available if you fill the memory disk. Specify the size with the -s flag and a number of sectors, or with a trailing b (bytes), m (megabytes), or g (gigabytes). Here’s how to create a 128MB MFS on /mnt:

# mount_mfs -s 128m /dev/sd0b /mnt

If you request an MFS larger than your system can support, you’ll get a warning like mmap: Cannot allocate memory. Try again, this time with a more reasonable size.

Mounting an MFS at Boot

You can mount an MFS at boot by adding an /etc/fstab entry. You only need a mount point and the partition size.

1swap   2/mnt   3mfs   4rw,async,-s=128m   50   60

You don’t need to specify a specific swap device; OpenBSD is smart enough to let you say the memory disk is generically swap-backed 1. Just as with any other partition, you also need to specify the mount point 2 and the filesystem type 3.

When dealing with a memory disk, you can use different options than you would for a traditional disk 4. Since a system crash would destroy all files on the MFS anyway, you can safely mount an MFS partition as asynchronous using the async option. You might also want to use nodev and nosuid mount options on this partition. You can specify the size with the -s option, but make sure that you put an equal sign (=) between the -s and the size. Because /etc/fstab uses whitespace to separate fields, OpenBSD will think the dump level is 128m if you don’t use an equal sign.[21]

Data on a memory disk is by definition disposable, so don’t back it up 5. Similarly, never use fsck(8) with a memory disk at boot 6. The memory disk is created anew at each boot, so it is automatically internally consistent.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.172.132