<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="article.xsl"?> 

<articles>
  <article date="17 Aug 2020" version="12">
    <pagetitle>Remember... something, probably</pagetitle>
    <articleheader>Using a memory disk to reduce hard drive wear and tear while building ports</articleheader>
    <articleabstract>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</articleabstract>
    <articlebody><p>In my <a href="http://wyrm.org/articles/freebsdinstallnotes.xml">FreeBSD install notes</a><sup class="inlinefootnote">1</sup> 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.</p>

<h2>Getting started</h2>

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

<h2>Setting up the filesystem</h2>

<p>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 <code>wrk</code> - <code>mkdir /wrk</code></p>

<p>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 <code>/etc/fstab</code> directly and add the following line:</p>

<pre><code>tmpfs           /wrk                    tmpfs   rw,size=2g      0       0</code></pre>

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

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

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

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

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

<pre><code>/boot/loader.conf</code>
tmpfs_load="YES"
</pre>

<h2>Configuring Ports</h2>

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

<pre><code>/etc/make.conf</code>
WRKDIRPREFIX=/wrk
</pre>

<p>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, <code>/wrk</code>.</p>

<h2>Monitoring and tweaking</h2>

<p>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 <code>df -h /wrk</code> to verify that your memory disk is being used. <code>df</code> will show you how full a given filesystem is, and the <code>-h</code> means to put it in a 'human readable' format.</p>

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

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

<p><code>gnu-watch</code> is a program that lets you run a program over and over again at the frequency you specify. <code>gnu-watch -n 1</code> tells gnu-watch to run the following command every 1 second. The command that we're running is <code>df -h /wrk</code>, 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.</p>

<p>For instance, if you build the ports for LLVM (which is required by roughly a bazillion ports including <code>www/firefox</code>) 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: </p>

<pre><code>tmpfs           /wrk                    tmpfs   rw,size=32g      0       0</code></pre>

<p>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.</p>

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

</articlebody>
    <footnotes>
      <footnote>&lt;img src="Website_under_construction.gif" /&gt;</footnote>
      <footnote>This is a little confusing since <code>tmpfs</code> isn't really a real device</footnote>
      <footnote>Or substitute whatever is was you used for your temp folder</footnote>
    </footnotes>
  </article>
</articles>
