<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="article.xsl"?>
<articles> <article date="17 Aug 2020"> <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<p>This was tested on FreeBSD 12 and may not be valid on other versions. Your mileage may vary.</p></articleabstract> <articlebody><p>In my <a href="https://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>
<p>Let's get started.</p>
<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>
<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> tmpfsload="YES" </pre>
<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 lin</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.</p>
<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.</p>
<pre><code>df -h /wrk</code> Filesystem Size Used Avail Capacity Mounted on tmpfs 2.0G 113M 1.9G 6% /wrk </pre>
<p>And that's it! Have fun with your new memory-backed disk.</p>
</articlebody> <footnotes> <footnote><img src="Websiteunderconstruction.gif" /></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>