Tuesday, March 2, 2010

/tmp and RAM disks in the Solaris OS - Creating Testing and automating

# 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 cannot 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:
# mkdir /mytmp
# mount -o size=100m -F tmpfs swap /mytmp
# 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
#
# ramdiskadm -a mydisk 40m
/dev/ramdisk/mydisk
# check the result
#
# ls -l /dev/ramdisk/mydisk
lrwxrwxrwx 1 root root 40 Mar 17 22:15 /dev/ramdisk/mydisk -> ../../devices/pseudo/ramdisk@1024:mydisk
# 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
#
# fstyp /dev/rramdisk/mydisk
unknown_fstyp (no matches)
# create a filesystem on the ramdisk
#
# 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
#
# mkdir /myramdisk
# mount /dev/ramdisk/mydisk /myramdisk
# df -k /myramdisk
Filesystem kbytes used avail capacity Mounted on
/dev/ramdisk/mydisk 38255 1041 33389 4% /myramdisk
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.
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 :
#cp /etc/system /etc/system.bck
#echo "set ramdisk:rd_percent_physmem=50" >> /etc/system
#reboot
A simple init script to create a ramdisk while booting the machine:
#!/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
To get an idea of what's going on when a RAM disk is created, let's use the Solaris kstat(1m) kernel statitics utility to see how memory is being allocated. First let's see what memory looks like before creating a RAM disk:
# pagesize
4096
# kstat -n system_pages | grep pagesfree
pagesfree 96334
# kstat -n system_pages | grep pageslocke
pageslocked 26170
So, on this particular system, where a page is 4096 bytes, there are currently 96334 pages free, and 26170 pages that are locked. Now let's create a 50MB RAM disk:
# ramdiskadm -a rd 50m
/dev/ramdisk/rd
# ramdiskadm
Block Device Size
Removable
/dev/ramdisk/rd 52428800 Yes
# kstat -n system_pages | grep pagesfree
pagesfree 83507
# kstat -n system_pages | grep pageslocked
pageslocked 38988
Let's subtract the original number of pageslocked from the latest value and multiply by the pagesize:
# pagesize
4096
# bc
(38988-26170)*4096
52502528
^D
The increase in locked pages can be attributed to the creation of the RAM disk (50m + a small amount of overhead). So yes, these pages are locked into memory.

No comments: