\begin{exe} {The tmpfs Special Filesystem}

   \textbf{tmpfs} is one of many special filesystems used
   under \textbf{Linux}.  Some of these are not really
   used as filesystems, but just take advantage of the
   filesystem abstraction. However, \textbf{tmpfs} is a
   real filesystem that applications can do I/O on.

   Essentially, \textbf{tmpfs} functions as a
   \textbf{ramdisk}; it resides purely in memory.  But it
   has some nice properties that old-fashioned
   conventional ramdisk implementations did not have:

   \begin{enumerate}

      \item
      The filesystem adjusts its size (and thus the memory
      that is used) dynamically; it starts at zero and
      expands as necessary up to the maximum size it was
      mounted with.
      \item
      If your RAM gets exhausted, \textbf{tmpfs} can utilize
      swap space.  (You still can't try to put more in the
      filesystem than its maximum capacity allows, however.)
      \item
      \textbf{tmpfs} does not require having a normal
      filesystem placed in it, such as \textbf{ext3} or
      \textbf{vfat}; it has its own methods for dealing
      with files and I/O that are aware that it is really
      just space in memory (it is not actually a block
      device), and as such are optimized for speed.

      Thus there is no need to pre-format the filesystem
      with a \verb?mkfs? command; you merely just
      have to mount it and use it.

   \end{enumerate}

   Mount a new instance of \textbf{tmpfs} anywhere on your directory
   structure with a command like:

   \begin{cmd}
$ sudo mkdir /mnt/tmpfs
$ sudo mount -t tmpfs none /mnt/tmpfs
   \end{cmd}

   See how much space the filesystem has been given and how much it
   is using:
   \begin{cmd}
$ df -h /mnt/tmpfs
   \end{cmd}

   You should see it has been allotted a default value of
   half of your RAM; however, the usage is zero, and will
   only start to grow as you place files on
   \filelink{/mnt/tmpfs}.

   You could change the allotted size as a mount option as in:
   \begin{cmd}
$ sudo mount -t tmpfs -o size=1G none /mnt/tmpfs
   \end{cmd}
   You might try filling it up until you reach full capacity and
   see what happens.  Do not forget to unmount when you are done with:
   \begin{cmd}
$ sudo umount /mnt/tmpfs
   \end{cmd}

   Virtually all modern \textbf{Linux} distributions mount an instance
   of \textbf{tmpfs} at \filelink{/dev/shm}:
   \begin{cmd}
$ df -h /dev/shm
   \end{cmd}
   \begin{response}
Filesystem     Type   Size  Used Avail Use% Mounted on
tmpfs          tmpfs  3.9G   24M  3.9G   1% /dev/shm
   \end{response}
   Many applications use this such as when they are using
   \textbf{POSIX} shared memory as an inter-process
   communication mechanism. Any user can create, read and
   write files in \filelink{/dev/shm}, so it is a good place
   to create temporary files in memory.

   Create some files in \filelink{/dev/shm} and note how
   the filesystem is filling up with \textbf{df}.

   In addition, many distributions mount multiple
   instances of \textbf{tmpfs}; for example, on a
   \textbf{RHEL} system:

   \begin{cmd}
$ df -h | grep ' tmpfs'
   \end{cmd}
   \begin{response}
tmpfs          tmpfs     7.8G   38M  7.8G   1% /dev/shm
tmpfs          tmpfs     7.8G   18M  7.8G   1% /run
tmpfs          tmpfs     7.8G     0  7.8G   0% /sys/fs/cgroup
tmpfs          tmpfs     1.6G  1.2M  1.6G   1% /run/user/42
tmpfs          tmpfs     1.6G   56K  1.6G   1% /run/user/1000
   \end{response}
   Notice this was run on a system with 16 GB of ram, so
   clearly you cannot have all these \textbf{tmpfs}
   filesystems actually using the default \verb?~8?
   GB they have each been allotted!

   \begin{info}
      Some distributions (such as \textbf{Fedora}) may (by
      default) mount \filelink{/tmp} as a \textbf{tmpfs}
      system; in such cases one has to avoid putting large
      files in \filelink{/tmp} to avoid running out of
      memory.  Or one can disable this behavior as we
      discussed earlier when describing \filelink{/tmp}.
   \end{info}
\end{exe}

