2013-03-22

NetBSD on RPi: Minimizing Disk Writes

I recently installed NetBSD on my RaspberryPi. Although not all the hardware is fully supported, enough is there to make it a usable system. It's nice to have my RPi provide the same system experience (configuration, organization, etc.) as other NetBSD machines I maintain. A big "Thank you!" to the developers that made this possible.

One concern I have, however, is that the boot drive is an SD card. Solid state cards have limited write cycles and I worry that, since most Unix systems assume a mechanical drive which allow essentially infinite writes, my SD card will not last very long. To measure this, I monitored the disk writes using 'iostat' and was disappointed in how many writes were occurring on an otherwise idle system.

I enabled cron, syslogd, postfix, ntpd, and sshd on my system. This post shows how I greatly reduced writes to the SD with these services running. If you enable other services, you may have to make further adjustments.

To monitor writes (and to see if my tweaks were having any effects), I opened two windows with ssh sessions into the RPi. One window was used to make the modifications and the other was running 'iostat -w10 -x' so I could see the effects.

Toning Down Filesystem Updates

The first adjustment stops updating file access times. Anytime a command is run, the access time to its executable gets updated which writes to the SD card (actually, I think it's two writes, since it looks like the 'log' option -- i.e. journaling -- is enabled which means the journal log gets written to before committing the change to the filesystem.) This means each command that is run from the shell causes a write to the disk. If you run any shell scripts, the commands in it each cause a filesystem update!

To shut this option off, I changed this line in /etc/fstab:

/dev/ld0a   /  ffs     rw,log    1 1

to:

/dev/ld0a   /  ffs     rw,log,noatime,nodevmtime    1 1

You can also make this change without rebooting by typing (as root):

mount -uo noatime /

Use RAM Disks for Temporary File Objects

My next step moves directories, which have frequent changes, to RAM. The following entries are now in my /etc/fstab:

tmpfs     /tmp                    tmpfs   rw,-s32M
tmpfs     /var/run                tmpfs   rw,union,-s1M
tmpfs     /var/mail               tmpfs   rw,union,-s10M
tmpfs     /var/spool/postfix      tmpfs   rw,union,-s20M
tmpfs     /var/db/postfix         tmpfs   rw,union,-s1M
tmpfs     /var/chroot             tmpfs   rw,union,-s10M

I used the 'union' option so mounting the RAM disks won't hide the underlying directory structure. You may also note I didn't include /var/log in the list. That's because I changed /etc/syslogd.conf to send all syslog messages to a Synology NAS (Google Affiliate Ad) that I use, so my /var/log doesn't get written to. If you don't forward your syslog messages, then you'll want to slap a tmpfs on top of it, as well.

It would be nice to put all of /var into a RAM drive, but NetBSD uses /var for longer lasting information, too (e.g. the pkgsrc database.) For now, I'll use the five mount points.

Further Changes for Postfix

The changes up to this point greatly reduced the writes which were occurring on an idle system. However there was one more 8k write which was happening once a minute. It turns out that the 'pickup' process in the postfix suite wakes up once a minute and writes to a FIFO. Apparently, writing to a FIFO updates its modify time.(?) Editing the 'pickup' line in /etc/postfix/master.cf so that 'fifo' is changed to 'unix' fixes this last problem.

Now, if I let the system alone for a few hours, I can scroll back through the iostat output and see no writes (or very, very few) are occurring. This will greatly extend the life of the SD card.

Hope this helps others!

3 comments:

  1. I was looking for this information just yesterday evening. A great help to me. Thanks.

    ReplyDelete
    Replies
    1. I'm glad you found it useful. As the RPi becomes better supported in the NetBSD build framework, I'd like to put together diffs so that these tweaks are the defaults when you upgrade the OS.

      Delete
  2. This is good stuff. I hope the NetBSD team will include this by default, or find some simple way to automagically do this stuff. Thanks.

    ReplyDelete