tmpfs for FreeBSD?

Paul Mather paul at gromit.dlib.vt.edu
Thu Aug 12 08:28:37 PDT 2004


On Thu, 12 Aug 2004 00:53:15 -0500, Dan Nelson <dnelson at allantgroup.com>
wrote:
 
> In the last episode (Aug 11), adp said:
> > I'm looking for a ramdisk-style filesystem for FreeBSD that can be
> > used for scratch space, e.g., tmpfs in Solaris. The filesystem should
> > be able to grow and shrink in memory (and use real disk space as
> > needed) depending on the amount of free RAM on the system. I don't
> > want just a fixed sized block of memory reserved for /tmp. I will be
> > using this for scratch files that are quickly created and then
> > destroyed, and will average around 2MB each. We are expecting out tmp
> > filesystem to need around 256MB to 512MB on average.
> 
> The best available at the moment is a swap-backed filesystem.  It will
> consume ram/swap as it grows, but won't release swap space when you
> delete files.  If you're running 5.*, just put this in rc.conf:
> 
> tmpmfs="YES"
> tmpsize="512m"
> 
> and make sure you have at least 512MB of swap, so if it does happen to
> grow to full size and then have most of its files deleted, the free
> blocks can be pushed out to swap.

Alternatively, for 4.x (and 5.x) you can simply have an mfs /tmp entry
in /etc/fstab.  Here is what I have in /etc/fstab for a 128 MB /tmp on
my 4.10-STABLE system:

     swap  /tmp  mfs  rw,nosuid,-s262144  0  0

On my 5.2-CURRENT system, I have this:

     md    /tmp  mfs  rw,-s128m  0  0

Note how the device to mount on is "swap" in 4.x and a "md" device in
5.x.  Under 4.x, a "df -h" looks something like this:

Filesystem         Size   Used  Avail Capacity  Mounted on
[[...]]
mfs:28             124M    29K   114M     0%    /tmp

and like this under 5.x:

Filesystem          Size    Used   Avail Capacity  Mounted on
[[...]]
/dev/md0            124M     12K    114M     0%    /tmp

The "-s" option in the /etc/fstab mount entry determines the size of the
underlying VM device.  In the case of the OP, that would be "-s512m" or
larger.  Make sure there is at least as much swap space to back it.

FWIW, in 5.x you can also use a malloc-backed md device for a true RAM
disk.

Also, under 5.x, you can use a vnode-backed md device to use a large
regular file as underlying backing storage.  This would be handy, I
presume, if you didn't want lots of your swap consumed by /tmp usage. 
In 4.x, this would be accomplished via vn devices.  Be careful of the
mount order when attempting this, though, if you want a virtual /tmp
created during boot.

See mount_mfs(8) for details and options.

Cheers,

Paul.
-- 
e-mail: paul at gromit.dlib.vt.edu

"Without music to decorate it, time is just a bunch of boring production
 deadlines or dates by which bills must be paid."
        --- Frank Vincent Zappa



More information about the freebsd-questions mailing list