Talking about RAM disks in the Solaris OS

Talking about /tmp and RAM disks in the Solaris OS in general

by Bernd Schemmer,  last update: December 2008

In Solaris /tmp is by default a memory based file system mounted on swap:
    

# df -k /tmp
Filesystem            kbytes    used   avail capacity  Mounted on
swap                 1961928     504 1961424     1%    /tmp

This has some advantages:

  •  the access of /tmp is fast
  •  there is always a writable directory even if Solaris can not mount the disks in read/write mode or if Solaris is booted from a read-only NFS share    
  •  you do not need to think about cleaning up /tmp after or before a reboot
  •  It's not neccessary to create a filesystem for tmp - just mount it und use it

On the other hand there are some things to take care of if using /tmp:
    
Because /tmp is mounted on swap you should not use it for files which should survive a reboot  - use the disk based directory for temporary files, /var/tmp, instead for these files.

One very important point:
    
Every user can write to /tmp. And in the default configuration /tmp is mounted without a size limitation. This fact results in the possibility that every user can use the whole virtual memory of the machine (that is physical memory and swap) by simply filling up /tmp with garbage.

To avoid this situation you should mount /tmp with an upper limit for the size, e.g in /etc/vfstab change the line
    

swap    -       /tmp    tmpfs   -       yes     -

to

swap    -       /tmp    tmpfs   -       yes     size=1024m

(replace 1024m with an approbiate value for the machine)
    
Unfortunately you can not change the size for /tmp while Solaris is running:

# lockfs /tmp
/tmp: Inappropriate ioctl for device

# mount -o remount,size=512m swap /tmp
mount: Operation not supported

Therefore you must reboot the machine to activate the change.

Because of the fact that tmpfs is a "normal" filesystem in Solaris you can always add additional memory based file systems, e.g.
to create another tmpfs on the fly use:

[Mon Mar 17 21:53:19 root@sol9 /]
# mkdir /mytmp

[Mon Mar 17 22:05:44 root@sol9 /]
# mount -o size=100m  -F tmpfs swap /mytmp

[Mon Mar 17 22:06:04 root@sol9 /]
# df -k /mytmp
Filesystem            kbytes    used   avail capacity  Mounted on
swap                  102400       0  102400     0%    /mytmp

To create this new filesystem every time the machine boots up simply add another line to the /etc/vfstab:

swap    -       /mytmp    tmpfs   -       yes     size=1024m

There are some restrictions for tmpfs Filesystems:

  •  There is not really a device for a memory based filesystems like /dev/dsk/c#t#d#s#.. for harddisks or /dev/lofi/# for lofi mounts.  Especially there is no raw device for the memory based filesystems.
  • There are some restrictions in tmpfs (see tmpfs(7FS) )
  • And you can only use the tmpfs filesystem on memory based file systems; you can not use for example ufs or vxfs on these kind of file systems.

But because Solaris is a real Operating system there is a solution for this problem also:

Instead of using tmpfs to create a memory based file system, use ramdiskadm. ramdiskadm is part of the Solaris OS since (at least) version 9.
ramdiskadm is part of the SUNWcsu package and therefore should be installed on every Solaris machine (x86 and SPARC, of course).

ramdiskadm can be used to create real ramdisk devices which can be used like  any other disk device, e.g:

# create the ramdisk
#
[Mon Mar 17 22:15:03 root@sol9 /]
# ramdiskadm -a mydisk 40m
/dev/ramdisk/mydisk

# check the result
#
[Mon Mar 17 22:15:21 root@sol9 /]
# ls -l /dev/ramdisk/mydisk
lrwxrwxrwx   1 root     root          40 Mar 17 22:15 /dev/ramdisk/mydisk -> ../../devices/pseudo/ramdisk@1024:mydisk

[Mon Mar 17 22:16:04 root@sol9 /]
# ls -l /dev/rramdisk/mydisk
lrwxrwxrwx   1 root     root          44 Mar 17 22:15 /dev/rramdisk/mydisk -> ../../devices/pseudo/ramdisk@1024:mydisk,raw

# check the fstype
#
[Mon Mar 17 22:16:07 root@sol9 /]
# fstyp /dev/rramdisk/mydisk
unknown_fstyp (no matches)

# create a filesystem on the ramdisk
#
[Mon Mar 17 22:16:22 root@sol9 /]
# newfs /dev/rramdisk/mydisk
/dev/rramdisk/mydisk: Unable to find Media type. Proceeding with system determined parameters.
newfs: construct a new file system /dev/rramdisk/mydisk: (y/n)? y
/dev/rramdisk/mydisk:   81872 sectors in 136 cylinders of 1 tracks, 602 sectors
        40.0MB in 9 cyl groups (16 c/g, 4.70MB/g, 2240 i/g)
super-block backups (for fsck -F ufs -o b=#) at:
 32, 9664, 19296, 28928, 38560, 48192, 57824, 67456, 77088,

# mount the ramdisk
#
[Mon Mar 17 22:16:44 root@sol9 /]
# mkdir /myramdisk

[Mon Mar 17 22:16:51 root@sol9 /]
# mount /dev/ramdisk/mydisk /myramdisk

[Mon Mar 17 22:17:01 root@sol9 /]
# df -k /myramdisk
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/ramdisk/mydisk    38255    1041   33389     4%    /myramdisk

[Mon Mar 17 22:17:06 root@sol9 /]

Be aware that these ramdisks are also gone after a reboot. If you need them permanent you should create an init script or an SMF service to recreate them while booting the machine.

For more detailed information about ramdiskadm please consult the man page of ramdiskadm(1m) and ramdisk(7d); The man page of ramdiskadm also describes how to give users other than root access to create and delete ramdisks and the man page for ramdisk explains how much memory can be used for ramdisks.

And, for the records, you can use a ramdisk created with ramdiskadm also for an SVM mirror . This can be useful if an application is mostly reading from
the disk; in this case you can change the read policy for the mirror to first read from the ramdisk..

But that's a story for another wiki entry.

Update 23.11.2008

A script to start and stop ramdisks in the Solaris OS can be find in this article: 

A script to start and stop ramdisks in the Solaris OS

Update 06.12.2008

There's an interesting blog entry about ramdisks and swap:

Are Solaris RAM Disks swappable?

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Oct 27, 2008

    rpetruzzelli says:

    Could you elaborate on how capping /tmp with the "size=Xg" value in /etc/vfstab ...

    Could you elaborate on how capping /tmp with the "size=Xg" value in /etc/vfstab impacts memory usage?
    If I've formatted a disk with a slice for swap, for say, 4G and then I cap /tmp with size=512m, what happens to the other 3.5G of disk space allocated? Is it still usable? Thanks.

    1. Oct 28, 2008

      bnsmb says:

      >>Could you elaborate on how capping /tmp with the "size=Xg" value in /etc...

      >>Could you elaborate on how capping /tmp with the "size=Xg" value in /etc/vfstab impacts memory usage?
      >>If I've formatted a disk with a slice for swap, for say, 4G and then I cap /tmp with size=512m, what
      >> happens to the other 3.5G of disk space allocated? Is it still usable? Thanks.

      Yes, it is still usable as swap space for Solaris (for example to pageout memory from running processes)

      The point is :

      If you do not limit the size for /tmp, /tmp can use the complete free memory. Example:

      Suppose you have 4 GB real memory and 4 GB swap. If you mount /tmp without a size restriction in this example /tmp could use maximal

      4 GB + 4 GB - <memory_used_by_solaris>.

      And /tmp is writable by every user on the system! Even nobody can write to /tmp.

      Example:

      # real memory: = 4 GB
      #
      prtconf | more
      System Configuration: Sun Microsystems sun4u
      Memory size: 4096 Megabytes

      # swap space: = 4 GB
      #
      swap -l
      swapfile dev swaplo blocks free
      /dev/md/dsk/d20 85,5 16 8094960 8094960

      # free space in /tmp (= free memory of the machine)
      #
      df -h /tmp
      Filesystem size used avail capacity Mounted on
      swap 6.5G 120K 6.5G 1% /tmp

      In this example every user can create files that use up to 6.5 GB in /tmp and fill all free memory on the machine.

  2. Nov 18, 2008

    Balkas says:

    Just a note to add : There is a restriction for the size of ramdisk. From ma...

    Just a note to add :

    There is a restriction for the size of ramdisk.

    From manual page of ramdisk(7D) from a Solaris 10 8/07 box :
    The percentage of available physical memory that can be
    allocated to ramdisks is constrained by the variable
    rd_percent_physmem. You can tune the rd_percent_physmem
    variable in /etc/system. By default, the percentage of
    available physical memory that can be allocated to ramdisks
    is fixed at 25%.

    The percentage can be increased by issuing following commands :

    1. cp /etc/system /etc/system.bck
    2. echo "set ramdisk:rd_percent_physmem=50" >> /etc/system
    3. reboot
    1. Nov 18, 2008

      bnsmb says:

      Balkas, thanks for the info. I only added a hint to the man page for this restri...

      Balkas, thanks for the info. I only added a hint to the man page for this restriction but it's more comfortable if the info is right here in the wiki entry, of course.

  3. Nov 19, 2008

    dmispd says:

    Hi,. Nice day~ I'm use Solaris 10 ramdisk file system.. How get init script recr...

    Hi,. Nice day~
    I'm use Solaris 10 ramdisk file system..
    How get init script recreate them while booting the machine.
    Thanks.

    1. Nov 20, 2008

      bnsmb says:

      dmispd, How get init script recreate them while booting the machine. A simple ...

      dmispd,

      How get init script recreate them while booting the machine.


      A simple init script to create a ramdisk while booting the machine with minimal error checking might look like:

      #!/sbin/sh
      
      RAMDISK_NAME="ramdisk1"
      RAMDISK_SIZE="40m"
      MOUNT_POINT="/ramdisk1"
      
      # check the status
      #
      ramdiskadm | grep -v grep | grep /dev/ramdisk/${RAMDISK_NAME} >/dev/null
      RAMDISK_CONFIGURED=$?
      
      case $1 in
      	start )
      		if [ ${RAMDISK_CONFIGURED} = 0 ] ; then
      			echo "The ramdisk \"${RAMDISK_NAME}\" is already defined"
      			exit 10
      		fi
      		# create the ramdisk
      		ramdiskadm -a ${RAMDISK_NAME} ${RAMDISK_SIZE} && \
      			yes | newfs /dev/rramdisk/${RAMDISK_NAME} && \
      				mount /dev/ramdisk/${RAMDISK_NAME} ${MOUNT_POINT}
      		;;
      
      	stop )
      		if [ ${RAMDISK_CONFIGURED} != 0 ] ; then
      			echo "The ramdisk \"${RAMDISK_NAME}\" is not defined"
      			exit 10
      		fi
      		umount -f ${MOUNT_POINT} 2>/dev/null
      		ramdiskadm -d ${RAMDISK_NAME}
      		;;
      
      	* )
      		echo "Usage: ` basename $0 ` [start|stop]"
      		exit 1
      		;;
      esac
      
       
      

      I did not test the script in depth so there might be some errors -- but I hope you get the idea how to do it. And in Solaris 10 you should create a SMF service for creating the ramdisk (using a script like the above as start and stop script).

      Note further that I'm just writing a much more comfortable script to start and stop ramdisks -- I'll publish it in this wiki when it's ready (sometime in the next days I hope)

  4. Dec 01, 2008

    russp says:

    I used the syntax in the vfstab example on this page, and it caused the machine ...

    I used the syntax in the vfstab example on this page, and it caused the machine to fail to reboot.
    "1g" in this example:
    swap - /tmp tmpfs - yes size=1g
    should be "1024m". It appears [Solaris 10u4 sparc] doesn't like the "g".

    1. Dec 02, 2008

      bnsmb says:

      russp, thanks for the hint. I replaced 1g with 1024m in the wiki entry so that t...

      russp, thanks for the hint. I replaced 1g with 1024m in the wiki entry so that the examples should also work on older Solaris releases.

      Bernd

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact