Using a memory disk to reduce hard drive wear and tear while building ports

There are lots of ways to use memory as a disk and lots of reasons you would want to do that. I detail one potential use case

In my FreeBSD install notes1 I mentioned that I put a stupid amount of RAM in my computer. More than I'll typically need for the workload that I expect this computer to be under, so I have a bit of extra to play around with. I also like to build the software I'm using from Ports for fun, but that can take up a lot of memory, which shouldn't be a problem, and it writes a lot of temp files to the disks during compiling, which also isn't really a problem, but since I'm using SSD's and SSD's have a large but finite number of write cycles they can do before the drive wears out, I figured I'd carve out some of my RAM to use as a disk and use that RAM-backed disk for my computer to write its temp files to when it's compiling software.

Getting started

You will either need to be root or use security/doas or security/sudo. If you haven't already, load up the tmpfs kernel module with the command kldload tmpfs. This gives us support for memory-backed disks. We'll make this permanent later, once we're sure it works.

Setting up the filesystem

Next, we'll need to create a directory to use as our mountpoint. This folder can be anywhere and be called anything. I'm going to put it in the root of the filesystem and call it wrk - mkdir /wrk

Next, we need to create our memory-backed disk. There are a lot of ways we can do this, but I'm going to edit my /etc/fstab directly and add the following line:

tmpfs           /wrk                    tmpfs   rw,size=2g      0       0

Going over the particulars of the fstab is beyond the scope of this article, but we will need to note a couple of things: the first tmpfs is the name of the device we're using2, /wrk is the directory we're going to use to mount our memory-backed filesystem, the second tmpfs is the filesystem type, rw,size=2g sets it up with read/write permissions and makes the size 2GB. 0 0 we won't bother with here since they don't really mean much to memory disks.

Now, to make sure that the fstab is correct and doesn't have any typos. As root, type mnt /wrk3. If it looks like nothing happens, then you were probably successful. You can check it with df and see if your newly-minted memory disk shows up.

df -h
Filesystem      Size    Used   Avail Capacity  Mounted on
tmpfs           2.0G    696K    2.0G     0%    /wrk

If it doesn't show up, you will want to re-edit your fstab and try again. Do not reboot your computer until you can mount the drive without errors. A broken fstab is annoying to try and recover from.

Assuming that your drive mounted successfully, now is probably a good time to enable the tmpfs driver to load at boot time. You can do that by adding the follwing line somewhere in your /boot/loader.conf


Configuring Ports

Now we want to tell the ports system about our new memory-backed disk. We do that by editing the /etc/make.conf file and adding the follwing line


This tells the build system where we want the 'work' to happen when we build software. In this case, it's our memory-backed disk, /wrk.

Monitoring and tweaking

Finally, we're ready to build something! Start building your favorite port and you can check to make sure that the disk is working as expected by using df -h /wrk to verify that your memory disk is being used. df will show you how full a given filesystem is, and the -h means to put it in a 'human readable' format.

df -h /wrk
Filesystem    Size    Used   Avail Capacity  Mounted on                                                                                                                                                      
tmpfs         2.0G    113M    1.9G     6%    /wrk 

You could even install a program like misc/gnu-watch to monitor how much of your newly-minted memory disk is being used. You might use something like: gnu-watch -n 1 df -h /wrk. That might looks cryptic, but it's actually pretty simple:

gnu-watch is a program that lets you run a program over and over again at the frequency you specify. gnu-watch -n 1 tells gnu-watch to run the following command every 1 second. The command that we're running is df -h /wrk, as explained above. That way when you start building a program, you see how much space you're burning through and if you allocated enough of your RAM. For a lot of programs, 2GB seems like it's enough. But depending on what you're building, you might find yourself running out of room and you will need to adjust your settings.

For instance, if you build the ports for LLVM (which is required by roughly a bazillion ports including www/firefox) you're going to need a lot more RAM. In my case, after a lot of experimentation, LLVM used over 16GB of my memory-disk at its peak. Now my fstab looks more like this:

tmpfs           /wrk                    tmpfs   rw,size=32g      0       0

32GB is probably overkill, but with a lot of these programs using gobs of space to build, I expect that this usage will only go up over time. And, that 32GB is just the maximum that the directory can go to, it's not sitting there hoarding 32GB of RAM if I'm not actively doing anything, so there's no harm in leaving it there, but you'll have to experiment to find out what works best for you.

Of course this isn't the only use for a memory-backed disk. But those uses are for another article.


  1. <img src="Website_under_construction.gif" />
  2. This is a little confusing since tmpfs isn't really a real device
  3. Or substitute whatever is was you used for your temp folder

Read more FreeBSD articles · Go back to the homepage